UNPKG

@esm2cjs/p-queue

Version:

Promise queue with concurrency control. This is a fork of sindresorhus/p-queue, but with CommonJS support.

289 lines (288 loc) 15.8 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var esm_exports = {}; __export(esm_exports, { AbortError: () => AbortError, default: () => PQueue }); module.exports = __toCommonJS(esm_exports); var import_eventemitter3 = __toESM(require("eventemitter3")); var import_p_timeout = __toESM(require("@esm2cjs/p-timeout")); var import_priority_queue = __toESM(require("./priority-queue.js")); var __classPrivateFieldSet = function(receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value; }; var __classPrivateFieldGet = function(receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _PQueue_instances, _PQueue_carryoverConcurrencyCount, _PQueue_isIntervalIgnored, _PQueue_intervalCount, _PQueue_intervalCap, _PQueue_interval, _PQueue_intervalEnd, _PQueue_intervalId, _PQueue_timeoutId, _PQueue_queue, _PQueue_queueClass, _PQueue_pendingCount, _PQueue_concurrency, _PQueue_isPaused, _PQueue_throwOnTimeout, _PQueue_doesIntervalAllowAnother_get, _PQueue_doesConcurrentAllowAnother_get, _PQueue_next, _PQueue_emitEvents, _PQueue_onResumeInterval, _PQueue_isIntervalPaused_get, _PQueue_tryToStartAnother, _PQueue_initializeIntervalIfNeeded, _PQueue_onInterval, _PQueue_processQueue, _PQueue_onEvent; const timeoutError = new import_p_timeout.TimeoutError(); class AbortError extends Error { } class PQueue extends import_eventemitter3.default { constructor(options) { var _a, _b, _c, _d; super(); _PQueue_instances.add(this); _PQueue_carryoverConcurrencyCount.set(this, void 0); _PQueue_isIntervalIgnored.set(this, void 0); _PQueue_intervalCount.set(this, 0); _PQueue_intervalCap.set(this, void 0); _PQueue_interval.set(this, void 0); _PQueue_intervalEnd.set(this, 0); _PQueue_intervalId.set(this, void 0); _PQueue_timeoutId.set(this, void 0); _PQueue_queue.set(this, void 0); _PQueue_queueClass.set(this, void 0); _PQueue_pendingCount.set(this, 0); _PQueue_concurrency.set(this, void 0); _PQueue_isPaused.set(this, void 0); _PQueue_throwOnTimeout.set(this, void 0); Object.defineProperty(this, "timeout", { enumerable: true, configurable: true, writable: true, value: void 0 }); options = { carryoverConcurrencyCount: false, intervalCap: Number.POSITIVE_INFINITY, interval: 0, concurrency: Number.POSITIVE_INFINITY, autoStart: true, queueClass: import_priority_queue.default, ...options }; if (!(typeof options.intervalCap === "number" && options.intervalCap >= 1)) { throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${(_b = (_a = options.intervalCap) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ""}\` (${typeof options.intervalCap})`); } if (options.interval === void 0 || !(Number.isFinite(options.interval) && options.interval >= 0)) { throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${(_d = (_c = options.interval) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : ""}\` (${typeof options.interval})`); } __classPrivateFieldSet(this, _PQueue_carryoverConcurrencyCount, options.carryoverConcurrencyCount, "f"); __classPrivateFieldSet(this, _PQueue_isIntervalIgnored, options.intervalCap === Number.POSITIVE_INFINITY || options.interval === 0, "f"); __classPrivateFieldSet(this, _PQueue_intervalCap, options.intervalCap, "f"); __classPrivateFieldSet(this, _PQueue_interval, options.interval, "f"); __classPrivateFieldSet(this, _PQueue_queue, new options.queueClass(), "f"); __classPrivateFieldSet(this, _PQueue_queueClass, options.queueClass, "f"); this.concurrency = options.concurrency; this.timeout = options.timeout; __classPrivateFieldSet(this, _PQueue_throwOnTimeout, options.throwOnTimeout === true, "f"); __classPrivateFieldSet(this, _PQueue_isPaused, options.autoStart === false, "f"); } get concurrency() { return __classPrivateFieldGet(this, _PQueue_concurrency, "f"); } set concurrency(newConcurrency) { if (!(typeof newConcurrency === "number" && newConcurrency >= 1)) { throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${newConcurrency}\` (${typeof newConcurrency})`); } __classPrivateFieldSet(this, _PQueue_concurrency, newConcurrency, "f"); __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this); } async add(fn, options = {}) { return new Promise((resolve, reject) => { const run = async () => { var _a; var _b, _c; __classPrivateFieldSet(this, _PQueue_pendingCount, (_b = __classPrivateFieldGet(this, _PQueue_pendingCount, "f"), _b++, _b), "f"); __classPrivateFieldSet(this, _PQueue_intervalCount, (_c = __classPrivateFieldGet(this, _PQueue_intervalCount, "f"), _c++, _c), "f"); try { if ((_a = options.signal) === null || _a === void 0 ? void 0 : _a.aborted) { reject(new AbortError("The task was aborted.")); return; } const operation = this.timeout === void 0 && options.timeout === void 0 ? fn({ signal: options.signal }) : (0, import_p_timeout.default)(Promise.resolve(fn({ signal: options.signal })), options.timeout === void 0 ? this.timeout : options.timeout, () => { if (options.throwOnTimeout === void 0 ? __classPrivateFieldGet(this, _PQueue_throwOnTimeout, "f") : options.throwOnTimeout) { reject(timeoutError); } return void 0; }); const result = await operation; resolve(result); this.emit("completed", result); } catch (error) { reject(error); this.emit("error", error); } __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_next).call(this); }; __classPrivateFieldGet(this, _PQueue_queue, "f").enqueue(run, options); __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this); this.emit("add"); }); } async addAll(functions, options) { return Promise.all(functions.map(async (function_) => this.add(function_, options))); } start() { if (!__classPrivateFieldGet(this, _PQueue_isPaused, "f")) { return this; } __classPrivateFieldSet(this, _PQueue_isPaused, false, "f"); __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this); return this; } pause() { __classPrivateFieldSet(this, _PQueue_isPaused, true, "f"); } clear() { __classPrivateFieldSet(this, _PQueue_queue, new (__classPrivateFieldGet(this, _PQueue_queueClass, "f"))(), "f"); } async onEmpty() { if (__classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) { return; } await __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onEvent).call(this, "empty"); } async onSizeLessThan(limit) { if (__classPrivateFieldGet(this, _PQueue_queue, "f").size < limit) { return; } await __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onEvent).call(this, "next", () => __classPrivateFieldGet(this, _PQueue_queue, "f").size < limit); } async onIdle() { if (__classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) { return; } await __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onEvent).call(this, "idle"); } get size() { return __classPrivateFieldGet(this, _PQueue_queue, "f").size; } sizeBy(options) { return __classPrivateFieldGet(this, _PQueue_queue, "f").filter(options).length; } get pending() { return __classPrivateFieldGet(this, _PQueue_pendingCount, "f"); } get isPaused() { return __classPrivateFieldGet(this, _PQueue_isPaused, "f"); } } _PQueue_carryoverConcurrencyCount = /* @__PURE__ */ new WeakMap(), _PQueue_isIntervalIgnored = /* @__PURE__ */ new WeakMap(), _PQueue_intervalCount = /* @__PURE__ */ new WeakMap(), _PQueue_intervalCap = /* @__PURE__ */ new WeakMap(), _PQueue_interval = /* @__PURE__ */ new WeakMap(), _PQueue_intervalEnd = /* @__PURE__ */ new WeakMap(), _PQueue_intervalId = /* @__PURE__ */ new WeakMap(), _PQueue_timeoutId = /* @__PURE__ */ new WeakMap(), _PQueue_queue = /* @__PURE__ */ new WeakMap(), _PQueue_queueClass = /* @__PURE__ */ new WeakMap(), _PQueue_pendingCount = /* @__PURE__ */ new WeakMap(), _PQueue_concurrency = /* @__PURE__ */ new WeakMap(), _PQueue_isPaused = /* @__PURE__ */ new WeakMap(), _PQueue_throwOnTimeout = /* @__PURE__ */ new WeakMap(), _PQueue_instances = /* @__PURE__ */ new WeakSet(), _PQueue_doesIntervalAllowAnother_get = function _PQueue_doesIntervalAllowAnother_get2() { return __classPrivateFieldGet(this, _PQueue_isIntervalIgnored, "f") || __classPrivateFieldGet(this, _PQueue_intervalCount, "f") < __classPrivateFieldGet(this, _PQueue_intervalCap, "f"); }, _PQueue_doesConcurrentAllowAnother_get = function _PQueue_doesConcurrentAllowAnother_get2() { return __classPrivateFieldGet(this, _PQueue_pendingCount, "f") < __classPrivateFieldGet(this, _PQueue_concurrency, "f"); }, _PQueue_next = function _PQueue_next2() { var _a; __classPrivateFieldSet(this, _PQueue_pendingCount, (_a = __classPrivateFieldGet(this, _PQueue_pendingCount, "f"), _a--, _a), "f"); __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this); this.emit("next"); }, _PQueue_emitEvents = function _PQueue_emitEvents2() { this.emit("empty"); if (__classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0) { this.emit("idle"); } }, _PQueue_onResumeInterval = function _PQueue_onResumeInterval2() { __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onInterval).call(this); __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_initializeIntervalIfNeeded).call(this); __classPrivateFieldSet(this, _PQueue_timeoutId, void 0, "f"); }, _PQueue_isIntervalPaused_get = function _PQueue_isIntervalPaused_get2() { const now = Date.now(); if (__classPrivateFieldGet(this, _PQueue_intervalId, "f") === void 0) { const delay = __classPrivateFieldGet(this, _PQueue_intervalEnd, "f") - now; if (delay < 0) { __classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pendingCount, "f") : 0, "f"); } else { if (__classPrivateFieldGet(this, _PQueue_timeoutId, "f") === void 0) { __classPrivateFieldSet(this, _PQueue_timeoutId, setTimeout(() => { __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onResumeInterval).call(this); }, delay), "f"); } return true; } } return false; }, _PQueue_tryToStartAnother = function _PQueue_tryToStartAnother2() { if (__classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) { if (__classPrivateFieldGet(this, _PQueue_intervalId, "f")) { clearInterval(__classPrivateFieldGet(this, _PQueue_intervalId, "f")); } __classPrivateFieldSet(this, _PQueue_intervalId, void 0, "f"); __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_emitEvents).call(this); return false; } if (!__classPrivateFieldGet(this, _PQueue_isPaused, "f")) { const canInitializeInterval = !__classPrivateFieldGet(this, _PQueue_instances, "a", _PQueue_isIntervalPaused_get); if (__classPrivateFieldGet(this, _PQueue_instances, "a", _PQueue_doesIntervalAllowAnother_get) && __classPrivateFieldGet(this, _PQueue_instances, "a", _PQueue_doesConcurrentAllowAnother_get)) { const job = __classPrivateFieldGet(this, _PQueue_queue, "f").dequeue(); if (!job) { return false; } this.emit("active"); job(); if (canInitializeInterval) { __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_initializeIntervalIfNeeded).call(this); } return true; } } return false; }, _PQueue_initializeIntervalIfNeeded = function _PQueue_initializeIntervalIfNeeded2() { if (__classPrivateFieldGet(this, _PQueue_isIntervalIgnored, "f") || __classPrivateFieldGet(this, _PQueue_intervalId, "f") !== void 0) { return; } __classPrivateFieldSet(this, _PQueue_intervalId, setInterval(() => { __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onInterval).call(this); }, __classPrivateFieldGet(this, _PQueue_interval, "f")), "f"); __classPrivateFieldSet(this, _PQueue_intervalEnd, Date.now() + __classPrivateFieldGet(this, _PQueue_interval, "f"), "f"); }, _PQueue_onInterval = function _PQueue_onInterval2() { if (__classPrivateFieldGet(this, _PQueue_intervalCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_intervalId, "f")) { clearInterval(__classPrivateFieldGet(this, _PQueue_intervalId, "f")); __classPrivateFieldSet(this, _PQueue_intervalId, void 0, "f"); } __classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pendingCount, "f") : 0, "f"); __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this); }, _PQueue_processQueue = function _PQueue_processQueue2() { while (__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this)) { } }, _PQueue_onEvent = async function _PQueue_onEvent2(event, filter) { return new Promise((resolve) => { const listener = () => { if (filter && !filter()) { return; } this.off(event, listener); resolve(); }; this.on(event, listener); }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AbortError }); //# sourceMappingURL=index.js.map