UNPKG

2.91 kBTypeScriptView Raw
1import { type Queue, type RunFunction } from './queue.js';
2type TimeoutOptions = {
3 /**
4 Per-operation timeout in milliseconds. Operations fulfill once `timeout` elapses if they haven't already.
5 */
6 timeout?: number;
7 /**
8 Whether or not a timeout is considered an exception.
9
10 @default false
11 */
12 throwOnTimeout?: boolean;
13};
14export type Options<QueueType extends Queue<RunFunction, QueueOptions>, QueueOptions extends QueueAddOptions> = {
15 /**
16 Concurrency limit.
17
18 Minimum: `1`.
19
20 @default Infinity
21 */
22 readonly concurrency?: number;
23 /**
24 Whether queue tasks within concurrency limit, are auto-executed as soon as they're added.
25
26 @default true
27 */
28 readonly autoStart?: boolean;
29 /**
30 Class with a `enqueue` and `dequeue` method, and a `size` getter. See the [Custom QueueClass](https://github.com/sindresorhus/p-queue#custom-queueclass) section.
31 */
32 readonly queueClass?: new () => QueueType;
33 /**
34 The max number of runs in the given interval of time.
35
36 Minimum: `1`.
37
38 @default Infinity
39 */
40 readonly intervalCap?: number;
41 /**
42 The length of time in milliseconds before the interval count resets. Must be finite.
43
44 Minimum: `0`.
45
46 @default 0
47 */
48 readonly interval?: number;
49 /**
50 Whether the task must finish in the given interval or will be carried over into the next interval count.
51
52 @default false
53 */
54 readonly carryoverConcurrencyCount?: boolean;
55} & TimeoutOptions;
56export type QueueAddOptions = {
57 /**
58 Priority of operation. Operations with greater priority will be scheduled first.
59
60 @default 0
61 */
62 readonly priority?: number;
63} & TaskOptions & TimeoutOptions;
64export type TaskOptions = {
65 /**
66 [`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.
67
68 @example
69 ```
70 import PQueue, {AbortError} from 'p-queue';
71 import got, {CancelError} from 'got';
72
73 const queue = new PQueue();
74
75 const controller = new AbortController();
76
77 try {
78 await queue.add(({signal}) => {
79 const request = got('https://sindresorhus.com');
80
81 signal.addEventListener('abort', () => {
82 request.cancel();
83 });
84
85 try {
86 return await request;
87 } catch (error) {
88 if (!(error instanceof CancelError)) {
89 throw error;
90 }
91 }
92 }, {signal: controller.signal});
93 } catch (error) {
94 if (!(error instanceof AbortError)) {
95 throw error;
96 }
97 }
98 ```
99 */
100 readonly signal?: AbortSignal;
101};
102export {};