| import { type Queue, type RunFunction } from './queue.js'; | ||
| type TimeoutOptions = { | ||
| /** | ||
| Per-operation timeout in milliseconds. Operations will throw a `TimeoutError` if they don't complete within the specified time. | ||
| The timeout begins when the operation is dequeued and starts execution, not while it's waiting in the queue. | ||
| @default undefined | ||
| Can be overridden per task using the `timeout` option in `.add()`: | ||
| @example | ||
| ``` | ||
| const queue = new PQueue({timeout: 5000}); | ||
| // This task uses the global 5s timeout | ||
| await queue.add(() => fetchData()); | ||
| // This task has a 10s timeout | ||
| await queue.add(() => slowTask(), {timeout: 10000}); | ||
| ``` | ||
| */ | ||
| timeout?: number; | ||
| }; | ||
| export type Options<QueueType extends Queue<RunFunction, QueueOptions>, QueueOptions extends QueueAddOptions> = { | ||
| /** | ||
| Concurrency limit. | ||
| Minimum: `1`. | ||
| @default Infinity | ||
| */ | ||
| readonly concurrency?: number; | ||
| /** | ||
| Whether queue tasks within concurrency limit, are auto-executed as soon as they're added. | ||
| @default true | ||
| */ | ||
| readonly autoStart?: boolean; | ||
| /** | ||
| Class with a `enqueue` and `dequeue` method, and a `size` getter. See the [Custom QueueClass](https://github.com/sindresorhus/p-queue#custom-queueclass) section. | ||
| */ | ||
| readonly queueClass?: new () => QueueType; | ||
| /** | ||
| The max number of runs in the given interval of time. | ||
| Minimum: `1`. | ||
| @default Infinity | ||
| */ | ||
| readonly intervalCap?: number; | ||
| /** | ||
| The length of time in milliseconds before the interval count resets. Must be finite. | ||
| Minimum: `0`. | ||
| @default 0 | ||
| */ | ||
| readonly interval?: number; | ||
| /** | ||
| Whether the task must finish in the given interval or will be carried over into the next interval count. | ||
| @default false | ||
| */ | ||
| readonly carryoverIntervalCount?: boolean; | ||
| /** | ||
| @deprecated Renamed to `carryoverIntervalCount`. | ||
| */ | ||
| readonly carryoverConcurrencyCount?: boolean; | ||
| } & TimeoutOptions; | ||
| export type QueueAddOptions = { | ||
| /** | ||
| Priority of operation. Operations with greater priority will be scheduled first. | ||
| @default 0 | ||
| */ | ||
| readonly priority?: number; | ||
| /** | ||
| Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned an incrementing BigInt starting from `1n`. | ||
| */ | ||
| id?: string; | ||
| } & TaskOptions & TimeoutOptions; | ||
| export type TaskOptions = { | ||
| /** | ||
| [`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. | ||
| @example | ||
| ``` | ||
| import PQueue, {AbortError} from 'p-queue'; | ||
| import got, {CancelError} from 'got'; | ||
| const queue = new PQueue(); | ||
| const controller = new AbortController(); | ||
| try { | ||
| await queue.add(({signal}) => { | ||
| const request = got('https://sindresorhus.com'); | ||
| signal.addEventListener('abort', () => { | ||
| request.cancel(); | ||
| }); | ||
| try { | ||
| return await request; | ||
| } catch (error) { | ||
| if (!(error instanceof CancelError)) { | ||
| throw error; | ||
| } | ||
| } | ||
| }, {signal: controller.signal}); | ||
| } catch (error) { | ||
| if (!(error instanceof AbortError)) { | ||
| throw error; | ||
| } | ||
| } | ||
| ``` | ||
| */ | ||
| readonly signal?: AbortSignal; | ||
| }; | ||
| export {}; |
| export {}; |
+12
-8
@@ -213,9 +213,2 @@ import { EventEmitter } from 'eventemitter3'; | ||
| } | ||
| async #throwOnAbort(signal) { | ||
| return new Promise((_resolve, reject) => { | ||
| signal.addEventListener('abort', () => { | ||
| reject(signal.reason); | ||
| }, { once: true }); | ||
| }); | ||
| } | ||
| /** | ||
@@ -282,2 +275,3 @@ Updates the priority of a promise function by its id, affecting its execution order. Requires a defined concurrency limit to take effect. | ||
| }); | ||
| let eventListener; | ||
| try { | ||
@@ -306,3 +300,9 @@ // Check abort signal - if aborted, need to decrement the counter | ||
| if (options.signal) { | ||
| operation = Promise.race([operation, this.#throwOnAbort(options.signal)]); | ||
| const { signal } = options; | ||
| operation = Promise.race([operation, new Promise((_resolve, reject) => { | ||
| eventListener = () => { | ||
| reject(signal.reason); | ||
| }; | ||
| signal.addEventListener('abort', eventListener, { once: true }); | ||
| })]); | ||
| } | ||
@@ -318,2 +318,6 @@ const result = await operation; | ||
| finally { | ||
| // Clean up abort event listener | ||
| if (eventListener) { | ||
| options.signal?.removeEventListener('abort', eventListener); | ||
| } | ||
| // Remove from running tasks | ||
@@ -320,0 +324,0 @@ this.#runningTasks.delete(taskSymbol); |
+1
-1
| { | ||
| "name": "p-queue", | ||
| "version": "9.0.0", | ||
| "version": "9.0.1", | ||
| "description": "Promise queue with concurrency control", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
72425
5.77%15
15.38%1090
10.77%5
25%