Skip to content

Class: BatchBuffer<T>

Package: @litert/concurrentImport path: @litert/concurrentSource: packages/partials/concurrent/src/Classes/BatchBuffer.ts

Accumulates items pushed one-by-one (or as arrays) and delivers them in batches to a callback. The flush is triggered either when the buffer reaches maxSize or when the debounce timer fires (whichever comes first).

Internally backed by a DebounceController, so the same delayMs / maxDelayMs semantics apply.


Constructor

ts
new BatchBuffer<T>(opts: IBatchBufferOptions<T>)

See IBatchBufferOptions<T>.


Methods

push(item)

ts
push(item: T | T[]): void

Adds one item or an array of items to the buffer.

  • If the buffer size reaches or exceeds maxSize after the push, the callback is invoked immediately with all buffered items, bypassing the debounce timer.
  • If an empty array is passed, it is ignored.
ParameterTypeDescription
itemT | T[]The item(s) to buffer

Example

ts
import { BatchBuffer } from '@litert/concurrent';

const buffer = new BatchBuffer<number>({
    delayMs: 1000,
    maxSize: 5,
    callback: (items) => {
        console.log('Processing batch:', items);
    },
});

buffer.push(1);
buffer.push(2);
buffer.push([3, 4, 5]); // Buffer full → callback called immediately with [1,2,3,4,5]

buffer.push(6);
// After 1 second of inactivity → callback called with [6]