« get me outta code hell

sugar: wrapQueue, use this in traverse - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/common-util
diff options
context:
space:
mode:
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
commitab038ee9aa304c7127ffd9832b236fb2a7a7a787 (patch)
treecc471b436667b3a11047382a736e6050ee671ee4 /src/common-util
parent930450338e309bafb9739f4705a4632359d42ae2 (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.js29
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));
 }