@common.js/p-queue 8.0.1 → 9.3.2

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 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@8.0.1](https://www.npmjs.com/package/p-queue/v/8.0.1) using https://github.com/etienne-martin/common.js.
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
@@ -3,37 +3,78 @@ import { type Queue, type RunFunction } from './queue.js';
3
3
  import PriorityQueue from './priority-queue.js';
4
4
  import { type QueueAddOptions, type Options, type TaskOptions } from './options.js';
5
5
  type Task<TaskResultType> = ((options: TaskOptions) => PromiseLike<TaskResultType>) | ((options: TaskOptions) => TaskResultType);
6
- type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error';
6
+ type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error' | 'pendingZero' | 'rateLimit' | 'rateLimitCleared';
7
7
  /**
8
8
  Promise queue with concurrency control.
9
9
  */
10
10
  export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = QueueAddOptions> extends EventEmitter<EventName> {
11
11
  #private;
12
12
  /**
13
- Per-operation timeout in milliseconds. Operations fulfill once `timeout` elapses if they haven't already.
13
+ Get or set the default timeout for all tasks. Can be changed at runtime.
14
14
 
15
- Applies to each future operation.
15
+ Operations will throw a `TimeoutError` if they don't complete within the specified time.
16
+
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
+ ```
16
26
  */
17
27
  timeout?: number;
18
28
  constructor(options?: Options<QueueType, EnqueueOptionsType>);
19
29
  get concurrency(): number;
20
30
  set concurrency(newConcurrency: number);
21
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
+ /**
22
69
  Adds a sync or async task to the queue. Always returns a promise.
23
70
  */
24
- add<TaskResultType>(function_: Task<TaskResultType>, options: {
25
- throwOnTimeout: true;
26
- } & Exclude<EnqueueOptionsType, 'throwOnTimeout'>): Promise<TaskResultType>;
27
- add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType | void>;
71
+ add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>;
28
72
  /**
29
73
  Same as `.add()`, but accepts an array of sync or async functions.
30
74
 
31
75
  @returns A promise that resolves when all functions are resolved.
32
76
  */
33
- addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: {
34
- throwOnTimeout: true;
35
- } & Partial<Exclude<EnqueueOptionsType, 'throwOnTimeout'>>): Promise<TaskResultsType[]>;
36
- 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[]>;
37
78
  /**
38
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.)
39
80
  */
@@ -67,6 +108,50 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
67
108
  */
68
109
  onIdle(): Promise<void>;
69
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
+ /**
70
155
  Size of the queue, the number of queued items waiting to run.
71
156
  */
72
157
  get size(): number;
@@ -84,6 +169,95 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
84
169
  Whether the queue is currently paused.
85
170
  */
86
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
+ }>;
87
241
  }
88
242
  export type { Queue } from './queue.js';
243
+ export { default as PriorityQueue } from './priority-queue.js';
89
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';