From 2b6adfd46accc631deebde56d0f36e18cdff37b0 Mon Sep 17 00:00:00 2001 From: Miodec Date: Fri, 11 Feb 2022 17:08:07 +0100 Subject: [PATCH] added timer event removes 1 circular dependency part of #2462 --- frontend/src/js/observables/timer-event.js | 16 ++++++++++++++++ frontend/src/js/test/test-logic.js | 6 ++++++ frontend/src/js/test/test-timer.js | 12 ++++++------ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 frontend/src/js/observables/timer-event.js diff --git a/frontend/src/js/observables/timer-event.js b/frontend/src/js/observables/timer-event.js new file mode 100644 index 000000000..52d2950c1 --- /dev/null +++ b/frontend/src/js/observables/timer-event.js @@ -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); + } + }); +} diff --git a/frontend/src/js/test/test-logic.js b/frontend/src/js/test/test-logic.js index 2005f82a2..7a1248051 100644 --- a/frontend/src/js/test/test-logic.js +++ b/frontend/src/js/test/test-logic.js @@ -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(); + }); }); diff --git a/frontend/src/js/test/test-timer.js b/frontend/src/js/test/test-timer.js index 6c9f35ec2..ed7375de7 100644 --- a/frontend/src/js/test/test-timer.js +++ b/frontend/src/js/test/test-timer.js @@ -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"); } } }