From 21d733233bfda9f9c6ad62a4889c093cad922dcb Mon Sep 17 00:00:00 2001 From: Miodec Date: Wed, 20 Jul 2022 16:13:56 +0200 Subject: [PATCH] rewrote restart function to use options object instead of inreasing number of parameters --- .../src/ts/controllers/account-controller.ts | 8 +- .../src/ts/controllers/input-controller.ts | 6 +- frontend/src/ts/elements/commandline-lists.ts | 20 +++-- frontend/src/ts/pages/test.ts | 4 +- frontend/src/ts/test/test-logic.ts | 83 +++++++++++++------ 5 files changed, 84 insertions(+), 37 deletions(-) diff --git a/frontend/src/ts/controllers/account-controller.ts b/frontend/src/ts/controllers/account-controller.ts index 49ace0a63..e15d99579 100644 --- a/frontend/src/ts/controllers/account-controller.ts +++ b/frontend/src/ts/controllers/account-controller.ts @@ -141,7 +141,9 @@ export async function getDataAndInit(): Promise { UpdateConfig.apply(snapshot.config); Settings.update(); UpdateConfig.saveFullConfigToLocalStorage(true); - TestLogic.restart(false, true); + TestLogic.restart({ + nosave: true, + }); } else if (snapshot.config !== undefined) { //loading db config, keep for now let configsDifferent = false; @@ -187,7 +189,9 @@ export async function getDataAndInit(): Promise { Settings.update(); UpdateConfig.saveFullConfigToLocalStorage(true); if (ActivePage.get() == "test") { - TestLogic.restart(false, true); + TestLogic.restart({ + nosave: true, + }); } DB.saveConfig(Config); } diff --git a/frontend/src/ts/controllers/input-controller.ts b/frontend/src/ts/controllers/input-controller.ts index 5faad17aa..f0bdd420d 100644 --- a/frontend/src/ts/controllers/input-controller.ts +++ b/frontend/src/ts/controllers/input-controller.ts @@ -660,7 +660,7 @@ function handleTab(event: JQuery.KeyDownEvent, popupVisible: boolean): void { } //otherwise restart - TestLogic.restart(false, false, event); + TestLogic.restart({ event }); } else { //quick tab off @@ -734,7 +734,9 @@ $(document).keydown(async (event) => { } //otherwise restart - TestLogic.restart(false, false, event); + TestLogic.restart({ + event, + }); } if (!allowTyping) return; diff --git a/frontend/src/ts/elements/commandline-lists.ts b/frontend/src/ts/elements/commandline-lists.ts index bde3a640e..cda323b2f 100644 --- a/frontend/src/ts/elements/commandline-lists.ts +++ b/frontend/src/ts/elements/commandline-lists.ts @@ -2465,7 +2465,9 @@ const commandsPractiseWords: MonkeyTypes.CommandsGroup = { noIcon: true, exec: (): void => { PractiseWords.init(true, false); - TestLogic.restart(false, false, undefined, true); + TestLogic.restart({ + practiseMissed: true, + }); }, }, { @@ -2474,7 +2476,9 @@ const commandsPractiseWords: MonkeyTypes.CommandsGroup = { noIcon: true, exec: (): void => { PractiseWords.init(false, true); - TestLogic.restart(false, false, undefined, true); + TestLogic.restart({ + practiseMissed: true, + }); }, }, { @@ -2483,7 +2487,9 @@ const commandsPractiseWords: MonkeyTypes.CommandsGroup = { noIcon: true, exec: (): void => { PractiseWords.init(true, true); - TestLogic.restart(false, false, undefined, true); + TestLogic.restart({ + practiseMissed: true, + }); }, }, ], @@ -2527,7 +2533,9 @@ Misc.getChallengeList().then((challenges) => { exec: (): void => { navigate("/"); ChallengeController.setup(challenge.name); - TestLogic.restart(false, true); + TestLogic.restart({ + nosave: true, + }); }, }); }); @@ -3258,7 +3266,9 @@ export const defaultCommands: MonkeyTypes.CommandsGroup = { display: "Repeat test", icon: "fa-sync-alt", exec: (): void => { - TestLogic.restart(true); + TestLogic.restart({ + withSameWordset: true, + }); }, available: (): boolean => { return TestUI.resultVisible; diff --git a/frontend/src/ts/pages/test.ts b/frontend/src/ts/pages/test.ts index 48a770e3d..7f1d568b1 100644 --- a/frontend/src/ts/pages/test.ts +++ b/frontend/src/ts/pages/test.ts @@ -24,7 +24,9 @@ export const page = new Page( TestConfig.show(); TestStats.resetIncomplete(); ManualRestart.set(); - TestLogic.restart(undefined, undefined, undefined, undefined, true); + TestLogic.restart({ + noAnim: true, + }); Funbox.activate(Config.funbox); }, async () => { diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index 6e3f4ea10..020756fec 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -320,13 +320,30 @@ export function startTest(): boolean { return true; } -export function restart( - withSameWordset = false, - _?: boolean, // this is nosave and should be renamed to nosave when needed - event?: JQuery.KeyDownEvent, - practiseMissed = false, - noAnim = false -): void { +interface RestartOptions { + withSameWordset?: boolean; + nosave?: boolean; + event?: JQuery.KeyDownEvent; + practiseMissed?: boolean; + noAnim?: boolean; +} + +// withSameWordset = false, +// _?: boolean, // this is nosave and should be renamed to nosave when needed +// event?: JQuery.KeyDownEvent, +// practiseMissed = false, +// noAnim = false + +export function restart(options = {} as RestartOptions): void { + const defaultOptions = { + withSameWordset: false, + practiseMissed: false, + noAnim: false, + nosave: false, + }; + + options = { ...defaultOptions, ...options }; + if (TestUI.testRestarting || TestUI.resultCalculating) { event?.preventDefault(); return; @@ -335,7 +352,7 @@ export function restart( if (!ManualRestart.get()) { if ( TestWords.hasTab && - !event?.shiftKey && + !options.event?.shiftKey && Config.quickRestart !== "esc" ) { return; @@ -369,10 +386,10 @@ export function restart( Config.mode === "quote" && Config.language.replace(/_\d*k$/g, "") === TestWords.randomQuote.language ) { - withSameWordset = true; + options.withSameWordset = true; } if (TestState.isRepeated) { - withSameWordset = true; + options.withSameWordset = true; } TestInput.pushKeypressesToHistory(); @@ -403,8 +420,8 @@ export function restart( if ( PractiseWords.before.mode !== null && - !withSameWordset && - !practiseMissed + !options.withSameWordset && + !options.practiseMissed ) { Notifications.add("Reverting to previous settings.", 0); if (PractiseWords.before.punctuation !== null) { @@ -418,7 +435,7 @@ export function restart( } let repeatWithPace = false; - if (TestUI.resultVisible && Config.repeatedPace && withSameWordset) { + if (TestUI.resultVisible && Config.repeatedPace && options.withSameWordset) { repeatWithPace = true; } @@ -476,7 +493,7 @@ export function restart( { opacity: 0, }, - noAnim ? 0 : 125, + options.noAnim ? 0 : 125, async () => { if (ActivePage.get() == "test") Focus.set(false); TestUI.focusWords(); @@ -509,7 +526,7 @@ export function restart( UpdateConfig.setNumbers(false, true); } if ( - withSameWordset && + options.withSameWordset && (Config.funbox === "plus_one" || Config.funbox === "plus_two") ) { const toPush = []; @@ -525,7 +542,7 @@ export function restart( TestWords.words.reset(); toPush.forEach((word) => TestWords.words.push(word)); } - if (!withSameWordset && !shouldQuoteRepeat) { + if (!options.withSameWordset && !shouldQuoteRepeat) { TestState.setRepeated(false); TestState.setPaceRepeat(repeatWithPace); TestWords.setHasTab(false); @@ -632,7 +649,7 @@ export function restart( { opacity: 1, }, - noAnim ? 0 : 125, + options.noAnim ? 0 : 125, () => { TestUI.setTestRestarting(false); // resetPaceCaret(); @@ -1698,7 +1715,9 @@ $(document.body).on("click", "#restartTestButton", () => { Config.repeatQuotes === "typing" && Config.mode === "quote" ) { - restart(true); + restart({ + withSameWordset: true, + }); } else { restart(); } @@ -1723,7 +1742,9 @@ $(document.body).on("click", "#restartTestButtonWithSameWordset", () => { return; } ManualRestart.set(); - restart(true); + restart({ + withSameWordset: true, + }); }); $(document).on("keypress", "#restartTestButtonWithSameWordset", (event) => { @@ -1732,7 +1753,9 @@ $(document).on("keypress", "#restartTestButtonWithSameWordset", (event) => { return; } if (event.key === "Enter") { - restart(true); + restart({ + withSameWordset: true, + }); } }); @@ -1798,19 +1821,25 @@ $(document).on("click", "#top .config .mode .textButton", (e) => { $("#practiseWordsPopup .button.missed").on("click", () => { PractiseWords.hidePopup(); PractiseWords.init(true, false); - restart(false, false, undefined, true); + restart({ + practiseMissed: true, + }); }); $("#practiseWordsPopup .button.slow").on("click", () => { PractiseWords.hidePopup(); PractiseWords.init(false, true); - restart(false, false, undefined, true); + restart({ + practiseMissed: true, + }); }); $("#practiseWordsPopup .button.both").on("click", () => { PractiseWords.hidePopup(); PractiseWords.init(true, true); - restart(false, false, undefined, true); + restart({ + practiseMissed: true, + }); }); $(document).on( @@ -1837,10 +1866,10 @@ $(document).on("click", "#top #menu #startTestButton, #top .logo", () => { ConfigEvent.subscribe((eventKey, eventValue, nosave) => { if (ActivePage.get() === "test") { - if (eventKey === "difficulty" && !nosave) restart(false, nosave); - if (eventKey === "showAllLines" && !nosave) restart(false, nosave); - if (eventKey === "keymapMode" && !nosave) restart(false, nosave); - if (eventKey === "tapeMode" && !nosave) restart(false, nosave); + if (eventKey === "difficulty" && !nosave) restart(); + if (eventKey === "showAllLines" && !nosave) restart(); + if (eventKey === "keymapMode" && !nosave) restart(); + if (eventKey === "tapeMode" && !nosave) restart(); } if (eventKey === "lazyMode" && eventValue === false && !nosave) { rememberLazyMode = false;