redis-smq-common
Version:
Provides essential components and utilities shared across RedisSMQ packages.
53 lines • 1.57 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timer = void 0;
const node_timers_1 = require("node:timers");
const index_js_1 = require("../runnable/index.js");
class Timer extends index_js_1.Runnable {
constructor(logger) {
super();
this.timer = null;
this.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;
};
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());
}
reset() {
if (this.timer) {
this.logger.debug('Resetting nextTick schedule...');
(0, node_timers_1.clearTimeout)(this.timer);
this.timer = null;
}
}
}
exports.Timer = Timer;
//# sourceMappingURL=timer.js.map