UNPKG

@configurator/ravendb

Version:
45 lines 1.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Timer = void 0; const BluebirdPromise = require("bluebird"); const LogUtil_1 = require("../Utility/LogUtil"); const log = (0, LogUtil_1.getLogger)({ module: "Timer" }); class Timer { 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 = BluebirdPromise.resolve(this._action()) .tapCatch(reason => log.warn(`Error executing timer action ${this._action.name}.`, reason)) .finally(() => log.info(`Finish timer action ${this._action.name}.`)); this._scheduledActionPromise = Promise.resolve(actionPromise); } _clearTimers() { clearTimeout(this._firstTimeDelayId); clearInterval(this._intervalId); } dispose() { log.info(`Dispose ${this._action.name}.`); this._clearTimers(); } } exports.Timer = Timer; //# sourceMappingURL=Timer.js.map