added timer event

removes 1 circular dependency
part of #2462
This commit is contained in:
Miodec 2022-02-11 17:08:07 +01:00
parent 913bed3352
commit 2b6adfd46a
3 changed files with 28 additions and 6 deletions

View file

@ -0,0 +1,16 @@
const subscribers = [];
export function subscribe(fn) {
subscribers.push(fn);
}
export function dispatch(key, value, value2) {
subscribers.forEach((fn) => {
try {
fn(key, value, value2);
} catch (e) {
console.error("Timer event subscriber threw an error");
console.error(e);
}
});
}

View file

@ -45,6 +45,7 @@ import * as TestState from "./test-state";
import * as ModesNotice from "./../elements/modes-notice";
import * as PageTransition from "./../states/page-transition";
import * as ConfigEvent from "./../observables/config-event";
import * as TimerEvent from "./../observables/timer-event";
const objecthash = require("node-object-hash")().hash;
@ -1654,4 +1655,9 @@ $(document).ready(() => {
if (eventKey === "showAllLines" && !nosave) restart();
if (eventKey === "keymapMode" && !nosave) restart(false, nosave);
});
TimerEvent.subscribe((eventKey, eventValue) => {
if (eventKey === "fail") fail(eventValue);
if (eventKey === "finish") finish();
});
});

View file

@ -11,11 +11,11 @@ import * as TestWords from "./test-words";
import * as Monkey from "./monkey";
import * as Misc from "../misc";
import * as Notifications from "../elements/notifications";
import * as TestLogic from "./test-logic";
import * as Caret from "./caret";
import * as SlowTimer from "../states/slow-timer";
import * as TestActive from "./../states/test-active";
import * as Time from "./../states/time";
import * as TimerEvent from "./../observables/timer-event";
let slowTimerCount = 0;
let timer = null;
@ -52,7 +52,7 @@ function updateTimer() {
function calculateWpmRaw() {
if (timerDebug) console.time("calculate wpm and raw");
let wpmAndRaw = TestLogic.calculateWpmAndRaw();
let wpmAndRaw = TestStats.calculateWpmAndRaw();
if (timerDebug) console.timeEnd("calculate wpm and raw");
if (timerDebug) console.time("update live wpm");
LiveWpm.update(wpmAndRaw.wpm, wpmAndRaw.raw);
@ -126,9 +126,9 @@ function checkIfFailed(wpmAndRaw, acc) {
TestWords.words.currentIndex > 3
) {
clearTimeout(timer);
TestLogic.fail("min wpm");
SlowTimer.clear();
slowTimerCount = 0;
TimerEvent.dispatch("fail", "min wpm");
return;
}
if (
@ -137,9 +137,9 @@ function checkIfFailed(wpmAndRaw, acc) {
TestWords.words.currentIndex > 3
) {
clearTimeout(timer);
TestLogic.fail("min accuracy");
SlowTimer.clear();
slowTimerCount = 0;
TimerEvent.dispatch("fail", "min accuracy");
return;
}
if (timerDebug) console.timeEnd("fail conditions");
@ -164,9 +164,9 @@ function checkIfTimeIsUp() {
Caret.hide();
TestInput.input.pushHistory();
TestInput.corrected.pushHistory();
TestLogic.finish();
SlowTimer.clear();
slowTimerCount = 0;
TimerEvent.dispatch("finish");
return;
}
}
@ -224,7 +224,7 @@ export async function start() {
"Stopping the test due to bad performance. This would cause test calculations to be incorrect. If this happens a lot, please report this.",
-1
);
TestLogic.fail("slow timer");
TimerEvent.dispatch("fail", "slow timer");
}
}
}