@peerbit/time 1.0.4 → 2.0.1
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 +14 -16
- package/lib/esm/index.js +51 -55
- package/lib/esm/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +70 -78
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
|
-
}) => Promise<T
|
|
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>;
|
|
21
|
+
}) => Promise<T>;
|
package/lib/esm/index.js
CHANGED
|
@@ -3,78 +3,74 @@ 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
|
-
|
|
24
|
+
const createTimeoutError = (options) => new TimeoutError(options?.timeoutMessage
|
|
25
|
+
? "Timed out: " + options?.timeoutMessage
|
|
26
|
+
: "Timed out");
|
|
27
|
+
export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval: 100 }) => {
|
|
28
|
+
const delayInterval = options.delayInterval || 100;
|
|
29
|
+
const timeout = options.timeout || 10 * 1000;
|
|
17
30
|
const startTime = +new Date();
|
|
18
31
|
let stop = false;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (stop) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const result = fn();
|
|
32
|
+
const handleAbort = () => {
|
|
33
|
+
stop = true;
|
|
34
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
35
|
+
};
|
|
36
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
37
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
38
|
+
const result = await fn();
|
|
30
39
|
if (result) {
|
|
40
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
31
41
|
return result;
|
|
32
42
|
}
|
|
33
|
-
await delay(
|
|
43
|
+
await delay(delayInterval, options);
|
|
34
44
|
}
|
|
35
|
-
throw
|
|
36
|
-
? "Timed out: " + options.timeoutMessage
|
|
37
|
-
: "Timed out");
|
|
45
|
+
throw createTimeoutError(options);
|
|
38
46
|
};
|
|
39
47
|
export const waitForResolved = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
|
|
48
|
+
const delayInterval = options.delayInterval || 50;
|
|
49
|
+
const timeout = options.timeout || 10 * 1000;
|
|
40
50
|
const startTime = +new Date();
|
|
41
|
-
|
|
51
|
+
let stop = false;
|
|
42
52
|
let lastError;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
const handleAbort = () => {
|
|
54
|
+
stop = true;
|
|
55
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
56
|
+
};
|
|
57
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
58
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
47
59
|
try {
|
|
48
|
-
|
|
60
|
+
const result = await fn();
|
|
61
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
62
|
+
return result;
|
|
49
63
|
}
|
|
50
64
|
catch (error) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
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;
|
|
65
|
+
if (error instanceof AbortError === false) {
|
|
66
|
+
lastError = error;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
73
71
|
}
|
|
74
|
-
await delay(
|
|
72
|
+
await delay(delayInterval, options);
|
|
75
73
|
}
|
|
76
|
-
throw
|
|
77
|
-
? "Timed out: " + options.timeoutMessage
|
|
78
|
-
: "Timed out");
|
|
74
|
+
throw lastError || createTimeoutError(options);
|
|
79
75
|
};
|
|
80
76
|
//# 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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,KAAK,CAAC;YACnB,CAAC;iBAAM,CAAC;gBACP,MAAM,KAAK,CAAC;YACb,CAAC;QACF,CAAC;QACD,MAAM,KAAK,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;IACrC,CAAC;IAED,MAAM,SAAS,IAAI,kBAAkB,CAAC,OAAO,CAAC,CAAC;AAChD,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/time",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
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": "f4cb6a526d26e98360c32fd28dcdf9eb7ea10551"
|
|
33
33
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,109 +3,101 @@ 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
|
|
|
26
|
+
const createTimeoutError = (options: { timeoutMessage?: string }) =>
|
|
27
|
+
new TimeoutError(
|
|
28
|
+
options?.timeoutMessage
|
|
29
|
+
? "Timed out: " + options?.timeoutMessage
|
|
30
|
+
: "Timed out"
|
|
31
|
+
);
|
|
32
|
+
|
|
20
33
|
export const waitFor = async <T>(
|
|
21
|
-
fn: () => T
|
|
34
|
+
fn: () => T | Promise<T>,
|
|
22
35
|
options: {
|
|
23
|
-
timeout
|
|
24
|
-
|
|
25
|
-
delayInterval
|
|
36
|
+
timeout?: number;
|
|
37
|
+
signal?: AbortSignal;
|
|
38
|
+
delayInterval?: number;
|
|
26
39
|
timeoutMessage?: string;
|
|
27
|
-
} = { timeout: 10 * 1000, delayInterval:
|
|
40
|
+
} = { timeout: 10 * 1000, delayInterval: 100 }
|
|
28
41
|
): Promise<T | undefined> => {
|
|
42
|
+
const delayInterval = options.delayInterval || 100;
|
|
43
|
+
const timeout = options.timeout || 10 * 1000;
|
|
29
44
|
const startTime = +new Date();
|
|
30
45
|
let stop = false;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
const result = fn();
|
|
46
|
+
|
|
47
|
+
const handleAbort = () => {
|
|
48
|
+
stop = true;
|
|
49
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
53
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
54
|
+
const result = await fn();
|
|
42
55
|
if (result) {
|
|
56
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
43
57
|
return result;
|
|
44
58
|
}
|
|
45
|
-
|
|
59
|
+
|
|
60
|
+
await delay(delayInterval, options);
|
|
46
61
|
}
|
|
47
|
-
throw
|
|
48
|
-
options.timeoutMessage
|
|
49
|
-
? "Timed out: " + options.timeoutMessage
|
|
50
|
-
: "Timed out"
|
|
51
|
-
);
|
|
62
|
+
throw createTimeoutError(options);
|
|
52
63
|
};
|
|
53
64
|
|
|
54
65
|
export const waitForResolved = async <T>(
|
|
55
66
|
fn: () => T | Promise<T>,
|
|
56
67
|
options: {
|
|
57
|
-
timeout
|
|
58
|
-
|
|
68
|
+
timeout?: number;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
delayInterval?: number;
|
|
59
71
|
timeoutMessage?: string;
|
|
60
72
|
} = { timeout: 10 * 1000, delayInterval: 50 }
|
|
61
|
-
): Promise<T
|
|
73
|
+
): Promise<T> => {
|
|
74
|
+
const delayInterval = options.delayInterval || 50;
|
|
75
|
+
const timeout = options.timeout || 10 * 1000;
|
|
76
|
+
|
|
62
77
|
const startTime = +new Date();
|
|
63
|
-
|
|
78
|
+
let stop = false;
|
|
64
79
|
let lastError: Error | undefined;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
80
|
+
|
|
81
|
+
const handleAbort = () => {
|
|
82
|
+
stop = true;
|
|
83
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
87
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
69
88
|
try {
|
|
70
|
-
|
|
89
|
+
const result = await fn();
|
|
90
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
91
|
+
return result;
|
|
71
92
|
} catch (error: any) {
|
|
72
|
-
|
|
93
|
+
if (error instanceof AbortError === false) {
|
|
94
|
+
lastError = error;
|
|
95
|
+
} else {
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
73
98
|
}
|
|
74
|
-
await delay(options
|
|
99
|
+
await delay(delayInterval, options);
|
|
75
100
|
}
|
|
76
|
-
throw lastError;
|
|
77
|
-
};
|
|
78
101
|
|
|
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
|
-
);
|
|
102
|
+
throw lastError || createTimeoutError(options);
|
|
111
103
|
};
|