@lodestar/utils 1.6.0-dev.e2312e0e93 → 1.6.0-dev.eb4b43da98
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/lib/promise.d.ts +21 -0
- package/lib/promise.js +98 -0
- package/lib/promise.js.map +1 -1
- package/package.json +2 -2
package/lib/promise.d.ts
CHANGED
|
@@ -2,4 +2,25 @@
|
|
|
2
2
|
* While promise t is not finished, call function `fn` per `interval`
|
|
3
3
|
*/
|
|
4
4
|
export declare function callFnWhenAwait<T>(p: Promise<NonNullable<T>>, fn: () => void, interval: number): Promise<NonNullable<T>>;
|
|
5
|
+
export declare enum RaceEvent {
|
|
6
|
+
/** all reject/resolve before cutoff */
|
|
7
|
+
precutoff = "precutoff-return",
|
|
8
|
+
/** cutoff reached as some were pending till cutoff **/
|
|
9
|
+
cutoff = "cutoff-reached",
|
|
10
|
+
/** atleast one resolved till cutoff so no race required */
|
|
11
|
+
resolvedatcutoff = "resolved-at-cutoff",
|
|
12
|
+
/** if none reject/resolve before cutoff but one resolves or all reject before timeout */
|
|
13
|
+
pretimeout = "pretimeout-return",
|
|
14
|
+
/** timeout reached as none resolved and some were pending till timeout*/
|
|
15
|
+
timeout = "timeout-reached",
|
|
16
|
+
/** promise resolved */
|
|
17
|
+
resolved = "resolved",
|
|
18
|
+
/** promise rejected */
|
|
19
|
+
rejected = "rejected"
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Wait for promises to resolve till cutoff and then race them beyond the cutoff with an overall timeout
|
|
23
|
+
* @return resolved values or rejections or still pending errors corresponding to input promises
|
|
24
|
+
*/
|
|
25
|
+
export declare function racePromisesWithCutoff<T>(promises: Promise<T>[], cutoffMs: number, timeoutMs: number, eventCb: (event: RaceEvent, delayMs: number, index?: number) => void): Promise<(Error | T)[]>;
|
|
5
26
|
//# sourceMappingURL=promise.d.ts.map
|
package/lib/promise.js
CHANGED
|
@@ -19,4 +19,102 @@ export async function callFnWhenAwait(p, fn, interval) {
|
|
|
19
19
|
}
|
|
20
20
|
return t;
|
|
21
21
|
}
|
|
22
|
+
var PromiseStatus;
|
|
23
|
+
(function (PromiseStatus) {
|
|
24
|
+
PromiseStatus[PromiseStatus["resolved"] = 0] = "resolved";
|
|
25
|
+
PromiseStatus[PromiseStatus["rejected"] = 1] = "rejected";
|
|
26
|
+
PromiseStatus[PromiseStatus["pending"] = 2] = "pending";
|
|
27
|
+
})(PromiseStatus || (PromiseStatus = {}));
|
|
28
|
+
function mapStatusesToResponses(promisesStates) {
|
|
29
|
+
return promisesStates.map((pmStatus) => {
|
|
30
|
+
switch (pmStatus.status) {
|
|
31
|
+
case PromiseStatus.resolved:
|
|
32
|
+
return pmStatus.value;
|
|
33
|
+
case PromiseStatus.rejected:
|
|
34
|
+
return pmStatus.value;
|
|
35
|
+
case PromiseStatus.pending:
|
|
36
|
+
return Error("pending");
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
export var RaceEvent;
|
|
41
|
+
(function (RaceEvent) {
|
|
42
|
+
/** all reject/resolve before cutoff */
|
|
43
|
+
RaceEvent["precutoff"] = "precutoff-return";
|
|
44
|
+
/** cutoff reached as some were pending till cutoff **/
|
|
45
|
+
RaceEvent["cutoff"] = "cutoff-reached";
|
|
46
|
+
/** atleast one resolved till cutoff so no race required */
|
|
47
|
+
RaceEvent["resolvedatcutoff"] = "resolved-at-cutoff";
|
|
48
|
+
/** if none reject/resolve before cutoff but one resolves or all reject before timeout */
|
|
49
|
+
RaceEvent["pretimeout"] = "pretimeout-return";
|
|
50
|
+
/** timeout reached as none resolved and some were pending till timeout*/
|
|
51
|
+
RaceEvent["timeout"] = "timeout-reached";
|
|
52
|
+
// events for the promises for better tracking
|
|
53
|
+
/** promise resolved */
|
|
54
|
+
RaceEvent["resolved"] = "resolved";
|
|
55
|
+
/** promise rejected */
|
|
56
|
+
RaceEvent["rejected"] = "rejected";
|
|
57
|
+
})(RaceEvent || (RaceEvent = {}));
|
|
58
|
+
/**
|
|
59
|
+
* Wait for promises to resolve till cutoff and then race them beyond the cutoff with an overall timeout
|
|
60
|
+
* @return resolved values or rejections or still pending errors corresponding to input promises
|
|
61
|
+
*/
|
|
62
|
+
export async function racePromisesWithCutoff(promises, cutoffMs, timeoutMs, eventCb) {
|
|
63
|
+
// start the cutoff and timeout timers
|
|
64
|
+
let cutoffObserved = false;
|
|
65
|
+
const cutoffPromise = new Promise((_resolve, reject) => setTimeout(reject, cutoffMs)).catch((e) => {
|
|
66
|
+
cutoffObserved = true;
|
|
67
|
+
throw e;
|
|
68
|
+
});
|
|
69
|
+
let timeoutObserved = false;
|
|
70
|
+
const timeoutPromise = new Promise((_resolve, reject) => setTimeout(reject, timeoutMs)).catch((e) => {
|
|
71
|
+
timeoutObserved = true;
|
|
72
|
+
throw e;
|
|
73
|
+
});
|
|
74
|
+
const startTime = Date.now();
|
|
75
|
+
// Track promises status and resolved values/rejected errors
|
|
76
|
+
// Even if the promises reject with the following decoration promises will not throw
|
|
77
|
+
const promisesStates = [];
|
|
78
|
+
promises.forEach((promise, index) => {
|
|
79
|
+
promisesStates[index] = { status: PromiseStatus.pending, value: null };
|
|
80
|
+
promise
|
|
81
|
+
.then((value) => {
|
|
82
|
+
eventCb(RaceEvent.resolved, Date.now() - startTime, index);
|
|
83
|
+
promisesStates[index] = { status: PromiseStatus.resolved, value };
|
|
84
|
+
})
|
|
85
|
+
.catch((e) => {
|
|
86
|
+
eventCb(RaceEvent.rejected, Date.now() - startTime, index);
|
|
87
|
+
promisesStates[index] = { status: PromiseStatus.rejected, value: e };
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
// Wait till cutoff time unless all original promises resolve/reject early
|
|
91
|
+
await Promise.allSettled(promises.map((promise) => Promise.race([promise, cutoffPromise])));
|
|
92
|
+
if (cutoffObserved) {
|
|
93
|
+
// If any is resolved, then just simply return as we are post cutoff
|
|
94
|
+
const anyResolved = promisesStates.reduce((acc, pmState) => acc || pmState.status === PromiseStatus.resolved, false);
|
|
95
|
+
if (anyResolved) {
|
|
96
|
+
eventCb(RaceEvent.resolvedatcutoff, Date.now() - startTime);
|
|
97
|
+
return mapStatusesToResponses(promisesStates);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
eventCb(RaceEvent.cutoff, Date.now() - startTime);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
eventCb(RaceEvent.precutoff, Date.now() - startTime);
|
|
105
|
+
return mapStatusesToResponses(promisesStates);
|
|
106
|
+
}
|
|
107
|
+
// Post deadline resolve with any of the promise or all rejected before timeout
|
|
108
|
+
await Promise.any(promises.map((promise) => Promise.race([promise, timeoutPromise]))).catch(
|
|
109
|
+
// just ignore if all reject as we will returned mapped rejections
|
|
110
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
111
|
+
(_e) => { });
|
|
112
|
+
if (timeoutObserved) {
|
|
113
|
+
eventCb(RaceEvent.timeout, Date.now() - startTime);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
eventCb(RaceEvent.pretimeout, Date.now() - startTime);
|
|
117
|
+
}
|
|
118
|
+
return mapStatusesToResponses(promisesStates);
|
|
119
|
+
}
|
|
22
120
|
//# sourceMappingURL=promise.js.map
|
package/lib/promise.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise.js","sourceRoot":"","sources":["../src/promise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AAEjC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,CAA0B,EAC1B,EAAc,EACd,QAAgB;IAEhB,IAAI,CAAC,GAA+B,SAAS,CAAC;IAC9C,MAAM,KAAK,GAAG,KAAK,IAAwB,EAAE;QAC3C,OAAO,CAAC,KAAK,SAAS,EAAE;YACtB,MAAM,KAAK,CAAC,QAAQ,CAAC,CAAC;YACtB,IAAI,CAAC,KAA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com/ChainSafe/lodestar/issues"
|
|
13
13
|
},
|
|
14
|
-
"version": "1.6.0-dev.
|
|
14
|
+
"version": "1.6.0-dev.eb4b43da98",
|
|
15
15
|
"type": "module",
|
|
16
16
|
"exports": "./lib/index.js",
|
|
17
17
|
"files": [
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"beacon",
|
|
58
58
|
"blockchain"
|
|
59
59
|
],
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "2dfbd4a2383767744afa5d35f39b922e5c5549a6"
|
|
61
61
|
}
|