UNPKG

3.73 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';
5declare type 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}
11declare type 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>(fn: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>;
30 /**
31 Same as `.add()`, but accepts an array of sync or async functions.
32
33 @returns A promise that resolves when all functions are resolved.
34 */
35 addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: EnqueueOptionsType): Promise<TaskResultsType[]>;
36 /**
37 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.)
38 */
39 start(): this;
40 /**
41 Put queue execution on hold.
42 */
43 pause(): void;
44 /**
45 Clear the queue.
46 */
47 clear(): void;
48 /**
49 Can be called multiple times. Useful if you for example add additional items at a later time.
50
51 @returns A promise that settles when the queue becomes empty.
52 */
53 onEmpty(): Promise<void>;
54 /**
55 @returns A promise that settles when the queue size is less than the given limit: `queue.size < limit`.
56
57 If you want to avoid having the queue grow beyond a certain size you can `await queue.onSizeLessThan()` before adding a new item.
58
59 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.
60 */
61 onSizeLessThan(limit: number): Promise<void>;
62 /**
63 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.
64
65 @returns A promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.
66 */
67 onIdle(): Promise<void>;
68 /**
69 Size of the queue, the number of queued items waiting to run.
70 */
71 get size(): number;
72 /**
73 Size of the queue, filtered by the given options.
74
75 For example, this can be used to find the number of items remaining in the queue with a specific priority level.
76 */
77 sizeBy(options: Readonly<Partial<EnqueueOptionsType>>): number;
78 /**
79 Number of running items (no longer in the queue).
80 */
81 get pending(): number;
82 /**
83 Whether the queue is currently paused.
84 */
85 get isPaused(): boolean;
86}
87export { Queue, QueueAddOptions, QueueAddOptions as DefaultAddOptions, Options };
88
\No newline at end of file