@common.js/p-queue 7.4.1 → 9.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +189 -19
- package/dist/index.js +1030 -296
- package/dist/options.d.ts +53 -18
- package/dist/priority-queue.d.ts +7 -4
- package/dist/priority-queue.js +216 -51
- package/dist/queue.d.ts +5 -3
- package/package.json +26 -33
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
The [p-queue](https://www.npmjs.com/package/p-queue) package exported as CommonJS modules.
|
|
4
4
|
|
|
5
|
-
Exported from [p-queue@
|
|
5
|
+
Exported from [p-queue@9.3.1](https://www.npmjs.com/package/p-queue/v/9.3.1) using https://github.com/etienne-martin/common.js.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,44 +1,80 @@
|
|
|
1
1
|
import { EventEmitter } from 'eventemitter3';
|
|
2
|
-
import { Queue, RunFunction } from './queue.js';
|
|
2
|
+
import { type Queue, type RunFunction } from './queue.js';
|
|
3
3
|
import PriorityQueue from './priority-queue.js';
|
|
4
|
-
import { QueueAddOptions, Options, TaskOptions } from './options.js';
|
|
4
|
+
import { type QueueAddOptions, type Options, type TaskOptions } from './options.js';
|
|
5
5
|
type Task<TaskResultType> = ((options: TaskOptions) => PromiseLike<TaskResultType>) | ((options: TaskOptions) => TaskResultType);
|
|
6
|
-
|
|
7
|
-
The error thrown by `queue.add()` when a job is aborted before it is run. See `signal`.
|
|
8
|
-
*/
|
|
9
|
-
export declare class AbortError extends Error {
|
|
10
|
-
}
|
|
11
|
-
type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error';
|
|
6
|
+
type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error' | 'pendingZero' | 'rateLimit' | 'rateLimitCleared';
|
|
12
7
|
/**
|
|
13
8
|
Promise queue with concurrency control.
|
|
14
9
|
*/
|
|
15
10
|
export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = QueueAddOptions> extends EventEmitter<EventName> {
|
|
16
11
|
#private;
|
|
17
12
|
/**
|
|
18
|
-
|
|
13
|
+
Get or set the default timeout for all tasks. Can be changed at runtime.
|
|
14
|
+
|
|
15
|
+
Operations will throw a `TimeoutError` if they don't complete within the specified time.
|
|
19
16
|
|
|
20
|
-
|
|
17
|
+
The timeout begins when the operation is dequeued and starts execution, not while it's waiting in the queue.
|
|
18
|
+
|
|
19
|
+
@example
|
|
20
|
+
```
|
|
21
|
+
const queue = new PQueue({timeout: 5000});
|
|
22
|
+
|
|
23
|
+
// Change timeout for all future tasks
|
|
24
|
+
queue.timeout = 10000;
|
|
25
|
+
```
|
|
21
26
|
*/
|
|
22
27
|
timeout?: number;
|
|
23
28
|
constructor(options?: Options<QueueType, EnqueueOptionsType>);
|
|
24
29
|
get concurrency(): number;
|
|
25
30
|
set concurrency(newConcurrency: number);
|
|
26
31
|
/**
|
|
32
|
+
Updates the priority of a promise function by its id, affecting its execution order. Requires a defined concurrency limit to take effect.
|
|
33
|
+
|
|
34
|
+
For example, this can be used to prioritize a promise function to run earlier.
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import PQueue from 'p-queue';
|
|
38
|
+
|
|
39
|
+
const queue = new PQueue({concurrency: 1});
|
|
40
|
+
|
|
41
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
42
|
+
queue.add(async () => '🦀', {priority: 0, id: '🦀'});
|
|
43
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
44
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
45
|
+
|
|
46
|
+
queue.setPriority('🦀', 2);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
In this case, the promise function with `id: '🦀'` runs second.
|
|
50
|
+
|
|
51
|
+
You can also deprioritize a promise function to delay its execution:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import PQueue from 'p-queue';
|
|
55
|
+
|
|
56
|
+
const queue = new PQueue({concurrency: 1});
|
|
57
|
+
|
|
58
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
59
|
+
queue.add(async () => '🦀', {priority: 1, id: '🦀'});
|
|
60
|
+
queue.add(async () => '🦄');
|
|
61
|
+
queue.add(async () => '🦄', {priority: 0});
|
|
62
|
+
|
|
63
|
+
queue.setPriority('🦀', -1);
|
|
64
|
+
```
|
|
65
|
+
Here, the promise function with `id: '🦀'` executes last.
|
|
66
|
+
*/
|
|
67
|
+
setPriority(id: string, priority: number): void;
|
|
68
|
+
/**
|
|
27
69
|
Adds a sync or async task to the queue. Always returns a promise.
|
|
28
70
|
*/
|
|
29
|
-
add<TaskResultType>(function_: Task<TaskResultType>, options:
|
|
30
|
-
throwOnTimeout: true;
|
|
31
|
-
} & Exclude<EnqueueOptionsType, 'throwOnTimeout'>): Promise<TaskResultType>;
|
|
32
|
-
add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType | void>;
|
|
71
|
+
add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>;
|
|
33
72
|
/**
|
|
34
73
|
Same as `.add()`, but accepts an array of sync or async functions.
|
|
35
74
|
|
|
36
75
|
@returns A promise that resolves when all functions are resolved.
|
|
37
76
|
*/
|
|
38
|
-
addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?:
|
|
39
|
-
throwOnTimeout: true;
|
|
40
|
-
} & Partial<Exclude<EnqueueOptionsType, 'throwOnTimeout'>>): Promise<TaskResultsType[]>;
|
|
41
|
-
addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: Partial<EnqueueOptionsType>): Promise<Array<TaskResultsType | void>>;
|
|
77
|
+
addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultsType[]>;
|
|
42
78
|
/**
|
|
43
79
|
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.)
|
|
44
80
|
*/
|
|
@@ -72,6 +108,50 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
|
|
|
72
108
|
*/
|
|
73
109
|
onIdle(): Promise<void>;
|
|
74
110
|
/**
|
|
111
|
+
The difference with `.onIdle` is that `.onPendingZero` only waits for currently running tasks to finish, ignoring queued tasks.
|
|
112
|
+
|
|
113
|
+
@returns A promise that settles when all currently running tasks have completed; `queue.pending === 0`.
|
|
114
|
+
*/
|
|
115
|
+
onPendingZero(): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
@returns A promise that settles when the queue becomes rate-limited due to intervalCap.
|
|
118
|
+
*/
|
|
119
|
+
onRateLimit(): Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
@returns A promise that settles when the queue is no longer rate-limited.
|
|
122
|
+
*/
|
|
123
|
+
onRateLimitCleared(): Promise<void>;
|
|
124
|
+
/**
|
|
125
|
+
@returns A promise that rejects when any task in the queue errors.
|
|
126
|
+
|
|
127
|
+
Use with `Promise.race([queue.onError(), queue.onIdle()])` to fail fast on the first error while still resolving normally when the queue goes idle.
|
|
128
|
+
|
|
129
|
+
Important: The promise returned by `add()` still rejects. You must handle each `add()` promise (for example, `.catch(() => {})`) to avoid unhandled rejections.
|
|
130
|
+
|
|
131
|
+
@example
|
|
132
|
+
```
|
|
133
|
+
import PQueue from 'p-queue';
|
|
134
|
+
|
|
135
|
+
const queue = new PQueue({concurrency: 2});
|
|
136
|
+
|
|
137
|
+
queue.add(() => fetchData(1)).catch(() => {});
|
|
138
|
+
queue.add(() => fetchData(2)).catch(() => {});
|
|
139
|
+
queue.add(() => fetchData(3)).catch(() => {});
|
|
140
|
+
|
|
141
|
+
// Stop processing on first error
|
|
142
|
+
try {
|
|
143
|
+
await Promise.race([
|
|
144
|
+
queue.onError(),
|
|
145
|
+
queue.onIdle()
|
|
146
|
+
]);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
queue.pause(); // Stop processing remaining tasks
|
|
149
|
+
console.error('Queue failed:', error);
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
*/
|
|
153
|
+
onError(): Promise<never>;
|
|
154
|
+
/**
|
|
75
155
|
Size of the queue, the number of queued items waiting to run.
|
|
76
156
|
*/
|
|
77
157
|
get size(): number;
|
|
@@ -89,5 +169,95 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
|
|
|
89
169
|
Whether the queue is currently paused.
|
|
90
170
|
*/
|
|
91
171
|
get isPaused(): boolean;
|
|
172
|
+
/**
|
|
173
|
+
Whether the queue is currently rate-limited due to intervalCap.
|
|
174
|
+
*/
|
|
175
|
+
get isRateLimited(): boolean;
|
|
176
|
+
/**
|
|
177
|
+
Whether the queue is saturated. Returns `true` when:
|
|
178
|
+
- All concurrency slots are occupied and tasks are waiting, OR
|
|
179
|
+
- The queue is rate-limited and tasks are waiting
|
|
180
|
+
|
|
181
|
+
Useful for detecting backpressure and potential hanging tasks.
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
import PQueue from 'p-queue';
|
|
185
|
+
|
|
186
|
+
const queue = new PQueue({concurrency: 2});
|
|
187
|
+
|
|
188
|
+
// Backpressure handling
|
|
189
|
+
if (queue.isSaturated) {
|
|
190
|
+
console.log('Queue is saturated, waiting for capacity...');
|
|
191
|
+
await queue.onSizeLessThan(queue.concurrency);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Monitoring for stuck tasks
|
|
195
|
+
setInterval(() => {
|
|
196
|
+
if (queue.isSaturated) {
|
|
197
|
+
console.warn(`Queue saturated: ${queue.pending} running, ${queue.size} waiting`);
|
|
198
|
+
}
|
|
199
|
+
}, 60000);
|
|
200
|
+
```
|
|
201
|
+
*/
|
|
202
|
+
get isSaturated(): boolean;
|
|
203
|
+
/**
|
|
204
|
+
The tasks currently being executed. Each task includes its `id`, `priority`, `startTime`, `timeout` (if set), and `timeoutRemaining` (milliseconds until the task times out, or `undefined` if no timeout is set).
|
|
205
|
+
|
|
206
|
+
Returns an array of task info objects.
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
import PQueue from 'p-queue';
|
|
210
|
+
|
|
211
|
+
const queue = new PQueue({concurrency: 2, timeout: 10000});
|
|
212
|
+
|
|
213
|
+
// Add tasks with IDs for better debugging
|
|
214
|
+
queue.add(() => fetchUser(123), {id: 'user-123'});
|
|
215
|
+
queue.add(() => fetchPosts(456), {id: 'posts-456', priority: 1});
|
|
216
|
+
|
|
217
|
+
// Check what's running
|
|
218
|
+
console.log(queue.runningTasks);
|
|
219
|
+
// => [{
|
|
220
|
+
// id: 'user-123',
|
|
221
|
+
// priority: 0,
|
|
222
|
+
// startTime: 1759253001716,
|
|
223
|
+
// timeout: 10000,
|
|
224
|
+
// timeoutRemaining: 9700
|
|
225
|
+
// }, {
|
|
226
|
+
// id: 'posts-456',
|
|
227
|
+
// priority: 1,
|
|
228
|
+
// startTime: 1759253001916,
|
|
229
|
+
// timeout: 10000,
|
|
230
|
+
// timeoutRemaining: 9900
|
|
231
|
+
// }]
|
|
232
|
+
```
|
|
233
|
+
*/
|
|
234
|
+
get runningTasks(): ReadonlyArray<{
|
|
235
|
+
readonly id?: string;
|
|
236
|
+
readonly priority: number;
|
|
237
|
+
readonly startTime: number;
|
|
238
|
+
readonly timeout?: number;
|
|
239
|
+
readonly timeoutRemaining?: number;
|
|
240
|
+
}>;
|
|
92
241
|
}
|
|
93
|
-
export { Queue
|
|
242
|
+
export type { Queue } from './queue.js';
|
|
243
|
+
export { default as PriorityQueue } from './priority-queue.js';
|
|
244
|
+
export { type QueueAddOptions, type Options } from './options.js';
|
|
245
|
+
/**
|
|
246
|
+
Error thrown when a task times out.
|
|
247
|
+
|
|
248
|
+
@example
|
|
249
|
+
```
|
|
250
|
+
import PQueue, {TimeoutError} from 'p-queue';
|
|
251
|
+
|
|
252
|
+
const queue = new PQueue({timeout: 1000});
|
|
253
|
+
|
|
254
|
+
try {
|
|
255
|
+
await queue.add(() => someTask());
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (error instanceof TimeoutError) {
|
|
258
|
+
console.log('Task timed out');
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
*/
|
|
263
|
+
export { TimeoutError } from 'p-timeout';
|