定义于:batcher.ts:144
一个收集项目并以批次处理它们的类。
批处理是一种将多个操作组合在一起作为单个单元进行处理的技术。
Batcher 提供了一种灵活的方式来实现具有可配置的批处理
状态管理
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>
定义于:batcher.ts:152
(items) => void
BatcherOptions<TValue>
Batcher<TValue>
fn: (items) => void;
fn: (items) => void;
定义于:batcher.ts:153
TValue[]
void
key: string;
key: string;
定义于:batcher.ts:148
options: BatcherOptionsWithOptionalCallbacks<TValue>;
options: BatcherOptionsWithOptionalCallbacks<TValue>;
定义于:batcher.ts:149
readonly store: Store<Readonly<BatcherState<TValue>>>;
readonly store: Store<Readonly<BatcherState<TValue>>>;
定义于:batcher.ts:145
addItem(item): void
addItem(item): void
定义于:batcher.ts:204
将一个项目添加到 batcher。如果达到批次大小、发生超时或 shouldProcess 返回 true,则将处理该批次。
TValue
void
clear(): void
clear(): void
定义于:batcher.ts:274
移除 batcher 中的所有项目
void
flush(): void
flush(): void
定义于:batcher.ts:252
立即处理当前批次的项目
void
peekAllItems(): TValue[]
peekAllItems(): TValue[]
定义于:batcher.ts:260
返回 batcher 中所有项目的副本
TValue[]
reset(): void
reset(): void
定义于:batcher.ts:281
将 batcher 状态重置为默认值
void
setOptions(newOptions): void
setOptions(newOptions): void
定义于:batcher.ts:173
更新 batcher 选项
Partial<BatcherOptions<TValue>>
void
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。