@creejs/commons-lang 2.1.25 → 2.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2045,6 +2045,9 @@ async function parallelAllSettled (tasks, maxParallel = 5) {
2045
2045
  * 2. Maybe multiple tasks are executed as a bulk block, and all of them resolved.
2046
2046
  * * only the first fulfilled value is returned
2047
2047
  * * other results are dropped
2048
+ * 3. Notice:
2049
+ * * In Beginning, we start bulk(maxParallel) of tasks
2050
+ * * One task failed, it will start next task immediately, Not Wait for the bulk(maxParallel) to complete
2048
2051
  * @param {Array<Function|Promise<any>>} tasks - Array of async functions to execute
2049
2052
  * @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
2050
2053
  * @returns {Promise<any>} Resolves with the result of the first successfully completed task
@@ -2082,12 +2085,13 @@ async function parallelAny (tasks, maxParallel = 5) {
2082
2085
  // @ts-ignore
2083
2086
  taskPromise = returnValuePromised(task);
2084
2087
  } else {
2085
- errors.push(new TypeError(`Invalid task: ${task}`));
2086
- takeTaskAndRun();
2087
- return
2088
+ taskPromise = Promise.reject(new TypeError(`Invalid task: ${typeof task}, Only Promise or Function allowed`));
2088
2089
  }
2090
+
2089
2091
  taskPromise.then(/** @type {any} */ rtnVal => {
2090
- deferred.resolve(rtnVal);
2092
+ runningTasksCount--; // DO Not put in finally
2093
+ // any task resolved, whole parallelAny resolved
2094
+ deferred.resolve(rtnVal); // This may be called many times: In worst case, "maxParallel" times
2091
2095
  }).catch(e => {
2092
2096
  errors.push(e);
2093
2097
  // No task left, and No successful execution, reject with errors
@@ -2097,13 +2101,13 @@ async function parallelAny (tasks, maxParallel = 5) {
2097
2101
  return
2098
2102
  }
2099
2103
  }
2100
- takeTaskAndRun();
2101
- }).finally(() => {
2102
- runningTasksCount--;
2104
+ runningTasksCount--; // DO Not put in finally, must reduce before takeTaskAndRun()
2105
+ takeTaskAndRun(); // start next task
2103
2106
  });
2104
2107
  }
2105
- // start tasks until maxParallel
2106
- while (runningTasksCount < maxParallel) {
2108
+ // start tasks until maxParallel or no more task
2109
+ const parallelCount = Math.min(tasks.length, maxParallel);
2110
+ for (let i = 0; i < parallelCount; i++) {
2107
2111
  takeTaskAndRun();
2108
2112
  }
2109
2113
  return deferred.promise
@@ -2531,6 +2535,7 @@ const s2ns = 1_000_000_000;
2531
2535
  var TimeUtils = {
2532
2536
  s2ns,
2533
2537
  ms2ns,
2538
+ timestamp,
2534
2539
  timestamp64,
2535
2540
  lapseNano,
2536
2541
  lapseMillis,
@@ -2539,8 +2544,9 @@ var TimeUtils = {
2539
2544
  };
2540
2545
 
2541
2546
  /**
2542
- * Gets the current timestamp in nanoseconds (if running in Node.js) or milliseconds (in browser).
2543
- * Uses `process.hrtime.bigint()` for high-resolution timestamps in Node.js, falls back to `Date.now()` in browsers.
2547
+ * Gets the current timestamp in nanoseconds
2548
+ * * If "performance" API is available, uses performance timeOrigin + now()
2549
+ * * Else fall back to `Date.now()` to get milliseconds and convert to nanoseconds
2544
2550
  * @returns {bigint} Current timestamp as a BigInt
2545
2551
  */
2546
2552
  function timestamp64 () {
@@ -2555,6 +2561,24 @@ function timestamp64 () {
2555
2561
  return BigInt(Date.now() * ms2ns)
2556
2562
  }
2557
2563
 
2564
+ /**
2565
+ * Gets the current timestamp in milliseconds
2566
+ * * If "performance" API is available, uses performance timeOrigin + now(), then convert to milliseconds
2567
+ * * Else fall back to `Date.now()`
2568
+ * @returns {number} Current timestamp in milliseconds
2569
+ */
2570
+ function timestamp () {
2571
+ // sinon can not fake performance.timeOrigin, so we have to check it
2572
+ if (typeof performance !== 'undefined' && typeof performance.timeOrigin === 'number') {
2573
+ // timeOrigin specifies the high resolution millisecond timestamp, eg. 1756350801931.159
2574
+ const base = performance.timeOrigin;
2575
+ // the current high resolution millisecond timestamp, eg.31767926.416357
2576
+ const now = performance.now();
2577
+ return Math.ceil((base + now) / ms2ns)
2578
+ }
2579
+ return Date.now()
2580
+ }
2581
+
2558
2582
  /**
2559
2583
  * Calculates the time elapsed in nanoseconds between the given timestamp and now.
2560
2584
  * @param {bigint} start - start timestamp64, in nanoseconds.