UNPKG

alm

Version:

The best IDE for TypeScript

62 lines (53 loc) 1.18 kB
export class RunOnceScheduler { private timeoutToken: any; private runner: () => void; private timeout: number; private timeoutHandler: () => void; constructor(runner: () => void, timeout: number) { this.timeoutToken = -1; this.runner = runner; this.timeout = timeout; this.timeoutHandler = this.onTimeout.bind(this); } /** * Dispose RunOnceScheduler */ dispose(): void { this.cancel(); this.runner = null; } /** * Cancel current scheduled runner (if any). */ cancel(): void { if (this.isScheduled()) { clearTimeout(this.timeoutToken); this.timeoutToken = -1; } } /** * Replace runner. If there is a runner already scheduled, the new runner will be called. */ setRunner(runner: () => void): void { this.runner = runner; } /** * Cancel previous runner (if any) & schedule a new runner. */ schedule(delay = this.timeout): void { this.cancel(); this.timeoutToken = setTimeout(this.timeoutHandler, this.timeout); } /** * Returns true if scheduled. */ isScheduled(): boolean { return this.timeoutToken !== -1; } private onTimeout() { this.timeoutToken = -1; if (this.runner) { this.runner(); } } }