Last active
November 5, 2020 20:35
-
-
Save ilyahuman/a253b6a57e7de9e5db26fe98524280bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Timer { | |
name: string; | |
delay: number; | |
interval: boolean; | |
cb: Function; | |
} | |
interface TimerForCalling { | |
name: string; | |
interval: boolean; | |
delay: number; | |
id: number | null; | |
cb: Function | |
timer: () => void | |
} | |
interface Log { | |
name: String, | |
in: any[], | |
out: any | |
created: string, | |
} | |
class TimersManager { | |
public timers: any[] = []; | |
public logs: Log[] = []; | |
add(timer: Timer, ...args: any) { | |
const logs = this.logs; | |
const { name, delay, interval, cb } = timer; | |
let tempTimer: TimerForCalling = { | |
name, | |
interval, | |
delay, | |
id: null, | |
cb, | |
timer: function() {} | |
}; | |
if (interval) { | |
tempTimer.timer = function() { | |
this.id = setInterval(() => { | |
const result = args ? cb(...args) : cb(); | |
const log: Log = { | |
name: this.name, | |
in: [...args], | |
out: result, | |
created: new Date().toJSON().slice(0,10).replace(/-/g,'/') | |
} | |
logs.push(log) | |
}, delay); | |
} | |
} else { | |
tempTimer.timer = function() { | |
this.id = setTimeout(cb, delay); | |
} | |
} | |
this.timers.push(tempTimer); | |
return this; | |
} | |
remove(name: string) { | |
this.timers = this.timers.filter((timer: TimerForCalling) => { | |
if (timer.name === name) { | |
timer.interval ? clearInterval(timer.id as number) : clearTimeout(timer.id as number) | |
return; | |
} | |
return timer.name !== name; | |
}) | |
return this; | |
} | |
start(fullStop: boolean = false) { | |
this.timers.forEach((timer: TimerForCalling) => timer.timer()); | |
if (fullStop) { | |
const commonTimeout = this.timers.reduce((acc: number, timer: TimerForCalling) => { | |
return acc + timer.delay; | |
}, 0) + 10000; | |
setTimeout(() => { | |
this.stop(); | |
console.log('All timers have been stopped!') | |
}, commonTimeout) | |
} | |
} | |
stop() { | |
this.timers.forEach((timer: TimerForCalling) => { | |
timer.interval ? clearInterval(timer.id as number) : clearTimeout(timer.id as number) | |
}) | |
this.timers = []; | |
} | |
pause(name: string) { | |
const timer: TimerForCalling = this.timers.find((timer: TimerForCalling) => timer.name === name); | |
timer.interval ? clearInterval(timer.id as number) : clearTimeout(timer.id as number); | |
} | |
resume(name: string) { | |
const timer: TimerForCalling = this.timers.find((timer: TimerForCalling) => timer.name === name); | |
timer.timer(); | |
} | |
print() { | |
return this.logs; | |
} | |
} | |
//Example | |
const timers = new TimersManager(); | |
const timer1: Timer = { | |
name: 'Check user', | |
delay: 2000, | |
interval: true, | |
cb: (a: number, b: number) => console.log(a + b) | |
} | |
timers.add(timer1, 5, 6) | |
timers.start(); | |
const iddd= setInterval(() => { | |
console.log(timers.print()) | |
}, 2000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment