Skip to content

Commit a0a09cc

Browse files
committed
refactor: move active word state to test-state
1 parent 01dee3f commit a0a09cc

File tree

9 files changed

+48
-57
lines changed

9 files changed

+48
-57
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ function backspaceToPrevious(): void {
123123

124124
const wordElements = document.querySelectorAll("#words > .word");
125125
if (
126-
(TestInput.input.history[TestWords.words.currentIndex - 1] ==
127-
TestWords.words.get(TestWords.words.currentIndex - 1) &&
126+
(TestInput.input.history[TestState.activeWordIndex - 1] ==
127+
TestWords.words.get(TestState.activeWordIndex - 1) &&
128128
!Config.freedomMode) ||
129-
wordElements[TestWords.words.currentIndex - 1]?.classList.contains("hidden")
129+
wordElements[TestState.activeWordIndex - 1]?.classList.contains("hidden")
130130
) {
131131
return;
132132
}
@@ -136,7 +136,7 @@ function backspaceToPrevious(): void {
136136
}
137137

138138
const incorrectLetterBackspaced =
139-
wordElements[TestWords.words.currentIndex]?.children[0]?.classList.contains(
139+
wordElements[TestState.activeWordIndex]?.children[0]?.classList.contains(
140140
"incorrect"
141141
);
142142
if (Config.stopOnError === "letter" && incorrectLetterBackspaced) {
@@ -149,7 +149,7 @@ function backspaceToPrevious(): void {
149149
TestInput.input.current = TestInput.input.current.slice(0, -1);
150150
setWordsInput(" " + TestInput.input.current + " ");
151151
}
152-
TestWords.words.decreaseCurrentIndex();
152+
TestState.decreaseActiveWordIndex();
153153
TestUI.setActiveWordElementIndex(TestUI.activeWordElementIndex - 1);
154154
TestUI.updateActiveElement(true);
155155
Funbox.toggleScript(TestWords.words.getCurrent());
@@ -219,10 +219,10 @@ async function handleSpace(): Promise<void> {
219219
}
220220
PaceCaret.handleSpace(true, currentWord);
221221
TestInput.input.pushHistory();
222-
TestWords.words.increaseCurrentIndex();
222+
TestState.increaseActiveWordIndex();
223223
Funbox.toggleScript(TestWords.words.getCurrent());
224224
TestInput.incrementKeypressCount();
225-
TestInput.pushKeypressWord(TestWords.words.currentIndex);
225+
TestInput.pushKeypressWord(TestState.activeWordIndex);
226226
if (!nospace) {
227227
void Sound.playClick();
228228
}
@@ -271,10 +271,10 @@ async function handleSpace(): Promise<void> {
271271
TestUI.highlightBadWord(TestUI.activeWordElementIndex);
272272
}
273273
TestInput.input.pushHistory();
274-
TestWords.words.increaseCurrentIndex();
274+
TestState.increaseActiveWordIndex();
275275
Funbox.toggleScript(TestWords.words.getCurrent());
276276
TestInput.incrementKeypressCount();
277-
TestInput.pushKeypressWord(TestWords.words.currentIndex);
277+
TestInput.pushKeypressWord(TestState.activeWordIndex);
278278
Replay.addReplayEvent("submitErrorWord");
279279
if (Config.difficulty === "expert" || Config.difficulty === "master") {
280280
TestLogic.fail("difficulty");
@@ -283,7 +283,7 @@ async function handleSpace(): Promise<void> {
283283

284284
TestInput.corrected.pushHistory();
285285

286-
const isLastWord = TestWords.words.currentIndex === TestWords.words.length;
286+
const isLastWord = TestState.activeWordIndex === TestWords.words.length;
287287
if (TestLogic.areAllTestWordsGenerated() && isLastWord) {
288288
void TestLogic.finish();
289289
return;
@@ -656,7 +656,7 @@ function handleChar(
656656
}
657657

658658
TestInput.incrementKeypressCount();
659-
TestInput.pushKeypressWord(TestWords.words.currentIndex);
659+
TestInput.pushKeypressWord(TestState.activeWordIndex);
660660

661661
if (
662662
Config.difficulty !== "master" &&
@@ -682,7 +682,7 @@ function handleChar(
682682
? TestInput.input.current.length
683683
: Hangul.disassemble(TestInput.input.current).length;
684684
//update the active word top, but only once
685-
if (testInputLength === 1 && TestWords.words.currentIndex === 0) {
685+
if (testInputLength === 1 && TestState.activeWordIndex === 0) {
686686
TestUI.setActiveWordTop(activeWord?.offsetTop);
687687
}
688688

@@ -707,7 +707,7 @@ function handleChar(
707707
//auto stop the test if the last word is correct
708708
//do not stop if not all characters have been parsed by handleChar yet
709709
const currentWord = TestWords.words.getCurrent();
710-
const lastWordIndex = TestWords.words.currentIndex;
710+
const lastWordIndex = TestState.activeWordIndex;
711711
const lastWord = lastWordIndex === TestWords.words.length - 1;
712712
const allWordGenerated = TestLogic.areAllTestWordsGenerated();
713713
const wordIsTheSame = currentWord === TestInput.input.current;
@@ -1061,8 +1061,8 @@ $(document).on("keydown", async (event) => {
10611061
TestInput.input.current.slice(-1),
10621062
TestInput.input.current.length - 1
10631063
) &&
1064-
(TestInput.input.history[TestWords.words.currentIndex - 1] !=
1065-
TestWords.words.get(TestWords.words.currentIndex - 1) ||
1064+
(TestInput.input.history[TestState.activeWordIndex - 1] !=
1065+
TestWords.words.get(TestState.activeWordIndex - 1) ||
10661066
Config.freedomMode)
10671067
) {
10681068
TestInput.input.current = "";

frontend/src/ts/test/funbox/funbox-functions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as JSONData from "../../utils/json-data";
2121
import { getSection } from "../wikipedia";
2222
import * as WeakSpot from "../weak-spot";
2323
import * as IPAddresses from "../../utils/ip-addresses";
24+
import * as TestState from "../test-state";
2425

2526
export type FunboxFunctions = {
2627
getWord?: (wordset?: Wordset, wordIndex?: number) => string;
@@ -62,8 +63,8 @@ async function readAheadHandleKeydown(
6263
event.key == "Backspace" &&
6364
!isCorrect &&
6465
(TestInput.input.current != "" ||
65-
TestInput.input.history[TestWords.words.currentIndex - 1] !=
66-
TestWords.words.get(TestWords.words.currentIndex - 1) ||
66+
TestInput.input.history[TestState.activeWordIndex - 1] !=
67+
TestWords.words.get(TestState.activeWordIndex - 1) ||
6768
Config.freedomMode)
6869
) {
6970
$("#words").addClass("read_ahead_disabled");
@@ -350,8 +351,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
350351
(TestInput.input.history.length + 1) / wordsPerLayout
351352
);
352353
const mod =
353-
wordsPerLayout -
354-
((TestWords.words.currentIndex + 1) % wordsPerLayout);
354+
wordsPerLayout - ((TestState.activeWordIndex + 1) % wordsPerLayout);
355355

356356
if (layouts[index] as string) {
357357
if (mod <= 3 && (layouts[index + 1] as string)) {

frontend/src/ts/test/pace-caret.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export async function update(expectedStepEnd: number): Promise<void> {
202202
try {
203203
const newIndex =
204204
settings.currentWordIndex -
205-
(TestWords.words.currentIndex - TestUI.activeWordElementIndex);
205+
(TestState.activeWordIndex - TestUI.activeWordElementIndex);
206206
const word = document.querySelectorAll("#words .word")[
207207
newIndex
208208
] as HTMLElement;
@@ -305,19 +305,19 @@ export function handleSpace(correct: boolean, currentWord: string): void {
305305
if (correct) {
306306
if (
307307
settings !== null &&
308-
settings.wordsStatus[TestWords.words.currentIndex] === true &&
308+
settings.wordsStatus[TestState.activeWordIndex] === true &&
309309
!Config.blindMode
310310
) {
311-
settings.wordsStatus[TestWords.words.currentIndex] = undefined;
311+
settings.wordsStatus[TestState.activeWordIndex] = undefined;
312312
settings.correction -= currentWord.length + 1;
313313
}
314314
} else {
315315
if (
316316
settings !== null &&
317-
settings.wordsStatus[TestWords.words.currentIndex] === undefined &&
317+
settings.wordsStatus[TestState.activeWordIndex] === undefined &&
318318
!Config.blindMode
319319
) {
320-
settings.wordsStatus[TestWords.words.currentIndex] = true;
320+
settings.wordsStatus[TestState.activeWordIndex] = true;
321321
settings.correction += currentWord.length + 1;
322322
}
323323
}

frontend/src/ts/test/test-input.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as TestWords from "./test-words";
21
import { lastElementFromArray } from "../utils/arrays";
32
import { mean, roundTo2 } from "@monkeytype/util/numbers";
3+
import * as TestState from "./test-state";
44

55
const keysToTrack = [
66
"NumpadMultiply",
@@ -97,31 +97,24 @@ type ErrorHistoryObject = {
9797
class Input {
9898
current: string;
9999
history: string[];
100-
historyLength: number;
101100
koreanStatus: boolean;
102-
length: number;
103101
constructor() {
104102
this.current = "";
105103
this.history = [];
106-
this.historyLength = 0;
107-
this.length = 0;
108104
this.koreanStatus = false;
109105
}
110106

111107
reset(): void {
112108
this.current = "";
113109
this.history = [];
114-
this.length = 0;
115110
}
116111

117112
resetHistory(): void {
118113
this.history = [];
119-
this.length = 0;
120114
}
121115

122116
setCurrent(val: string): void {
123117
this.current = val;
124-
this.length = this.current.length;
125118
}
126119

127120
setKoreanStatus(val: boolean): void {
@@ -130,7 +123,6 @@ class Input {
130123

131124
appendCurrent(val: string): void {
132125
this.current += val;
133-
this.length = this.current.length;
134126
}
135127

136128
resetCurrent(): void {
@@ -147,13 +139,11 @@ class Input {
147139

148140
pushHistory(): void {
149141
this.history.push(this.current);
150-
this.historyLength = this.history.length;
151142
this.resetCurrent();
152143
}
153144

154145
popHistory(): string {
155146
const ret = this.history.pop() ?? "";
156-
this.historyLength = this.history.length;
157147
return ret;
158148
}
159149

@@ -427,11 +417,11 @@ export function pushToRawHistory(raw: number): void {
427417
}
428418

429419
export function pushBurstToHistory(speed: number): void {
430-
if (burstHistory[TestWords.words.currentIndex] === undefined) {
420+
if (burstHistory[TestState.activeWordIndex] === undefined) {
431421
burstHistory.push(speed);
432422
} else {
433423
//repeated word - override
434-
burstHistory[TestWords.words.currentIndex] = speed;
424+
burstHistory[TestState.activeWordIndex] = speed;
435425
}
436426
}
437427

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ export async function init(): Promise<void> {
393393
MonkeyPower.reset();
394394
Replay.stopReplayRecording();
395395
TestWords.words.reset();
396+
TestState.setActiveWordIndex(0);
396397
TestUI.setActiveWordElementIndex(0);
397398
TestInput.input.resetHistory();
398399
TestInput.input.resetCurrent();
@@ -563,7 +564,7 @@ export async function addWord(): Promise<void> {
563564
);
564565

565566
if (sectionFunbox?.functions?.pullSection) {
566-
if (TestWords.words.length - TestWords.words.currentIndex < 20) {
567+
if (TestWords.words.length - TestState.activeWordIndex < 20) {
567568
const section = await sectionFunbox.functions.pullSection(
568569
Config.language
569570
);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export let activeChallenge: null | Challenge = null;
77
export let savingEnabled = true;
88
export let bailedOut = false;
99
export let selectedQuoteId = 1;
10+
export let activeWordIndex = 0;
1011

1112
export function setRepeated(tf: boolean): void {
1213
isRepeated = tf;
@@ -35,3 +36,15 @@ export function setBailedOut(tf: boolean): void {
3536
export function setSelectedQuoteId(id: number): void {
3637
selectedQuoteId = id;
3738
}
39+
40+
export function setActiveWordIndex(index: number): void {
41+
activeWordIndex = index;
42+
}
43+
44+
export function increaseActiveWordIndex(): void {
45+
activeWordIndex++;
46+
}
47+
48+
export function decreaseActiveWordIndex(): void {
49+
activeWordIndex--;
50+
}

frontend/src/ts/test/test-timer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as TimerProgress from "./timer-progress";
77
import * as LiveWpm from "./live-speed";
88
import * as TestStats from "./test-stats";
99
import * as TestInput from "./test-input";
10-
import * as TestWords from "./test-words";
1110
import * as Monkey from "./monkey";
1211
import * as Numbers from "@monkeytype/util/numbers";
1312
import * as Notifications from "../elements/notifications";
@@ -137,7 +136,7 @@ function checkIfFailed(
137136
if (
138137
Config.minWpm === "custom" &&
139138
wpmAndRaw.wpm < Config.minWpmCustomSpeed &&
140-
TestWords.words.currentIndex > 3
139+
TestState.activeWordIndex > 3
141140
) {
142141
if (timer !== null) clearTimeout(timer);
143142
SlowTimer.clear();

frontend/src/ts/test/test-words.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { QuoteWithTextSplit } from "../controllers/quotes-controller";
2+
import * as TestState from "./test-state";
23

34
class Words {
45
public list: string[];
56
public sectionIndexList: number[];
67
public length: number;
7-
public currentIndex: number;
88

99
constructor() {
1010
this.list = [];
1111
this.sectionIndexList = [];
1212
this.length = 0;
13-
this.currentIndex = 0;
1413
}
1514

1615
get(i?: undefined, raw?: boolean): string[];
@@ -27,7 +26,7 @@ class Words {
2726
}
2827
}
2928
getCurrent(): string {
30-
return this.list[this.currentIndex] ?? "";
29+
return this.list[TestState.activeWordIndex] ?? "";
3130
}
3231
getLast(): string {
3332
return this.list[this.list.length - 1] as string;
@@ -41,18 +40,8 @@ class Words {
4140
reset(): void {
4241
this.list = [];
4342
this.sectionIndexList = [];
44-
this.currentIndex = 0;
4543
this.length = this.list.length;
4644
}
47-
resetCurrentIndex(): void {
48-
this.currentIndex = 0;
49-
}
50-
decreaseCurrentIndex(): void {
51-
this.currentIndex--;
52-
}
53-
increaseCurrentIndex(): void {
54-
this.currentIndex++;
55-
}
5645
clean(): void {
5746
for (const s of this.list) {
5847
if (/ +/.test(s)) {

frontend/src/ts/test/timer-progress.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ const miniTimerNumberElement = miniEl[0] as HTMLElement;
9090
function getCurrentCount(): number {
9191
if (Config.mode === "custom" && CustomText.getLimitMode() === "section") {
9292
return (
93-
(TestWords.words.sectionIndexList[
94-
TestWords.words.currentIndex
95-
] as number) - 1
93+
(TestWords.words.sectionIndexList[TestState.activeWordIndex] as number) -
94+
1
9695
);
9796
} else {
9897
return TestInput.input.history.length;
@@ -152,7 +151,7 @@ export function update(): void {
152151
}
153152
if (Config.timerStyle === "bar") {
154153
const percent = Math.floor(
155-
((TestWords.words.currentIndex + 1) / outof) * 100
154+
((TestState.activeWordIndex + 1) / outof) * 100
156155
);
157156
barEl.stop(true, true).animate(
158157
{

0 commit comments

Comments
 (0)