diff options
| author | (quasar) nebula <qznebula@protonmail.com> | 2025-12-20 18:30:14 -0400 |
|---|---|---|
| committer | (quasar) nebula <qznebula@protonmail.com> | 2025-12-20 18:30:14 -0400 |
| commit | ab038ee9aa304c7127ffd9832b236fb2a7a7a787 (patch) | |
| tree | cc471b436667b3a11047382a736e6050ee671ee4 /src/common-util | |
| parent | 930450338e309bafb9739f4705a4632359d42ae2 (diff) | |
sugar: wrapQueue, use this in traverse
doubt this is half the answer for performance but...
Diffstat (limited to 'src/common-util')
| -rw-r--r-- | src/common-util/sugar.js | 29 |
1 files changed, 29 insertions, 0 deletions
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)); } |