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