Defined in: batcher.ts:144
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: (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:152
(items) => void
BatcherOptions<TValue>
Batcher<TValue>
fn: (items) => void;
fn: (items) => void;
Defined in: batcher.ts:153
TValue[]
void
key: string;
key: string;
Defined in: batcher.ts:148
options: BatcherOptionsWithOptionalCallbacks<TValue>;
options: BatcherOptionsWithOptionalCallbacks<TValue>;
Defined in: batcher.ts:149
readonly store: Store<Readonly<BatcherState<TValue>>>;
readonly store: Store<Readonly<BatcherState<TValue>>>;
Defined in: batcher.ts:145
addItem(item): void
addItem(item): void
Defined in: batcher.ts:204
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:274
Removes all items from the batcher
void
flush(): void
flush(): void
Defined in: batcher.ts:252
Processes the current batch of items immediately
void
peekAllItems(): TValue[]
peekAllItems(): TValue[]
Defined in: batcher.ts:260
Returns a copy of all items in the batcher
TValue[]
reset(): void
reset(): void
Defined in: batcher.ts:281
Resets the batcher state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: batcher.ts:173
Updates the batcher options
Partial<BatcherOptions<TValue>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.