Repair task-reordering bug

This commit is contained in:
Tony Garnock-Jones 2021-03-03 11:44:45 +01:00
parent b922a53d6a
commit f8f643000a
1 changed files with 7 additions and 2 deletions

View File

@ -1,13 +1,18 @@
const LIMIT = 25000;
let taskCounter = 0;
let delayedTasks: Array<() => void> = [];
export function queueTask(f: () => void) {
taskCounter++;
if (taskCounter === LIMIT) {
setTimeout(() => taskCounter = 0, 0);
setTimeout(() => {
taskCounter = 0;
delayedTasks.forEach(queueMicrotask);
delayedTasks = [];
}, 0);
}
if (taskCounter >= LIMIT) {
setTimeout(f, 0);
delayedTasks.push(f);
} else {
queueMicrotask(f);
}