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