Skip to content

Commit

Permalink
make timer chime consistent over all tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
wang9hu committed Oct 29, 2023
1 parent 29e9bb2 commit bd8f4de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/models/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
12 changes: 12 additions & 0 deletions src/websockets/TimerService/clientsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -97,6 +105,7 @@ const stopTimer = (client) => {
const clearTimer = (client) => {
stopTimer(client);
client.goal = client.initialGoal;
client.chiming = false;
client.time = client.goal;
};

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bd8f4de

Please sign in to comment.