p-queue-compat
Advanced tools
Comparing version 1.0.219 to 1.0.220
@@ -12,7 +12,6 @@ var __classPrivateFieldSet = this && this.__classPrivateFieldSet || function (receiver, state, value, kind, f) { | ||
}; | ||
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; | ||
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; | ||
import EventEmitter from 'eventemitter3'; | ||
import pTimeout, { TimeoutError } from "p-timeout-compat"; | ||
import PriorityQueue from './priority-queue.js'; | ||
const timeoutError = new TimeoutError(); | ||
/** | ||
@@ -26,2 +25,3 @@ The error thrown by `queue.add()` when a job is aborted before it is run. See `signal`. | ||
export default class PQueue extends EventEmitter { | ||
// TODO: The `throwOnTimeout` option should affect the return types of `add()` and `addAll()` | ||
constructor(options) { | ||
@@ -41,3 +41,3 @@ var _a, _b, _c, _d; | ||
_PQueue_queueClass.set(this, void 0); | ||
_PQueue_pendingCount.set(this, 0); | ||
_PQueue_pending.set(this, 0); | ||
// The `!` is needed because of https://github.com/microsoft/TypeScript/issues/32194 | ||
@@ -94,28 +94,29 @@ _PQueue_concurrency.set(this, void 0); | ||
} | ||
/** | ||
Adds a sync or async task to the queue. Always returns a promise. | ||
*/ | ||
async add(fn, options = {}) { | ||
async add(function_, options = {}) { | ||
options = { | ||
timeout: this.timeout, | ||
throwOnTimeout: __classPrivateFieldGet(this, _PQueue_throwOnTimeout, "f"), | ||
...options | ||
}; | ||
return new Promise((resolve, reject) => { | ||
const run = async () => { | ||
__classPrivateFieldGet(this, _PQueue_queue, "f").enqueue(async () => { | ||
var _a; | ||
var _b, _c; | ||
__classPrivateFieldSet(this, _PQueue_pendingCount, (_b = __classPrivateFieldGet(this, _PQueue_pendingCount, "f"), _b++, _b), "f"); | ||
__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"); | ||
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) | ||
reject(new AbortError('The task was aborted.')); | ||
return; | ||
throw new AbortError('The task was aborted.'); | ||
} | ||
const operation = this.timeout === undefined && options.timeout === undefined ? fn({ | ||
let operation = function_({ | ||
signal: options.signal | ||
}) : pTimeout(Promise.resolve(fn({ | ||
signal: options.signal | ||
})), options.timeout === undefined ? this.timeout : options.timeout, () => { | ||
if (options.throwOnTimeout === undefined ? __classPrivateFieldGet(this, _PQueue_throwOnTimeout, "f") : options.throwOnTimeout) { | ||
reject(timeoutError); | ||
} | ||
return undefined; | ||
}); | ||
if (options.timeout) { | ||
operation = pTimeout(Promise.resolve(operation), options.timeout); | ||
} | ||
if (options.signal) { | ||
operation = Promise.race([operation, __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_throwOnAbort).call(this, options.signal)]); | ||
} | ||
const result = await operation; | ||
@@ -125,16 +126,16 @@ resolve(result); | ||
} catch (error) { | ||
if (error instanceof TimeoutError && !options.throwOnTimeout) { | ||
resolve(); | ||
return; | ||
} | ||
reject(error); | ||
this.emit('error', error); | ||
} finally { | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_next).call(this); | ||
} | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_next).call(this); | ||
}; | ||
__classPrivateFieldGet(this, _PQueue_queue, "f").enqueue(run, options); | ||
}, options); | ||
this.emit('add'); | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this); | ||
this.emit('add'); | ||
}); | ||
} | ||
/** | ||
Same as `.add()`, but accepts an array of sync or async functions. | ||
@returns A promise that resolves when all functions are resolved. | ||
*/ | ||
async addAll(functions, options) { | ||
@@ -195,3 +196,3 @@ return Promise.all(functions.map(async function_ => this.add(function_, options))); | ||
// Instantly resolve if none pending and if nothing else is queued | ||
if (__classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) { | ||
if (__classPrivateFieldGet(this, _PQueue_pending, "f") === 0 && __classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) { | ||
return; | ||
@@ -219,3 +220,3 @@ } | ||
get pending() { | ||
return __classPrivateFieldGet(this, _PQueue_pendingCount, "f"); | ||
return __classPrivateFieldGet(this, _PQueue_pending, "f"); | ||
} | ||
@@ -229,16 +230,11 @@ /** | ||
} | ||
_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_pendingCount = 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() { | ||
_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_pendingCount, "f") < __classPrivateFieldGet(this, _PQueue_concurrency, "f"); | ||
return __classPrivateFieldGet(this, _PQueue_pending, "f") < __classPrivateFieldGet(this, _PQueue_concurrency, "f"); | ||
}, _PQueue_next = function _PQueue_next() { | ||
var _a; | ||
__classPrivateFieldSet(this, _PQueue_pendingCount, (_a = __classPrivateFieldGet(this, _PQueue_pendingCount, "f"), _a--, _a), "f"); | ||
__classPrivateFieldSet(this, _PQueue_pending, (_a = __classPrivateFieldGet(this, _PQueue_pending, "f"), _a--, _a), "f"); | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this); | ||
this.emit('next'); | ||
}, _PQueue_emitEvents = function _PQueue_emitEvents() { | ||
this.emit('empty'); | ||
if (__classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0) { | ||
this.emit('idle'); | ||
} | ||
}, _PQueue_onResumeInterval = function _PQueue_onResumeInterval() { | ||
@@ -255,3 +251,3 @@ __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onInterval).call(this); | ||
// 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_pendingCount, "f") : 0, "f"); | ||
__classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pending, "f") : 0, "f"); | ||
} else { | ||
@@ -276,3 +272,6 @@ // Act as the interval is pending | ||
__classPrivateFieldSet(this, _PQueue_intervalId, undefined, "f"); | ||
__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_emitEvents).call(this); | ||
this.emit('empty'); | ||
if (__classPrivateFieldGet(this, _PQueue_pending, "f") === 0) { | ||
this.emit('idle'); | ||
} | ||
return false; | ||
@@ -305,7 +304,7 @@ } | ||
}, _PQueue_onInterval = function _PQueue_onInterval() { | ||
if (__classPrivateFieldGet(this, _PQueue_intervalCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_intervalId, "f")) { | ||
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(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pendingCount, "f") : 0, "f"); | ||
__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); | ||
@@ -315,2 +314,12 @@ }, _PQueue_processQueue = function _PQueue_processQueue() { | ||
while (__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this)) {} | ||
}, _PQueue_throwOnAbort = async function _PQueue_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.')); | ||
}, { | ||
once: true | ||
}); | ||
}); | ||
}, _PQueue_onEvent = async function _PQueue_onEvent(event, filter) { | ||
@@ -317,0 +326,0 @@ return new Promise(resolve => { |
@@ -5,3 +5,3 @@ import EventEmitter from 'eventemitter3'; | ||
import { QueueAddOptions, Options, TaskOptions } from './options.js'; | ||
declare type Task<TaskResultType> = ((options: TaskOptions) => PromiseLike<TaskResultType>) | ((options: TaskOptions) => TaskResultType); | ||
type Task<TaskResultType> = ((options: TaskOptions) => PromiseLike<TaskResultType>) | ((options: TaskOptions) => TaskResultType); | ||
/** | ||
@@ -12,3 +12,3 @@ The error thrown by `queue.add()` when a job is aborted before it is run. See `signal`. | ||
} | ||
declare type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error'; | ||
type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error'; | ||
/** | ||
@@ -31,3 +31,6 @@ Promise queue with concurrency control. | ||
*/ | ||
add<TaskResultType>(fn: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>; | ||
add<TaskResultType>(function_: Task<TaskResultType>, options: { | ||
throwOnTimeout: true; | ||
} & Exclude<EnqueueOptionsType, 'throwOnTimeout'>): Promise<TaskResultType>; | ||
add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType | void>; | ||
/** | ||
@@ -38,3 +41,6 @@ Same as `.add()`, but accepts an array of sync or async functions. | ||
*/ | ||
addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: EnqueueOptionsType): Promise<TaskResultsType[]>; | ||
addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: { | ||
throwOnTimeout: true; | ||
} & Partial<Exclude<EnqueueOptionsType, 'throwOnTimeout'>>): Promise<TaskResultsType[]>; | ||
addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: Partial<EnqueueOptionsType>): Promise<Array<TaskResultsType | void>>; | ||
/** | ||
@@ -41,0 +47,0 @@ Start (or resume) executing enqueued tasks within concurrency limit. No need to call this if queue is not paused (via `options.autoStart = false` or by `.pause()` method.) |
@@ -1,2 +0,2 @@ | ||
export declare type RunFunction = () => Promise<unknown>; | ||
export type RunFunction = () => Promise<unknown>; | ||
export interface Queue<Element, Options> { | ||
@@ -3,0 +3,0 @@ size: number; |
{ | ||
"name": "p-queue-compat", | ||
"version": "1.0.219", | ||
"version": "1.0.220", | ||
"description": "Compatible version of p-queue", | ||
@@ -82,3 +82,3 @@ "type": "module", | ||
"@semantic-release/github": "^8.0.0", | ||
"@semantic-release/npm": "^9.0.0", | ||
"@semantic-release/npm": "^10.0.2", | ||
"@semantic-release/release-notes-generator": "^10.0.2", | ||
@@ -96,3 +96,3 @@ "babel-plugin-add-import-extension": "^1.6.0", | ||
"p-queue": "^7.1.0", | ||
"semantic-release": "^19.0.2" | ||
"semantic-release": "^21.0.0" | ||
}, | ||
@@ -99,0 +99,0 @@ "release": { |
Sorry, the diff of this file is not supported yet
68355
986