@kwiz/common 1.0.66 → 1.0.68
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/helpers/promises.d.ts +35 -10
- package/dist/helpers/promises.js +44 -19
- package/dist/helpers/promises.js.map +1 -1
- package/package.json +1 -1
- package/src/helpers/promises.ts +51 -23
|
@@ -1,14 +1,39 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Ensures that a promise runs only once
|
|
3
|
+
* @param {string} key - Unique key to identify the promise.
|
|
4
|
+
* @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
|
|
5
|
+
* @param {(result: T) => Promise<boolean>} isValidResult - Optional function that returns boolean to indicate if the result returned
|
|
6
|
+
* by the promise is valid and should be kepy in memory.
|
|
7
|
+
* @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
|
|
8
|
+
* @example
|
|
9
|
+
* // returns Promise<string>
|
|
10
|
+
* export var initTests = promiseOnce<string>("initTests", async () => { ... });
|
|
11
|
+
*/
|
|
12
|
+
export declare function promiseOnce<T>(key: string, promiseFunc: () => Promise<T>, isValidResult?: (result: T) => Promise<boolean>): Promise<T>;
|
|
13
|
+
/**
|
|
14
|
+
* Runs all promises in sequential order.
|
|
15
|
+
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
16
|
+
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
17
|
+
* provided promise functions
|
|
18
|
+
*/
|
|
9
19
|
export declare function promiseAllSequential<T = any>(asyncFuncs: (() => Promise<T>)[]): Promise<T[]>;
|
|
20
|
+
/**
|
|
21
|
+
* Runs N promises in parallel.
|
|
22
|
+
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
23
|
+
* @param {number} [maxParallel] - Max number of promises to run in parallel (default=8).
|
|
24
|
+
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
25
|
+
* provided promise functions
|
|
26
|
+
*/
|
|
10
27
|
export declare function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParallel?: number): Promise<T[]>;
|
|
11
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* Provides an asnyc sleep function that allows you to delay async/wait calls.
|
|
30
|
+
* @param {number} [seconds] - Time to sleep in seconds.
|
|
31
|
+
*/
|
|
12
32
|
export declare function sleepAsync(seconds?: number): Promise<void>;
|
|
13
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* Provides the ability to retry an async function n times with a optional delay between calls
|
|
35
|
+
* @param {(...args) => Promise<T>} fn - Function to retry,
|
|
36
|
+
* @param {number} numberOfRetries - Number of times to retry.
|
|
37
|
+
* @param {number} [seconds] - Delay between retries in seconds (default=1).
|
|
38
|
+
*/
|
|
14
39
|
export declare function retryAsync<T>(fn: (...args: any[]) => Promise<T>, numberOfRetries: number, seconds?: number): Promise<T>;
|
package/dist/helpers/promises.js
CHANGED
|
@@ -3,15 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.retryAsync = exports.sleepAsync = exports.promiseNParallel = exports.promiseAllSequential = exports.promiseOnce = void 0;
|
|
4
4
|
const objects_1 = require("./objects");
|
|
5
5
|
const typecheckers_1 = require("./typecheckers");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* }
|
|
6
|
+
let _global = (0, objects_1.getGlobal)("helpers_promises", {
|
|
7
|
+
promises: {}
|
|
8
|
+
});
|
|
9
|
+
/**
|
|
10
|
+
* Ensures that a promise runs only once
|
|
11
|
+
* @param {string} key - Unique key to identify the promise.
|
|
12
|
+
* @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
|
|
13
|
+
* @param {(result: T) => Promise<boolean>} isValidResult - Optional function that returns boolean to indicate if the result returned
|
|
14
|
+
* by the promise is valid and should be kepy in memory.
|
|
15
|
+
* @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
|
|
16
|
+
* @example
|
|
17
|
+
* // returns Promise<string>
|
|
18
|
+
* export var initTests = promiseOnce<string>("initTests", async () => { ... });
|
|
13
19
|
*/
|
|
14
|
-
async function promiseOnce(key,
|
|
20
|
+
async function promiseOnce(key, promiseFunc, isValidResult) {
|
|
21
|
+
let promises = _global.promises;
|
|
15
22
|
if ((0, objects_1.hasOwnProperty)(promises, key) && (0, typecheckers_1.isFunction)(isValidResult)) {
|
|
16
23
|
//we have en existing pending promise...
|
|
17
24
|
let queuedResult = null;
|
|
@@ -23,11 +30,17 @@ async function promiseOnce(key, promise, isValidResult) {
|
|
|
23
30
|
delete promises[key];
|
|
24
31
|
}
|
|
25
32
|
if (!(0, objects_1.hasOwnProperty)(promises, key)) {
|
|
26
|
-
promises[key] =
|
|
33
|
+
promises[key] = promiseFunc();
|
|
27
34
|
}
|
|
28
35
|
return promises[key];
|
|
29
36
|
}
|
|
30
37
|
exports.promiseOnce = promiseOnce;
|
|
38
|
+
/**
|
|
39
|
+
* Runs all promises in sequential order.
|
|
40
|
+
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
41
|
+
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
42
|
+
* provided promise functions
|
|
43
|
+
*/
|
|
31
44
|
function promiseAllSequential(asyncFuncs) {
|
|
32
45
|
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
33
46
|
return Promise.resolve([]);
|
|
@@ -40,15 +53,19 @@ function promiseAllSequential(asyncFuncs) {
|
|
|
40
53
|
})), Promise.resolve([]));
|
|
41
54
|
}
|
|
42
55
|
exports.promiseAllSequential = promiseAllSequential;
|
|
43
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Runs N promises in parallel.
|
|
58
|
+
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
59
|
+
* @param {number} [maxParallel] - Max number of promises to run in parallel (default=8).
|
|
60
|
+
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
61
|
+
* provided promise functions
|
|
62
|
+
*/
|
|
63
|
+
function promiseNParallel(asyncFuncs, maxParallel = 8) {
|
|
44
64
|
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
45
65
|
return Promise.resolve([]);
|
|
46
66
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
var startChain = () => {
|
|
51
|
-
var chainData = [];
|
|
67
|
+
let startChain = () => {
|
|
68
|
+
let chainData = [];
|
|
52
69
|
if (asyncFuncs.length) {
|
|
53
70
|
let next = (data) => {
|
|
54
71
|
chainData.push(data);
|
|
@@ -60,8 +77,8 @@ function promiseNParallel(asyncFuncs, maxParallel) {
|
|
|
60
77
|
return Promise.resolve(chainData);
|
|
61
78
|
}
|
|
62
79
|
};
|
|
63
|
-
|
|
64
|
-
for (
|
|
80
|
+
let chains = [];
|
|
81
|
+
for (let k = 0; k < maxParallel; k += 1) {
|
|
65
82
|
chains.push(startChain());
|
|
66
83
|
}
|
|
67
84
|
return Promise.all(chains).then(d => {
|
|
@@ -70,14 +87,22 @@ function promiseNParallel(asyncFuncs, maxParallel) {
|
|
|
70
87
|
});
|
|
71
88
|
}
|
|
72
89
|
exports.promiseNParallel = promiseNParallel;
|
|
73
|
-
/**
|
|
90
|
+
/**
|
|
91
|
+
* Provides an asnyc sleep function that allows you to delay async/wait calls.
|
|
92
|
+
* @param {number} [seconds] - Time to sleep in seconds.
|
|
93
|
+
*/
|
|
74
94
|
function sleepAsync(seconds) {
|
|
75
95
|
return new Promise(resolve => {
|
|
76
96
|
window.setTimeout(() => resolve(), seconds > 0 ? seconds * 1000 : 3000);
|
|
77
97
|
});
|
|
78
98
|
}
|
|
79
99
|
exports.sleepAsync = sleepAsync;
|
|
80
|
-
/**
|
|
100
|
+
/**
|
|
101
|
+
* Provides the ability to retry an async function n times with a optional delay between calls
|
|
102
|
+
* @param {(...args) => Promise<T>} fn - Function to retry,
|
|
103
|
+
* @param {number} numberOfRetries - Number of times to retry.
|
|
104
|
+
* @param {number} [seconds] - Delay between retries in seconds (default=1).
|
|
105
|
+
*/
|
|
81
106
|
async function retryAsync(fn, numberOfRetries, seconds = 1) {
|
|
82
107
|
let error = null;
|
|
83
108
|
for (let i = 0; i < numberOfRetries; i++) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promises.js","sourceRoot":"","sources":["../../src/helpers/promises.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"promises.js","sourceRoot":"","sources":["../../src/helpers/promises.ts"],"names":[],"mappings":";;;AACA,uCAAsD;AACtD,iDAA+D;AAE/D,IAAI,OAAO,GAAG,IAAA,mBAAS,EAA0C,kBAAkB,EAC/E;IACI,QAAQ,EAAE,EAAE;CACf,CAAC,CAAC;AAEP;;;;;;;;;;GAUG;AACI,KAAK,UAAU,WAAW,CAAI,GAAW,EAAE,WAA6B,EAAE,aAA+C;IAC5H,IAAI,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;IAEhC,IAAI,IAAA,wBAAc,EAAC,QAAQ,EAAE,GAAG,CAAC,IAAI,IAAA,yBAAU,EAAC,aAAa,CAAC,EAAE,CAAC;QAC7D,wCAAwC;QACxC,IAAI,YAAY,GAAM,IAAI,CAAC;QAC3B,IAAI,CAAC;YAAC,YAAY,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAC;QAAC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;QACzD,IAAI,CAAC,MAAM,aAAa,CAAC,YAAY,CAAC,CAAC,KAAK,IAAI;YAC5C,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAED,IAAI,CAAC,IA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AAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;AACP,CAAC;AAJD,gCAIC;AAED;;;;;GAKG;AACI,KAAK,UAAU,UAAU,CAAI,EAA2B,EAAE,eAAuB,EAAE,OAAO,GAAG,CAAC;IACjG,IAAI,KAAK,GAAU,IAAI,CAAC;IAExB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,eAAe,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,IAAI,CAAC;YACD,KAAK,GAAG,IAAI,CAAC;YACb,MAAM,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;YACxC,OAAO,MAAM,EAAE,EAAE,CAAC;QACtB,CAAC;QAAC,OAAO,EAAE,EAAE,CAAC;YACV,KAAK,GAAG,EAAE,CAAC;QACf,CAAC;IACL,CAAC;IAED,IAAI,KAAK,EAAE,CAAC;QACR,MAAM,KAAK,CAAC;IAChB,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,mBAAmB,eAAe,QAAQ,CAAC,CAAC;AAChE,CAAC;AAjBD,gCAiBC"}
|
package/package.json
CHANGED
package/src/helpers/promises.ts
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import { IDictionary } from "./_dependencies";
|
|
2
|
-
import { hasOwnProperty } from "./objects";
|
|
3
|
-
import { isFunction, isNullOrUndefined
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*
|
|
2
|
+
import { getGlobal, hasOwnProperty } from "./objects";
|
|
3
|
+
import { isFunction, isNullOrUndefined } from "./typecheckers";
|
|
4
|
+
|
|
5
|
+
let _global = getGlobal<{ promises: IDictionary<Promise<any>> }>("helpers_promises",
|
|
6
|
+
{
|
|
7
|
+
promises: {}
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Ensures that a promise runs only once
|
|
12
|
+
* @param {string} key - Unique key to identify the promise.
|
|
13
|
+
* @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
|
|
14
|
+
* @param {(result: T) => Promise<boolean>} isValidResult - Optional function that returns boolean to indicate if the result returned
|
|
15
|
+
* by the promise is valid and should be kepy in memory.
|
|
16
|
+
* @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
|
|
17
|
+
* @example
|
|
18
|
+
* // returns Promise<string>
|
|
19
|
+
* export var initTests = promiseOnce<string>("initTests", async () => { ... });
|
|
12
20
|
*/
|
|
13
|
-
export async function promiseOnce<T>(key: string,
|
|
21
|
+
export async function promiseOnce<T>(key: string, promiseFunc: () => Promise<T>, isValidResult?: (result: T) => Promise<boolean>): Promise<T> {
|
|
22
|
+
let promises = _global.promises;
|
|
23
|
+
|
|
14
24
|
if (hasOwnProperty(promises, key) && isFunction(isValidResult)) {
|
|
15
25
|
//we have en existing pending promise...
|
|
16
26
|
let queuedResult: T = null;
|
|
@@ -19,13 +29,19 @@ export async function promiseOnce<T>(key: string, promise: () => Promise<T>, isV
|
|
|
19
29
|
delete promises[key];
|
|
20
30
|
}
|
|
21
31
|
|
|
22
|
-
|
|
23
32
|
if (!hasOwnProperty(promises, key)) {
|
|
24
|
-
promises[key] =
|
|
33
|
+
promises[key] = promiseFunc();
|
|
25
34
|
}
|
|
35
|
+
|
|
26
36
|
return promises[key];
|
|
27
37
|
}
|
|
28
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Runs all promises in sequential order.
|
|
41
|
+
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
42
|
+
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
43
|
+
* provided promise functions
|
|
44
|
+
*/
|
|
29
45
|
export function promiseAllSequential<T = any>(asyncFuncs: (() => Promise<T>)[]): Promise<T[]> {
|
|
30
46
|
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
31
47
|
return Promise.resolve([]);
|
|
@@ -41,16 +57,20 @@ export function promiseAllSequential<T = any>(asyncFuncs: (() => Promise<T>)[]):
|
|
|
41
57
|
), Promise.resolve([]));
|
|
42
58
|
}
|
|
43
59
|
|
|
44
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Runs N promises in parallel.
|
|
62
|
+
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
63
|
+
* @param {number} [maxParallel] - Max number of promises to run in parallel (default=8).
|
|
64
|
+
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
65
|
+
* provided promise functions
|
|
66
|
+
*/
|
|
67
|
+
export function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParallel: number = 8): Promise<T[]> {
|
|
45
68
|
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
46
69
|
return Promise.resolve([]);
|
|
47
70
|
}
|
|
48
|
-
if (!isNumeric(maxParallel)) {
|
|
49
|
-
maxParallel = asyncFuncs.length;
|
|
50
|
-
}
|
|
51
71
|
|
|
52
|
-
|
|
53
|
-
|
|
72
|
+
let startChain = () => {
|
|
73
|
+
let chainData = [];
|
|
54
74
|
|
|
55
75
|
if (asyncFuncs.length) {
|
|
56
76
|
let next = (data: T) => {
|
|
@@ -63,8 +83,8 @@ export function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParalle
|
|
|
63
83
|
}
|
|
64
84
|
};
|
|
65
85
|
|
|
66
|
-
|
|
67
|
-
for (
|
|
86
|
+
let chains = [];
|
|
87
|
+
for (let k = 0; k < maxParallel; k += 1) {
|
|
68
88
|
chains.push(startChain());
|
|
69
89
|
}
|
|
70
90
|
|
|
@@ -74,14 +94,22 @@ export function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParalle
|
|
|
74
94
|
});
|
|
75
95
|
}
|
|
76
96
|
|
|
77
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* Provides an asnyc sleep function that allows you to delay async/wait calls.
|
|
99
|
+
* @param {number} [seconds] - Time to sleep in seconds.
|
|
100
|
+
*/
|
|
78
101
|
export function sleepAsync(seconds?: number): Promise<void> {
|
|
79
102
|
return new Promise(resolve => {
|
|
80
103
|
window.setTimeout(() => resolve(), seconds > 0 ? seconds * 1000 : 3000);
|
|
81
104
|
});
|
|
82
105
|
}
|
|
83
106
|
|
|
84
|
-
/**
|
|
107
|
+
/**
|
|
108
|
+
* Provides the ability to retry an async function n times with a optional delay between calls
|
|
109
|
+
* @param {(...args) => Promise<T>} fn - Function to retry,
|
|
110
|
+
* @param {number} numberOfRetries - Number of times to retry.
|
|
111
|
+
* @param {number} [seconds] - Delay between retries in seconds (default=1).
|
|
112
|
+
*/
|
|
85
113
|
export async function retryAsync<T>(fn: (...args) => Promise<T>, numberOfRetries: number, seconds = 1) {
|
|
86
114
|
let error: Error = null;
|
|
87
115
|
|