UNPKG

4.35 kBTypeScriptView Raw
1import EventEmitter from 'eventemitter3';
2import { Queue, RunFunction } from './queue.js';
3import PriorityQueue from './priority-queue.js';
4import { QueueAddOptions, DefaultAddOptions, Options } from './options.js';
5declare type Task<TaskResultType> = (() => PromiseLike<TaskResultType>) | (() => TaskResultType);
6/**
7Promise queue with concurrency control.
8*/
9export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = DefaultAddOptions> extends EventEmitter<'active' | 'idle' | 'add' | 'next' | 'completed' | 'error'> {
10 private readonly _carryoverConcurrencyCount;
11 private readonly _isIntervalIgnored;
12 private _intervalCount;
13 private readonly _intervalCap;
14 private readonly _interval;
15 private _intervalEnd;
16 private _intervalId?;
17 private _timeoutId?;
18 private _queue;
19 private readonly _queueClass;
20 private _pendingCount;
21 private _concurrency;
22 private _isPaused;
23 private _resolveEmpty;
24 private _resolveIdle;
25 private _timeout?;
26 private readonly _throwOnTimeout;
27 constructor(options?: Options<QueueType, EnqueueOptionsType>);
28 private get _doesIntervalAllowAnother();
29 private get _doesConcurrentAllowAnother();
30 private _next;
31 private _resolvePromises;
32 private _onResumeInterval;
33 private _isIntervalPaused;
34 private _tryToStartAnother;
35 private _initializeIntervalIfNeeded;
36 private _onInterval;
37 /**
38 Executes all queued functions until it reaches the limit.
39 */
40 private _processQueue;
41 get concurrency(): number;
42 set concurrency(newConcurrency: number);
43 /**
44 Adds a sync or async task to the queue. Always returns a promise.
45 */
46 add<TaskResultType>(fn: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>;
47 /**
48 Same as `.add()`, but accepts an array of sync or async functions.
49
50 @returns A promise that resolves when all functions are resolved.
51 */
52 addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: EnqueueOptionsType): Promise<TaskResultsType[]>;
53 /**
54 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.)
55 */
56 start(): this;
57 /**
58 Put queue execution on hold.
59 */
60 pause(): void;
61 /**
62 Clear the queue.
63 */
64 clear(): void;
65 /**
66 Can be called multiple times. Useful if you for example add additional items at a later time.
67
68 @returns A promise that settles when the queue becomes empty.
69 */
70 onEmpty(): Promise<void>;
71 /**
72 @returns A promise that settles when the queue size is less than the given limit: `queue.size < limit`.
73
74 If you want to avoid having the queue grow beyond a certain size you can `await queue.onSizeLessThan()` before adding a new item.
75
76 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.
77 */
78 onSizeLessThan(limit: number): Promise<void>;
79 /**
80 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.
81
82 @returns A promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.
83 */
84 onIdle(): Promise<void>;
85 /**
86 Size of the queue, the number of queued items waiting to run.
87 */
88 get size(): number;
89 /**
90 Size of the queue, filtered by the given options.
91
92 For example, this can be used to find the number of items remaining in the queue with a specific priority level.
93 */
94 sizeBy(options: Readonly<Partial<EnqueueOptionsType>>): number;
95 /**
96 Number of running items (no longer in the queue).
97 */
98 get pending(): number;
99 /**
100 Whether the queue is currently paused.
101 */
102 get isPaused(): boolean;
103 get timeout(): number | undefined;
104 /**
105 Set the timeout for future operations.
106 */
107 set timeout(milliseconds: number | undefined);
108}
109export { Queue, QueueAddOptions, DefaultAddOptions, Options };
110
\No newline at end of file