Batch redux conversation changed / added actions
This commit is contained in:
parent
8953bc1edd
commit
77ceb8ee67
8 changed files with 504 additions and 186 deletions
|
@ -36,7 +36,7 @@ window.waitForAllBatchers = async () => {
|
|||
|
||||
export type BatcherOptionsType<ItemType> = {
|
||||
name: string;
|
||||
wait: number;
|
||||
wait: number | (() => number);
|
||||
maxSize: number;
|
||||
processBatch: (items: Array<ItemType>) => void | Promise<void>;
|
||||
};
|
||||
|
@ -56,12 +56,20 @@ export function createBatcher<ItemType>(
|
|||
let batcher: BatcherType<ItemType>;
|
||||
let timeout: NodeJS.Timeout | null;
|
||||
let items: Array<ItemType> = [];
|
||||
|
||||
const queue = new PQueue({
|
||||
concurrency: 1,
|
||||
timeout: MINUTE * 30,
|
||||
throwOnTimeout: true,
|
||||
});
|
||||
|
||||
function _getWait() {
|
||||
if (typeof options.wait === 'number') {
|
||||
return options.wait;
|
||||
}
|
||||
return options.wait();
|
||||
}
|
||||
|
||||
function _kickBatchOff() {
|
||||
clearTimeoutIfNecessary(timeout);
|
||||
timeout = null;
|
||||
|
@ -81,7 +89,7 @@ export function createBatcher<ItemType>(
|
|||
if (items.length === 1) {
|
||||
// Set timeout once when we just pushed the first item so that the wait
|
||||
// time is bounded by `options.wait` and not extended by further pushes.
|
||||
timeout = setTimeout(_kickBatchOff, options.wait);
|
||||
timeout = setTimeout(_kickBatchOff, _getWait());
|
||||
} else if (items.length >= options.maxSize) {
|
||||
_kickBatchOff();
|
||||
}
|
||||
|
@ -104,7 +112,7 @@ export function createBatcher<ItemType>(
|
|||
|
||||
if (items.length > 0) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await sleep(options.wait * 2);
|
||||
await sleep(_getWait() * 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue