UNPKG

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