Skip to content
Snippets Groups Projects
Commit aac0dd5f authored by Ayc0's avatar Ayc0
Browse files

feat: add start, startNow, restart, restartNow, stop in interval

parent 835203e4
No related branches found
No related tags found
No related merge requests found
...@@ -55,10 +55,8 @@ ...@@ -55,10 +55,8 @@
justify-content: space-between; 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 { .image-text-bloc > .text-section > .text-element:last-child {
white-space: nowrap; white-space: nowrap;
flex-grow: 1;
text-align: right;
} }
...@@ -41,6 +41,6 @@ io.of('/').on('connection', (socket) => { ...@@ -41,6 +41,6 @@ io.of('/').on('connection', (socket) => {
}); });
socket.on('disconnect', () => { socket.on('disconnect', () => {
chrono.clear(); chrono.stop();
}); });
}); });
...@@ -2,14 +2,6 @@ ...@@ -2,14 +2,6 @@
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const { cert } = require('./config'); 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) { function Timeout(fn, interval) {
this.id = setTimeout(fn, interval); this.id = setTimeout(fn, interval);
this.cleared = false; this.cleared = false;
...@@ -19,7 +11,7 @@ function Timeout(fn, interval) { ...@@ -19,7 +11,7 @@ function Timeout(fn, interval) {
}; };
} }
const interval = (fn, initialTTL, output = initialOutput) => { const interval = (fn, initialTTL, output = {}) => {
const getTimeout = ttl => const getTimeout = ttl =>
new Timeout(async () => { new Timeout(async () => {
let TTL; let TTL;
...@@ -41,18 +33,48 @@ const interval = (fn, initialTTL, output = initialOutput) => { ...@@ -41,18 +33,48 @@ const interval = (fn, initialTTL, output = initialOutput) => {
const nextTTL = parseInt(TTL, 10) || initialTTL; const nextTTL = parseInt(TTL, 10) || initialTTL;
interval(fn, nextTTL, output); interval(fn, nextTTL, output);
}, ttl); }, ttl);
const getOutput = timeout => ({
id: timeout.id, const getOutput = (timeout) => {
clear: () => timeout.clear(), const stop = () => {
trigger: () => { if (timeout) {
timeout.clear(); timeout.clear();
const newTimeout = getTimeout(0); }
replace(output, getOutput(newTimeout)); };
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;
},
};
};
const timeout = getTimeout(initialTTL); if (Object.keys(output).length === 0) {
replace(output, getOutput(timeout)); Object.assign(output, getOutput(null));
} else {
Object.assign(output, getOutput(getTimeout(initialTTL)));
}
return output; return output;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment