diff --git a/src/lib/polling.ts b/src/lib/polling.ts new file mode 100644 index 0000000..fb04bc3 --- /dev/null +++ b/src/lib/polling.ts @@ -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; \ No newline at end of file