UNPKG

@hisorange/resistor

Version:

Versatily resource load throttler with extensible strategies, configuration and virtual thread management.

32 lines 1.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IntervalStrategy = void 0; /** * Respecting the minimum delay between executions. */ class IntervalStrategy { constructor(config) { this.monitor = new Map(); this.config = { interval: 1000, }; this.config = { ...this.config, ...config }; } async handleWaitPass(threadId, waitPass) { var _a; // Retrive the last finish epoch, so we can calculate the wait time until the next invokation. const lastFinishedAt = (_a = this.monitor.get(threadId)) !== null && _a !== void 0 ? _a : 0; // Wait the minimum delay between execution const delay = Math.max(0, this.config.interval - (Date.now() - lastFinishedAt)); // Respect the minimum wait between the calls in single thread mode. if (delay > 0) { await new Promise(delayPass => setTimeout(delayPass, delay)); } waitPass(); } threadFinished(threadId, finishedAt) { this.monitor.set(threadId, finishedAt); } } exports.IntervalStrategy = IntervalStrategy; //# sourceMappingURL=interval.strategy.js.map