@aircall/request-batcher
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @aircall/request-batcher

Weekly Downloads

1,765

Version

0.0.4

License

none

Unpacked Size

9.92 kB

Total Files

6

Last publish

Collaborators

  • xdurand
  • aircall-ci
  • kamalbennani
  • danielmarcano