Fix: clean up batcher timeouts in all cases
This commit is contained in:
parent
b1c1bd5e41
commit
1d3e06e3f5
1 changed files with 6 additions and 12 deletions
|
@ -45,6 +45,11 @@ export function createBatcher<ItemType>(
|
||||||
const queue = new PQueue({ concurrency: 1, timeout: 1000 * 60 * 2 });
|
const queue = new PQueue({ concurrency: 1, timeout: 1000 * 60 * 2 });
|
||||||
|
|
||||||
function _kickBatchOff() {
|
function _kickBatchOff() {
|
||||||
|
if (timeout) {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = null;
|
||||||
|
}
|
||||||
|
|
||||||
const itemsRef = items;
|
const itemsRef = items;
|
||||||
items = [];
|
items = [];
|
||||||
queue.add(async () => {
|
queue.add(async () => {
|
||||||
|
@ -58,15 +63,8 @@ export function createBatcher<ItemType>(
|
||||||
if (items.length === 1) {
|
if (items.length === 1) {
|
||||||
// Set timeout once when we just pushed the first item so that the wait
|
// 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.
|
// time is bounded by `options.wait` and not extended by further pushes.
|
||||||
timeout = setTimeout(() => {
|
timeout = setTimeout(_kickBatchOff, options.wait);
|
||||||
timeout = null;
|
|
||||||
_kickBatchOff();
|
|
||||||
}, options.wait);
|
|
||||||
} else if (items.length >= options.maxSize) {
|
} else if (items.length >= options.maxSize) {
|
||||||
if (timeout) {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
timeout = null;
|
|
||||||
}
|
|
||||||
_kickBatchOff();
|
_kickBatchOff();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,10 +95,6 @@ export function createBatcher<ItemType>(
|
||||||
window.log.info(
|
window.log.info(
|
||||||
`Flushing ${options.name} batcher items.length=${items.length}`
|
`Flushing ${options.name} batcher items.length=${items.length}`
|
||||||
);
|
);
|
||||||
if (timeout) {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
timeout = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (anyPending()) {
|
while (anyPending()) {
|
||||||
_kickBatchOff();
|
_kickBatchOff();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue