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:
State Management:
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
• TValue
new Batcher<TValue>(fn, initialOptions): Batcher<TValue>
new Batcher<TValue>(fn, initialOptions): Batcher<TValue>
Defined in: batcher.ts:150
(items) => void
BatcherOptions<TValue>
Batcher<TValue>
options: BatcherOptionsWithOptionalCallbacks<TValue>;
options: BatcherOptionsWithOptionalCallbacks<TValue>;
Defined in: batcher.ts:147
readonly store: Store<Readonly<BatcherState<TValue>>>;
readonly store: Store<Readonly<BatcherState<TValue>>>;
Defined in: batcher.ts:144
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
TValue
void
clear(): void
clear(): void
Defined in: batcher.ts:282
Removes all items from the batcher
void
flush(): void
flush(): void
Defined in: batcher.ts:242
Processes the current batch of items immediately
void
peekAllItems(): TValue[]
peekAllItems(): TValue[]
Defined in: batcher.ts:268
Returns a copy of all items in the batcher
TValue[]
reset(): void
reset(): void
Defined in: batcher.ts:289
Resets the batcher state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: batcher.ts:164
Updates the batcher options
Partial<BatcherOptions<TValue>>
void
start(): void
start(): void
Defined in: batcher.ts:258
Starts the batcher and processes any pending items
void
stop(): void
stop(): void
Defined in: batcher.ts:250
Stops the batcher from processing batches
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.