Skip to content

Instantly share code, notes, and snippets.

@bpalij
Last active August 22, 2022 15:25
Show Gist options
  • Save bpalij/959ba72a9e54aff22720e1b5a8cbab80 to your computer and use it in GitHub Desktop.
Save bpalij/959ba72a9e54aff22720e1b5a8cbab80 to your computer and use it in GitHub Desktop.
AsyncIntervalTimer
const minDelay = 1000; // it will be possible to control it finishing with setInterval 0
export class AsyncIntervalTimer {
private realDelay: number;
// eslint-disable-next-line @typescript-eslint/ban-types
private fn: Function;
private args: Array<any>;
private fnIsRunning: boolean;
private interval: ReturnType<typeof setInterval>;
private intervalIsActive: boolean;
private runFn(...args) {
if (!this.fnIsRunning) {
this.fnIsRunning = true;
return (
Promise.resolve(this.fn(...args))
// eslint-disable-next-line @typescript-eslint/no-empty-function
.then(() => {})
.catch((e) => {
throw e;
})
.finally(() => {
this.fnIsRunning = false;
})
);
} else {
return Promise.resolve(undefined);
}
}
doNow() {
return this.runFn(...this.args);
}
start() {
if (this.intervalIsActive) {
return;
}
this.interval = setInterval(
this.runFn.bind(this),
this.realDelay,
...this.args,
);
this.intervalIsActive = true;
}
stop() {
if (!this.intervalIsActive) {
return Promise.resolve(undefined);
}
return new Promise((resolve) => {
const localInterval = setInterval(() => {
if (this.fnIsRunning) {
return;
}
clearInterval(this.interval);
clearInterval(localInterval);
this.interval = undefined;
this.intervalIsActive = false;
resolve(undefined);
}, 0);
});
}
constructor(fn, delay, ...args) {
if (typeof fn !== 'function') {
throw new Error(
'AsyncIntervalTimer constructor requires timer as first parameter',
);
}
this.fn = fn;
this.args = args;
this.fnIsRunning = false;
this.realDelay =
delay && typeof delay === 'number' && delay >= minDelay
? delay
: minDelay;
this.intervalIsActive = false;
}
}
Copyright 2022 Bohdan Palii
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment