| | "use strict"; |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | Object.defineProperty(exports, "__esModule", { value: true }); |
| | exports.BackoffTimeout = void 0; |
| | const INITIAL_BACKOFF_MS = 1000; |
| | const BACKOFF_MULTIPLIER = 1.6; |
| | const MAX_BACKOFF_MS = 120000; |
| | const BACKOFF_JITTER = 0.2; |
| | |
| | |
| | |
| | |
| | |
| | function uniformRandom(min, max) { |
| | return Math.random() * (max - min) + min; |
| | } |
| | class BackoffTimeout { |
| | constructor(callback, options) { |
| | this.callback = callback; |
| | |
| | |
| | |
| | this.initialDelay = INITIAL_BACKOFF_MS; |
| | |
| | |
| | |
| | this.multiplier = BACKOFF_MULTIPLIER; |
| | |
| | |
| | |
| | this.maxDelay = MAX_BACKOFF_MS; |
| | |
| | |
| | |
| | |
| | this.jitter = BACKOFF_JITTER; |
| | |
| | |
| | |
| | this.running = false; |
| | |
| | |
| | |
| | |
| | this.hasRef = true; |
| | |
| | |
| | |
| | |
| | this.startTime = new Date(); |
| | |
| | |
| | |
| | |
| | this.endTime = new Date(); |
| | if (options) { |
| | if (options.initialDelay) { |
| | this.initialDelay = options.initialDelay; |
| | } |
| | if (options.multiplier) { |
| | this.multiplier = options.multiplier; |
| | } |
| | if (options.jitter) { |
| | this.jitter = options.jitter; |
| | } |
| | if (options.maxDelay) { |
| | this.maxDelay = options.maxDelay; |
| | } |
| | } |
| | this.nextDelay = this.initialDelay; |
| | this.timerId = setTimeout(() => { }, 0); |
| | clearTimeout(this.timerId); |
| | } |
| | runTimer(delay) { |
| | var _a, _b; |
| | this.endTime = this.startTime; |
| | this.endTime.setMilliseconds(this.endTime.getMilliseconds() + this.nextDelay); |
| | clearTimeout(this.timerId); |
| | this.timerId = setTimeout(() => { |
| | this.callback(); |
| | this.running = false; |
| | }, delay); |
| | if (!this.hasRef) { |
| | (_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a); |
| | } |
| | } |
| | |
| | |
| | |
| | runOnce() { |
| | this.running = true; |
| | this.startTime = new Date(); |
| | this.runTimer(this.nextDelay); |
| | const nextBackoff = Math.min(this.nextDelay * this.multiplier, this.maxDelay); |
| | const jitterMagnitude = nextBackoff * this.jitter; |
| | this.nextDelay = |
| | nextBackoff + uniformRandom(-jitterMagnitude, jitterMagnitude); |
| | } |
| | |
| | |
| | |
| | |
| | stop() { |
| | clearTimeout(this.timerId); |
| | this.running = false; |
| | } |
| | |
| | |
| | |
| | |
| | reset() { |
| | this.nextDelay = this.initialDelay; |
| | if (this.running) { |
| | const now = new Date(); |
| | const newEndTime = this.startTime; |
| | newEndTime.setMilliseconds(newEndTime.getMilliseconds() + this.nextDelay); |
| | clearTimeout(this.timerId); |
| | if (now < newEndTime) { |
| | this.runTimer(newEndTime.getTime() - now.getTime()); |
| | } |
| | else { |
| | this.running = false; |
| | } |
| | } |
| | } |
| | |
| | |
| | |
| | isRunning() { |
| | return this.running; |
| | } |
| | |
| | |
| | |
| | |
| | ref() { |
| | var _a, _b; |
| | this.hasRef = true; |
| | (_b = (_a = this.timerId).ref) === null || _b === void 0 ? void 0 : _b.call(_a); |
| | } |
| | |
| | |
| | |
| | |
| | unref() { |
| | var _a, _b; |
| | this.hasRef = false; |
| | (_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a); |
| | } |
| | |
| | |
| | |
| | |
| | getEndTime() { |
| | return this.endTime; |
| | } |
| | } |
| | exports.BackoffTimeout = BackoffTimeout; |
| | |