UNPKG

haystack-nclient

Version:

Project Haystack Network Client

70 lines (69 loc) 2.49 kB
/** * The function used to batch arguments together and return their results. * * The returned array size must be the same size as the arguments. If there's * an error instead of a return value, an error must be returned for the specified * argument. */ export interface BatcherFunc<ArgType, ReturnValueType> { (args: ArgType[]): Promise<(ReturnValueType | Error)[]>; } /** * Used for batching asynchronous requests together. * * The supplied `batcher` function is used to send all the requests * together in one go and return the response values. * * ```typescript * const batcher = async (values: string[]): Promise<string[]> => makeMyNetworkRequest(values) * ... * const processor = new BatchProcessor<string, string>({ batcher }) * ... * await Promise.all([ * processor.invoke('foo'), * processor.invoke('bar'), * processor.invoke('today') * ]) * ``` */ export declare class BatchProcessor<ArgType, ReturnValueType> { #private; /** * Construct a new batch processor that's used for batching * requests together. * * @param options.batcher The batcher function that typically makes a network request * with all the supplied arguments and returns the results. Each result can either * be the expected result or an Error object. * @param options.timeoutWindowMs Optional timeout window in milliseconds. By * default this is set to zero ms. This is the time used to wait before * invoking the `batcher` function. * @param options.limit Optional number of arguments limit in one call to the batcher * function. For instance, if the limit is 10 and there are 100 arguments batched * then the batcher will be called 10 times with 10 arguments each time. * By default, this is set to 100. */ constructor({ batcher, timeoutWindowMs, limit, }: { batcher: BatcherFunc<ArgType, ReturnValueType>; timeoutWindowMs?: number; limit?: number; }); /** * Asynchronously invoke an operation with some arguments and return a * promise with the resolved data. * * @param arg The invocation arguments. * @returns The invoked response. */ invoke(arg: ArgType): Promise<ReturnValueType>; /** * Restart the batch poll timer. */ private restartBatchTimer; /** * Return a list of invocations bounded by a limit. * * @returns A list of invocations. */ private getInvocationsByLimit; }