UNPKG

redis-smq-common

Version:

Provides essential components and utilities shared across RedisSMQ packages.

50 lines 1.35 kB
import { clearTimeout } from 'node:timers'; import { Runnable } from '../runnable/index.js'; export class Timer extends Runnable { timer = null; logger; constructor(logger) { super(); this.logger = logger.createLogger(this.constructor.name); } goingUp() { return super.goingUp().concat([ (cb) => { this.reset(); cb(); }, ]); } goingDown() { return [ (cb) => { this.reset(); cb(); }, ].concat(super.goingDown()); } schedule = (fn, delayMs) => { if (!this.isRunning()) { this.logger.debug('Instance not operational - Skipping next tick scheduling'); return false; } if (this.timer) { this.logger.debug('Could not schedule the requested operation: timer is busy'); return false; } this.timer = setTimeout(() => { this.timer = null; if (this.isRunning()) fn(); }, delayMs); return true; }; reset() { if (this.timer) { this.logger.debug('Resetting nextTick schedule...'); clearTimeout(this.timer); this.timer = null; } } } //# sourceMappingURL=timer.js.map