0.0.5 • Published 3 months ago

@aircall/multi-tab-batcher v0.0.5

Weekly downloads
-
License
-
Repository
-
Last release
3 months ago

MultiTabBatcher

MultiTabBatcher is a Module designed to coordinate and synchronize batch processing of requests across multiple browser tabs or windows using the Broadcast Channel API.

Installation

To use MultiTabBatcher in your project, you need to install the required dependencies. Make sure you have Node.js installed.

Install the @aircall/multi-tab-batcher library using pnpm:

pnpm  add  @aircall/multi-tab-batcher

Usage

import { MultiTabBatcher } from '@aircall/multi-tab-batcher';


// Instantiate MultiTabBatcher
const multiTabBatcher = new MultiTabBatcher(
  {
    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)
    fallbackDelay: 4000, // Optional: Timeout duration to await for before fallback to the regular batch (default is 4000ms 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 */);
    },
  },
  'your_manager_name' // Optional: Suffix for Broadcast Channel if you need to instantiate multiple managers
);

// Example: Batch processing
try {
  const result = await multiTabBatcher.batch(yourPayloadData);
  console.log('Batch processing result:', result);
} catch (error) {
  console.error('Batch processing error:', error);
}

// Close the MultiTabBatcher
multiTabBatcher.close();

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).
  • fallbackDelay (optional): This is a timeout duration to await for before we fallback to the regular batch
  • requestHandler: A function that takes an array of data and returns a promise that resolves with an array of results,

API Reference

batch(payload: TPayload): Promise<TResult>

Batches the provided payload for processing. If the current instance is the leader, the payload is processed immediately. Otherwise, the payload is broadcasted to all tabs, and the result is awaited.

cleanup(): void

cleanup the MultiTabBatcher, closing the underlying communication channel and flushing any remaining batches.

flushBatch()

Processes the batch by calling the requestHandler function and resolving/rejecting promises.
0.0.5

3 months ago

0.0.4

5 months ago

0.0.3

6 months ago