@aircall/request-batcher
v0.0.4
Published
Aircall Request batcher module
Downloads
160
Keywords
Readme
RequestBatcher
A utility for batching and processing requests efficiently.
Table of Contents
Installation
Install the @aircall/request-batcher
library using pnpm:
pnpm add @aircall/request-batcher
Usage
import { RequestBatcher } from '@aircall/request-batcher';
// Create an instance of RequestBatcher
const requestBatcher = new RequestBatcher({
maxBatchSize: 10, // Optional: Maximum size of each batch (default is 3 from constants)
delay: 1000, // Optional: Maximum delay in milliseconds before flushing a batch (default is 1000ms from constants)
requestHandler: async (data) => {
// Your custom logic to handle the batched requests
// Should return an array of results corresponding to each input in the batch
// Example:
return data.map(item => /* your processing logic here */);
},
});
// Add data to the batch and get a promise that resolves when the batch is processed
const resultPromise = await requestBatcher.batch(yourRequestData);
Options
maxBatchSize
(optional): Maximum size of each batch (default is BATCH_SIZE from constants).delay
(optional): Maximum delay in milliseconds before flushing a batch (default is MAX_INTERVAL from constants).requestHandler
: A function that takes an array of data and returns a promise that resolves with an array of results.
Methods
batch(data)
Adds data to the batch and returns a promise that will be resolved when the batch is processed.- data: The data to be added to the batch.
flushBatch()
Processes the batch by calling the requestHandler function and resolving/rejecting promises.