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:
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
• TValue
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>
fn: (items) => void;
fn: (items) => void;
Defined in: batcher.ts:154
TValue[]
void
key: string;
key: string;
Defined in: batcher.ts:149
options: BatcherOptionsWithOptionalCallbacks<TValue>;
options: BatcherOptionsWithOptionalCallbacks<TValue>;
Defined in: batcher.ts:150
readonly store: Store<Readonly<BatcherState<TValue>>>;
readonly store: Store<Readonly<BatcherState<TValue>>>;
Defined in: batcher.ts:146
_emit(): void
_emit(): void
Defined in: batcher.ts:174
Emits a change event for the batcher instance. Mostly useful for devtools.
void
addItem(item): void
addItem(item): void
Defined in: batcher.ts:210
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
cancel(): void
cancel(): void
Defined in: batcher.ts:288
Cancels any pending execution that was scheduled. Does NOT clear out the items.
void
clear(): void
clear(): void
Defined in: batcher.ts:280
Removes all items from the batcher
void
flush(): void
flush(): void
Defined in: batcher.ts:258
Processes the current batch of items immediately
void
peekAllItems(): TValue[]
peekAllItems(): TValue[]
Defined in: batcher.ts:266
Returns a copy of all items in the batcher
TValue[]
reset(): void
reset(): void
Defined in: batcher.ts:296
Resets the batcher state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: batcher.ts:179
Updates the batcher options
Partial<BatcherOptions<TValue>>
void