UNPKG

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