UNPKG

4.15 kBTypeScriptView Raw
1import { EventEmitter } from 'eventemitter3';
2import { Queue, RunFunction } from './queue.js';
3import PriorityQueue from './priority-queue.js';
4import { QueueAddOptions, Options, TaskOptions } from './options.js';
5type Task<TaskResultType> = ((options: TaskOptions) => PromiseLike<TaskResultType>) | ((options: TaskOptions) => TaskResultType);
6/**
7The error thrown by `queue.add()` when a job is aborted before it is run. See `signal`.
8*/
9export declare class AbortError extends Error {
10}
11type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error';
12/**
13Promise queue with concurrency control.
14*/
15export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = QueueAddOptions> extends EventEmitter<EventName> {
16 #private;
17 /**
18 Per-operation timeout in milliseconds. Operations fulfill once `timeout` elapses if they haven't already.
19
20 Applies to each future operation.
21 */
22 timeout?: number;
23 constructor(options?: Options<QueueType, EnqueueOptionsType>);
24 get concurrency(): number;
25 set concurrency(newConcurrency: number);
26 /**
27 Adds a sync or async task to the queue. Always returns a promise.
28 */
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>;
33 /**
34 Same as `.add()`, but accepts an array of sync or async functions.
35
36 @returns A promise that resolves when all functions are resolved.
37 */
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>>;
42 /**
43 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 */
45 start(): this;
46 /**
47 Put queue execution on hold.
48 */
49 pause(): void;
50 /**
51 Clear the queue.
52 */
53 clear(): void;
54 /**
55 Can be called multiple times. Useful if you for example add additional items at a later time.
56
57 @returns A promise that settles when the queue becomes empty.
58 */
59 onEmpty(): Promise<void>;
60 /**
61 @returns A promise that settles when the queue size is less than the given limit: `queue.size < limit`.
62
63 If you want to avoid having the queue grow beyond a certain size you can `await queue.onSizeLessThan()` before adding a new item.
64
65 Note that this only limits the number of items waiting to start. There could still be up to `concurrency` jobs already running that this call does not include in its calculation.
66 */
67 onSizeLessThan(limit: number): Promise<void>;
68 /**
69 The difference with `.onEmpty` is that `.onIdle` guarantees that all work from the queue has finished. `.onEmpty` merely signals that the queue is empty, but it could mean that some promises haven't completed yet.
70
71 @returns A promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.
72 */
73 onIdle(): Promise<void>;
74 /**
75 Size of the queue, the number of queued items waiting to run.
76 */
77 get size(): number;
78 /**
79 Size of the queue, filtered by the given options.
80
81 For example, this can be used to find the number of items remaining in the queue with a specific priority level.
82 */
83 sizeBy(options: Readonly<Partial<EnqueueOptionsType>>): number;
84 /**
85 Number of running items (no longer in the queue).
86 */
87 get pending(): number;
88 /**
89 Whether the queue is currently paused.
90 */
91 get isPaused(): boolean;
92}
93export { Queue, QueueAddOptions, QueueAddOptions as DefaultAddOptions, Options };
94
\No newline at end of file