@sha1n/about-time 0.0.5 → 0.0.6
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 +7 -0
- package/dist/lib/utilities.js +43 -3
- package/dist/types/lib/utilities.d.ts +10 -1
- package/lib/utilities.ts +49 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@ A collection of essential time related utilities.
|
|
|
12
12
|
- [About-Time](#about-time)
|
|
13
13
|
- [Install](#install)
|
|
14
14
|
- [Delay](#delay)
|
|
15
|
+
- [WithTimeout](#withtimeout)
|
|
15
16
|
- [Sleep](#sleep)
|
|
16
17
|
- [Stopwatch](#stopwatch)
|
|
17
18
|
- [Until / Eventually](#until--eventually)
|
|
@@ -35,6 +36,12 @@ npm i @sha1n/about-time
|
|
|
35
36
|
await delay(action, 10, TimeUnit.Milliseconds);
|
|
36
37
|
```
|
|
37
38
|
|
|
39
|
+
## WithTimeout
|
|
40
|
+
```ts
|
|
41
|
+
// Execute a function and guards it with a specified timeout
|
|
42
|
+
await withTimeout(action, 10, TimeUnit.Milliseconds);
|
|
43
|
+
```
|
|
44
|
+
|
|
38
45
|
## Sleep
|
|
39
46
|
```ts
|
|
40
47
|
// Pause execution for a specified amount of time
|
package/dist/lib/utilities.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.eventually = exports.until = exports.stopwatch = exports.delay = exports.sleep = void 0;
|
|
3
|
+
exports.eventually = exports.until = exports.stopwatch = exports.delay = exports.sleep = exports.withTimeout = void 0;
|
|
4
4
|
const timeunit_1 = require("./timeunit");
|
|
5
|
+
class TimeoutError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message || 'Timeout');
|
|
8
|
+
}
|
|
9
|
+
}
|
|
5
10
|
/**
|
|
6
11
|
* Zzzz...
|
|
7
12
|
*
|
|
@@ -26,7 +31,10 @@ exports.sleep = sleep;
|
|
|
26
31
|
function delay(action, time, units) {
|
|
27
32
|
const delayMs = (0, timeunit_1.toMilliseconds)(time, units);
|
|
28
33
|
return new Promise((resolve, reject) => {
|
|
29
|
-
setTimeout(() => Promise.resolve(action()).then(resolve, reject), delayMs);
|
|
34
|
+
const timer = setTimeout(() => Promise.resolve(action()).then(resolve, reject), delayMs);
|
|
35
|
+
process.on('beforeExit', () => {
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
});
|
|
30
38
|
});
|
|
31
39
|
}
|
|
32
40
|
exports.delay = delay;
|
|
@@ -56,7 +64,7 @@ async function until(condition, opts) {
|
|
|
56
64
|
const handle = setInterval(() => {
|
|
57
65
|
if (Date.now() > deadline) {
|
|
58
66
|
clearInterval(handle);
|
|
59
|
-
reject(new
|
|
67
|
+
reject(new TimeoutError());
|
|
60
68
|
}
|
|
61
69
|
try {
|
|
62
70
|
if (condition()) {
|
|
@@ -77,3 +85,35 @@ exports.until = until;
|
|
|
77
85
|
*/
|
|
78
86
|
const eventually = until;
|
|
79
87
|
exports.eventually = eventually;
|
|
88
|
+
/**
|
|
89
|
+
* Executes an action with a specified timeout. If the action times out, rejects with TimeoutError.
|
|
90
|
+
*
|
|
91
|
+
* @param action an action to execute with timeout
|
|
92
|
+
* @param timeout the timeout to set for the action
|
|
93
|
+
* @param units the time units
|
|
94
|
+
* @returns the action result
|
|
95
|
+
*/
|
|
96
|
+
async function withTimeout(action, timeout, units) {
|
|
97
|
+
const promisedAction = new Promise((resolve, reject) => {
|
|
98
|
+
try {
|
|
99
|
+
resolve(action());
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
reject(e);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
const race = new Promise((resolve, reject) => {
|
|
106
|
+
const timer = setTimeout(() => {
|
|
107
|
+
reject(new TimeoutError());
|
|
108
|
+
}, (0, timeunit_1.toMilliseconds)(timeout, units));
|
|
109
|
+
return Promise.resolve(promisedAction).then(r => {
|
|
110
|
+
clearTimeout(timer);
|
|
111
|
+
resolve(r);
|
|
112
|
+
}, err => {
|
|
113
|
+
clearTimeout(timer);
|
|
114
|
+
reject(err);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
return race;
|
|
118
|
+
}
|
|
119
|
+
exports.withTimeout = withTimeout;
|
|
@@ -37,4 +37,13 @@ declare function until(condition: () => boolean, opts?: {
|
|
|
37
37
|
* Alias to `until`
|
|
38
38
|
*/
|
|
39
39
|
declare const eventually: typeof until;
|
|
40
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Executes an action with a specified timeout. If the action times out, rejects with TimeoutError.
|
|
42
|
+
*
|
|
43
|
+
* @param action an action to execute with timeout
|
|
44
|
+
* @param timeout the timeout to set for the action
|
|
45
|
+
* @param units the time units
|
|
46
|
+
* @returns the action result
|
|
47
|
+
*/
|
|
48
|
+
declare function withTimeout<T>(action: () => T | Promise<T>, timeout: number, units?: TimeUnit): Promise<T>;
|
|
49
|
+
export { withTimeout, sleep, delay, stopwatch, until, eventually };
|
package/lib/utilities.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { TimeUnit, toMilliseconds } from './timeunit';
|
|
2
2
|
|
|
3
|
+
class TimeoutError extends Error {
|
|
4
|
+
constructor(message?: string) {
|
|
5
|
+
super(message || 'Timeout');
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
3
9
|
/**
|
|
4
10
|
* Zzzz...
|
|
5
11
|
*
|
|
@@ -24,7 +30,10 @@ function sleep(time: number, units?: TimeUnit): Promise<void> {
|
|
|
24
30
|
function delay<T>(action: () => T | Promise<T>, time: number, units?: TimeUnit): Promise<T> {
|
|
25
31
|
const delayMs = toMilliseconds(time, units);
|
|
26
32
|
return new Promise((resolve, reject) => {
|
|
27
|
-
setTimeout(() => Promise.resolve(action()).then(resolve, reject), delayMs);
|
|
33
|
+
const timer = setTimeout(() => Promise.resolve(action()).then(resolve, reject), delayMs);
|
|
34
|
+
process.on('beforeExit', () => {
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
});
|
|
28
37
|
});
|
|
29
38
|
}
|
|
30
39
|
|
|
@@ -59,7 +68,7 @@ async function until(
|
|
|
59
68
|
const handle = setInterval(() => {
|
|
60
69
|
if (Date.now() > deadline) {
|
|
61
70
|
clearInterval(handle);
|
|
62
|
-
reject(new
|
|
71
|
+
reject(new TimeoutError());
|
|
63
72
|
}
|
|
64
73
|
|
|
65
74
|
try {
|
|
@@ -80,4 +89,41 @@ async function until(
|
|
|
80
89
|
*/
|
|
81
90
|
const eventually = until;
|
|
82
91
|
|
|
83
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Executes an action with a specified timeout. If the action times out, rejects with TimeoutError.
|
|
94
|
+
*
|
|
95
|
+
* @param action an action to execute with timeout
|
|
96
|
+
* @param timeout the timeout to set for the action
|
|
97
|
+
* @param units the time units
|
|
98
|
+
* @returns the action result
|
|
99
|
+
*/
|
|
100
|
+
async function withTimeout<T>(action: () => T | Promise<T>, timeout: number, units?: TimeUnit): Promise<T> {
|
|
101
|
+
const promisedAction = new Promise<T>((resolve, reject) => {
|
|
102
|
+
try {
|
|
103
|
+
resolve(action());
|
|
104
|
+
} catch (e) {
|
|
105
|
+
reject(e);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const race = new Promise<T>((resolve, reject) => {
|
|
110
|
+
const timer = setTimeout(() => {
|
|
111
|
+
reject(new TimeoutError());
|
|
112
|
+
}, toMilliseconds(timeout, units));
|
|
113
|
+
|
|
114
|
+
return Promise.resolve(promisedAction).then(
|
|
115
|
+
r => {
|
|
116
|
+
clearTimeout(timer);
|
|
117
|
+
resolve(r);
|
|
118
|
+
},
|
|
119
|
+
err => {
|
|
120
|
+
clearTimeout(timer);
|
|
121
|
+
reject(err);
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
return race;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { withTimeout, sleep, delay, stopwatch, until, eventually };
|