From bd8f4de8a7ff455500408c608a32f81ddb4c79a3 Mon Sep 17 00:00:00 2001 From: wang9hu Date: Sun, 29 Oct 2023 14:49:00 -0700 Subject: [PATCH] make timer chime consistent over all tabs --- src/models/timer.js | 1 + src/websockets/TimerService/clientsHandler.js | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/models/timer.js b/src/models/timer.js index 09c2f89da..f50921fb3 100644 --- a/src/models/timer.js +++ b/src/models/timer.js @@ -9,6 +9,7 @@ const timerSchema = new Schema({ time: { type: Number, default: 900000 }, goal: { type: Number, default: 900000 }, initialGoal: { type: Number, default: 900000 }, + chiming: { type: Boolean, default: false }, paused: { type: Boolean, default: false }, forcedPause: { type: Boolean, default: false }, started: { type: Boolean, default: false }, diff --git a/src/websockets/TimerService/clientsHandler.js b/src/websockets/TimerService/clientsHandler.js index 3bb2c358d..60eb33fa4 100644 --- a/src/websockets/TimerService/clientsHandler.js +++ b/src/websockets/TimerService/clientsHandler.js @@ -43,6 +43,7 @@ export const action = { REMOVE_GOAL: 'REMOVE_FROM_GOAL=', FORCED_PAUSE: 'FORCED_PAUSE', ACK_FORCED: 'ACK_FORCED', + START_CHIME: 'START_CHIME', }; const MAX_HOURS = 5; @@ -69,11 +70,17 @@ const startTimer = (client) => { const pauseTimer = (client, forced = false) => { client.time = updatedTimeSinceStart(client); + if (client.time === 0) client.chiming = true; client.startAt = moment.invalid(); // invalid can not be saved in database client.paused = true; if (forced) client.forcedPause = true; }; +const startChime = (client, msg) => { + const state = msg.split('=')[1]; + client.chiming = state === 'true'; +}; + const ackForcedPause = (client) => { client.forcedPause = false; client.paused = true; @@ -86,6 +93,7 @@ const stopTimer = (client) => { client.started = false; client.pause = false; client.forcedPause = false; + if (client.chiming) client.chiming = false; if (client.time === 0) { client.goal = client.initialGoal; client.time = client.goal; @@ -97,6 +105,7 @@ const stopTimer = (client) => { const clearTimer = (client) => { stopTimer(client); client.goal = client.initialGoal; + client.chiming = false; client.time = client.goal; }; @@ -175,6 +184,9 @@ export const handleMessage = async (msg, clients, userId) => { case req.match(/REMOVE_FROM_GOAL=/i)?.input: removeGoal(client, req); break; + case req.match(/START_CHIME=/i)?.input: + startChime(client, req); + break; case action.PAUSE_TIMER: pauseTimer(client); break;