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:
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
• TValue
new Batcher<TValue>(fn, initialOptions): Batcher<TValue>
new Batcher<TValue>(fn, initialOptions): Batcher<TValue>
Defined in: batcher.ts:92
(items) => void
BatcherOptions<TValue>
Batcher<TValue>
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
TValue
void
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:
You can also call this method manually to process the current batch at any time.
void
getAllItems(): TValue[]
getAllItems(): TValue[]
Defined in: batcher.ts:213
Returns a copy of all items currently in the batcher
TValue[]
getBatchExecutionCount(): number
getBatchExecutionCount(): number
Defined in: batcher.ts:220
Returns the number of times batches have been processed
number
getIsEmpty(): boolean
getIsEmpty(): boolean
Defined in: batcher.ts:199
Returns true if the batcher is empty
boolean
getIsRunning(): boolean
getIsRunning(): boolean
Defined in: batcher.ts:206
Returns true if the batcher is running
boolean
getItemExecutionCount(): number
getItemExecutionCount(): number
Defined in: batcher.ts:227
Returns the total number of individual items that have been processed
number
getOptions(): BatcherOptions<TValue>
getOptions(): BatcherOptions<TValue>
Defined in: batcher.ts:110
Returns the current batcher options
BatcherOptions<TValue>
getSize(): number
getSize(): number
Defined in: batcher.ts:192
Returns the current number of items in the batcher
number
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: batcher.ts:103
Updates the batcher options
Partial<BatcherOptions<TValue>>
void
start(): void
start(): void
Defined in: batcher.ts:181
Starts the batcher and processes any pending items
void
stop(): void
stop(): void
Defined in: batcher.ts:169
Stops the batcher from processing batches
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.