2021-12-01 16:24:29 +00:00
|
|
|
/// SPDX-License-Identifier: GPL-3.0-or-later
|
2022-01-26 13:38:38 +00:00
|
|
|
/// SPDX-FileCopyrightText: Copyright © 2016-2022 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
2018-11-01 15:29:20 +00:00
|
|
|
|
2021-12-01 16:13:00 +00:00
|
|
|
const LIMIT = 25000;
|
2018-11-01 15:29:20 +00:00
|
|
|
|
2021-12-01 16:13:00 +00:00
|
|
|
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);
|
2021-01-11 22:35:36 +00:00
|
|
|
}
|
|
|
|
}
|