1 | import EventEmitter = require('eventemitter3');
|
2 | import { Queue, RunFunction } from './queue';
|
3 | import PriorityQueue from './priority-queue';
|
4 | import { QueueAddOptions, DefaultAddOptions, Options } from './options';
|
5 | declare type Task<TaskResultType> = (() => PromiseLike<TaskResultType>) | (() => TaskResultType);
|
6 | /**
|
7 | Promise queue with concurrency control.
|
8 | */
|
9 | export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = DefaultAddOptions> extends EventEmitter<'active' | 'idle' | 'add' | 'next'> {
|
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 | 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.
|
73 |
|
74 | @returns A promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.
|
75 | */
|
76 | onIdle(): Promise<void>;
|
77 | /**
|
78 | Size of the queue.
|
79 | */
|
80 | get size(): number;
|
81 | /**
|
82 | Size of the queue, filtered by the given options.
|
83 |
|
84 | For example, this can be used to find the number of items remaining in the queue with a specific priority level.
|
85 | */
|
86 | sizeBy(options: Readonly<Partial<EnqueueOptionsType>>): number;
|
87 | /**
|
88 | Number of pending promises.
|
89 | */
|
90 | get pending(): number;
|
91 | /**
|
92 | Whether the queue is currently paused.
|
93 | */
|
94 | get isPaused(): boolean;
|
95 | get timeout(): number | undefined;
|
96 | /**
|
97 | Set the timeout for future operations.
|
98 | */
|
99 | set timeout(milliseconds: number | undefined);
|
100 | }
|
101 | export { Queue, QueueAddOptions, DefaultAddOptions, Options };
|
102 |
|
\ | No newline at end of file |