23 lines
593 B
TypeScript
23 lines
593 B
TypeScript
/// SPDX-License-Identifier: GPL-3.0-or-later
|
|
/// SPDX-FileCopyrightText: Copyright © 2016-2022 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
|
|
|
const LIMIT = 25000;
|
|
|
|
let taskCounter = 0;
|
|
let delayedTasks: Array<() => void> = [];
|
|
export function queueTask(f: () => void) {
|
|
taskCounter++;
|
|
if (taskCounter === LIMIT) {
|
|
setTimeout(() => {
|
|
taskCounter = 0;
|
|
delayedTasks.forEach(queueMicrotask);
|
|
delayedTasks = [];
|
|
}, 0);
|
|
}
|
|
if (taskCounter >= LIMIT) {
|
|
delayedTasks.push(f);
|
|
} else {
|
|
queueMicrotask(f);
|
|
}
|
|
}
|