UNPKG

2.64 kBTypeScriptView Raw
1export interface BatcherOptions<I, O> {
2 /**
3 * The maximum number of requests that can be combined in a single batch.
4 */
5 maxBatchSize?: number;
6 /**
7 * The number of milliseconds to wait before running a batch of requests.
8 *
9 * This is used to allow time for the requests to queue up. Defaults to 1ms.
10 * This delay does not apply if the limit set by options.maxBatchSize is reached.
11 */
12 queuingDelay?: number;
13 /**
14 * An array containing the number of requests that must be queued in order to trigger a batch request at each level
15 * of concurrency.
16 *
17 * For example [1, 5], would require at least 1 queued request when no batch requests are active,
18 * and 5 queued requests when 1 (or more) batch requests are active. Defaults to [1]. Note that the delay imposed
19 * by options.queuingDelay still applies when a batch request is triggered.
20 */
21 queuingThresholds?: number[];
22 /**
23 * A function which is passed an array of request values, returning a promise which resolves to an array of
24 * response values.
25 *
26 * The request and response arrays must be of equal length. To reject an individual request, return an Error object
27 * (or class which extends Error) at the corresponding element in the response array.
28 */
29 batchingFunction(inputs: I[]): Array<O | Error> | PromiseLike<Array<O | Error>>;
30 /**
31 * A function which can delay a batch by returning a promise which resolves when the batch should be run.
32 * If the function does not return a promise, no delay will be applied.
33 */
34 delayFunction?(): PromiseLike<void> | undefined | null | void;
35}
36export declare class Batcher<I, O> {
37 private _maxBatchSize;
38 private _queuingDelay;
39 private _queuingThresholds;
40 private _inputQueue;
41 private _outputQueue;
42 private _delayFunction?;
43 private _batchingFunction;
44 private _waitTimeout?;
45 private _waiting;
46 private _activePromiseCount;
47 constructor(options: BatcherOptions<I, O>);
48 /**
49 * Returns a promise which resolves or rejects with the individual result returned from the batching function.
50 */
51 getResult(input: I): Promise<O>;
52 /**
53 * Triggers a batch to run, adhering to the maxBatchSize, queueingThresholds, and queuingDelay
54 */
55 private _trigger();
56 /**
57 * Runs the batch, while respecting delays imposed by the supplied delayFunction
58 */
59 private _run();
60 /**
61 * Runs the batch immediately without further delay
62 */
63 private _runImmediately();
64}