From ab038ee9aa304c7127ffd9832b236fb2a7a7a787 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 20 Dec 2025 18:30:14 -0400 Subject: sugar: wrapQueue, use this in traverse doubt this is half the answer for performance but... --- src/common-util/sugar.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/common-util/sugar.js') diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js index 354cf5cc..fba06d76 100644 --- a/src/common-util/sugar.js +++ b/src/common-util/sugar.js @@ -395,6 +395,35 @@ export function queue(functionList, queueSize = 50) { return promiseList; } +export function wrapQueue(fn, queueSize = 50) { + if (queueSize === 0) return fn; + + let running = 0; + let resume = []; + + let proceed = (...args) => { + running++; + return Promise.try(fn, ...args).finally(() => { + running--; + if (resume.length) { + resume.shift()(); + } + }); + }; + + return (...args) => { + if (running === queueSize) { + return new Promise(resolve => { + resume.push(resolve); + }).then(() => { + return proceed(...args); + }); + } else { + return proceed(...args); + } + }; +} + export function delay(ms) { return new Promise((res) => setTimeout(res, ms)); } -- cgit 1.3.0-6-gf8a5