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