From aac0dd5f6b4d6bc8c3d1c8e4b1c10a8750aea7a1 Mon Sep 17 00:00:00 2001 From: Ayc0 <benjamin.koltes@gmail.com> Date: Tue, 10 Apr 2018 14:38:32 +0200 Subject: [PATCH] feat: add start, startNow, restart, restartNow, stop in interval --- front/src/style.css | 6 ++--- server/index.js | 2 +- server/utils.js | 62 ++++++++++++++++++++++++++++++--------------- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/front/src/style.css b/front/src/style.css index e0eaae4..29ef3cc 100644 --- a/front/src/style.css +++ b/front/src/style.css @@ -55,10 +55,8 @@ justify-content: space-between; } -.image-text-bloc > .text-section > .text-element:first-child { - flex-grow: 1; -} - .image-text-bloc > .text-section > .text-element:last-child { white-space: nowrap; + flex-grow: 1; + text-align: right; } diff --git a/server/index.js b/server/index.js index a9a740d..7efe6a4 100644 --- a/server/index.js +++ b/server/index.js @@ -41,6 +41,6 @@ io.of('/').on('connection', (socket) => { }); socket.on('disconnect', () => { - chrono.clear(); + chrono.stop(); }); }); diff --git a/server/utils.js b/server/utils.js index 0716563..ebc1057 100644 --- a/server/utils.js +++ b/server/utils.js @@ -2,14 +2,6 @@ const jwt = require('jsonwebtoken'); const { cert } = require('./config'); -const initialOutput = { id: null, clear: () => undefined, trigger: () => undefined }; - -const replace = (toReplace, replacer) => { - Object.keys(replacer).forEach((x) => { - toReplace[x] = replacer[x]; - }); -}; - function Timeout(fn, interval) { this.id = setTimeout(fn, interval); this.cleared = false; @@ -19,7 +11,7 @@ function Timeout(fn, interval) { }; } -const interval = (fn, initialTTL, output = initialOutput) => { +const interval = (fn, initialTTL, output = {}) => { const getTimeout = ttl => new Timeout(async () => { let TTL; @@ -41,18 +33,48 @@ const interval = (fn, initialTTL, output = initialOutput) => { const nextTTL = parseInt(TTL, 10) || initialTTL; interval(fn, nextTTL, output); }, ttl); - const getOutput = timeout => ({ - id: timeout.id, - clear: () => timeout.clear(), - trigger: () => { - timeout.clear(); - const newTimeout = getTimeout(0); - replace(output, getOutput(newTimeout)); - }, - }); - const timeout = getTimeout(initialTTL); - replace(output, getOutput(timeout)); + const getOutput = (timeout) => { + const stop = () => { + if (timeout) { + timeout.clear(); + } + }; + return { + start: (ttl) => { + if (timeout && !timeout.cleared) { + throw new Error('Interval is already started'); + } + const newOutput = getOutput(getTimeout(ttl || initialTTL)); + Object.assign(output, getOutput(newOutput)); + return output; + }, + startNow: () => { + if (timeout && !timeout.cleared) { + throw new Error('Interval is already started'); + } + Object.assign(output, getOutput(getTimeout(0))); + return output; + }, + restart: (ttl) => { + stop(); + Object.assign(output, getOutput(getTimeout(ttl || initialTTL))); + return output; + }, + stop, + restartNow: () => { + stop(); + Object.assign(output, getOutput(getTimeout(0))); + return output; + }, + }; + }; + + if (Object.keys(output).length === 0) { + Object.assign(output, getOutput(null)); + } else { + Object.assign(output, getOutput(getTimeout(initialTTL))); + } return output; }; -- GitLab