UNPKG

ravendb

Version:
56 lines 1.69 kB
import { getLogger } from "../Utility/LogUtil.js"; const log = getLogger({ module: "Timer" }); export 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(); } } //# sourceMappingURL=Timer.js.map