@peerbit/time 2.0.6-efee9d3 → 2.0.7-3a75d6e
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/src/hrtime.browser.d.ts.map +1 -1
- package/dist/src/hrtime.browser.js.map +1 -1
- package/dist/src/hrtime.d.ts +0 -1
- package/dist/src/hrtime.d.ts.map +1 -1
- package/dist/src/hrtime.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/metrics.d.ts.map +1 -1
- package/dist/src/metrics.js +2 -2
- package/dist/src/metrics.js.map +1 -1
- package/dist/src/wait.d.ts +0 -2
- package/dist/src/wait.d.ts.map +1 -1
- package/dist/src/wait.js +11 -15
- package/dist/src/wait.js.map +1 -1
- package/package.json +4 -3
- package/src/hrtime.browser.ts +2 -3
- package/src/hrtime.ts +1 -1
- package/src/index.ts +2 -1
- package/src/metrics.ts +4 -3
- package/src/wait.ts +18 -22
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hrtime.browser.d.ts","sourceRoot":"","sources":["../../src/hrtime.browser.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hrtime.browser.d.ts","sourceRoot":"","sources":["../../src/hrtime.browser.ts"],"names":[],"mappings":"AA2BA,QAAA,MAAM,MAAM;yBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;kBAiB/C,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM;CAFhD,CAAC;AAOF,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hrtime.browser.js","sourceRoot":"","sources":["../../src/hrtime.browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;EAyBE;
|
|
1
|
+
{"version":3,"file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rtime.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hrtime.d.ts","sourceRoot":"","sources":["../../src/hrtime.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hrtime.d.ts","sourceRoot":"","sources":["../../src/hrtime.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,MAAM,eAAiB,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
package/dist/src/hrtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hrtime.js","sourceRoot":"","sources":["../../src/hrtime.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"hrtime.js","sourceRoot":"","sources":["../../src/hrtime.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
package/dist/src/index.js
CHANGED
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metrics.d.ts","sourceRoot":"","sources":["../../src/metrics.ts"],"names":[],"mappings":"AAEA,qBAAa,oBAAoB;IAKpB,QAAQ,CAAC,GAAG;IAJxB,OAAO,CAAC,MAAM,CAAS;IAEvB,KAAK,SAAK;gBAEW,GAAG,SAAK;
|
|
1
|
+
{"version":3,"file":"metrics.d.ts","sourceRoot":"","sources":["../../src/metrics.ts"],"names":[],"mappings":"AAEA,qBAAa,oBAAoB;IAKpB,QAAQ,CAAC,GAAG;IAJxB,OAAO,CAAC,MAAM,CAAS;IAEvB,KAAK,SAAK;gBAEW,GAAG,SAAK;IAI7B,GAAG,CAAC,MAAM,EAAE,MAAM;CAWlB"}
|
package/dist/src/metrics.js
CHANGED
|
@@ -15,8 +15,8 @@ export class MovingAverageTracker {
|
|
|
15
15
|
}
|
|
16
16
|
const dt = diff / 1e9;
|
|
17
17
|
this.lastTS = now;
|
|
18
|
-
const
|
|
19
|
-
this.value = (1 -
|
|
18
|
+
const alphaT = 1 - Math.exp(-dt / this.tau);
|
|
19
|
+
this.value = (1 - alphaT) * this.value + (alphaT * number) / dt;
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
//# sourceMappingURL=metrics.js.map
|
package/dist/src/metrics.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../src/metrics.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../src/metrics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,OAAO,oBAAoB;IAKX;IAJb,MAAM,CAAS;IAEvB,KAAK,GAAG,CAAC,CAAC;IAEV,YAAqB,MAAM,EAAE;QAAR,QAAG,GAAH,GAAG,CAAK;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;IAC/B,CAAC;IAED,GAAG,CAAC,MAAc;QACjB,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QAC5B,IAAI,IAAI,GAAG,MAAM,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;QACrC,IAAI,IAAI,IAAI,CAAC,EAAE,CAAC;YACf,IAAI,GAAG,CAAC,CAAC,CAAC,oCAAoC;QAC/C,CAAC;QACD,MAAM,EAAE,GAAG,IAAI,GAAG,GAAG,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;QAClB,MAAM,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QAC5C,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,EAAE,CAAC;IACjE,CAAC;CACD"}
|
package/dist/src/wait.d.ts
CHANGED
package/dist/src/wait.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wait.d.ts","sourceRoot":"","sources":["../../src/wait.ts"],"names":[],"mappings":"AAAA,qBAAa,YAAa,SAAQ,KAAK;
|
|
1
|
+
{"version":3,"file":"wait.d.ts","sourceRoot":"","sources":["../../src/wait.ts"],"names":[],"mappings":"AAAA,qBAAa,YAAa,SAAQ,KAAK;CAAG;AAE1C,qBAAa,UAAW,SAAQ,KAAK;CAAG;AACxC,eAAO,MAAM,KAAK,OAAc,MAAM,YAAY;IAAE,MAAM,CAAC,EAAE,WAAW,CAAA;CAAE,kBAYzE,CAAC;AASF,eAAO,MAAM,OAAO,GAAU,CAAC,MAC1B,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,YACf;IACR,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB,KACC,OAAO,CAAC,CAAC,GAAG,SAAS,CAwBvB,CAAC;AAEF,eAAO,MAAM,eAAe,GAAU,CAAC,MAClC,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,YACf;IACR,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB,KACC,OAAO,CAAC,CAAC,CAgCX,CAAC"}
|
package/dist/src/wait.js
CHANGED
|
@@ -1,23 +1,17 @@
|
|
|
1
1
|
export class TimeoutError extends Error {
|
|
2
|
-
constructor(message) {
|
|
3
|
-
super(message);
|
|
4
|
-
}
|
|
5
2
|
}
|
|
6
3
|
export class AbortError extends Error {
|
|
7
|
-
constructor(message) {
|
|
8
|
-
super(message);
|
|
9
|
-
}
|
|
10
4
|
}
|
|
11
|
-
export const delay = (ms, options) => {
|
|
12
|
-
return new Promise((
|
|
5
|
+
export const delay = async (ms, options) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
13
7
|
function handleAbort() {
|
|
14
8
|
clearTimeout(timer);
|
|
15
|
-
|
|
9
|
+
reject(new AbortError());
|
|
16
10
|
}
|
|
17
11
|
options?.signal?.addEventListener("abort", handleAbort);
|
|
18
12
|
const timer = setTimeout(() => {
|
|
19
13
|
options?.signal?.removeEventListener("abort", handleAbort);
|
|
20
|
-
|
|
14
|
+
resolve();
|
|
21
15
|
}, ms);
|
|
22
16
|
});
|
|
23
17
|
};
|
|
@@ -27,14 +21,15 @@ const createTimeoutError = (options) => new TimeoutError(options?.timeoutMessage
|
|
|
27
21
|
export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval: 100 }) => {
|
|
28
22
|
const delayInterval = options.delayInterval || 100;
|
|
29
23
|
const timeout = options.timeout || 10 * 1000;
|
|
30
|
-
const startTime =
|
|
24
|
+
const startTime = Number(new Date());
|
|
31
25
|
let stop = false;
|
|
32
26
|
const handleAbort = () => {
|
|
33
27
|
stop = true;
|
|
34
28
|
options.signal?.removeEventListener("abort", handleAbort);
|
|
35
29
|
};
|
|
36
30
|
options.signal?.addEventListener("abort", handleAbort);
|
|
37
|
-
|
|
31
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
32
|
+
while (!stop && Number(new Date()) - startTime < timeout) {
|
|
38
33
|
const result = await fn();
|
|
39
34
|
if (result) {
|
|
40
35
|
options.signal?.removeEventListener("abort", handleAbort);
|
|
@@ -47,7 +42,7 @@ export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval:
|
|
|
47
42
|
export const waitForResolved = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
|
|
48
43
|
const delayInterval = options.delayInterval || 50;
|
|
49
44
|
const timeout = options.timeout || 10 * 1000;
|
|
50
|
-
const startTime =
|
|
45
|
+
const startTime = Number(new Date());
|
|
51
46
|
let stop = false;
|
|
52
47
|
let lastError;
|
|
53
48
|
const handleAbort = () => {
|
|
@@ -55,14 +50,15 @@ export const waitForResolved = async (fn, options = { timeout: 10 * 1000, delayI
|
|
|
55
50
|
options.signal?.removeEventListener("abort", handleAbort);
|
|
56
51
|
};
|
|
57
52
|
options.signal?.addEventListener("abort", handleAbort);
|
|
58
|
-
|
|
53
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
54
|
+
while (!stop && Number(new Date()) - startTime < timeout) {
|
|
59
55
|
try {
|
|
60
56
|
const result = await fn();
|
|
61
57
|
options.signal?.removeEventListener("abort", handleAbort);
|
|
62
58
|
return result;
|
|
63
59
|
}
|
|
64
60
|
catch (error) {
|
|
65
|
-
if (error instanceof AbortError
|
|
61
|
+
if (!(error instanceof AbortError)) {
|
|
66
62
|
lastError = error;
|
|
67
63
|
}
|
|
68
64
|
else {
|
package/dist/src/wait.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wait.js","sourceRoot":"","sources":["../../src/wait.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;
|
|
1
|
+
{"version":3,"file":"wait.js","sourceRoot":"","sources":["../../src/wait.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;CAAG;AAE1C,MAAM,OAAO,UAAW,SAAQ,KAAK;CAAG;AACxC,MAAM,CAAC,MAAM,KAAK,GAAG,KAAK,EAAE,EAAU,EAAE,OAAkC,EAAE,EAAE;IAC7E,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC5C,SAAS,WAAW;YACnB,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,MAAM,CAAC,IAAI,UAAU,EAAE,CAAC,CAAC;QAC1B,CAAC;QACD,OAAO,EAAE,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QACxD,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;YAC7B,OAAO,EAAE,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;YAC3D,OAAO,EAAE,CAAC;QACX,CAAC,EAAE,EAAE,CAAC,CAAC;IACR,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,kBAAkB,GAAG,CAAC,OAAoC,EAAE,EAAE,CACnE,IAAI,YAAY,CACf,OAAO,EAAE,cAAc;IACtB,CAAC,CAAC,aA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erbit/time",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7-3a75d6e",
|
|
4
4
|
"description": "Utility functions for time",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
}
|
|
35
35
|
},
|
|
36
36
|
"eslintConfig": {
|
|
37
|
-
"extends": "
|
|
37
|
+
"extends": "peerbit",
|
|
38
38
|
"parserOptions": {
|
|
39
39
|
"project": true,
|
|
40
40
|
"sourceType": "module"
|
|
@@ -54,7 +54,8 @@
|
|
|
54
54
|
"scripts": {
|
|
55
55
|
"clean": "aegir clean",
|
|
56
56
|
"build": "aegir build --no-bundle",
|
|
57
|
-
"test": "aegir test"
|
|
57
|
+
"test": "aegir test",
|
|
58
|
+
"lint": "aegir lint"
|
|
58
59
|
},
|
|
59
60
|
"author": "dao.xyz",
|
|
60
61
|
"license": "MIT"
|
package/src/hrtime.browser.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*
|
|
1
|
+
/*
|
|
2
2
|
|
|
3
3
|
MIT License
|
|
4
4
|
|
|
@@ -25,7 +25,6 @@ SOFTWARE.
|
|
|
25
25
|
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
-
|
|
29
28
|
const hrtime = (previousTimestamp?: [number, number]): [number, number] => {
|
|
30
29
|
const baseNow = Math.floor((Date.now() - performance.now()) * 1e-3);
|
|
31
30
|
const clocktime = performance.now() * 1e-3;
|
|
@@ -48,4 +47,4 @@ hrtime.bigint = (time?: [number, number]): bigint => {
|
|
|
48
47
|
return BigInt(diff[0] * NS_PER_SEC + diff[1]);
|
|
49
48
|
};
|
|
50
49
|
|
|
51
|
-
export { hrtime }
|
|
50
|
+
export { hrtime };
|
package/src/hrtime.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
const hrtime = process.hrtime;
|
|
2
|
-
export { hrtime }
|
|
2
|
+
export { hrtime };
|
package/src/index.ts
CHANGED
package/src/metrics.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {hrtime} from "./hrtime.js";
|
|
1
|
+
import { hrtime } from "./hrtime.js";
|
|
2
2
|
|
|
3
3
|
export class MovingAverageTracker {
|
|
4
4
|
private lastTS: bigint;
|
|
@@ -8,6 +8,7 @@ export class MovingAverageTracker {
|
|
|
8
8
|
constructor(readonly tau = 10) {
|
|
9
9
|
this.lastTS = hrtime.bigint();
|
|
10
10
|
}
|
|
11
|
+
|
|
11
12
|
add(number: number) {
|
|
12
13
|
const now = hrtime.bigint();
|
|
13
14
|
let diff = Number(now - this.lastTS);
|
|
@@ -16,7 +17,7 @@ export class MovingAverageTracker {
|
|
|
16
17
|
}
|
|
17
18
|
const dt = diff / 1e9;
|
|
18
19
|
this.lastTS = now;
|
|
19
|
-
const
|
|
20
|
-
this.value = (1 -
|
|
20
|
+
const alphaT = 1 - Math.exp(-dt / this.tau);
|
|
21
|
+
this.value = (1 - alphaT) * this.value + (alphaT * number) / dt;
|
|
21
22
|
}
|
|
22
23
|
}
|
package/src/wait.ts
CHANGED
|
@@ -1,24 +1,16 @@
|
|
|
1
|
-
export class TimeoutError extends Error {
|
|
2
|
-
constructor(message?: string) {
|
|
3
|
-
super(message);
|
|
4
|
-
}
|
|
5
|
-
}
|
|
1
|
+
export class TimeoutError extends Error {}
|
|
6
2
|
|
|
7
|
-
export class AbortError extends Error {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
export const delay = (ms: number, options?: { signal?: AbortSignal }) => {
|
|
13
|
-
return new Promise<void>((res, rej) => {
|
|
3
|
+
export class AbortError extends Error {}
|
|
4
|
+
export const delay = async (ms: number, options?: { signal?: AbortSignal }) => {
|
|
5
|
+
return new Promise<void>((resolve, reject) => {
|
|
14
6
|
function handleAbort() {
|
|
15
7
|
clearTimeout(timer);
|
|
16
|
-
|
|
8
|
+
reject(new AbortError());
|
|
17
9
|
}
|
|
18
10
|
options?.signal?.addEventListener("abort", handleAbort);
|
|
19
11
|
const timer = setTimeout(() => {
|
|
20
12
|
options?.signal?.removeEventListener("abort", handleAbort);
|
|
21
|
-
|
|
13
|
+
resolve();
|
|
22
14
|
}, ms);
|
|
23
15
|
});
|
|
24
16
|
};
|
|
@@ -27,7 +19,7 @@ const createTimeoutError = (options: { timeoutMessage?: string }) =>
|
|
|
27
19
|
new TimeoutError(
|
|
28
20
|
options?.timeoutMessage
|
|
29
21
|
? "Timed out: " + options?.timeoutMessage
|
|
30
|
-
: "Timed out"
|
|
22
|
+
: "Timed out",
|
|
31
23
|
);
|
|
32
24
|
|
|
33
25
|
export const waitFor = async <T>(
|
|
@@ -37,11 +29,11 @@ export const waitFor = async <T>(
|
|
|
37
29
|
signal?: AbortSignal;
|
|
38
30
|
delayInterval?: number;
|
|
39
31
|
timeoutMessage?: string;
|
|
40
|
-
} = { timeout: 10 * 1000, delayInterval: 100 }
|
|
32
|
+
} = { timeout: 10 * 1000, delayInterval: 100 },
|
|
41
33
|
): Promise<T | undefined> => {
|
|
42
34
|
const delayInterval = options.delayInterval || 100;
|
|
43
35
|
const timeout = options.timeout || 10 * 1000;
|
|
44
|
-
const startTime =
|
|
36
|
+
const startTime = Number(new Date());
|
|
45
37
|
let stop = false;
|
|
46
38
|
|
|
47
39
|
const handleAbort = () => {
|
|
@@ -50,7 +42,9 @@ export const waitFor = async <T>(
|
|
|
50
42
|
};
|
|
51
43
|
|
|
52
44
|
options.signal?.addEventListener("abort", handleAbort);
|
|
53
|
-
|
|
45
|
+
|
|
46
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
47
|
+
while (!stop && Number(new Date()) - startTime < timeout) {
|
|
54
48
|
const result = await fn();
|
|
55
49
|
if (result) {
|
|
56
50
|
options.signal?.removeEventListener("abort", handleAbort);
|
|
@@ -69,12 +63,12 @@ export const waitForResolved = async <T>(
|
|
|
69
63
|
signal?: AbortSignal;
|
|
70
64
|
delayInterval?: number;
|
|
71
65
|
timeoutMessage?: string;
|
|
72
|
-
} = { timeout: 10 * 1000, delayInterval: 50 }
|
|
66
|
+
} = { timeout: 10 * 1000, delayInterval: 50 },
|
|
73
67
|
): Promise<T> => {
|
|
74
68
|
const delayInterval = options.delayInterval || 50;
|
|
75
69
|
const timeout = options.timeout || 10 * 1000;
|
|
76
70
|
|
|
77
|
-
const startTime =
|
|
71
|
+
const startTime = Number(new Date());
|
|
78
72
|
let stop = false;
|
|
79
73
|
let lastError: Error | undefined;
|
|
80
74
|
|
|
@@ -84,13 +78,15 @@ export const waitForResolved = async <T>(
|
|
|
84
78
|
};
|
|
85
79
|
|
|
86
80
|
options.signal?.addEventListener("abort", handleAbort);
|
|
87
|
-
|
|
81
|
+
|
|
82
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
83
|
+
while (!stop && Number(new Date()) - startTime < timeout) {
|
|
88
84
|
try {
|
|
89
85
|
const result = await fn();
|
|
90
86
|
options.signal?.removeEventListener("abort", handleAbort);
|
|
91
87
|
return result;
|
|
92
88
|
} catch (error: any) {
|
|
93
|
-
if (error instanceof AbortError
|
|
89
|
+
if (!(error instanceof AbortError)) {
|
|
94
90
|
lastError = error;
|
|
95
91
|
} else {
|
|
96
92
|
throw error;
|