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