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