@creejs/commons-lang 2.1.12 → 2.1.13

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.
@@ -1399,7 +1399,20 @@ var ExecUtils = {
1399
1399
  quietKeepError
1400
1400
  };
1401
1401
 
1402
- // @ts-nocheck
1402
+ // owned
1403
+
1404
+ /**
1405
+ * @typedef {{
1406
+ * promise: Promise<*>,
1407
+ * timerHandler: NodeJS.Timeout,
1408
+ * timerCleared: boolean,
1409
+ * resolved: boolean,
1410
+ * rejected: boolean,
1411
+ * canceled: boolean,
1412
+ * reject: (reason: Error)=> void,
1413
+ * resolve: (...args:any[])=> void
1414
+ * }} Deferred
1415
+ */
1403
1416
 
1404
1417
  /**
1405
1418
  * @module PromiseUtils
@@ -1422,12 +1435,16 @@ var PromiseUtils = {
1422
1435
  * 1. timeout=-1, it means no timeout check
1423
1436
  * @param {number} [timeout=-1] - Timeout duration in milliseconds
1424
1437
  * @param {string} [timeoutMessage]
1425
- * @returns {{promise: Promise<*>, reject: function, resolve: function}}
1438
+ * @returns {Deferred}
1426
1439
  */
1427
1440
  function defer (timeout = -1, timeoutMessage) {
1428
1441
  assertNumber(timeout);
1442
+ /** @type {Deferred} */
1429
1443
  const rtnVal = {};
1430
1444
 
1445
+ /**
1446
+ * @type {NodeJS.Timeout}
1447
+ */
1431
1448
  let timerHandler;
1432
1449
  if (timeout >= 0) {
1433
1450
  rtnVal.timerHandler = timerHandler = setTimeout(() => {
@@ -1435,17 +1452,16 @@ function defer (timeout = -1, timeoutMessage) {
1435
1452
  rtnVal.timerCleared = true; // easy to check in test case
1436
1453
  rtnVal.reject(new Error(timeoutMessage ?? `Promise Timeout: ${timeout}ms`));
1437
1454
  }, timeout);
1438
- rtnVal.timerHandler = timerHandler;
1439
1455
  }
1440
1456
 
1441
1457
  rtnVal.promise = new Promise((resolve, reject) => {
1442
- rtnVal.resolve = (...args) => {
1458
+ rtnVal.resolve = (arg) => {
1443
1459
  if (timerHandler != null) {
1444
1460
  clearTimeout(timerHandler); // must clear it
1445
1461
  rtnVal.timerCleared = true; // easy to check in test case
1446
1462
  }
1447
1463
  rtnVal.resolved = true;
1448
- resolve(...args);
1464
+ resolve(arg);
1449
1465
  };
1450
1466
 
1451
1467
  rtnVal.reject = (err) => {
@@ -1457,12 +1473,14 @@ function defer (timeout = -1, timeoutMessage) {
1457
1473
  reject(err);
1458
1474
  };
1459
1475
  });
1476
+ // @ts-ignore
1460
1477
  rtnVal.promise.cancel = () => {
1461
1478
  if (timerHandler != null) {
1462
1479
  clearTimeout(timerHandler); // must clear it
1463
1480
  rtnVal.timerCleared = true; // easy to check in test case
1464
1481
  }
1465
1482
  rtnVal.rejected = true; // easy to check in test case
1483
+ // @ts-ignore
1466
1484
  rtnVal.canceled = rtnVal.promise.canceled = true; // easy to check in test case
1467
1485
  rtnVal.reject(new Error('Cancelled'));
1468
1486
  };
@@ -1509,8 +1527,8 @@ function timeout (promise, time, message) {
1509
1527
  * 2. It's NOT convenient to use Promise.allSettled() to get the results of all promises.
1510
1528
  * * the data structure is not consistent when fullfilled or rejected
1511
1529
  * * have to check "string" type of status to know sucess or failure
1512
- * @param {Promise} promises
1513
- * @returns {Array<{ok: boolean, result: any}>}
1530
+ * @param {Promise<*>[]} promises
1531
+ * @returns {Promise<{ok: boolean, result: any}[]>}
1514
1532
  */
1515
1533
  async function allSettled (promises) {
1516
1534
  assertArray(promises);
@@ -1553,13 +1571,14 @@ function returnValuePromised (task) {
1553
1571
  *
1554
1572
  * @param {Promise<*>|number|undefined} [promise] - The input promise to delay
1555
1573
  * @param {number|undefined} [ms] - Minimum delay in milliseconds (default: 1)
1556
- * @returns {Promise} A new promise that settles after the delay period
1574
+ * @returns {Promise<*>} A new promise that settles after the delay period
1557
1575
  */
1558
1576
  function delay (promise, ms) {
1559
- if (isNumber(promise)) {
1577
+ if (isNumber(promise)) { // defer(ms)
1578
+ // @ts-ignore
1560
1579
  ms = promise;
1561
1580
  promise = Promise.resolve();
1562
- } else if (promise == null && ms == null) {
1581
+ } else if (promise == null && ms == null) { // defer(promise, ms)
1563
1582
  ms = 1;
1564
1583
  promise = Promise.resolve();
1565
1584
  }
@@ -1568,16 +1587,16 @@ function delay (promise, ms) {
1568
1587
  assertNumber(ms);
1569
1588
  const deferred = defer();
1570
1589
  const startTs = Date.now();
1571
- promise
1572
- .then((...args) => {
1573
- const escaped = Date.now() - startTs;
1574
- if (escaped < ms) {
1575
- setTimeout(() => deferred.resolve(...args), ms - escaped);
1576
- } else {
1577
- deferred.resolve(...args);
1578
- }
1579
- })
1580
- .catch((err) => {
1590
+ // @ts-ignore
1591
+ promise.then((...args) => {
1592
+ const escaped = Date.now() - startTs;
1593
+ if (escaped < ms) {
1594
+ setTimeout(() => deferred.resolve(...args), ms - escaped);
1595
+ } else {
1596
+ deferred.resolve(...args);
1597
+ }
1598
+ })
1599
+ .catch((/** @type {Error} */ err) => {
1581
1600
  const escaped = Date.now() - startTs;
1582
1601
  if (escaped < ms) {
1583
1602
  setTimeout(() => deferred.reject(err), ms - escaped);
@@ -1593,8 +1612,8 @@ function delay (promise, ms) {
1593
1612
  * 1. export function are executed one by one
1594
1613
  * 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
1595
1614
  * 3. if an element is not function, rejects the whole chain with Error(Not Function)
1596
- * @param {Function[]} promises
1597
- * @returns {Promise<Array>} Promise that resolves with an array of results in the same order as input tasks
1615
+ * @param {Function[]} tasks
1616
+ * @returns {Promise<any[]>} Promise that resolves with an array of results in the same order as input tasks
1598
1617
  */
1599
1618
  async function series (tasks) {
1600
1619
  assertArray(tasks);
@@ -1634,7 +1653,7 @@ async function seriesAllSettled (tasks) {
1634
1653
  * 2. rejects whole chain with the first error, when first task fails
1635
1654
  * @param {Function[]} tasks
1636
1655
  * @param {number} [maxParallel=5]
1637
- * @returns {Promise<Array>} Array of resolved values from all promises
1656
+ * @returns {Promise<any[]>} Array of resolved values from all promises
1638
1657
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
1639
1658
  */
1640
1659
  async function parallel (tasks, maxParallel = 5) {
@@ -1676,7 +1695,7 @@ async function parallel (tasks, maxParallel = 5) {
1676
1695
  * 2. all tasks will be executed, even some of them failed.
1677
1696
  * @param {Function[]} tasks
1678
1697
  * @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
1679
- * @returns {Promise<Array>} Array of resolved values from all promises
1698
+ * @returns {Promise<any[]>} Array of resolved values from all promises
1680
1699
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
1681
1700
  */
1682
1701
  async function parallelAllSettled (tasks, maxParallel = 5) {