@peerbit/time 1.0.4 → 2.0.0
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/esm/index.d.ts +13 -15
- package/lib/esm/index.js +46 -51
- package/lib/esm/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +61 -72
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
export declare class TimeoutError extends Error {
|
|
2
2
|
constructor(message: string);
|
|
3
3
|
}
|
|
4
|
+
export declare class AbortError extends Error {
|
|
5
|
+
constructor();
|
|
6
|
+
}
|
|
4
7
|
export declare const delay: (ms: number, options?: {
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
export declare const waitFor: <T>(fn: () => T
|
|
8
|
-
timeout
|
|
9
|
-
|
|
10
|
-
delayInterval
|
|
11
|
-
timeoutMessage?: string
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}) => Promise<void>;
|
|
10
|
+
export declare const waitFor: <T>(fn: () => T | Promise<T>, options?: {
|
|
11
|
+
timeout?: number;
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
delayInterval?: number;
|
|
14
|
+
timeoutMessage?: string;
|
|
12
15
|
}) => Promise<T | undefined>;
|
|
13
16
|
export declare const waitForResolved: <T>(fn: () => T | Promise<T>, options?: {
|
|
14
|
-
timeout
|
|
15
|
-
|
|
17
|
+
timeout?: number;
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
delayInterval?: number;
|
|
16
20
|
timeoutMessage?: string;
|
|
17
21
|
}) => Promise<T | undefined>;
|
|
18
|
-
export declare const waitForAsync: <T>(fn: () => Promise<T>, options?: {
|
|
19
|
-
timeout: number;
|
|
20
|
-
stopperCallback?: ((stopper: () => void) => void) | undefined;
|
|
21
|
-
delayInterval: number;
|
|
22
|
-
timeoutMessage?: string | undefined;
|
|
23
|
-
}) => Promise<T | undefined>;
|
package/lib/esm/index.js
CHANGED
|
@@ -3,78 +3,73 @@ export class TimeoutError extends Error {
|
|
|
3
3
|
super(message);
|
|
4
4
|
}
|
|
5
5
|
}
|
|
6
|
+
export class AbortError extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
}
|
|
10
|
+
}
|
|
6
11
|
export const delay = (ms, options) => {
|
|
7
|
-
return new Promise((res) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
return new Promise((res, rej) => {
|
|
13
|
+
function handleAbort() {
|
|
14
|
+
clearTimeout(timer);
|
|
15
|
+
rej(new AbortError());
|
|
16
|
+
}
|
|
17
|
+
options?.signal?.addEventListener("abort", handleAbort);
|
|
18
|
+
const timer = setTimeout(() => {
|
|
19
|
+
options?.signal?.removeEventListener("abort", handleAbort);
|
|
20
|
+
res();
|
|
21
|
+
}, ms);
|
|
14
22
|
});
|
|
15
23
|
};
|
|
16
|
-
export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval:
|
|
24
|
+
export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval: 100 }) => {
|
|
25
|
+
const delayInterval = options.delayInterval || 100;
|
|
26
|
+
const timeout = options.timeout || 10 * 1000;
|
|
17
27
|
const startTime = +new Date();
|
|
18
28
|
let stop = false;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (stop) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const result = fn();
|
|
29
|
+
const handleAbort = () => {
|
|
30
|
+
stop = true;
|
|
31
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
32
|
+
};
|
|
33
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
34
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
35
|
+
const result = await fn();
|
|
30
36
|
if (result) {
|
|
37
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
31
38
|
return result;
|
|
32
39
|
}
|
|
33
|
-
await delay(
|
|
40
|
+
await delay(delayInterval, options);
|
|
34
41
|
}
|
|
35
42
|
throw new TimeoutError(options.timeoutMessage
|
|
36
43
|
? "Timed out: " + options.timeoutMessage
|
|
37
44
|
: "Timed out");
|
|
38
45
|
};
|
|
39
46
|
export const waitForResolved = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
|
|
47
|
+
const delayInterval = options.delayInterval || 50;
|
|
48
|
+
const timeout = options.timeout || 10 * 1000;
|
|
40
49
|
const startTime = +new Date();
|
|
41
|
-
|
|
50
|
+
let stop = false;
|
|
42
51
|
let lastError;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
52
|
+
const handleAbort = () => {
|
|
53
|
+
stop = true;
|
|
54
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
55
|
+
};
|
|
56
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
57
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
47
58
|
try {
|
|
48
|
-
|
|
59
|
+
const result = await fn();
|
|
60
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
61
|
+
return result;
|
|
49
62
|
}
|
|
50
63
|
catch (error) {
|
|
51
|
-
|
|
64
|
+
if (error instanceof AbortError === false) {
|
|
65
|
+
lastError = error;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
52
70
|
}
|
|
53
|
-
await delay(options
|
|
71
|
+
await delay(delayInterval, options);
|
|
54
72
|
}
|
|
55
73
|
throw lastError;
|
|
56
74
|
};
|
|
57
|
-
export const waitForAsync = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
|
|
58
|
-
const startTime = +new Date();
|
|
59
|
-
let stop = false;
|
|
60
|
-
if (options.stopperCallback) {
|
|
61
|
-
const stopper = () => {
|
|
62
|
-
stop = true;
|
|
63
|
-
};
|
|
64
|
-
options.stopperCallback(stopper);
|
|
65
|
-
}
|
|
66
|
-
while (+new Date() - startTime < options.timeout) {
|
|
67
|
-
if (stop) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
const result = await fn();
|
|
71
|
-
if (result) {
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
await delay(options.delayInterval, options);
|
|
75
|
-
}
|
|
76
|
-
throw new TimeoutError(options.timeoutMessage
|
|
77
|
-
? "Timed out: " + options.timeoutMessage
|
|
78
|
-
: "Timed out");
|
|
79
|
-
};
|
|
80
75
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;IACtC,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;IAChB,CAAC;CACD;AACD,MAAM,CAAC,MAAM,KAAK,GAAG,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;IACtC,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;IAChB,CAAC;CACD;AAED,MAAM,OAAO,UAAW,SAAQ,KAAK;IACpC;QACC,KAAK,EAAE,CAAC;IACT,CAAC;CACD;AACD,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,OAAkC,EAAE,EAAE;IACvE,OAAO,IAAI,OAAO,CAAO,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACrC,SAAS,WAAW;YACnB,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,GAAG,CAAC,IAAI,UAAU,EAAE,CAAC,CAAC;QACvB,CAAC;QACD,OAAO,EAAE,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QACxD,MAAM,KAAK,GAAG,UA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SAAS,CAAC;AACjB,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/time",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Utility functions for time",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
},
|
|
30
30
|
"author": "dao.xyz",
|
|
31
31
|
"license": "MIT",
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "c48cb37d237a25b0bcc849482b43f6941d53e3d5"
|
|
33
33
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,46 +3,54 @@ export class TimeoutError extends Error {
|
|
|
3
3
|
super(message);
|
|
4
4
|
}
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
6
|
+
|
|
7
|
+
export class AbortError extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export const delay = (ms: number, options?: { signal?: AbortSignal }) => {
|
|
13
|
+
return new Promise<void>((res, rej) => {
|
|
14
|
+
function handleAbort() {
|
|
15
|
+
clearTimeout(timer);
|
|
16
|
+
rej(new AbortError());
|
|
17
|
+
}
|
|
18
|
+
options?.signal?.addEventListener("abort", handleAbort);
|
|
19
|
+
const timer = setTimeout(() => {
|
|
20
|
+
options?.signal?.removeEventListener("abort", handleAbort);
|
|
21
|
+
res();
|
|
22
|
+
}, ms);
|
|
17
23
|
});
|
|
18
24
|
};
|
|
19
25
|
|
|
20
26
|
export const waitFor = async <T>(
|
|
21
|
-
fn: () => T
|
|
27
|
+
fn: () => T | Promise<T>,
|
|
22
28
|
options: {
|
|
23
|
-
timeout
|
|
24
|
-
|
|
25
|
-
delayInterval
|
|
29
|
+
timeout?: number;
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
delayInterval?: number;
|
|
26
32
|
timeoutMessage?: string;
|
|
27
|
-
} = { timeout: 10 * 1000, delayInterval:
|
|
33
|
+
} = { timeout: 10 * 1000, delayInterval: 100 }
|
|
28
34
|
): Promise<T | undefined> => {
|
|
35
|
+
const delayInterval = options.delayInterval || 100;
|
|
36
|
+
const timeout = options.timeout || 10 * 1000;
|
|
29
37
|
const startTime = +new Date();
|
|
30
38
|
let stop = false;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
const result = fn();
|
|
39
|
+
|
|
40
|
+
const handleAbort = () => {
|
|
41
|
+
stop = true;
|
|
42
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
46
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
47
|
+
const result = await fn();
|
|
42
48
|
if (result) {
|
|
49
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
43
50
|
return result;
|
|
44
51
|
}
|
|
45
|
-
|
|
52
|
+
|
|
53
|
+
await delay(delayInterval, options);
|
|
46
54
|
}
|
|
47
55
|
throw new TimeoutError(
|
|
48
56
|
options.timeoutMessage
|
|
@@ -54,58 +62,39 @@ export const waitFor = async <T>(
|
|
|
54
62
|
export const waitForResolved = async <T>(
|
|
55
63
|
fn: () => T | Promise<T>,
|
|
56
64
|
options: {
|
|
57
|
-
timeout
|
|
58
|
-
|
|
65
|
+
timeout?: number;
|
|
66
|
+
signal?: AbortSignal;
|
|
67
|
+
delayInterval?: number;
|
|
59
68
|
timeoutMessage?: string;
|
|
60
69
|
} = { timeout: 10 * 1000, delayInterval: 50 }
|
|
61
70
|
): Promise<T | undefined> => {
|
|
71
|
+
const delayInterval = options.delayInterval || 50;
|
|
72
|
+
const timeout = options.timeout || 10 * 1000;
|
|
73
|
+
|
|
62
74
|
const startTime = +new Date();
|
|
63
|
-
|
|
75
|
+
let stop = false;
|
|
64
76
|
let lastError: Error | undefined;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
77
|
+
|
|
78
|
+
const handleAbort = () => {
|
|
79
|
+
stop = true;
|
|
80
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
84
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
69
85
|
try {
|
|
70
|
-
|
|
86
|
+
const result = await fn();
|
|
87
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
88
|
+
return result;
|
|
71
89
|
} catch (error: any) {
|
|
72
|
-
|
|
90
|
+
if (error instanceof AbortError === false) {
|
|
91
|
+
lastError = error;
|
|
92
|
+
} else {
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
73
95
|
}
|
|
74
|
-
await delay(options
|
|
96
|
+
await delay(delayInterval, options);
|
|
75
97
|
}
|
|
76
|
-
throw lastError;
|
|
77
|
-
};
|
|
78
98
|
|
|
79
|
-
|
|
80
|
-
fn: () => Promise<T>,
|
|
81
|
-
options: {
|
|
82
|
-
timeout: number;
|
|
83
|
-
stopperCallback?: (stopper: () => void) => void;
|
|
84
|
-
delayInterval: number;
|
|
85
|
-
timeoutMessage?: string;
|
|
86
|
-
} = { timeout: 10 * 1000, delayInterval: 50 }
|
|
87
|
-
): Promise<T | undefined> => {
|
|
88
|
-
const startTime = +new Date();
|
|
89
|
-
let stop = false;
|
|
90
|
-
if (options.stopperCallback) {
|
|
91
|
-
const stopper = () => {
|
|
92
|
-
stop = true;
|
|
93
|
-
};
|
|
94
|
-
options.stopperCallback(stopper);
|
|
95
|
-
}
|
|
96
|
-
while (+new Date() - startTime < options.timeout) {
|
|
97
|
-
if (stop) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
const result = await fn();
|
|
101
|
-
if (result) {
|
|
102
|
-
return result;
|
|
103
|
-
}
|
|
104
|
-
await delay(options.delayInterval, options);
|
|
105
|
-
}
|
|
106
|
-
throw new TimeoutError(
|
|
107
|
-
options.timeoutMessage
|
|
108
|
-
? "Timed out: " + options.timeoutMessage
|
|
109
|
-
: "Timed out"
|
|
110
|
-
);
|
|
99
|
+
throw lastError;
|
|
111
100
|
};
|