Framework
Version
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference

Batcher

Class: Batcher<TValue>

Defined in: batcher.ts:143

A class that collects items and processes them in batches.

Batching is a technique for grouping multiple operations together to be processed as a single unit.

The Batcher provides a flexible way to implement batching with configurable:

  • Maximum batch size (number of items per batch)
  • Time-based batching (process after X milliseconds)
  • Custom batch processing logic via getShouldExecute
  • Event callbacks for monitoring batch operations

State Management:

  • Uses TanStack Store for reactive state management
  • Use initialState to provide initial state values when creating the batcher
  • Use onExecute callback to react to batch execution and implement custom logic
  • Use onItemsChange callback to react to items being added or removed from the batcher
  • The state includes batch execution count, total items processed, items, and running status
  • State can be accessed via batcher.store.state when using the class directly
  • When using framework adapters (React/Solid), state is accessed from batcher.state

Example

ts
const batcher = new Batcher<number>(
  (items) => console.log('Processing batch:', items),
  {
    maxSize: 5,
    wait: 2000,
    onExecute: (batcher) => console.log('Batch executed:', batcher.peekAllItems())
  }
);

batcher.addItem(1);
batcher.addItem(2);
// After 2 seconds or when 5 items are added, whichever comes first,
// the batch will be processed
// batcher.flush() // manually trigger a batch
const batcher = new Batcher<number>(
  (items) => console.log('Processing batch:', items),
  {
    maxSize: 5,
    wait: 2000,
    onExecute: (batcher) => console.log('Batch executed:', batcher.peekAllItems())
  }
);

batcher.addItem(1);
batcher.addItem(2);
// After 2 seconds or when 5 items are added, whichever comes first,
// the batch will be processed
// batcher.flush() // manually trigger a batch

Type Parameters

TValue

Constructors

new Batcher()

ts
new Batcher<TValue>(fn, initialOptions): Batcher<TValue>
new Batcher<TValue>(fn, initialOptions): Batcher<TValue>

Defined in: batcher.ts:150

Parameters

fn

(items) => void

initialOptions

BatcherOptions<TValue>

Returns

Batcher<TValue>

Properties

options

ts
options: BatcherOptionsWithOptionalCallbacks<TValue>;
options: BatcherOptionsWithOptionalCallbacks<TValue>;

Defined in: batcher.ts:147


store

ts
readonly store: Store<Readonly<BatcherState<TValue>>>;
readonly store: Store<Readonly<BatcherState<TValue>>>;

Defined in: batcher.ts:144

Methods

addItem()

ts
addItem(item): void
addItem(item): void

Defined in: batcher.ts:194

Adds an item to the batcher If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed

Parameters

item

TValue

Returns

void


clear()

ts
clear(): void
clear(): void

Defined in: batcher.ts:282

Removes all items from the batcher

Returns

void


flush()

ts
flush(): void
flush(): void

Defined in: batcher.ts:242

Processes the current batch of items immediately

Returns

void


peekAllItems()

ts
peekAllItems(): TValue[]
peekAllItems(): TValue[]

Defined in: batcher.ts:268

Returns a copy of all items in the batcher

Returns

TValue[]


reset()

ts
reset(): void
reset(): void

Defined in: batcher.ts:289

Resets the batcher state to its default values

Returns

void


setOptions()

ts
setOptions(newOptions): void
setOptions(newOptions): void

Defined in: batcher.ts:164

Updates the batcher options

Parameters

newOptions

Partial<BatcherOptions<TValue>>

Returns

void


start()

ts
start(): void
start(): void

Defined in: batcher.ts:258

Starts the batcher and processes any pending items

Returns

void


stop()

ts
stop(): void
stop(): void

Defined in: batcher.ts:250

Stops the batcher from processing batches

Returns

void

Our Partners
Unkey
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.