@peerbit/time 1.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/LICENSE +24 -0
- package/lib/esm/index.d.ts +23 -0
- package/lib/esm/index.js +80 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/package.json +34 -0
- package/src/index.ts +111 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-2018 shamb0t
|
|
4
|
+
Copyright (c) 2018 Haja Networks Oy
|
|
5
|
+
Copyright (c) 2020 dao.xyz
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
24
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare class TimeoutError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
export declare const delay: (ms: number, options?: {
|
|
5
|
+
stopperCallback?: ((stopper: () => void) => void) | undefined;
|
|
6
|
+
} | undefined) => Promise<void>;
|
|
7
|
+
export declare const waitFor: <T>(fn: () => T, options?: {
|
|
8
|
+
timeout: number;
|
|
9
|
+
stopperCallback?: ((stopper: () => void) => void) | undefined;
|
|
10
|
+
delayInterval: number;
|
|
11
|
+
timeoutMessage?: string | undefined;
|
|
12
|
+
}) => Promise<T | undefined>;
|
|
13
|
+
export declare const waitForResolved: <T>(fn: () => T, options?: {
|
|
14
|
+
timeout: number;
|
|
15
|
+
delayInterval: number;
|
|
16
|
+
timeoutMessage?: string;
|
|
17
|
+
}) => 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
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export class TimeoutError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export const delay = (ms, options) => {
|
|
7
|
+
return new Promise((res) => {
|
|
8
|
+
const timer = setTimeout(res, ms);
|
|
9
|
+
if (options?.stopperCallback)
|
|
10
|
+
options?.stopperCallback(() => {
|
|
11
|
+
clearTimeout(timer);
|
|
12
|
+
res();
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
|
|
17
|
+
const startTime = +new Date();
|
|
18
|
+
let stop = false;
|
|
19
|
+
if (options.stopperCallback) {
|
|
20
|
+
const stopper = () => {
|
|
21
|
+
stop = true;
|
|
22
|
+
};
|
|
23
|
+
options.stopperCallback(stopper);
|
|
24
|
+
}
|
|
25
|
+
while (+new Date() - startTime < options.timeout) {
|
|
26
|
+
if (stop) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const result = fn();
|
|
30
|
+
if (result) {
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
await delay(options.delayInterval, options);
|
|
34
|
+
}
|
|
35
|
+
throw new TimeoutError(options.timeoutMessage
|
|
36
|
+
? "Timed out: " + options.timeoutMessage
|
|
37
|
+
: "Timed out");
|
|
38
|
+
};
|
|
39
|
+
export const waitForResolved = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
|
|
40
|
+
const startTime = +new Date();
|
|
41
|
+
const stop = false;
|
|
42
|
+
let lastError;
|
|
43
|
+
while (+new Date() - startTime < options.timeout) {
|
|
44
|
+
if (stop) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
return await fn();
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
lastError = error;
|
|
52
|
+
}
|
|
53
|
+
await delay(options.delayInterval);
|
|
54
|
+
}
|
|
55
|
+
throw lastError;
|
|
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;
|
|
73
|
+
}
|
|
74
|
+
await delay(options.delayInterval, options);
|
|
75
|
+
}
|
|
76
|
+
throw new TimeoutError(options.timeoutMessage
|
|
77
|
+
? "Timed out: " + options.timeoutMessage
|
|
78
|
+
: "Timed out");
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,CACpB,EAAU,EACV,OAA6D,EAC5D,EAAE;IACH,OAAO,IAAI,OAAO,CAAO,CAAC,GAAG,EAAE,EAAE;QAChC,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QAClC,IAAI,OAAO,EAAE,eAAe;YAC3B,OAAO,EAAE,eAAe,CAAC,GAAG,EAAE;gBAC7B,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,GAAG,EAAE,CAAC;YACP,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,EAC3B,EAAW,EACX,UAKI,EAAE,OAAO,EAAE,EAAE,GAAG,IAAI,EAAE,aAAa,EAAE,EAAE,EAAE,EACpB,EAAE;IAC3B,MAAM,SAAS,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC;IAC9B,IAAI,IAAI,GAAG,KAAK,CAAC;IACjB,IAAI,OAAO,CAAC,eAAe,EAAE;QAC5B,MAAM,OAAO,GAAG,GAAG,EAAE;YACpB,IAAI,GAAG,IAAI,CAAC;QACb,CAAC,CAAC;QACF,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;KACjC;IACD,OAAO,CAAC,IAAI,IAAI,EAAE,GAAG,SAAS,GAAG,OA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AAE;QAC5B,MAAM,OAAO,GAAG,GAAG,EAAE;YACpB,IAAI,GAAG,IAAI,CAAC;QACb,CAAC,CAAC;QACF,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;KACjC;IACD,OAAO,CAAC,IAAI,IAAI,EAAE,GAAG,SAAS,GAAG,OAAO,CAAC,OAAO,EAAE;QACjD,IAAI,IAAI,EAAE;YACT,OAAO;SACP;QACD,MAAM,MAAM,GAAG,MAAM,EAAE,EAAE,CAAC;QAC1B,IAAI,MAAM,EAAE;YACX,OAAO,MAAM,CAAC;SACd;QACD,MAAM,KAAK,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;KAC5C;IACD,MAAM,IAAI,YAAY,CACrB,OAAO,CAAC,cAAc;QACrB,CAAC,CAAC,aAAa,GAAG,OAAO,CAAC,cAAc;QACxC,CAAC,CAAC,WAAW,CACd,CAAC;AACH,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peerbit/time",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Utility functions for time",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"module": "lib/esm/index.js",
|
|
8
|
+
"types": "lib/esm/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"import": "./lib/esm/index.js",
|
|
11
|
+
"require": "./lib/cjs/index.js"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"lib",
|
|
15
|
+
"src",
|
|
16
|
+
"!src/**/__tests__",
|
|
17
|
+
"!lib/**/__tests__",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clean": "shx rm -rf lib/*",
|
|
25
|
+
"build": "yarn clean && tsc -p tsconfig.json",
|
|
26
|
+
"postbuild": "echo '{\"type\":\"module\"} ' | node ../../../node_modules/.bin/json > lib/esm/package.json",
|
|
27
|
+
"test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit",
|
|
28
|
+
"test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit",
|
|
29
|
+
"test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit"
|
|
30
|
+
},
|
|
31
|
+
"author": "dao.xyz",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"gitHead": "069ce2f62a76c342875a1cc695c6f210beff13fd"
|
|
34
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export class TimeoutError extends Error {
|
|
2
|
+
constructor(message: string) {
|
|
3
|
+
super(message);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export const delay = (
|
|
7
|
+
ms: number,
|
|
8
|
+
options?: { stopperCallback?: (stopper: () => void) => void }
|
|
9
|
+
) => {
|
|
10
|
+
return new Promise<void>((res) => {
|
|
11
|
+
const timer = setTimeout(res, ms);
|
|
12
|
+
if (options?.stopperCallback)
|
|
13
|
+
options?.stopperCallback(() => {
|
|
14
|
+
clearTimeout(timer);
|
|
15
|
+
res();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const waitFor = async <T>(
|
|
21
|
+
fn: () => T,
|
|
22
|
+
options: {
|
|
23
|
+
timeout: number;
|
|
24
|
+
stopperCallback?: (stopper: () => void) => void;
|
|
25
|
+
delayInterval: number;
|
|
26
|
+
timeoutMessage?: string;
|
|
27
|
+
} = { timeout: 10 * 1000, delayInterval: 50 }
|
|
28
|
+
): Promise<T | undefined> => {
|
|
29
|
+
const startTime = +new Date();
|
|
30
|
+
let stop = false;
|
|
31
|
+
if (options.stopperCallback) {
|
|
32
|
+
const stopper = () => {
|
|
33
|
+
stop = true;
|
|
34
|
+
};
|
|
35
|
+
options.stopperCallback(stopper);
|
|
36
|
+
}
|
|
37
|
+
while (+new Date() - startTime < options.timeout) {
|
|
38
|
+
if (stop) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const result = fn();
|
|
42
|
+
if (result) {
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
await delay(options.delayInterval, options);
|
|
46
|
+
}
|
|
47
|
+
throw new TimeoutError(
|
|
48
|
+
options.timeoutMessage
|
|
49
|
+
? "Timed out: " + options.timeoutMessage
|
|
50
|
+
: "Timed out"
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const waitForResolved = async <T>(
|
|
55
|
+
fn: () => T,
|
|
56
|
+
options: {
|
|
57
|
+
timeout: number;
|
|
58
|
+
delayInterval: number;
|
|
59
|
+
timeoutMessage?: string;
|
|
60
|
+
} = { timeout: 10 * 1000, delayInterval: 50 }
|
|
61
|
+
): Promise<T | undefined> => {
|
|
62
|
+
const startTime = +new Date();
|
|
63
|
+
const stop = false;
|
|
64
|
+
let lastError: Error | undefined;
|
|
65
|
+
while (+new Date() - startTime < options.timeout) {
|
|
66
|
+
if (stop) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
return await fn();
|
|
71
|
+
} catch (error: any) {
|
|
72
|
+
lastError = error;
|
|
73
|
+
}
|
|
74
|
+
await delay(options.delayInterval);
|
|
75
|
+
}
|
|
76
|
+
throw lastError;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const waitForAsync = async <T>(
|
|
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
|
+
);
|
|
111
|
+
};
|