Skip to content

Commit 2fc2c7b

Browse files
committed
refactor: move testRestarting state to TestState
1 parent 5275947 commit 2fc2c7b

File tree

6 files changed

+36
-34
lines changed

6 files changed

+36
-34
lines changed

frontend/src/ts/controllers/challenge-controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as ManualRestart from "../test/manual-restart-tracker";
55
import * as CustomText from "../test/custom-text";
66
import * as Funbox from "../test/funbox/funbox";
77
import Config, * as UpdateConfig from "../config";
8-
import * as TestUI from "../test/test-ui";
98
import * as ConfigEvent from "../observables/config-event";
109
import * as TestState from "../test/test-state";
1110
import * as Loader from "../elements/loader";
@@ -28,7 +27,7 @@ export function clearActive(): void {
2827
if (
2928
TestState.activeChallenge &&
3029
!challengeLoading &&
31-
!TestUI.testRestarting
30+
!TestState.testRestarting
3231
) {
3332
Notifications.add("Challenge cleared", 0);
3433
TestState.setActiveChallenge(null);

frontend/src/ts/controllers/input-controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ $(document).on("keydown", async (event) => {
10131013

10141014
if (!allowTyping) return;
10151015

1016-
if (!event.originalEvent?.isTrusted || TestUI.testRestarting) {
1016+
if (!event.originalEvent?.isTrusted || TestState.testRestarting) {
10171017
event.preventDefault();
10181018
return;
10191019
}
@@ -1249,7 +1249,7 @@ $("#wordsInput").on("keyup", (event) => {
12491249
});
12501250

12511251
$("#wordsInput").on("keyup", (event) => {
1252-
if (!event.originalEvent?.isTrusted || TestUI.testRestarting) {
1252+
if (!event.originalEvent?.isTrusted || TestState.testRestarting) {
12531253
event.preventDefault();
12541254
return;
12551255
}
@@ -1269,7 +1269,7 @@ $("#wordsInput").on("beforeinput", (event) => {
12691269
});
12701270

12711271
$("#wordsInput").on("input", async (event) => {
1272-
if (!event.originalEvent?.isTrusted || TestUI.testRestarting) {
1272+
if (!event.originalEvent?.isTrusted || TestState.testRestarting) {
12731273
(event.target as HTMLInputElement).value = " ";
12741274
return;
12751275
}

frontend/src/ts/controllers/route-controller.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ export async function navigate(
154154
): Promise<void> {
155155
if (
156156
!options.force &&
157-
(TestUI.testRestarting || TestUI.resultCalculating || PageTransition.get())
157+
(TestState.testRestarting ||
158+
TestUI.resultCalculating ||
159+
PageTransition.get())
158160
) {
159161
console.debug(
160162
`navigate: ${url} ignored, page is busy (testRestarting: ${
161-
TestUI.testRestarting
163+
TestState.testRestarting
162164
}, resultCalculating: ${
163165
TestUI.resultCalculating
164166
}, pageTransition: ${PageTransition.get()})`

frontend/src/ts/test/test-logic.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export function restart(options = {} as RestartOptions): void {
176176
return;
177177
}
178178

179-
if (TestUI.testRestarting || TestUI.resultCalculating) {
179+
if (TestState.testRestarting || TestUI.resultCalculating) {
180180
options.event?.preventDefault();
181181
return;
182182
}
@@ -314,7 +314,7 @@ export function restart(options = {} as RestartOptions): void {
314314
el = $("#typingTest");
315315
}
316316
TestUI.setResultVisible(false);
317-
TestUI.setTestRestarting(true);
317+
TestState.setTestRestarting(true);
318318
el.stop(true, true).animate(
319319
{
320320
opacity: 0,
@@ -358,7 +358,7 @@ export function restart(options = {} as RestartOptions): void {
358358
const initResult = await init();
359359

360360
if (!initResult) {
361-
TestUI.setTestRestarting(false);
361+
TestState.setTestRestarting(false);
362362
return;
363363
}
364364

@@ -396,7 +396,7 @@ export function restart(options = {} as RestartOptions): void {
396396
LiveBurst.reset();
397397
TestUI.updatePremid();
398398
ManualRestart.reset();
399-
TestUI.setTestRestarting(false);
399+
TestState.setTestRestarting(false);
400400
}
401401
);
402402
}
@@ -420,7 +420,7 @@ async function init(): Promise<boolean> {
420420
);
421421
}
422422
TestInitFailed.show();
423-
TestUI.setTestRestarting(false);
423+
TestState.setTestRestarting(false);
424424
TestState.setTestInitSuccess(false);
425425
Focus.set(false);
426426
// Notifications.add(
@@ -1442,7 +1442,7 @@ $(".pageTest").on("click", "#restartTestButtonWithSameWordset", () => {
14421442
});
14431443

14441444
$(".pageTest").on("click", "#testConfig .mode .textButton", (e) => {
1445-
if (TestUI.testRestarting) return;
1445+
if (TestState.testRestarting) return;
14461446
if ($(e.currentTarget).hasClass("active")) return;
14471447
const mode = ($(e.currentTarget).attr("mode") ?? "time") as Mode;
14481448
if (mode === undefined) return;
@@ -1453,7 +1453,7 @@ $(".pageTest").on("click", "#testConfig .mode .textButton", (e) => {
14531453
});
14541454

14551455
$(".pageTest").on("click", "#testConfig .wordCount .textButton", (e) => {
1456-
if (TestUI.testRestarting) return;
1456+
if (TestState.testRestarting) return;
14571457
const wrd = $(e.currentTarget).attr("wordCount") ?? "15";
14581458
if (wrd !== "custom") {
14591459
if (UpdateConfig.setWordCount(parseInt(wrd))) {
@@ -1464,7 +1464,7 @@ $(".pageTest").on("click", "#testConfig .wordCount .textButton", (e) => {
14641464
});
14651465

14661466
$(".pageTest").on("click", "#testConfig .time .textButton", (e) => {
1467-
if (TestUI.testRestarting) return;
1467+
if (TestState.testRestarting) return;
14681468
const mode = $(e.currentTarget).attr("timeConfig") ?? "10";
14691469
if (mode !== "custom") {
14701470
if (UpdateConfig.setTimeConfig(parseInt(mode))) {
@@ -1475,7 +1475,7 @@ $(".pageTest").on("click", "#testConfig .time .textButton", (e) => {
14751475
});
14761476

14771477
$(".pageTest").on("click", "#testConfig .quoteLength .textButton", (e) => {
1478-
if (TestUI.testRestarting) return;
1478+
if (TestState.testRestarting) return;
14791479
const lenAttr = $(e.currentTarget).attr("quoteLength");
14801480
if (lenAttr === "all") {
14811481
if (UpdateConfig.setQuoteLengthAll()) {
@@ -1503,15 +1503,15 @@ $(".pageTest").on("click", "#testConfig .quoteLength .textButton", (e) => {
15031503
});
15041504

15051505
$(".pageTest").on("click", "#testConfig .punctuationMode.textButton", () => {
1506-
if (TestUI.testRestarting) return;
1506+
if (TestState.testRestarting) return;
15071507
if (UpdateConfig.setPunctuation(!Config.punctuation)) {
15081508
ManualRestart.set();
15091509
restart();
15101510
}
15111511
});
15121512

15131513
$(".pageTest").on("click", "#testConfig .numbersMode.textButton", () => {
1514-
if (TestUI.testRestarting) return;
1514+
if (TestState.testRestarting) return;
15151515
if (UpdateConfig.setNumbers(!Config.numbers)) {
15161516
ManualRestart.set();
15171517
restart();

frontend/src/ts/test/test-state.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Challenge } from "@monkeytype/schemas/challenges";
2+
import { promiseWithResolvers } from "../utils/misc";
23

34
export let isRepeated = false;
45
export let isPaceRepeat = false;
@@ -12,6 +13,7 @@ export let testInitSuccess = true;
1213
export let lineScrollDistance = 0;
1314
export let isLanguageRightToLeft = false;
1415
export let isDirectionReversed = false;
16+
export let testRestarting = false;
1517

1618
export function setRepeated(tf: boolean): void {
1719
isRepeated = tf;
@@ -68,3 +70,18 @@ export function setIsLanguageRightToLeft(rtl: boolean): void {
6870
export function setIsDirectionReversed(val: boolean): void {
6971
isDirectionReversed = val;
7072
}
73+
74+
let { promise: testRestartingPromise, resolve: restartingResolve } =
75+
promiseWithResolvers();
76+
77+
export { testRestartingPromise };
78+
79+
export function setTestRestarting(val: boolean): void {
80+
testRestarting = val;
81+
if (val) {
82+
({ promise: testRestartingPromise, resolve: restartingResolve } =
83+
promiseWithResolvers());
84+
} else {
85+
restartingResolve();
86+
}
87+
}

frontend/src/ts/test/test-ui.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => {
118118

119119
export let resultVisible = false;
120120
export let activeWordTop = 0;
121-
export let testRestarting = false;
122121
export let lineTransition = false;
123122
export let currentTestLine = 0;
124123
export let resultCalculating = false;
@@ -131,21 +130,6 @@ export function setActiveWordTop(val: number): void {
131130
activeWordTop = val;
132131
}
133132

134-
let { promise: testRestartingPromise, resolve: restartingResolve } =
135-
Misc.promiseWithResolvers();
136-
137-
export { testRestartingPromise };
138-
139-
export function setTestRestarting(val: boolean): void {
140-
testRestarting = val;
141-
if (val) {
142-
({ promise: testRestartingPromise, resolve: restartingResolve } =
143-
Misc.promiseWithResolvers());
144-
} else {
145-
restartingResolve();
146-
}
147-
}
148-
149133
export function setResultCalculating(val: boolean): void {
150134
resultCalculating = val;
151135
}

0 commit comments

Comments
 (0)