@creejs/commons-lang 2.1.25 → 2.1.27

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,11 +2045,15 @@ 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
2053
+ * @param {string} [failureMessage] - Error message when all tasks failed
2050
2054
  * @returns {Promise<any>} Resolves with the result of the first successfully completed task
2051
2055
  */
2052
- async function parallelAny (tasks, maxParallel = 5) {
2056
+ async function parallelAny (tasks, maxParallel = 5, failureMessage) {
2053
2057
  assertArray(tasks, 'tasks');
2054
2058
  assertNumber(maxParallel);
2055
2059
  if (tasks.length === 0) {
@@ -2082,28 +2086,29 @@ async function parallelAny (tasks, maxParallel = 5) {
2082
2086
  // @ts-ignore
2083
2087
  taskPromise = returnValuePromised(task);
2084
2088
  } else {
2085
- errors.push(new TypeError(`Invalid task: ${task}`));
2086
- takeTaskAndRun();
2087
- return
2089
+ taskPromise = Promise.reject(new TypeError(`Invalid task: ${typeof task}, Only Promise or Function allowed`));
2088
2090
  }
2091
+
2089
2092
  taskPromise.then(/** @type {any} */ rtnVal => {
2090
- deferred.resolve(rtnVal);
2093
+ runningTasksCount--; // DO Not put in finally
2094
+ // any task resolved, whole parallelAny resolved
2095
+ deferred.resolve(rtnVal); // This may be called many times: In worst case, "maxParallel" times
2091
2096
  }).catch(e => {
2092
2097
  errors.push(e);
2093
2098
  // No task left, and No successful execution, reject with errors
2094
2099
  if (errors.length >= tasks.length) {
2095
2100
  if (deferred.pending) {
2096
- deferred.reject(new AggregatedError('All Tasks Failed', errors));
2101
+ deferred.reject(new AggregatedError(failureMessage ?? 'All Tasks Failed', errors));
2097
2102
  return
2098
2103
  }
2099
2104
  }
2100
- takeTaskAndRun();
2101
- }).finally(() => {
2102
- runningTasksCount--;
2105
+ runningTasksCount--; // DO Not put in finally, must reduce before takeTaskAndRun()
2106
+ takeTaskAndRun(); // start next task
2103
2107
  });
2104
2108
  }
2105
- // start tasks until maxParallel
2106
- while (runningTasksCount < maxParallel) {
2109
+ // start tasks until maxParallel or no more task
2110
+ const parallelCount = Math.min(tasks.length, maxParallel);
2111
+ for (let i = 0; i < parallelCount; i++) {
2107
2112
  takeTaskAndRun();
2108
2113
  }
2109
2114
  return deferred.promise
@@ -2531,6 +2536,7 @@ const s2ns = 1_000_000_000;
2531
2536
  var TimeUtils = {
2532
2537
  s2ns,
2533
2538
  ms2ns,
2539
+ timestamp,
2534
2540
  timestamp64,
2535
2541
  lapseNano,
2536
2542
  lapseMillis,
@@ -2539,8 +2545,9 @@ var TimeUtils = {
2539
2545
  };
2540
2546
 
2541
2547
  /**
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.
2548
+ * Gets the current timestamp in nanoseconds
2549
+ * * If "performance" API is available, uses performance timeOrigin + now()
2550
+ * * Else fall back to `Date.now()` to get milliseconds and convert to nanoseconds
2544
2551
  * @returns {bigint} Current timestamp as a BigInt
2545
2552
  */
2546
2553
  function timestamp64 () {
@@ -2555,6 +2562,24 @@ function timestamp64 () {
2555
2562
  return BigInt(Date.now() * ms2ns)
2556
2563
  }
2557
2564
 
2565
+ /**
2566
+ * Gets the current timestamp in milliseconds
2567
+ * * If "performance" API is available, uses performance timeOrigin + now(), then convert to milliseconds
2568
+ * * Else fall back to `Date.now()`
2569
+ * @returns {number} Current timestamp in milliseconds
2570
+ */
2571
+ function timestamp () {
2572
+ // sinon can not fake performance.timeOrigin, so we have to check it
2573
+ if (typeof performance !== 'undefined' && typeof performance.timeOrigin === 'number') {
2574
+ // timeOrigin specifies the high resolution millisecond timestamp, eg. 1756350801931.159
2575
+ const base = performance.timeOrigin;
2576
+ // the current high resolution millisecond timestamp, eg.31767926.416357
2577
+ const now = performance.now();
2578
+ return Math.ceil((base + now) / ms2ns)
2579
+ }
2580
+ return Date.now()
2581
+ }
2582
+
2558
2583
  /**
2559
2584
  * Calculates the time elapsed in nanoseconds between the given timestamp and now.
2560
2585
  * @param {bigint} start - start timestamp64, in nanoseconds.