redis-smq-common
Version:
RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.
55 lines • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timer = void 0;
const index_js_1 = require("../event/index.js");
const index_js_2 = require("./errors/index.js");
class Timer extends index_js_1.EventEmitter {
constructor() {
super(...arguments);
this.timer = null;
this.onTick = () => {
if (!this.timer)
this.emit('error', new index_js_2.TimerError('Expected a non-empty timer property'));
else {
const { fn, periodic } = this.timer;
if (!periodic)
this.timer = null;
fn();
}
};
}
setTimeout(fn, timeout) {
if (this.timer) {
return false;
}
this.timer = {
timer: setTimeout(() => this.onTick(), timeout),
periodic: false,
fn,
};
return true;
}
setInterval(fn, interval = 1000) {
if (this.timer) {
return false;
}
this.timer = {
timer: setInterval(() => this.onTick(), interval),
periodic: true,
fn,
};
return true;
}
reset() {
if (this.timer) {
const { timer, periodic } = this.timer;
if (periodic)
clearInterval(timer);
else
clearTimeout(timer);
this.timer = null;
}
}
}
exports.Timer = Timer;
//# sourceMappingURL=timer.js.map