File: Batcher.md | Updated: 11/15/2025
Search...
+ K
Auto
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide
Documentation
Framework
React
Version
Latest
Search...
+ K
Menu
Getting Started
Guides
API Reference
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference
Debouncer Examples
Throttler Examples
Rate Limiter Examples
Queue Examples
Batcher Examples
TanStack Query Examples
Framework
React
Version
Latest
Menu
Getting Started
Guides
API Reference
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference
Debouncer Examples
Throttler Examples
Rate Limiter Examples
Queue Examples
Batcher Examples
TanStack Query Examples
On this page
Copy Markdown
Class: Batcher<TValue>
======================
Defined in: batcher.ts:145
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. This synchronous version is lighter weight and often all you need - upgrade to AsyncBatcher when you need promises, retry support, abort/cancel capabilities, or advanced error handling.
The Batcher provides a flexible way to implement batching with configurable:
State Management:
ts
const batcher = new Batcher<number>(
(items) => console.log('Processing batch:', items),
{
maxSize: 5,
wait: 2000,
onExecute: (batch, batcher) => console.log('Batch executed:', batch)
}
);
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: (batch, batcher) => console.log('Batch executed:', batch)
}
);
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
TValue
Constructors
------------
### Constructor
ts
new Batcher<TValue>(fn, initialOptions): Batcher<TValue>;
new Batcher<TValue>(fn, initialOptions): Batcher<TValue>;
Defined in: batcher.ts:153
(items) => void
BatcherOptions <TValue>
Batcher<TValue>
Properties
----------
### fn()
ts
fn: (items) => void;
fn: (items) => void;
Defined in: batcher.ts:154
TValue[]
void
ts
key: string | undefined;
key: string | undefined;
Defined in: batcher.ts:149
ts
options: BatcherOptionsWithOptionalCallbacks<TValue>;
options: BatcherOptionsWithOptionalCallbacks<TValue>;
Defined in: batcher.ts:150
ts
readonly store: Store<Readonly<BatcherState<TValue>>>;
readonly store: Store<Readonly<BatcherState<TValue>>>;
Defined in: batcher.ts:146
ts
addItem(item): void;
addItem(item): void;
Defined in: batcher.ts:205
Adds an item to the batcher If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed
TValue
void
ts
cancel(): void;
cancel(): void;
Defined in: batcher.ts:283
Cancels any pending execution that was scheduled. Does NOT clear out the items.
void
ts
clear(): void;
clear(): void;
Defined in: batcher.ts:275
Removes all items from the batcher
void
ts
flush(): void;
flush(): void;
Defined in: batcher.ts:253
Processes the current batch of items immediately
void
ts
peekAllItems(): TValue[];
peekAllItems(): TValue[];
Defined in: batcher.ts:261
Returns a copy of all items in the batcher
TValue[]
ts
reset(): void;
reset(): void;
Defined in: batcher.ts:291
Resets the batcher state to its default values
void
ts
setOptions(newOptions): void;
setOptions(newOptions): void;
Defined in: batcher.ts:174
Updates the batcher options
#### Parameters ##### newOptions
Partial<BatcherOptions <TValue>>
void
