@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.
- package/dist/cjs/index-dev.cjs +43 -24
- package/dist/cjs/index-dev.cjs.map +1 -1
- package/dist/cjs/index-min.cjs +1 -1
- package/dist/cjs/index-min.cjs.map +1 -1
- package/dist/esm/index-dev.js +43 -24
- package/dist/esm/index-dev.js.map +1 -1
- package/dist/esm/index-min.js +1 -1
- package/dist/esm/index-min.js.map +1 -1
- package/dist/umd/index.dev.js +43 -24
- package/dist/umd/index.dev.js.map +1 -1
- package/dist/umd/index.min.js +1 -1
- package/dist/umd/index.min.js.map +1 -1
- package/package.json +1 -1
- package/types/promise-utils.d.ts +22 -16
package/dist/cjs/index-dev.cjs
CHANGED
|
@@ -1403,7 +1403,20 @@ var ExecUtils = {
|
|
|
1403
1403
|
quietKeepError
|
|
1404
1404
|
};
|
|
1405
1405
|
|
|
1406
|
-
//
|
|
1406
|
+
// owned
|
|
1407
|
+
|
|
1408
|
+
/**
|
|
1409
|
+
* @typedef {{
|
|
1410
|
+
* promise: Promise<*>,
|
|
1411
|
+
* timerHandler: NodeJS.Timeout,
|
|
1412
|
+
* timerCleared: boolean,
|
|
1413
|
+
* resolved: boolean,
|
|
1414
|
+
* rejected: boolean,
|
|
1415
|
+
* canceled: boolean,
|
|
1416
|
+
* reject: (reason: Error)=> void,
|
|
1417
|
+
* resolve: (...args:any[])=> void
|
|
1418
|
+
* }} Deferred
|
|
1419
|
+
*/
|
|
1407
1420
|
|
|
1408
1421
|
/**
|
|
1409
1422
|
* @module PromiseUtils
|
|
@@ -1426,12 +1439,16 @@ var PromiseUtils = {
|
|
|
1426
1439
|
* 1. timeout=-1, it means no timeout check
|
|
1427
1440
|
* @param {number} [timeout=-1] - Timeout duration in milliseconds
|
|
1428
1441
|
* @param {string} [timeoutMessage]
|
|
1429
|
-
* @returns {
|
|
1442
|
+
* @returns {Deferred}
|
|
1430
1443
|
*/
|
|
1431
1444
|
function defer (timeout = -1, timeoutMessage) {
|
|
1432
1445
|
assertNumber(timeout);
|
|
1446
|
+
/** @type {Deferred} */
|
|
1433
1447
|
const rtnVal = {};
|
|
1434
1448
|
|
|
1449
|
+
/**
|
|
1450
|
+
* @type {NodeJS.Timeout}
|
|
1451
|
+
*/
|
|
1435
1452
|
let timerHandler;
|
|
1436
1453
|
if (timeout >= 0) {
|
|
1437
1454
|
rtnVal.timerHandler = timerHandler = setTimeout(() => {
|
|
@@ -1439,17 +1456,16 @@ function defer (timeout = -1, timeoutMessage) {
|
|
|
1439
1456
|
rtnVal.timerCleared = true; // easy to check in test case
|
|
1440
1457
|
rtnVal.reject(new Error(timeoutMessage ?? `Promise Timeout: ${timeout}ms`));
|
|
1441
1458
|
}, timeout);
|
|
1442
|
-
rtnVal.timerHandler = timerHandler;
|
|
1443
1459
|
}
|
|
1444
1460
|
|
|
1445
1461
|
rtnVal.promise = new Promise((resolve, reject) => {
|
|
1446
|
-
rtnVal.resolve = (
|
|
1462
|
+
rtnVal.resolve = (arg) => {
|
|
1447
1463
|
if (timerHandler != null) {
|
|
1448
1464
|
clearTimeout(timerHandler); // must clear it
|
|
1449
1465
|
rtnVal.timerCleared = true; // easy to check in test case
|
|
1450
1466
|
}
|
|
1451
1467
|
rtnVal.resolved = true;
|
|
1452
|
-
resolve(
|
|
1468
|
+
resolve(arg);
|
|
1453
1469
|
};
|
|
1454
1470
|
|
|
1455
1471
|
rtnVal.reject = (err) => {
|
|
@@ -1461,12 +1477,14 @@ function defer (timeout = -1, timeoutMessage) {
|
|
|
1461
1477
|
reject(err);
|
|
1462
1478
|
};
|
|
1463
1479
|
});
|
|
1480
|
+
// @ts-ignore
|
|
1464
1481
|
rtnVal.promise.cancel = () => {
|
|
1465
1482
|
if (timerHandler != null) {
|
|
1466
1483
|
clearTimeout(timerHandler); // must clear it
|
|
1467
1484
|
rtnVal.timerCleared = true; // easy to check in test case
|
|
1468
1485
|
}
|
|
1469
1486
|
rtnVal.rejected = true; // easy to check in test case
|
|
1487
|
+
// @ts-ignore
|
|
1470
1488
|
rtnVal.canceled = rtnVal.promise.canceled = true; // easy to check in test case
|
|
1471
1489
|
rtnVal.reject(new Error('Cancelled'));
|
|
1472
1490
|
};
|
|
@@ -1513,8 +1531,8 @@ function timeout (promise, time, message) {
|
|
|
1513
1531
|
* 2. It's NOT convenient to use Promise.allSettled() to get the results of all promises.
|
|
1514
1532
|
* * the data structure is not consistent when fullfilled or rejected
|
|
1515
1533
|
* * have to check "string" type of status to know sucess or failure
|
|
1516
|
-
* @param {Promise} promises
|
|
1517
|
-
* @returns {
|
|
1534
|
+
* @param {Promise<*>[]} promises
|
|
1535
|
+
* @returns {Promise<{ok: boolean, result: any}[]>}
|
|
1518
1536
|
*/
|
|
1519
1537
|
async function allSettled (promises) {
|
|
1520
1538
|
assertArray(promises);
|
|
@@ -1557,13 +1575,14 @@ function returnValuePromised (task) {
|
|
|
1557
1575
|
*
|
|
1558
1576
|
* @param {Promise<*>|number|undefined} [promise] - The input promise to delay
|
|
1559
1577
|
* @param {number|undefined} [ms] - Minimum delay in milliseconds (default: 1)
|
|
1560
|
-
* @returns {Promise} A new promise that settles after the delay period
|
|
1578
|
+
* @returns {Promise<*>} A new promise that settles after the delay period
|
|
1561
1579
|
*/
|
|
1562
1580
|
function delay (promise, ms) {
|
|
1563
|
-
if (isNumber(promise)) {
|
|
1581
|
+
if (isNumber(promise)) { // defer(ms)
|
|
1582
|
+
// @ts-ignore
|
|
1564
1583
|
ms = promise;
|
|
1565
1584
|
promise = Promise.resolve();
|
|
1566
|
-
} else if (promise == null && ms == null) {
|
|
1585
|
+
} else if (promise == null && ms == null) { // defer(promise, ms)
|
|
1567
1586
|
ms = 1;
|
|
1568
1587
|
promise = Promise.resolve();
|
|
1569
1588
|
}
|
|
@@ -1572,16 +1591,16 @@ function delay (promise, ms) {
|
|
|
1572
1591
|
assertNumber(ms);
|
|
1573
1592
|
const deferred = defer();
|
|
1574
1593
|
const startTs = Date.now();
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
.catch((err) => {
|
|
1594
|
+
// @ts-ignore
|
|
1595
|
+
promise.then((...args) => {
|
|
1596
|
+
const escaped = Date.now() - startTs;
|
|
1597
|
+
if (escaped < ms) {
|
|
1598
|
+
setTimeout(() => deferred.resolve(...args), ms - escaped);
|
|
1599
|
+
} else {
|
|
1600
|
+
deferred.resolve(...args);
|
|
1601
|
+
}
|
|
1602
|
+
})
|
|
1603
|
+
.catch((/** @type {Error} */ err) => {
|
|
1585
1604
|
const escaped = Date.now() - startTs;
|
|
1586
1605
|
if (escaped < ms) {
|
|
1587
1606
|
setTimeout(() => deferred.reject(err), ms - escaped);
|
|
@@ -1597,8 +1616,8 @@ function delay (promise, ms) {
|
|
|
1597
1616
|
* 1. export function are executed one by one
|
|
1598
1617
|
* 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
|
|
1599
1618
|
* 3. if an element is not function, rejects the whole chain with Error(Not Function)
|
|
1600
|
-
* @param {Function[]}
|
|
1601
|
-
* @returns {Promise<
|
|
1619
|
+
* @param {Function[]} tasks
|
|
1620
|
+
* @returns {Promise<any[]>} Promise that resolves with an array of results in the same order as input tasks
|
|
1602
1621
|
*/
|
|
1603
1622
|
async function series (tasks) {
|
|
1604
1623
|
assertArray(tasks);
|
|
@@ -1638,7 +1657,7 @@ async function seriesAllSettled (tasks) {
|
|
|
1638
1657
|
* 2. rejects whole chain with the first error, when first task fails
|
|
1639
1658
|
* @param {Function[]} tasks
|
|
1640
1659
|
* @param {number} [maxParallel=5]
|
|
1641
|
-
* @returns {Promise<
|
|
1660
|
+
* @returns {Promise<any[]>} Array of resolved values from all promises
|
|
1642
1661
|
* @throws {TypeError} If input is not an array of export function or maxParallel is not a number
|
|
1643
1662
|
*/
|
|
1644
1663
|
async function parallel (tasks, maxParallel = 5) {
|
|
@@ -1680,7 +1699,7 @@ async function parallel (tasks, maxParallel = 5) {
|
|
|
1680
1699
|
* 2. all tasks will be executed, even some of them failed.
|
|
1681
1700
|
* @param {Function[]} tasks
|
|
1682
1701
|
* @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
|
|
1683
|
-
* @returns {Promise<
|
|
1702
|
+
* @returns {Promise<any[]>} Array of resolved values from all promises
|
|
1684
1703
|
* @throws {TypeError} If input is not an array of export function or maxParallel is not a number
|
|
1685
1704
|
*/
|
|
1686
1705
|
async function parallelAllSettled (tasks, maxParallel = 5) {
|