1 |
|
2 |
|
3 |
|
4 |
|
5 | export declare const BATCHER_RETRY_TOKEN: unique symbol;
|
6 | export type BatchingResult<T> = T | Error | typeof BATCHER_RETRY_TOKEN;
|
7 | export interface BatcherOptions<I, O> {
|
8 | |
9 |
|
10 |
|
11 | readonly maxBatchSize?: number;
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | readonly queuingDelay?: number;
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | readonly queuingThresholds?: readonly number[];
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | readonly batchingFunction: (
|
36 | this: Batcher<I, O>,
|
37 | inputs: readonly I[],
|
38 | ) => ReadonlyArray<BatchingResult<O>> | PromiseLike<ReadonlyArray<BatchingResult<O>>>;
|
39 | |
40 |
|
41 |
|
42 |
|
43 | readonly delayFunction?: () => PromiseLike<void> | undefined | null | void;
|
44 | }
|
45 | export 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 | }
|