igir
Version:
🕹 A zero-setup ROM collection manager that sorts, filters, extracts or archives, patches, and reports on collections of any size on any OS.
42 lines (41 loc) • 1.03 kB
JavaScript
import { clearTimeout } from 'node:timers';
/**
* A wrapper to centrally manage Node.js timeouts.
*/
export default class Timer {
static TIMERS = new Set();
timeoutId;
constructor(timeoutId) {
this.timeoutId = timeoutId;
Timer.TIMERS.add(this);
}
static setTimeout(runnable, timeoutMillis) {
const timer = new Timer(setTimeout(() => {
runnable();
Timer.TIMERS.delete(timer);
}, timeoutMillis));
return timer;
}
static setInterval(runnable, timeoutMillis) {
const timer = new Timer(setInterval(() => {
runnable();
Timer.TIMERS.delete(timer);
}, timeoutMillis));
return timer;
}
/**
* Cancel all pending timeouts.
*/
static cancelAll() {
Timer.TIMERS.forEach((timer) => {
timer.cancel();
});
}
/**
* Cancel this timeout.
*/
cancel() {
clearTimeout(this.timeoutId);
Timer.TIMERS.delete(this);
}
}