ravendb
Version:
RavenDB client for Node.js
60 lines • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timer = void 0;
const LogUtil_js_1 = require("../Utility/LogUtil.js");
const log = (0, LogUtil_js_1.getLogger)({ module: "Timer" });
class Timer {
_action;
_scheduledActionPromise;
_firstTimeDelayId;
_intervalId;
_intervalTimerId;
/** period in milliseconds */
_periodInMs;
constructor(action, dueTimeInMs, periodInMs) {
this._action = action;
this._periodInMs = periodInMs;
if (dueTimeInMs != null) {
this._schedule(dueTimeInMs);
}
}
change(dueTimeInMs, period) {
this._periodInMs = period;
this._clearTimers();
this._schedule(dueTimeInMs);
}
_schedule(dueTimeInMs) {
this._firstTimeDelayId = setTimeout(() => {
if (this._periodInMs) {
this._intervalId = setInterval(() => this._timerAction(), this._periodInMs);
}
this._timerAction();
}, dueTimeInMs);
}
_timerAction() {
log.info(`Start timer action ${this._action.name}`);
const actionPromise = async () => {
try {
return await this._action();
}
catch (e) {
log.warn(`Error executing timer action ${this._action.name}.`, e);
throw e;
}
finally {
log.info(`Finish timer action ${this._action.name}.`);
}
};
this._scheduledActionPromise = actionPromise();
}
_clearTimers() {
clearTimeout(this._firstTimeDelayId);
clearInterval(this._intervalId);
}
dispose() {
log.info(`Dispose ${this._action.name}.`);
this._clearTimers();
}
}
exports.Timer = Timer;
//# sourceMappingURL=Timer.js.map