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 @@
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;
}
......@@ -41,6 +41,6 @@ io.of('/').on('connection', (socket) => {
});
socket.on('disconnect', () => {
chrono.clear();
chrono.stop();
});
});
......@@ -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: () => {
const getOutput = (timeout) => {
const stop = () => {
if (timeout) {
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);
replace(output, getOutput(timeout));
if (Object.keys(output).length === 0) {
Object.assign(output, getOutput(null));
} else {
Object.assign(output, getOutput(getTimeout(initialTTL)));
}
return output;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment