Skip to content

Commit fd177c9

Browse files
byseif21Miodec
andauthored
chore: TypeError when deleting in zen (@byseif21) (monkeytypegame#7282)
* fix error from race condition `Cannot read properties of null (reading 'remove')` when deletion in zen mode, added null check. The active word could already be removed when the debounced update runs, which caused a null error --------- Co-authored-by: Jack <[email protected]>
1 parent a1240d3 commit fd177c9

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,17 @@ export function updateActiveElement(
133133

134134
let previousActiveWordTop: number | null = null;
135135
if (initial === undefined) {
136-
const previousActiveWord = wordsEl.querySelector(
137-
".active",
138-
) as HTMLElement;
139-
if (direction === "forward") {
140-
previousActiveWord.classList.add("typed");
141-
} else if (direction === "back") {
142-
if (Config.mode === "zen") {
143-
previousActiveWord.remove();
136+
const previousActiveWord = wordsEl.querySelector<HTMLElement>(".active");
137+
// in zen mode, because of the animation frame, previousActiveWord will be removed at this point, so check for null
138+
if (previousActiveWord !== null) {
139+
if (direction === "forward") {
140+
previousActiveWord.classList.add("typed");
141+
} else if (direction === "back") {
142+
//
144143
}
144+
previousActiveWord.classList.remove("active");
145+
previousActiveWordTop = previousActiveWord.offsetTop;
145146
}
146-
previousActiveWord.classList.remove("active");
147-
previousActiveWordTop = previousActiveWord.offsetTop;
148147
}
149148

150149
const newActiveWord = getActiveWordElement();

0 commit comments

Comments
 (0)