@creejs/commons-lang 2.1.12 → 2.1.14

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.
@@ -1359,11 +1359,34 @@
1359
1359
  }
1360
1360
 
1361
1361
  /**
1362
- * Executes a task silently, suppressing any errors or rejections.
1362
+ * @module ExecUtils
1363
+ * @description Utils about how to execute task functions.
1364
+ */
1365
+
1366
+
1367
+ /**
1368
+ * @typedef {{
1369
+ * error: (...args:any[]) => void,
1370
+ * warn: (...args:any[]) => void,
1371
+ * info: (...args:any[]) => void,
1372
+ * debug: (...args:any[]) => void,
1373
+ * trace: (...args:any[]) => void,
1374
+ * isErrorEnabled: () => boolean,
1375
+ * isWarnEnabled: () => boolean,
1376
+ * isInfoEnabled: () => boolean,
1377
+ * isDebugEnabled: () => boolean,
1378
+ * isTraceEnabled: () => boolean,
1379
+ * }} LoggerLike
1380
+ */
1381
+
1382
+ /**
1383
+ * 1. Executes a task silently, suppressing any errors or rejections.
1384
+ * 2. if loggerLike is provided, will log the error via it
1363
1385
  * @param {Function} task - The export function to execute.
1386
+ * @param {LoggerLike} loggerLike - The logger-like object to use for logging.
1364
1387
  * @returns {Promise<*>|*} The return value of the task, or a Promise if the task is asynchronous.
1365
1388
  */
1366
- function quiet (task) {
1389
+ function quiet (task, loggerLike) {
1367
1390
  assertFunction(task);
1368
1391
  try {
1369
1392
  const rtnVal = task();
@@ -1405,7 +1428,20 @@
1405
1428
  quietKeepError
1406
1429
  };
1407
1430
 
1408
- // @ts-nocheck
1431
+ // owned
1432
+
1433
+ /**
1434
+ * @typedef {{
1435
+ * promise: Promise<*>,
1436
+ * timerHandler: NodeJS.Timeout,
1437
+ * timerCleared: boolean,
1438
+ * resolved: boolean,
1439
+ * rejected: boolean,
1440
+ * canceled: boolean,
1441
+ * reject: (reason: Error)=> void,
1442
+ * resolve: (...args:any[])=> void
1443
+ * }} Deferred
1444
+ */
1409
1445
 
1410
1446
  /**
1411
1447
  * @module PromiseUtils
@@ -1428,12 +1464,16 @@
1428
1464
  * 1. timeout=-1, it means no timeout check
1429
1465
  * @param {number} [timeout=-1] - Timeout duration in milliseconds
1430
1466
  * @param {string} [timeoutMessage]
1431
- * @returns {{promise: Promise<*>, reject: function, resolve: function}}
1467
+ * @returns {Deferred}
1432
1468
  */
1433
1469
  function defer (timeout = -1, timeoutMessage) {
1434
1470
  assertNumber(timeout);
1471
+ /** @type {Deferred} */
1435
1472
  const rtnVal = {};
1436
1473
 
1474
+ /**
1475
+ * @type {NodeJS.Timeout}
1476
+ */
1437
1477
  let timerHandler;
1438
1478
  if (timeout >= 0) {
1439
1479
  rtnVal.timerHandler = timerHandler = setTimeout(() => {
@@ -1441,17 +1481,16 @@
1441
1481
  rtnVal.timerCleared = true; // easy to check in test case
1442
1482
  rtnVal.reject(new Error(timeoutMessage ?? `Promise Timeout: ${timeout}ms`));
1443
1483
  }, timeout);
1444
- rtnVal.timerHandler = timerHandler;
1445
1484
  }
1446
1485
 
1447
1486
  rtnVal.promise = new Promise((resolve, reject) => {
1448
- rtnVal.resolve = (...args) => {
1487
+ rtnVal.resolve = (arg) => {
1449
1488
  if (timerHandler != null) {
1450
1489
  clearTimeout(timerHandler); // must clear it
1451
1490
  rtnVal.timerCleared = true; // easy to check in test case
1452
1491
  }
1453
1492
  rtnVal.resolved = true;
1454
- resolve(...args);
1493
+ resolve(arg);
1455
1494
  };
1456
1495
 
1457
1496
  rtnVal.reject = (err) => {
@@ -1463,12 +1502,14 @@
1463
1502
  reject(err);
1464
1503
  };
1465
1504
  });
1505
+ // @ts-ignore
1466
1506
  rtnVal.promise.cancel = () => {
1467
1507
  if (timerHandler != null) {
1468
1508
  clearTimeout(timerHandler); // must clear it
1469
1509
  rtnVal.timerCleared = true; // easy to check in test case
1470
1510
  }
1471
1511
  rtnVal.rejected = true; // easy to check in test case
1512
+ // @ts-ignore
1472
1513
  rtnVal.canceled = rtnVal.promise.canceled = true; // easy to check in test case
1473
1514
  rtnVal.reject(new Error('Cancelled'));
1474
1515
  };
@@ -1515,8 +1556,8 @@
1515
1556
  * 2. It's NOT convenient to use Promise.allSettled() to get the results of all promises.
1516
1557
  * * the data structure is not consistent when fullfilled or rejected
1517
1558
  * * have to check "string" type of status to know sucess or failure
1518
- * @param {Promise} promises
1519
- * @returns {Array<{ok: boolean, result: any}>}
1559
+ * @param {Promise<*>[]} promises
1560
+ * @returns {Promise<{ok: boolean, result: any}[]>}
1520
1561
  */
1521
1562
  async function allSettled (promises) {
1522
1563
  assertArray(promises);
@@ -1559,13 +1600,14 @@
1559
1600
  *
1560
1601
  * @param {Promise<*>|number|undefined} [promise] - The input promise to delay
1561
1602
  * @param {number|undefined} [ms] - Minimum delay in milliseconds (default: 1)
1562
- * @returns {Promise} A new promise that settles after the delay period
1603
+ * @returns {Promise<*>} A new promise that settles after the delay period
1563
1604
  */
1564
1605
  function delay (promise, ms) {
1565
- if (isNumber(promise)) {
1606
+ if (isNumber(promise)) { // defer(ms)
1607
+ // @ts-ignore
1566
1608
  ms = promise;
1567
1609
  promise = Promise.resolve();
1568
- } else if (promise == null && ms == null) {
1610
+ } else if (promise == null && ms == null) { // defer(promise, ms)
1569
1611
  ms = 1;
1570
1612
  promise = Promise.resolve();
1571
1613
  }
@@ -1574,16 +1616,16 @@
1574
1616
  assertNumber(ms);
1575
1617
  const deferred = defer();
1576
1618
  const startTs = Date.now();
1577
- promise
1578
- .then((...args) => {
1579
- const escaped = Date.now() - startTs;
1580
- if (escaped < ms) {
1581
- setTimeout(() => deferred.resolve(...args), ms - escaped);
1582
- } else {
1583
- deferred.resolve(...args);
1584
- }
1585
- })
1586
- .catch((err) => {
1619
+ // @ts-ignore
1620
+ promise.then((...args) => {
1621
+ const escaped = Date.now() - startTs;
1622
+ if (escaped < ms) {
1623
+ setTimeout(() => deferred.resolve(...args), ms - escaped);
1624
+ } else {
1625
+ deferred.resolve(...args);
1626
+ }
1627
+ })
1628
+ .catch((/** @type {Error} */ err) => {
1587
1629
  const escaped = Date.now() - startTs;
1588
1630
  if (escaped < ms) {
1589
1631
  setTimeout(() => deferred.reject(err), ms - escaped);
@@ -1599,8 +1641,8 @@
1599
1641
  * 1. export function are executed one by one
1600
1642
  * 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
1601
1643
  * 3. if an element is not function, rejects the whole chain with Error(Not Function)
1602
- * @param {Function[]} promises
1603
- * @returns {Promise<Array>} Promise that resolves with an array of results in the same order as input tasks
1644
+ * @param {Function[]} tasks
1645
+ * @returns {Promise<any[]>} Promise that resolves with an array of results in the same order as input tasks
1604
1646
  */
1605
1647
  async function series (tasks) {
1606
1648
  assertArray(tasks);
@@ -1640,7 +1682,7 @@
1640
1682
  * 2. rejects whole chain with the first error, when first task fails
1641
1683
  * @param {Function[]} tasks
1642
1684
  * @param {number} [maxParallel=5]
1643
- * @returns {Promise<Array>} Array of resolved values from all promises
1685
+ * @returns {Promise<any[]>} Array of resolved values from all promises
1644
1686
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
1645
1687
  */
1646
1688
  async function parallel (tasks, maxParallel = 5) {
@@ -1682,7 +1724,7 @@
1682
1724
  * 2. all tasks will be executed, even some of them failed.
1683
1725
  * @param {Function[]} tasks
1684
1726
  * @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
1685
- * @returns {Promise<Array>} Array of resolved values from all promises
1727
+ * @returns {Promise<any[]>} Array of resolved values from all promises
1686
1728
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
1687
1729
  */
1688
1730
  async function parallelAllSettled (tasks, maxParallel = 5) {
@@ -1720,6 +1762,7 @@
1720
1762
 
1721
1763
  // owned
1722
1764
 
1765
+ // module vars
1723
1766
  const { isPlainObject } = TypeUtils;
1724
1767
 
1725
1768
  /**
@@ -1784,6 +1827,8 @@
1784
1827
  return Reflect.construct(proxyCls, args ?? [])
1785
1828
  }
1786
1829
 
1830
+ // owned
1831
+
1787
1832
  /**
1788
1833
  * @module InstanceProxyUtils
1789
1834
  */
@@ -1810,7 +1855,6 @@
1810
1855
 
1811
1856
  // owned
1812
1857
 
1813
-
1814
1858
  /**
1815
1859
  * Retrieves all unique method names from a class's prototype chain. *
1816
1860
  * @param {Function} cls - The class to inspect
@@ -1887,8 +1931,6 @@
1887
1931
  };
1888
1932
 
1889
1933
  // internal
1890
-
1891
-
1892
1934
  // owned
1893
1935
 
1894
1936
  /**
@@ -1972,6 +2014,8 @@
1972
2014
  return true
1973
2015
  }
1974
2016
 
2017
+ // owned
2018
+
1975
2019
  var ArrayBufferUtils = {
1976
2020
  readString,
1977
2021
  writeString,