added polling class
This commit is contained in:
49
src/lib/polling.ts
Normal file
49
src/lib/polling.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
type callback = (...args: any[]) => void
|
||||
|
||||
class Polling {
|
||||
private static instance: Polling;
|
||||
private timeoutID: number;
|
||||
private delay: number = 1000;
|
||||
private enabled: boolean = false;
|
||||
constructor() {
|
||||
this.enabled = false;
|
||||
}
|
||||
static create(): Polling {
|
||||
this.instance = this.instance || new Polling();
|
||||
return this.instance;
|
||||
}
|
||||
|
||||
enable() {
|
||||
this.enabled = true;
|
||||
return this;
|
||||
}
|
||||
disable() {
|
||||
this.enabled = false;
|
||||
return this;
|
||||
}
|
||||
isEnabled() {
|
||||
return this.enabled;
|
||||
}
|
||||
setDelay(time: number): Polling {
|
||||
this.delay = time;
|
||||
return this;
|
||||
}
|
||||
|
||||
run(callback: callback, ...args: any[]) {
|
||||
this.clear().enable()
|
||||
callback(...args);
|
||||
let timeoutHandler = () => {
|
||||
if (this.enabled) {
|
||||
this.run(callback, ...args);
|
||||
}
|
||||
}
|
||||
this.timeoutID = window.setTimeout(timeoutHandler, this.delay);
|
||||
}
|
||||
clear() {
|
||||
if (this.timeoutID)
|
||||
window.clearTimeout(this.timeoutID);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export default Polling;
|
||||
Reference in New Issue
Block a user