« get me outta code hell

sugar: queue: kick start - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/common-util/sugar.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-06-20 11:41:56 -0300
committer(quasar) nebula <qznebula@protonmail.com>2025-06-20 11:41:56 -0300
commitb65799c809ec7badc9cdf051e3f5feb078a6ab20 (patch)
tree1041dc2e4912842b3eb18c78ec0faf3bfa8c506a /src/common-util/sugar.js
parentf6ec834606af4a783f33aa2ec132593624f8744b (diff)
sugar: queue: kick start
Diffstat (limited to 'src/common-util/sugar.js')
-rw-r--r--src/common-util/sugar.js22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js
index aeb0fe4f..c7e15dd3 100644
--- a/src/common-util/sugar.js
+++ b/src/common-util/sugar.js
@@ -360,13 +360,27 @@ export function queue(functionList, queueSize = 50) {
       thisReject(error);
     } finally {
       running--;
-      next();
+
+      // If the cursor is at 1, this is the first promise that resolved,
+      // so we're now done the "kick start", and can start the remaining
+      // promises (up to queueSize).
+      if (cursor === 1) {
+        // Since only one promise is used for the "kick start", and that one
+        // has just resolved, we know there's none running at all right now,
+        // and can start as many as specified in the queueSize right away.
+        for (let i = 0; i < queueSize; i++) {
+          setTimeout(next);
+        }
+      } else {
+        setTimeout(next);
+      }
     }
   };
 
-  for (let i = 0; i < queueSize; i++) {
-    next();
-  }
+  // Only start a single promise, as a "kick start", so that it resolves as
+  // early as possible (it will resolve before we use CPU to start the rest
+  // of the promises, up to queueSize).
+  next();
 
   return promiseList;
 }