@sha1n/about-time 0.0.7 → 0.0.8
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/README.md +13 -5
- package/dist/lib/utilities.js +26 -18
- package/dist/types/lib/utilities.d.ts +41 -15
- package/lib/utilities.ts +64 -21
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -35,19 +35,25 @@ npm i @sha1n/about-time
|
|
|
35
35
|
## Delay
|
|
36
36
|
```ts
|
|
37
37
|
// Execute a function with delay and return it's value
|
|
38
|
-
await delay(action, 10
|
|
38
|
+
await delay(action, { time: 10 });
|
|
39
|
+
await delay(action, { time: 10, units: TimeUnit.Milliseconds });
|
|
40
|
+
await delay(action, { time: 10, units: TimeUnit.Milliseconds, unref: true });
|
|
39
41
|
```
|
|
40
42
|
|
|
41
43
|
## WithTimeout
|
|
42
44
|
```ts
|
|
43
45
|
// Execute a function and guards it with a specified timeout
|
|
44
|
-
await withTimeout(action, 10
|
|
46
|
+
await withTimeout(action, { time: 10 });
|
|
47
|
+
await withTimeout(action, { time: 10, units: TimeUnit.Milliseconds });
|
|
48
|
+
await withTimeout(action, { time: 10, units: TimeUnit.Milliseconds, unref: true });
|
|
45
49
|
```
|
|
46
50
|
|
|
47
51
|
## Sleep
|
|
48
52
|
```ts
|
|
49
53
|
// Pause execution for a specified amount of time
|
|
50
|
-
await sleep(10
|
|
54
|
+
await sleep(10);
|
|
55
|
+
await sleep(10, { units: TimeUnit.Seconds });
|
|
56
|
+
await sleep(10, { units: TimeUnit.Seconds, unref: true });
|
|
51
57
|
```
|
|
52
58
|
|
|
53
59
|
## Stopwatch
|
|
@@ -67,8 +73,10 @@ const elapsed2 = elapsed(TimeUnit.Seconds);
|
|
|
67
73
|
## Until / Eventually
|
|
68
74
|
```ts
|
|
69
75
|
// Wait for a condition to become true
|
|
70
|
-
await until(condition, {
|
|
71
|
-
await
|
|
76
|
+
await until(condition, { deadline: 10000 });
|
|
77
|
+
await until(condition, { deadline: 10000, interval: 100 });
|
|
78
|
+
await until(condition, { deadline: 10000, interval: 100, units: TimeUnit.Milliseconds });
|
|
79
|
+
await until(condition, { deadline: 10000, interval: 100, units: TimeUnit.Milliseconds, unref: true });
|
|
72
80
|
```
|
|
73
81
|
|
|
74
82
|
## Retry
|
package/dist/lib/utilities.js
CHANGED
|
@@ -10,13 +10,16 @@ class TimeoutError extends Error {
|
|
|
10
10
|
/**
|
|
11
11
|
* Zzzz...
|
|
12
12
|
*
|
|
13
|
-
* @param time time to sleep
|
|
14
|
-
* @param
|
|
13
|
+
* @param time the approximate time to sleep (expect it to be as accurate as setTimeout)
|
|
14
|
+
* @param options timer options minus the time property
|
|
15
15
|
* @returns a promise that resolves when the specified time has elapsed.
|
|
16
16
|
*/
|
|
17
|
-
function sleep(time,
|
|
17
|
+
function sleep(time, options) {
|
|
18
18
|
return new Promise(resolve => {
|
|
19
|
-
setTimeout(resolve, (0, timeunit_1.toMilliseconds)(time, units));
|
|
19
|
+
const timeout = setTimeout(resolve, (0, timeunit_1.toMilliseconds)(time, options === null || options === void 0 ? void 0 : options.units));
|
|
20
|
+
if (options === null || options === void 0 ? void 0 : options.unref) {
|
|
21
|
+
timeout.unref();
|
|
22
|
+
}
|
|
20
23
|
});
|
|
21
24
|
}
|
|
22
25
|
exports.sleep = sleep;
|
|
@@ -24,17 +27,17 @@ exports.sleep = sleep;
|
|
|
24
27
|
* Delays the execution of the specified action and returns its value.
|
|
25
28
|
*
|
|
26
29
|
* @param action a function to execute with delay
|
|
27
|
-
* @param
|
|
28
|
-
* @param units the units on time
|
|
30
|
+
* @param options timer options
|
|
29
31
|
* @returns a promise that resolves when the specified time has elapsed.
|
|
30
32
|
*/
|
|
31
|
-
function delay(action,
|
|
33
|
+
function delay(action, options) {
|
|
34
|
+
const { time, units, unref } = options;
|
|
32
35
|
const delayMs = (0, timeunit_1.toMilliseconds)(time, units);
|
|
33
36
|
return new Promise((resolve, reject) => {
|
|
34
37
|
const timer = setTimeout(() => Promise.resolve(action()).then(resolve, reject), delayMs);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
+
if (unref) {
|
|
39
|
+
timer.unref();
|
|
40
|
+
}
|
|
38
41
|
});
|
|
39
42
|
}
|
|
40
43
|
exports.delay = delay;
|
|
@@ -53,13 +56,13 @@ exports.stopwatch = stopwatch;
|
|
|
53
56
|
* Awaits a specified condition to evaluate to true with or without a timeout.
|
|
54
57
|
*
|
|
55
58
|
* @param condition the condition to wait for
|
|
56
|
-
* @param
|
|
59
|
+
* @param options poll-options
|
|
57
60
|
* @returns a promise that resolves when the condition becomes true, or rejects when a set timeout is crossed.
|
|
58
61
|
*/
|
|
59
|
-
async function until(condition,
|
|
62
|
+
async function until(condition, options) {
|
|
60
63
|
const defaultInterval = 50;
|
|
61
|
-
const deadline =
|
|
62
|
-
const interval = (
|
|
64
|
+
const deadline = (options === null || options === void 0 ? void 0 : options.deadline) ? Date.now() + (0, timeunit_1.toMilliseconds)(options.deadline, options.units) : Number.MAX_VALUE;
|
|
65
|
+
const interval = (options === null || options === void 0 ? void 0 : options.interval) ? (0, timeunit_1.toMilliseconds)(options.interval, options.units) : defaultInterval;
|
|
63
66
|
return new Promise((resolve, reject) => {
|
|
64
67
|
const handle = setInterval(() => {
|
|
65
68
|
if (Date.now() > deadline) {
|
|
@@ -77,6 +80,9 @@ async function until(condition, opts) {
|
|
|
77
80
|
reject(e);
|
|
78
81
|
}
|
|
79
82
|
}, interval);
|
|
83
|
+
if (options === null || options === void 0 ? void 0 : options.unref) {
|
|
84
|
+
handle.unref();
|
|
85
|
+
}
|
|
80
86
|
});
|
|
81
87
|
}
|
|
82
88
|
exports.until = until;
|
|
@@ -89,11 +95,10 @@ exports.eventually = eventually;
|
|
|
89
95
|
* Executes an action with a specified timeout. If the action times out, rejects with TimeoutError.
|
|
90
96
|
*
|
|
91
97
|
* @param action an action to execute with timeout
|
|
92
|
-
* @param
|
|
93
|
-
* @param units the time units
|
|
98
|
+
* @param options timer options
|
|
94
99
|
* @returns the action result
|
|
95
100
|
*/
|
|
96
|
-
async function withTimeout(action,
|
|
101
|
+
async function withTimeout(action, options) {
|
|
97
102
|
const promisedAction = new Promise((resolve, reject) => {
|
|
98
103
|
try {
|
|
99
104
|
resolve(action());
|
|
@@ -105,7 +110,10 @@ async function withTimeout(action, timeout, units) {
|
|
|
105
110
|
const race = new Promise((resolve, reject) => {
|
|
106
111
|
const timer = setTimeout(() => {
|
|
107
112
|
reject(new TimeoutError());
|
|
108
|
-
}, (0, timeunit_1.toMilliseconds)(
|
|
113
|
+
}, (0, timeunit_1.toMilliseconds)(options.time, options.units));
|
|
114
|
+
if (options.unref) {
|
|
115
|
+
timer.unref();
|
|
116
|
+
}
|
|
109
117
|
return Promise.resolve(promisedAction).then(r => {
|
|
110
118
|
clearTimeout(timer);
|
|
111
119
|
resolve(r);
|
|
@@ -1,21 +1,52 @@
|
|
|
1
1
|
import { TimeUnit } from './timeunit';
|
|
2
|
+
declare type TimerOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* The time to set
|
|
5
|
+
*/
|
|
6
|
+
readonly time: number;
|
|
7
|
+
/**
|
|
8
|
+
* Optional units for the specified time
|
|
9
|
+
*/
|
|
10
|
+
readonly units?: TimeUnit;
|
|
11
|
+
/**
|
|
12
|
+
* Whether to call unref on the timer
|
|
13
|
+
*/
|
|
14
|
+
readonly unref?: boolean;
|
|
15
|
+
};
|
|
16
|
+
declare type PollOptions = {
|
|
17
|
+
/**
|
|
18
|
+
* The poll interval to set
|
|
19
|
+
*/
|
|
20
|
+
readonly interval?: number;
|
|
21
|
+
/**
|
|
22
|
+
* The overall deadline to set
|
|
23
|
+
*/
|
|
24
|
+
readonly deadline?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Time units for specified time properties
|
|
27
|
+
*/
|
|
28
|
+
readonly units?: TimeUnit;
|
|
29
|
+
/**
|
|
30
|
+
* Whether to call unref on any intervals/timers
|
|
31
|
+
*/
|
|
32
|
+
readonly unref?: boolean;
|
|
33
|
+
};
|
|
2
34
|
/**
|
|
3
35
|
* Zzzz...
|
|
4
36
|
*
|
|
5
|
-
* @param time time to sleep
|
|
6
|
-
* @param
|
|
37
|
+
* @param time the approximate time to sleep (expect it to be as accurate as setTimeout)
|
|
38
|
+
* @param options timer options minus the time property
|
|
7
39
|
* @returns a promise that resolves when the specified time has elapsed.
|
|
8
40
|
*/
|
|
9
|
-
declare function sleep(time: number,
|
|
41
|
+
declare function sleep(time: number, options?: Omit<TimerOptions, 'time'>): Promise<void>;
|
|
10
42
|
/**
|
|
11
43
|
* Delays the execution of the specified action and returns its value.
|
|
12
44
|
*
|
|
13
45
|
* @param action a function to execute with delay
|
|
14
|
-
* @param
|
|
15
|
-
* @param units the units on time
|
|
46
|
+
* @param options timer options
|
|
16
47
|
* @returns a promise that resolves when the specified time has elapsed.
|
|
17
48
|
*/
|
|
18
|
-
declare function delay<T>(action: () => T | Promise<T>,
|
|
49
|
+
declare function delay<T>(action: () => T | Promise<T>, options: TimerOptions): Promise<T>;
|
|
19
50
|
/**
|
|
20
51
|
* Return a function that returns the elapsed time relative to this call.
|
|
21
52
|
* @returns a function
|
|
@@ -25,14 +56,10 @@ declare function stopwatch(): (units?: TimeUnit) => number;
|
|
|
25
56
|
* Awaits a specified condition to evaluate to true with or without a timeout.
|
|
26
57
|
*
|
|
27
58
|
* @param condition the condition to wait for
|
|
28
|
-
* @param
|
|
59
|
+
* @param options poll-options
|
|
29
60
|
* @returns a promise that resolves when the condition becomes true, or rejects when a set timeout is crossed.
|
|
30
61
|
*/
|
|
31
|
-
declare function until(condition: () => boolean,
|
|
32
|
-
interval?: number;
|
|
33
|
-
timeout: number;
|
|
34
|
-
units?: TimeUnit;
|
|
35
|
-
}): Promise<void>;
|
|
62
|
+
declare function until(condition: () => boolean, options?: PollOptions): Promise<void>;
|
|
36
63
|
/**
|
|
37
64
|
* Alias to `until`
|
|
38
65
|
*/
|
|
@@ -41,9 +68,8 @@ declare const eventually: typeof until;
|
|
|
41
68
|
* Executes an action with a specified timeout. If the action times out, rejects with TimeoutError.
|
|
42
69
|
*
|
|
43
70
|
* @param action an action to execute with timeout
|
|
44
|
-
* @param
|
|
45
|
-
* @param units the time units
|
|
71
|
+
* @param options timer options
|
|
46
72
|
* @returns the action result
|
|
47
73
|
*/
|
|
48
|
-
declare function withTimeout<T>(action: () => T | Promise<T>,
|
|
74
|
+
declare function withTimeout<T>(action: () => T | Promise<T>, options: TimerOptions): Promise<T>;
|
|
49
75
|
export { withTimeout, sleep, delay, stopwatch, until, eventually };
|
package/lib/utilities.ts
CHANGED
|
@@ -6,16 +6,54 @@ class TimeoutError extends Error {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
type TimerOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* The time to set
|
|
12
|
+
*/
|
|
13
|
+
readonly time: number;
|
|
14
|
+
/**
|
|
15
|
+
* Optional units for the specified time
|
|
16
|
+
*/
|
|
17
|
+
readonly units?: TimeUnit;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to call unref on the timer
|
|
20
|
+
*/
|
|
21
|
+
readonly unref?: boolean;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type PollOptions = {
|
|
25
|
+
/**
|
|
26
|
+
* The poll interval to set
|
|
27
|
+
*/
|
|
28
|
+
readonly interval?: number;
|
|
29
|
+
/**
|
|
30
|
+
* The overall deadline to set
|
|
31
|
+
*/
|
|
32
|
+
readonly deadline?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Time units for specified time properties
|
|
35
|
+
*/
|
|
36
|
+
readonly units?: TimeUnit;
|
|
37
|
+
/**
|
|
38
|
+
* Whether to call unref on any intervals/timers
|
|
39
|
+
*/
|
|
40
|
+
readonly unref?: boolean;
|
|
41
|
+
};
|
|
42
|
+
|
|
9
43
|
/**
|
|
10
44
|
* Zzzz...
|
|
11
45
|
*
|
|
12
|
-
* @param time time to sleep
|
|
13
|
-
* @param
|
|
46
|
+
* @param time the approximate time to sleep (expect it to be as accurate as setTimeout)
|
|
47
|
+
* @param options timer options minus the time property
|
|
14
48
|
* @returns a promise that resolves when the specified time has elapsed.
|
|
15
49
|
*/
|
|
16
|
-
function sleep(time: number,
|
|
50
|
+
function sleep(time: number, options?: Omit<TimerOptions, 'time'>): Promise<void> {
|
|
17
51
|
return new Promise(resolve => {
|
|
18
|
-
setTimeout(resolve, toMilliseconds(time, units));
|
|
52
|
+
const timeout = setTimeout(resolve, toMilliseconds(time, options?.units));
|
|
53
|
+
|
|
54
|
+
if (options?.unref) {
|
|
55
|
+
timeout.unref();
|
|
56
|
+
}
|
|
19
57
|
});
|
|
20
58
|
}
|
|
21
59
|
|
|
@@ -23,17 +61,18 @@ function sleep(time: number, units?: TimeUnit): Promise<void> {
|
|
|
23
61
|
* Delays the execution of the specified action and returns its value.
|
|
24
62
|
*
|
|
25
63
|
* @param action a function to execute with delay
|
|
26
|
-
* @param
|
|
27
|
-
* @param units the units on time
|
|
64
|
+
* @param options timer options
|
|
28
65
|
* @returns a promise that resolves when the specified time has elapsed.
|
|
29
66
|
*/
|
|
30
|
-
function delay<T>(action: () => T | Promise<T>,
|
|
67
|
+
function delay<T>(action: () => T | Promise<T>, options: TimerOptions): Promise<T> {
|
|
68
|
+
const { time, units, unref } = options;
|
|
31
69
|
const delayMs = toMilliseconds(time, units);
|
|
32
70
|
return new Promise((resolve, reject) => {
|
|
33
71
|
const timer = setTimeout(() => Promise.resolve(action()).then(resolve, reject), delayMs);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
72
|
+
|
|
73
|
+
if (unref) {
|
|
74
|
+
timer.unref();
|
|
75
|
+
}
|
|
37
76
|
});
|
|
38
77
|
}
|
|
39
78
|
|
|
@@ -53,16 +92,13 @@ function stopwatch(): (units?: TimeUnit) => number {
|
|
|
53
92
|
* Awaits a specified condition to evaluate to true with or without a timeout.
|
|
54
93
|
*
|
|
55
94
|
* @param condition the condition to wait for
|
|
56
|
-
* @param
|
|
95
|
+
* @param options poll-options
|
|
57
96
|
* @returns a promise that resolves when the condition becomes true, or rejects when a set timeout is crossed.
|
|
58
97
|
*/
|
|
59
|
-
async function until(
|
|
60
|
-
condition: () => boolean,
|
|
61
|
-
opts?: { interval?: number; timeout: number; units?: TimeUnit }
|
|
62
|
-
): Promise<void> {
|
|
98
|
+
async function until(condition: () => boolean, options?: PollOptions): Promise<void> {
|
|
63
99
|
const defaultInterval = 50;
|
|
64
|
-
const deadline =
|
|
65
|
-
const interval =
|
|
100
|
+
const deadline = options?.deadline ? Date.now() + toMilliseconds(options.deadline, options.units) : Number.MAX_VALUE;
|
|
101
|
+
const interval = options?.interval ? toMilliseconds(options.interval, options.units) : defaultInterval;
|
|
66
102
|
|
|
67
103
|
return new Promise<void>((resolve, reject) => {
|
|
68
104
|
const handle = setInterval(() => {
|
|
@@ -81,6 +117,10 @@ async function until(
|
|
|
81
117
|
reject(e);
|
|
82
118
|
}
|
|
83
119
|
}, interval);
|
|
120
|
+
|
|
121
|
+
if (options?.unref) {
|
|
122
|
+
handle.unref();
|
|
123
|
+
}
|
|
84
124
|
});
|
|
85
125
|
}
|
|
86
126
|
|
|
@@ -93,11 +133,10 @@ const eventually = until;
|
|
|
93
133
|
* Executes an action with a specified timeout. If the action times out, rejects with TimeoutError.
|
|
94
134
|
*
|
|
95
135
|
* @param action an action to execute with timeout
|
|
96
|
-
* @param
|
|
97
|
-
* @param units the time units
|
|
136
|
+
* @param options timer options
|
|
98
137
|
* @returns the action result
|
|
99
138
|
*/
|
|
100
|
-
async function withTimeout<T>(action: () => T | Promise<T>,
|
|
139
|
+
async function withTimeout<T>(action: () => T | Promise<T>, options: TimerOptions): Promise<T> {
|
|
101
140
|
const promisedAction = new Promise<T>((resolve, reject) => {
|
|
102
141
|
try {
|
|
103
142
|
resolve(action());
|
|
@@ -109,7 +148,11 @@ async function withTimeout<T>(action: () => T | Promise<T>, timeout: number, uni
|
|
|
109
148
|
const race = new Promise<T>((resolve, reject) => {
|
|
110
149
|
const timer = setTimeout(() => {
|
|
111
150
|
reject(new TimeoutError());
|
|
112
|
-
}, toMilliseconds(
|
|
151
|
+
}, toMilliseconds(options.time, options.units));
|
|
152
|
+
|
|
153
|
+
if (options.unref) {
|
|
154
|
+
timer.unref();
|
|
155
|
+
}
|
|
113
156
|
|
|
114
157
|
return Promise.resolve(promisedAction).then(
|
|
115
158
|
r => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sha1n/about-time",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "A set of essential time related utilities",
|
|
5
5
|
"repository": "https://github.com/sha1n/about-time",
|
|
6
6
|
"author": "Shai Nagar",
|
|
@@ -22,38 +22,38 @@
|
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
|
24
24
|
"clean": "rm -rf ./dist",
|
|
25
|
-
"prebuild": "yarn run clean",
|
|
26
25
|
"build": "tsc",
|
|
27
|
-
"test": "DEBUG=error:* jest --coverage",
|
|
26
|
+
"test": "DEBUG='error:*' jest --coverage && run lint",
|
|
28
27
|
"lint": "eslint --fix --ext .js,.ts .",
|
|
29
|
-
"
|
|
30
|
-
"prepublish": "yarn run build"
|
|
28
|
+
"prepublish": "run build"
|
|
31
29
|
},
|
|
32
|
-
"dependencies": {},
|
|
33
30
|
"devDependencies": {
|
|
34
31
|
"@types/chance": "^1.1.3",
|
|
32
|
+
"@types/is-ci": "^3.0.0",
|
|
35
33
|
"@types/jest": "^27.4.0",
|
|
36
34
|
"@types/node": "^17.0.8",
|
|
37
35
|
"@typescript-eslint/eslint-plugin": "^5.1.0",
|
|
38
36
|
"@typescript-eslint/parser": "^5.1.0",
|
|
39
37
|
"chance": "^1.1.8",
|
|
40
|
-
"eslint": "^
|
|
38
|
+
"eslint": "^8.9.0",
|
|
41
39
|
"eslint-config-prettier": "^8.3.0",
|
|
42
40
|
"eslint-plugin-import": "^2.25.2",
|
|
43
|
-
"eslint-plugin-jest": "^
|
|
41
|
+
"eslint-plugin-jest": "^26.0.0",
|
|
44
42
|
"eslint-plugin-no-floating-promise": "^1.0.2",
|
|
45
43
|
"eslint-plugin-node": "^11.1.0",
|
|
46
44
|
"eslint-plugin-prettier": "^4.0.0",
|
|
47
|
-
"eslint-plugin-unused-imports": "^
|
|
48
|
-
"
|
|
45
|
+
"eslint-plugin-unused-imports": "^2.0.0",
|
|
46
|
+
"is-ci": "^3.0.1",
|
|
47
|
+
"jest": "^27.5.1",
|
|
49
48
|
"jest-environment-node": "^27.2.0",
|
|
50
|
-
"jest-extended": "^
|
|
51
|
-
"jest-html-reporters": "^3.0.
|
|
49
|
+
"jest-extended": "^2.0.0",
|
|
50
|
+
"jest-html-reporters": "^3.0.5",
|
|
52
51
|
"jest-mock-extended": "^2.0.4",
|
|
53
52
|
"jest-summary-reporter": "^0.0.2",
|
|
54
53
|
"prettier": "^2.4.1",
|
|
55
54
|
"ts-jest": "^27.1.3",
|
|
56
55
|
"ts-node": "^10.4.0",
|
|
57
56
|
"typescript": "^4.5.4"
|
|
58
|
-
}
|
|
57
|
+
},
|
|
58
|
+
"packageManager": "yarn@3.2.0"
|
|
59
59
|
}
|