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';
const multiTabBatcher = new MultiTabBatcher(
{
maxBatchSize: 10,
delay: 1000,
fallbackDelay: 4000,
requestHandler: async (data) => {
return data.map(item => );
},
},
'your_manager_name'
);
try {
const result = await multiTabBatcher.batch(yourPayloadData);
console.log('Batch processing result:', result);
} catch (error) {
console.error('Batch processing error:', error);
}
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.