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:84

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

Example

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

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

batcher.addItem(1);
batcher.addItem(2);
// After 2 seconds or when 5 items are added, whichever comes first,
// the batch will be processed
// batcher.execute() // 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:92

Parameters

fn

(items) => void

initialOptions

BatcherOptions<TValue>

Returns

Batcher<TValue>

Methods

addItem()

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

Defined in: batcher.ts:118

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


execute()

ts
execute(): void
execute(): void

Defined in: batcher.ts:146

Processes the current batch of items. This method will automatically be triggered if the batcher is running and any of these conditions are met:

  • The number of items reaches batchSize
  • The wait duration has elapsed
  • The getShouldExecute function returns true upon adding an item

You can also call this method manually to process the current batch at any time.

Returns

void


getAllItems()

ts
getAllItems(): TValue[]
getAllItems(): TValue[]

Defined in: batcher.ts:213

Returns a copy of all items currently in the batcher

Returns

TValue[]


getBatchExecutionCount()

ts
getBatchExecutionCount(): number
getBatchExecutionCount(): number

Defined in: batcher.ts:220

Returns the number of times batches have been processed

Returns

number


getIsEmpty()

ts
getIsEmpty(): boolean
getIsEmpty(): boolean

Defined in: batcher.ts:199

Returns true if the batcher is empty

Returns

boolean


getIsRunning()

ts
getIsRunning(): boolean
getIsRunning(): boolean

Defined in: batcher.ts:206

Returns true if the batcher is running

Returns

boolean


getItemExecutionCount()

ts
getItemExecutionCount(): number
getItemExecutionCount(): number

Defined in: batcher.ts:227

Returns the total number of individual items that have been processed

Returns

number


getOptions()

ts
getOptions(): BatcherOptions<TValue>
getOptions(): BatcherOptions<TValue>

Defined in: batcher.ts:110

Returns the current batcher options

Returns

BatcherOptions<TValue>


getSize()

ts
getSize(): number
getSize(): number

Defined in: batcher.ts:192

Returns the current number of items in the batcher

Returns

number


setOptions()

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

Defined in: batcher.ts:103

Updates the batcher options

Parameters

newOptions

Partial<BatcherOptions<TValue>>

Returns

void


start()

ts
start(): void
start(): void

Defined in: batcher.ts:181

Starts the batcher and processes any pending items

Returns

void


stop()

ts
stop(): void
stop(): void

Defined in: batcher.ts:169

Stops the batcher from processing batches

Returns

void

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.