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