p-queue-compat
Advanced tools
Comparing version 1.0.225 to 1.0.226
@@ -1,13 +0,11 @@ | ||
var __classPrivateFieldSet = this && this.__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 = this && this.__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_pending, _PQueue_concurrency, _PQueue_isPaused, _PQueue_throwOnTimeout, _PQueue_doesIntervalAllowAnother_get, _PQueue_doesConcurrentAllowAnother_get, _PQueue_next, _PQueue_onResumeInterval, _PQueue_isIntervalPaused_get, _PQueue_tryToStartAnother, _PQueue_initializeIntervalIfNeeded, _PQueue_onInterval, _PQueue_processQueue, _PQueue_throwOnAbort, _PQueue_onEvent; | ||
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); } | ||
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } | ||
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } | ||
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } | ||
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); } | ||
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } | ||
function _classPrivateGetter(s, r, a) { return a(_assertClassBrand(s, r)); } | ||
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); } | ||
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; } | ||
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } | ||
import { EventEmitter } from 'eventemitter3'; | ||
@@ -17,40 +15,45 @@ import pTimeout, { TimeoutError } from "p-timeout-compat"; | ||
/** | ||
The error thrown by `queue.add()` when a job is aborted before it is run. See `signal`. | ||
*/ | ||
export class AbortError extends Error {} | ||
/** | ||
Promise queue with concurrency control. | ||
*/ | ||
class PQueue extends EventEmitter { | ||
var _carryoverConcurrencyCount = /*#__PURE__*/new WeakMap(); | ||
var _isIntervalIgnored = /*#__PURE__*/new WeakMap(); | ||
var _intervalCount = /*#__PURE__*/new WeakMap(); | ||
var _intervalCap = /*#__PURE__*/new WeakMap(); | ||
var _interval = /*#__PURE__*/new WeakMap(); | ||
var _intervalEnd = /*#__PURE__*/new WeakMap(); | ||
var _intervalId = /*#__PURE__*/new WeakMap(); | ||
var _timeoutId = /*#__PURE__*/new WeakMap(); | ||
var _queue = /*#__PURE__*/new WeakMap(); | ||
var _queueClass = /*#__PURE__*/new WeakMap(); | ||
var _pending = /*#__PURE__*/new WeakMap(); | ||
var _concurrency = /*#__PURE__*/new WeakMap(); | ||
var _isPaused = /*#__PURE__*/new WeakMap(); | ||
var _throwOnTimeout = /*#__PURE__*/new WeakMap(); | ||
var _PQueue_brand = /*#__PURE__*/new WeakSet(); | ||
export default class PQueue extends EventEmitter { | ||
// TODO: The `throwOnTimeout` option should affect the return types of `add()` and `addAll()` | ||
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_pending.set(this, 0); | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
_classPrivateMethodInitSpec(this, _PQueue_brand); | ||
_classPrivateFieldInitSpec(this, _carryoverConcurrencyCount, void 0); | ||
_classPrivateFieldInitSpec(this, _isIntervalIgnored, void 0); | ||
_classPrivateFieldInitSpec(this, _intervalCount, 0); | ||
_classPrivateFieldInitSpec(this, _intervalCap, void 0); | ||
_classPrivateFieldInitSpec(this, _interval, void 0); | ||
_classPrivateFieldInitSpec(this, _intervalEnd, 0); | ||
_classPrivateFieldInitSpec(this, _intervalId, void 0); | ||
_classPrivateFieldInitSpec(this, _timeoutId, void 0); | ||
_classPrivateFieldInitSpec(this, _queue, void 0); | ||
_classPrivateFieldInitSpec(this, _queueClass, void 0); | ||
_classPrivateFieldInitSpec(this, _pending, 0); | ||
// The `!` is needed because of https://github.com/microsoft/TypeScript/issues/32194 | ||
_PQueue_concurrency.set(this, void 0); | ||
_PQueue_isPaused.set(this, void 0); | ||
_PQueue_throwOnTimeout.set(this, void 0); | ||
_classPrivateFieldInitSpec(this, _concurrency, void 0); | ||
_classPrivateFieldInitSpec(this, _isPaused, void 0); | ||
_classPrivateFieldInitSpec(this, _throwOnTimeout, void 0); | ||
/** | ||
Per-operation timeout in milliseconds. Operations fulfill once `timeout` elapses if they haven't already. | ||
Applies to each future operation. | ||
Applies to each future operation. | ||
*/ | ||
Object.defineProperty(this, "timeout", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
_defineProperty(this, "timeout", void 0); | ||
options = { | ||
@@ -66,20 +69,22 @@ carryoverConcurrencyCount: false, | ||
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})`); | ||
var _options$intervalCap$, _options$intervalCap; | ||
throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${(_options$intervalCap$ = (_options$intervalCap = options.intervalCap) === null || _options$intervalCap === void 0 ? void 0 : _options$intervalCap.toString()) !== null && _options$intervalCap$ !== void 0 ? _options$intervalCap$ : ''}\` (${typeof options.intervalCap})`); | ||
} | ||
if (options.interval === undefined || !(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})`); | ||
var _options$interval$toS, _options$interval; | ||
throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${(_options$interval$toS = (_options$interval = options.interval) === null || _options$interval === void 0 ? void 0 : _options$interval.toString()) !== null && _options$interval$toS !== void 0 ? _options$interval$toS : ''}\` (${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"); | ||
_classPrivateFieldSet(_carryoverConcurrencyCount, this, options.carryoverConcurrencyCount); | ||
_classPrivateFieldSet(_isIntervalIgnored, this, options.intervalCap === Number.POSITIVE_INFINITY || options.interval === 0); | ||
_classPrivateFieldSet(_intervalCap, this, options.intervalCap); | ||
_classPrivateFieldSet(_interval, this, options.interval); | ||
_classPrivateFieldSet(_queue, this, new options.queueClass()); | ||
_classPrivateFieldSet(_queueClass, this, options.queueClass); | ||
this.concurrency = options.concurrency; | ||
this.timeout = options.timeout; | ||
__classPrivateFieldSet(this, _PQueue_throwOnTimeout, options.throwOnTimeout === true, "f"); | ||
__classPrivateFieldSet(this, _PQueue_isPaused, options.autoStart === false, "f"); | ||
_classPrivateFieldSet(_throwOnTimeout, this, options.throwOnTimeout === true); | ||
_classPrivateFieldSet(_isPaused, this, options.autoStart === false); | ||
} | ||
get concurrency() { | ||
return __classPrivateFieldGet(this, _PQueue_concurrency, "f"); | ||
return _classPrivateFieldGet(_concurrency, this); | ||
} | ||
@@ -90,4 +95,4 @@ set concurrency(newConcurrency) { | ||
} | ||
__classPrivateFieldSet(this, _PQueue_concurrency, newConcurrency, "f"); | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this); | ||
_classPrivateFieldSet(_concurrency, this, newConcurrency); | ||
_assertClassBrand(_PQueue_brand, this, _processQueue).call(this); | ||
} | ||
@@ -97,17 +102,13 @@ async add(function_, options = {}) { | ||
timeout: this.timeout, | ||
throwOnTimeout: __classPrivateFieldGet(this, _PQueue_throwOnTimeout, "f"), | ||
throwOnTimeout: _classPrivateFieldGet(_throwOnTimeout, this), | ||
...options | ||
}; | ||
return new Promise((resolve, reject) => { | ||
__classPrivateFieldGet(this, _PQueue_queue, "f").enqueue(async () => { | ||
var _a; | ||
var _b, _c; | ||
__classPrivateFieldSet(this, _PQueue_pending, (_b = __classPrivateFieldGet(this, _PQueue_pending, "f"), _b++, _b), "f"); | ||
__classPrivateFieldSet(this, _PQueue_intervalCount, (_c = __classPrivateFieldGet(this, _PQueue_intervalCount, "f"), _c++, _c), "f"); | ||
_classPrivateFieldGet(_queue, this).enqueue(async () => { | ||
var _this$pending3, _this$pending4, _this$intervalCount, _this$intervalCount2; | ||
_classPrivateFieldSet(_pending, this, (_this$pending3 = _classPrivateFieldGet(_pending, this), _this$pending4 = _this$pending3++, _this$pending3)), _this$pending4; | ||
_classPrivateFieldSet(_intervalCount, this, (_this$intervalCount = _classPrivateFieldGet(_intervalCount, this), _this$intervalCount2 = _this$intervalCount++, _this$intervalCount)), _this$intervalCount2; | ||
try { | ||
// TODO: Use options.signal?.throwIfAborted() when targeting Node.js 18 | ||
if ((_a = options.signal) === null || _a === void 0 ? void 0 : _a.aborted) { | ||
// TODO: Use ABORT_ERR code when targeting Node.js 16 (https://nodejs.org/docs/latest-v16.x/api/errors.html#abort_err) | ||
throw new AbortError('The task was aborted.'); | ||
} | ||
var _options$signal; | ||
(_options$signal = options.signal) === null || _options$signal === void 0 || _options$signal.throwIfAborted(); | ||
let operation = function_({ | ||
@@ -117,6 +118,8 @@ signal: options.signal | ||
if (options.timeout) { | ||
operation = pTimeout(Promise.resolve(operation), options.timeout); | ||
operation = pTimeout(Promise.resolve(operation), { | ||
milliseconds: options.timeout | ||
}); | ||
} | ||
if (options.signal) { | ||
operation = Promise.race([operation, __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_throwOnAbort).call(this, options.signal)]); | ||
operation = Promise.race([operation, _assertClassBrand(_PQueue_brand, this, _throwOnAbort).call(this, options.signal)]); | ||
} | ||
@@ -134,7 +137,7 @@ const result = await operation; | ||
} finally { | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_next).call(this); | ||
_assertClassBrand(_PQueue_brand, this, _next).call(this); | ||
} | ||
}, options); | ||
this.emit('add'); | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this); | ||
_assertClassBrand(_PQueue_brand, this, _tryToStartAnother).call(this); | ||
}); | ||
@@ -149,7 +152,7 @@ } | ||
start() { | ||
if (!__classPrivateFieldGet(this, _PQueue_isPaused, "f")) { | ||
if (!_classPrivateFieldGet(_isPaused, this)) { | ||
return this; | ||
} | ||
__classPrivateFieldSet(this, _PQueue_isPaused, false, "f"); | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this); | ||
_classPrivateFieldSet(_isPaused, this, false); | ||
_assertClassBrand(_PQueue_brand, this, _processQueue).call(this); | ||
return this; | ||
@@ -161,3 +164,3 @@ } | ||
pause() { | ||
__classPrivateFieldSet(this, _PQueue_isPaused, true, "f"); | ||
_classPrivateFieldSet(_isPaused, this, true); | ||
} | ||
@@ -168,3 +171,3 @@ /** | ||
clear() { | ||
__classPrivateFieldSet(this, _PQueue_queue, new (__classPrivateFieldGet(this, _PQueue_queueClass, "f"))(), "f"); | ||
_classPrivateFieldSet(_queue, this, new (_classPrivateFieldGet(_queueClass, this))()); | ||
} | ||
@@ -177,6 +180,6 @@ /** | ||
// Instantly resolve if the queue is empty | ||
if (__classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) { | ||
if (_classPrivateFieldGet(_queue, this).size === 0) { | ||
return; | ||
} | ||
await __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onEvent).call(this, 'empty'); | ||
await _assertClassBrand(_PQueue_brand, this, _onEvent).call(this, 'empty'); | ||
} | ||
@@ -190,6 +193,6 @@ /** | ||
// Instantly resolve if the queue is empty. | ||
if (__classPrivateFieldGet(this, _PQueue_queue, "f").size < limit) { | ||
if (_classPrivateFieldGet(_queue, this).size < limit) { | ||
return; | ||
} | ||
await __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onEvent).call(this, 'next', () => __classPrivateFieldGet(this, _PQueue_queue, "f").size < limit); | ||
await _assertClassBrand(_PQueue_brand, this, _onEvent).call(this, 'next', () => _classPrivateFieldGet(_queue, this).size < limit); | ||
} | ||
@@ -202,6 +205,6 @@ /** | ||
// Instantly resolve if none pending and if nothing else is queued | ||
if (__classPrivateFieldGet(this, _PQueue_pending, "f") === 0 && __classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) { | ||
if (_classPrivateFieldGet(_pending, this) === 0 && _classPrivateFieldGet(_queue, this).size === 0) { | ||
return; | ||
} | ||
await __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onEvent).call(this, 'idle'); | ||
await _assertClassBrand(_PQueue_brand, this, _onEvent).call(this, 'idle'); | ||
} | ||
@@ -212,3 +215,3 @@ /** | ||
get size() { | ||
return __classPrivateFieldGet(this, _PQueue_queue, "f").size; | ||
return _classPrivateFieldGet(_queue, this).size; | ||
} | ||
@@ -221,3 +224,3 @@ /** | ||
// eslint-disable-next-line unicorn/no-array-callback-reference | ||
return __classPrivateFieldGet(this, _PQueue_queue, "f").filter(options).length; | ||
return _classPrivateFieldGet(_queue, this).filter(options).length; | ||
} | ||
@@ -228,3 +231,3 @@ /** | ||
get pending() { | ||
return __classPrivateFieldGet(this, _PQueue_pending, "f"); | ||
return _classPrivateFieldGet(_pending, this); | ||
} | ||
@@ -235,32 +238,36 @@ /** | ||
get isPaused() { | ||
return __classPrivateFieldGet(this, _PQueue_isPaused, "f"); | ||
return _classPrivateFieldGet(_isPaused, this); | ||
} | ||
} | ||
_PQueue_carryoverConcurrencyCount = new WeakMap(), _PQueue_isIntervalIgnored = new WeakMap(), _PQueue_intervalCount = new WeakMap(), _PQueue_intervalCap = new WeakMap(), _PQueue_interval = new WeakMap(), _PQueue_intervalEnd = new WeakMap(), _PQueue_intervalId = new WeakMap(), _PQueue_timeoutId = new WeakMap(), _PQueue_queue = new WeakMap(), _PQueue_queueClass = new WeakMap(), _PQueue_pending = new WeakMap(), _PQueue_concurrency = new WeakMap(), _PQueue_isPaused = new WeakMap(), _PQueue_throwOnTimeout = new WeakMap(), _PQueue_instances = new WeakSet(), _PQueue_doesIntervalAllowAnother_get = function _PQueue_doesIntervalAllowAnother_get() { | ||
return __classPrivateFieldGet(this, _PQueue_isIntervalIgnored, "f") || __classPrivateFieldGet(this, _PQueue_intervalCount, "f") < __classPrivateFieldGet(this, _PQueue_intervalCap, "f"); | ||
}, _PQueue_doesConcurrentAllowAnother_get = function _PQueue_doesConcurrentAllowAnother_get() { | ||
return __classPrivateFieldGet(this, _PQueue_pending, "f") < __classPrivateFieldGet(this, _PQueue_concurrency, "f"); | ||
}, _PQueue_next = function _PQueue_next() { | ||
var _a; | ||
__classPrivateFieldSet(this, _PQueue_pending, (_a = __classPrivateFieldGet(this, _PQueue_pending, "f"), _a--, _a), "f"); | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this); | ||
function _get_doesIntervalAllowAnother(_this) { | ||
return _classPrivateFieldGet(_isIntervalIgnored, _this) || _classPrivateFieldGet(_intervalCount, _this) < _classPrivateFieldGet(_intervalCap, _this); | ||
} | ||
function _get_doesConcurrentAllowAnother(_this2) { | ||
return _classPrivateFieldGet(_pending, _this2) < _classPrivateFieldGet(_concurrency, _this2); | ||
} | ||
function _next() { | ||
var _this$pending, _this$pending2; | ||
_classPrivateFieldSet(_pending, this, (_this$pending = _classPrivateFieldGet(_pending, this), _this$pending2 = _this$pending--, _this$pending)), _this$pending2; | ||
_assertClassBrand(_PQueue_brand, this, _tryToStartAnother).call(this); | ||
this.emit('next'); | ||
}, _PQueue_onResumeInterval = function _PQueue_onResumeInterval() { | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onInterval).call(this); | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_initializeIntervalIfNeeded).call(this); | ||
__classPrivateFieldSet(this, _PQueue_timeoutId, undefined, "f"); | ||
}, _PQueue_isIntervalPaused_get = function _PQueue_isIntervalPaused_get() { | ||
} | ||
function _onResumeInterval() { | ||
_assertClassBrand(_PQueue_brand, this, _onInterval).call(this); | ||
_assertClassBrand(_PQueue_brand, this, _initializeIntervalIfNeeded).call(this); | ||
_classPrivateFieldSet(_timeoutId, this, undefined); | ||
} | ||
function _get_isIntervalPaused(_this3) { | ||
const now = Date.now(); | ||
if (__classPrivateFieldGet(this, _PQueue_intervalId, "f") === undefined) { | ||
const delay = __classPrivateFieldGet(this, _PQueue_intervalEnd, "f") - now; | ||
if (_classPrivateFieldGet(_intervalId, _this3) === undefined) { | ||
const delay = _classPrivateFieldGet(_intervalEnd, _this3) - now; | ||
if (delay < 0) { | ||
// Act as the interval was done | ||
// We don't need to resume it here because it will be resumed on line 160 | ||
__classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pending, "f") : 0, "f"); | ||
_classPrivateFieldSet(_intervalCount, _this3, _classPrivateFieldGet(_carryoverConcurrencyCount, _this3) ? _classPrivateFieldGet(_pending, _this3) : 0); | ||
} else { | ||
// Act as the interval is pending | ||
if (__classPrivateFieldGet(this, _PQueue_timeoutId, "f") === undefined) { | ||
__classPrivateFieldSet(this, _PQueue_timeoutId, setTimeout(() => { | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onResumeInterval).call(this); | ||
}, delay), "f"); | ||
if (_classPrivateFieldGet(_timeoutId, _this3) === undefined) { | ||
_classPrivateFieldSet(_timeoutId, _this3, setTimeout(() => { | ||
_assertClassBrand(_PQueue_brand, _this3, _onResumeInterval).call(_this3); | ||
}, delay)); | ||
} | ||
@@ -271,12 +278,13 @@ return true; | ||
return false; | ||
}, _PQueue_tryToStartAnother = function _PQueue_tryToStartAnother() { | ||
if (__classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) { | ||
} | ||
function _tryToStartAnother() { | ||
if (_classPrivateFieldGet(_queue, this).size === 0) { | ||
// We can clear the interval ("pause") | ||
// Because we can redo it later ("resume") | ||
if (__classPrivateFieldGet(this, _PQueue_intervalId, "f")) { | ||
clearInterval(__classPrivateFieldGet(this, _PQueue_intervalId, "f")); | ||
if (_classPrivateFieldGet(_intervalId, this)) { | ||
clearInterval(_classPrivateFieldGet(_intervalId, this)); | ||
} | ||
__classPrivateFieldSet(this, _PQueue_intervalId, undefined, "f"); | ||
_classPrivateFieldSet(_intervalId, this, undefined); | ||
this.emit('empty'); | ||
if (__classPrivateFieldGet(this, _PQueue_pending, "f") === 0) { | ||
if (_classPrivateFieldGet(_pending, this) === 0) { | ||
this.emit('idle'); | ||
@@ -286,6 +294,6 @@ } | ||
} | ||
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 (!_classPrivateFieldGet(_isPaused, this)) { | ||
const canInitializeInterval = !_classPrivateGetter(_PQueue_brand, this, _get_isIntervalPaused); | ||
if (_classPrivateGetter(_PQueue_brand, this, _get_doesIntervalAllowAnother) && _classPrivateGetter(_PQueue_brand, this, _get_doesConcurrentAllowAnother)) { | ||
const job = _classPrivateFieldGet(_queue, this).dequeue(); | ||
if (!job) { | ||
@@ -297,3 +305,3 @@ return false; | ||
if (canInitializeInterval) { | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_initializeIntervalIfNeeded).call(this); | ||
_assertClassBrand(_PQueue_brand, this, _initializeIntervalIfNeeded).call(this); | ||
} | ||
@@ -304,26 +312,31 @@ return true; | ||
return false; | ||
}, _PQueue_initializeIntervalIfNeeded = function _PQueue_initializeIntervalIfNeeded() { | ||
if (__classPrivateFieldGet(this, _PQueue_isIntervalIgnored, "f") || __classPrivateFieldGet(this, _PQueue_intervalId, "f") !== undefined) { | ||
} | ||
function _initializeIntervalIfNeeded() { | ||
if (_classPrivateFieldGet(_isIntervalIgnored, this) || _classPrivateFieldGet(_intervalId, this) !== undefined) { | ||
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_onInterval() { | ||
if (__classPrivateFieldGet(this, _PQueue_intervalCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_pending, "f") === 0 && __classPrivateFieldGet(this, _PQueue_intervalId, "f")) { | ||
clearInterval(__classPrivateFieldGet(this, _PQueue_intervalId, "f")); | ||
__classPrivateFieldSet(this, _PQueue_intervalId, undefined, "f"); | ||
_classPrivateFieldSet(_intervalId, this, setInterval(() => { | ||
_assertClassBrand(_PQueue_brand, this, _onInterval).call(this); | ||
}, _classPrivateFieldGet(_interval, this))); | ||
_classPrivateFieldSet(_intervalEnd, this, Date.now() + _classPrivateFieldGet(_interval, this)); | ||
} | ||
function _onInterval() { | ||
if (_classPrivateFieldGet(_intervalCount, this) === 0 && _classPrivateFieldGet(_pending, this) === 0 && _classPrivateFieldGet(_intervalId, this)) { | ||
clearInterval(_classPrivateFieldGet(_intervalId, this)); | ||
_classPrivateFieldSet(_intervalId, this, undefined); | ||
} | ||
__classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pending, "f") : 0, "f"); | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this); | ||
}, _PQueue_processQueue = function _PQueue_processQueue() { | ||
_classPrivateFieldSet(_intervalCount, this, _classPrivateFieldGet(_carryoverConcurrencyCount, this) ? _classPrivateFieldGet(_pending, this) : 0); | ||
_assertClassBrand(_PQueue_brand, this, _processQueue).call(this); | ||
} | ||
/** | ||
Executes all queued functions until it reaches the limit. | ||
*/ | ||
function _processQueue() { | ||
// eslint-disable-next-line no-empty | ||
while (__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this)) {} | ||
}, _PQueue_throwOnAbort = async function _PQueue_throwOnAbort(signal) { | ||
while (_assertClassBrand(_PQueue_brand, this, _tryToStartAnother).call(this)) {} | ||
} | ||
async function _throwOnAbort(signal) { | ||
return new Promise((_resolve, reject) => { | ||
signal.addEventListener('abort', () => { | ||
// TODO: Reject with signal.throwIfAborted() when targeting Node.js 18 | ||
// TODO: Use ABORT_ERR code when targeting Node.js 16 (https://nodejs.org/docs/latest-v16.x/api/errors.html#abort_err) | ||
reject(new AbortError('The task was aborted.')); | ||
reject(signal.reason); | ||
}, { | ||
@@ -333,3 +346,4 @@ once: true | ||
}); | ||
}, _PQueue_onEvent = async function _PQueue_onEvent(event, filter) { | ||
} | ||
async function _onEvent(event, filter) { | ||
return new Promise(resolve => { | ||
@@ -345,3 +359,2 @@ const listener = () => { | ||
}); | ||
}; | ||
export default PQueue; | ||
} |
@@ -1,12 +0,4 @@ | ||
var __classPrivateFieldGet = this && this.__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 _PriorityQueue_queue; | ||
import lowerBound from './lower-bound.js'; | ||
class PriorityQueue { | ||
constructor() { | ||
_PriorityQueue_queue.set(this, []); | ||
} | ||
export default class PriorityQueue { | ||
#queue = []; | ||
enqueue(run, options) { | ||
@@ -21,21 +13,19 @@ options = { | ||
}; | ||
if (this.size && __classPrivateFieldGet(this, _PriorityQueue_queue, "f")[this.size - 1].priority >= options.priority) { | ||
__classPrivateFieldGet(this, _PriorityQueue_queue, "f").push(element); | ||
if (this.size && this.#queue[this.size - 1].priority >= options.priority) { | ||
this.#queue.push(element); | ||
return; | ||
} | ||
const index = lowerBound(__classPrivateFieldGet(this, _PriorityQueue_queue, "f"), element, (a, b) => b.priority - a.priority); | ||
__classPrivateFieldGet(this, _PriorityQueue_queue, "f").splice(index, 0, element); | ||
const index = lowerBound(this.#queue, element, (a, b) => b.priority - a.priority); | ||
this.#queue.splice(index, 0, element); | ||
} | ||
dequeue() { | ||
const item = __classPrivateFieldGet(this, _PriorityQueue_queue, "f").shift(); | ||
const item = this.#queue.shift(); | ||
return item === null || item === void 0 ? void 0 : item.run; | ||
} | ||
filter(options) { | ||
return __classPrivateFieldGet(this, _PriorityQueue_queue, "f").filter(element => element.priority === options.priority).map(element => element.run); | ||
return this.#queue.filter(element => element.priority === options.priority).map(element => element.run); | ||
} | ||
get size() { | ||
return __classPrivateFieldGet(this, _PriorityQueue_queue, "f").length; | ||
return this.#queue.length; | ||
} | ||
} | ||
_PriorityQueue_queue = new WeakMap(); | ||
export default PriorityQueue; | ||
} |
import { EventEmitter } from 'eventemitter3'; | ||
import { Queue, RunFunction } from './queue.js'; | ||
import { type Queue, type RunFunction } from './queue.js'; | ||
import PriorityQueue from './priority-queue.js'; | ||
import { QueueAddOptions, Options, TaskOptions } from './options.js'; | ||
import { type QueueAddOptions, type Options, type TaskOptions } from './options.js'; | ||
type Task<TaskResultType> = ((options: TaskOptions) => PromiseLike<TaskResultType>) | ((options: TaskOptions) => TaskResultType); | ||
/** | ||
The error thrown by `queue.add()` when a job is aborted before it is run. See `signal`. | ||
*/ | ||
export declare class AbortError extends Error { | ||
} | ||
type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error'; | ||
@@ -93,2 +88,3 @@ /** | ||
} | ||
export { Queue, QueueAddOptions, QueueAddOptions as DefaultAddOptions, Options }; | ||
export type { Queue } from './queue.js'; | ||
export { type QueueAddOptions, type Options } from './options.js'; |
@@ -1,3 +0,3 @@ | ||
import { Queue, RunFunction } from './queue.js'; | ||
interface TimeoutOptions { | ||
import { type Queue, type RunFunction } from './queue.js'; | ||
type TimeoutOptions = { | ||
/** | ||
@@ -13,4 +13,4 @@ Per-operation timeout in milliseconds. Operations fulfill once `timeout` elapses if they haven't already. | ||
throwOnTimeout?: boolean; | ||
} | ||
export interface Options<QueueType extends Queue<RunFunction, QueueOptions>, QueueOptions extends QueueAddOptions> extends TimeoutOptions { | ||
}; | ||
export type Options<QueueType extends Queue<RunFunction, QueueOptions>, QueueOptions extends QueueAddOptions> = { | ||
/** | ||
@@ -56,4 +56,4 @@ Concurrency limit. | ||
readonly carryoverConcurrencyCount?: boolean; | ||
} | ||
export interface QueueAddOptions extends TaskOptions, TimeoutOptions { | ||
} & TimeoutOptions; | ||
export type QueueAddOptions = { | ||
/** | ||
@@ -65,4 +65,4 @@ Priority of operation. Operations with greater priority will be scheduled first. | ||
readonly priority?: number; | ||
} | ||
export interface TaskOptions { | ||
} & TaskOptions & TimeoutOptions; | ||
export type TaskOptions = { | ||
/** | ||
@@ -104,3 +104,3 @@ [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an `AbortError`. If the operation is already running, the signal will need to be handled by the operation itself. | ||
readonly signal?: AbortSignal; | ||
} | ||
}; | ||
export {}; |
@@ -1,6 +0,6 @@ | ||
import { Queue, RunFunction } from './queue.js'; | ||
import { QueueAddOptions } from './options.js'; | ||
export interface PriorityQueueOptions extends QueueAddOptions { | ||
import { type Queue, type RunFunction } from './queue.js'; | ||
import { type QueueAddOptions } from './options.js'; | ||
export type PriorityQueueOptions = { | ||
priority?: number; | ||
} | ||
} & QueueAddOptions; | ||
export default class PriorityQueue implements Queue<RunFunction, PriorityQueueOptions> { | ||
@@ -7,0 +7,0 @@ #private; |
export type RunFunction = () => Promise<unknown>; | ||
export interface Queue<Element, Options> { | ||
export type Queue<Element, Options> = { | ||
size: number; | ||
filter: (options: Partial<Options>) => Element[]; | ||
filter: (options: Readonly<Partial<Options>>) => Element[]; | ||
dequeue: () => Element | undefined; | ||
enqueue: (run: Element, options?: Partial<Options>) => void; | ||
} | ||
}; |
{ | ||
"name": "p-queue-compat", | ||
"version": "1.0.225", | ||
"version": "1.0.226", | ||
"description": "Compatible version of p-queue", | ||
@@ -76,14 +76,14 @@ "type": "module", | ||
"@babel/preset-env": "^7.14.7", | ||
"@commitlint/cli": "^17.0.0", | ||
"@commitlint/config-conventional": "^17.0.0", | ||
"@semantic-release/changelog": "^6.0.0", | ||
"@semantic-release/commit-analyzer": "^10.0.0", | ||
"@semantic-release/git": "^10.0.0", | ||
"@semantic-release/github": "^9.0.2", | ||
"@semantic-release/npm": "^10.0.2", | ||
"@semantic-release/release-notes-generator": "^11.0.1", | ||
"@commitlint/cli": "^19.0.1", | ||
"@commitlint/config-conventional": "^19.0.0", | ||
"@semantic-release/changelog": "^6.0.3", | ||
"@semantic-release/commit-analyzer": "^9.0.2", | ||
"@semantic-release/git": "^10.0.1", | ||
"@semantic-release/github": "^8.1.0", | ||
"@semantic-release/npm": "^9.0.2", | ||
"@semantic-release/release-notes-generator": "^10.0.3", | ||
"babel-plugin-add-import-extension": "^1.6.0", | ||
"babel-plugin-replace-import-extension": "^1.1.3", | ||
"babel-plugin-search-and-replace": "^1.1.0", | ||
"conventional-changelog-angular": "^7.0.0", | ||
"conventional-changelog-angular": "^8.0.0", | ||
"cz-conventional-changelog": "^3.3.0", | ||
@@ -93,6 +93,6 @@ "delay": "^5.0.0", | ||
"jest-config": "^29.0.0", | ||
"npm-check-updates": "^16.0.0", | ||
"npm-check-updates": "^17.0.0", | ||
"npm-run-all": "^4.1.5", | ||
"p-queue": "^7.1.0", | ||
"semantic-release": "^21.0.0" | ||
"p-queue": "^8.0.0", | ||
"semantic-release": "^22.0.0" | ||
}, | ||
@@ -99,0 +99,0 @@ "release": { |
@@ -38,3 +38,3 @@ # p-queue-compat | ||
**Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) or use the [dynamic `import()`](https://v8.dev/features/dynamic-import) function. Please don't open issues for questions regarding CommonJS / ESM. | ||
**Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). Please don't open issues for questions regarding CommonJS / ESM. | ||
@@ -164,8 +164,6 @@ ## Usage | ||
*Requires Node.js 16 or later.* | ||
[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an [error](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/reason). If the operation is already running, the signal will need to be handled by the operation itself. | ||
[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an `AbortError`. If the operation is already running, the signal will need to be handled by the operation itself. | ||
```js | ||
import PQueue, {AbortError} from 'p-queue'; | ||
import PQueue from 'p-queue'; | ||
import got, {CancelError} from 'got'; | ||
@@ -194,3 +192,3 @@ | ||
} catch (error) { | ||
if (!(error instanceof AbortError)) { | ||
if (!(error instanceof DOMException)) { | ||
throw error; | ||
@@ -405,6 +403,2 @@ } | ||
### AbortError | ||
The error thrown by `queue.add()` when a job is aborted before it is run. See [`signal`](#signal). | ||
## Advanced example | ||
@@ -411,0 +405,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
62368
987
522