From 467cbb84140103fcdd0ad97c728584d1d6156229 Mon Sep 17 00:00:00 2001 From: wang9hu Date: Wed, 25 Oct 2023 18:31:29 -0700 Subject: [PATCH] remove 2h default time when clear timer --- src/websockets/TimerService/clientsHandler.js | 77 +------------------ 1 file changed, 1 insertion(+), 76 deletions(-) diff --git a/src/websockets/TimerService/clientsHandler.js b/src/websockets/TimerService/clientsHandler.js index 6990ead71..e9e932fd7 100644 --- a/src/websockets/TimerService/clientsHandler.js +++ b/src/websockets/TimerService/clientsHandler.js @@ -4,13 +4,6 @@ const moment = require('moment'); const Timer = require('../../models/timer'); const logger = require('../../startup/logger'); -/** - * Here we get the timer. - * If the timer already exists in memory, we return it. - * If it doesn't exist, we try to get it from MongoDB. - * If it doesn't exist in MongoDB, we create it and save it to MongoDB. - * Then we save it to memory and return it. - */ export const getClient = async (clients, userId) => { // In case of there is already a connection that is open for this user // for example user open a new connection @@ -29,13 +22,6 @@ export const getClient = async (clients, userId) => { return clients.get(userId); }; -/** - * Save client info to database - * Save under these conditions: - * connection is normally closed (paused and closed); - * connection is forced-paused (timer still on and connection closed) - * message: STOP_TIMER - */ export const saveClient = async (client) => { try { await Timer.findOneAndUpdate({ userId: client.userId }, client); @@ -47,10 +33,6 @@ export const saveClient = async (client) => { } }; -/** - * This is the contract between client and server. - * The client can send one of the following messages to the server: - */ export const action = { START_TIMER: 'START_TIMER', PAUSE_TIMER: 'PAUSE_TIMER', @@ -75,12 +57,6 @@ const updatedTimeSinceStart = (client) => { return updatedTime > 0 ? updatedTime : 0; }; -/** - * Here we start the timer, if it is not already started. - * We set the last access time to now, and set the paused and stopped flags to false. - * If the timer was paused, we need to check if it was paused by the user or by the server. - * If it was paused by the server, we need to set the forcedPause flag to true. - */ const startTimer = (client) => { client.startAt = moment.utc(); client.paused = false; @@ -91,31 +67,19 @@ const startTimer = (client) => { if (client.forcedPause) client.forcedPause = false; }; -/** - * Here we pause the timer, if it is not already paused. - * We get the total elapsed time since the last access, and set it as the new time. - * We set the last access time to now, and set the paused flag to true. - * If the timer was paused by the server, we need to set the forcedPause flag to true. - * It'll only be triggered when the user closes the connection sudenlly or lacks of ACKs. - */ const pauseTimer = (client, forced = false) => { client.time = updatedTimeSinceStart(client); - client.startAt = moment.invalid(); + client.startAt = moment.invalid(); // invalid can not be saved in database client.paused = true; if (forced) client.forcedPause = true; }; -// Here we acknowledge the forced pause. To prevent the modal for beeing displayed again. const ackForcedPause = (client) => { client.forcedPause = false; client.paused = true; client.startAt = moment.invalid(); }; -/** - * Here we stop the timer. - * We pause the timer and set the stopped flag to true. - */ const stopTimer = (client) => { client.startAt = moment.invalid(); client.started = false; @@ -123,25 +87,11 @@ const stopTimer = (client) => { client.forcedPause = false; }; -/** - * Here we clear the timer. - * We pause the timer and check it's mode to set the time to 0 or the goal. - * Then we set the stopped flag to false. - */ const clearTimer = (client) => { stopTimer(client); - client.goal = moment.duration(2, 'hours').asMilliseconds(); client.time = client.goal; }; -// Here we set the goal and time to the goal time. -/** - * Here we set the goal. - * if timer has not started, we set both time and goal to the new goal - * if timer has started, we calculate the passed time and remove that from new goal - * and if passed time is greater than new goal, then set time to 0, but this should - * not be prohibited by frontend. - */ const setGoal = (client, msg) => { const newGoal = parseInt(msg.split('=')[1]); if (!client.started) { @@ -159,13 +109,6 @@ const setGoal = (client, msg) => { } }; -/** - * Here we add the goal time. - * Each addition add 15min - * First we get the goal time from the message. - * Then we add it to the current goal time and set it as the new goal time. - * We also add it to the current time and set it as the new time. - */ const addGoal = (client, msg) => { const duration = parseInt(msg.split('=')[1]); const goalAfterAddition = moment @@ -187,14 +130,6 @@ const addGoal = (client, msg) => { .toFixed(); }; -/** - * Here we try to remove a goal time. - * First we get the goal time from the message. - * Then we subtract it from the current goal time and set it as the new goal time. - * We also subtract it from the current time and set it as the new time. - * If the new goal time is less than 15 minutes, we don't do anything. - * If the new time is less than 0, we set it to 0. - */ const removeGoal = (client, msg) => { const duration = parseInt(msg.split('=')[1]); const goalAfterRemoval = moment @@ -220,16 +155,6 @@ const removeGoal = (client, msg) => { .toFixed(); }; - -/** - * Here is were we handle the messages. - * First we check if the user is in memory, if not, we throw an error. - * Then we parse the request and check which action it is and call the corresponding function. - * If we don't have a match, we just return an error. - * The only operation that we write to Mongo it's the stop timer. Other operations are just in memory. - * So the slowest part of the app is the save to Mongo. - * Then we update the current client in hash map and return the response. - */ export const handleMessage = async (msg, clients, userId) => { if (!clients.has(userId)) { throw new Error('It should have this user in memory');