UNPKG

2 kBTypeScriptView Raw
1/** A simple queue that holds promises. */
2export declare class PromiseBuffer<T> {
3 protected _limit?: number | undefined;
4 /** Internal set of queued Promises */
5 private readonly _buffer;
6 constructor(_limit?: number | undefined);
7 /**
8 * Says if the buffer is ready to take more requests
9 */
10 isReady(): boolean;
11 /**
12 * Add a promise (representing an in-flight action) to the queue, and set it to remove itself on fulfillment.
13 *
14 * @param taskProducer A function producing any PromiseLike<T>; In previous versions this used to be `task:
15 * PromiseLike<T>`, but under that model, Promises were instantly created on the call-site and their executor
16 * functions therefore ran immediately. Thus, even if the buffer was full, the action still happened. By
17 * requiring the promise to be wrapped in a function, we can defer promise creation until after the buffer
18 * limit check.
19 * @returns The original promise.
20 */
21 add(taskProducer: () => PromiseLike<T>): PromiseLike<T>;
22 /**
23 * Remove a promise from the queue.
24 *
25 * @param task Can be any PromiseLike<T>
26 * @returns Removed promise.
27 */
28 remove(task: PromiseLike<T>): PromiseLike<T>;
29 /**
30 * This function returns the number of unresolved promises in the queue.
31 */
32 length(): number;
33 /**
34 * Wait for all promises in the queue to resolve or for timeout to expire, whichever comes first.
35 *
36 * @param timeout The time, in ms, after which to resolve to `false` if the queue is still non-empty. Passing `0` (or
37 * not passing anything) will make the promise wait as long as it takes for the queue to drain before resolving to
38 * `true`.
39 * @returns A promise which will resolve to `true` if the queue is already empty or drains before the timeout, and
40 * `false` otherwise
41 */
42 drain(timeout?: number): PromiseLike<boolean>;
43}
44//# sourceMappingURL=promisebuffer.d.ts.map
\No newline at end of file