@sha1n/about-time 0.0.6 → 0.0.9
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 +40 -21
- package/dist/index.js +2 -1
- package/dist/lib/utilities.js +26 -18
- package/dist/types/index.d.ts +1 -1
- package/dist/types/lib/utilities.d.ts +41 -15
- package/index.ts +1 -1
- package/lib/utilities.ts +71 -22
- package/package.json +14 -13
package/README.md
CHANGED
|
@@ -10,45 +10,62 @@
|
|
|
10
10
|
A collection of essential time related utilities.
|
|
11
11
|
|
|
12
12
|
- [About-Time](#about-time)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
- [
|
|
16
|
-
- [
|
|
17
|
-
- [
|
|
18
|
-
- [
|
|
13
|
+
- [Install](#install)
|
|
14
|
+
- [Utilities & Features](#utilities--features)
|
|
15
|
+
- [delay](#delay)
|
|
16
|
+
- [timeoutAround](#timeoutaround)
|
|
17
|
+
- [timeBounded](#timebounded)
|
|
18
|
+
- [sleep](#sleep)
|
|
19
|
+
- [stopwatch](#stopwatch)
|
|
20
|
+
- [until / eventually](#until--eventually)
|
|
19
21
|
- [Retry](#retry)
|
|
20
22
|
- [RetryPolicy](#retrypolicy)
|
|
21
23
|
- [Simple retry policy](#simple-retry-policy)
|
|
22
24
|
- [Fixed retry policy](#fixed-retry-policy)
|
|
23
25
|
- [Exponential backoff retry policy](#exponential-backoff-retry-policy)
|
|
24
|
-
- [
|
|
25
|
-
- [
|
|
26
|
+
- [retryAround](#retryaround)
|
|
27
|
+
- [retriable](#retriable)
|
|
26
28
|
|
|
27
29
|
|
|
28
|
-
|
|
30
|
+
# Install
|
|
29
31
|
```bash
|
|
30
32
|
npm i @sha1n/about-time
|
|
31
33
|
```
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
# Utilities & Features
|
|
36
|
+
## delay
|
|
34
37
|
```ts
|
|
35
38
|
// Execute a function with delay and return it's value
|
|
36
|
-
await delay(action, 10
|
|
39
|
+
await delay(action, { time: 10 });
|
|
40
|
+
await delay(action, { time: 10, units: TimeUnit.Milliseconds });
|
|
41
|
+
await delay(action, { time: 10, units: TimeUnit.Milliseconds, unref: true });
|
|
37
42
|
```
|
|
38
43
|
|
|
39
|
-
##
|
|
44
|
+
## timeoutAround
|
|
40
45
|
```ts
|
|
41
46
|
// Execute a function and guards it with a specified timeout
|
|
42
|
-
await
|
|
47
|
+
await timeoutAround(action, { time: 10 });
|
|
48
|
+
await timeoutAround(action, { time: 10, units: TimeUnit.Milliseconds });
|
|
49
|
+
await timeoutAround(action, { time: 10, units: TimeUnit.Milliseconds, unref: true });
|
|
43
50
|
```
|
|
44
51
|
|
|
45
|
-
##
|
|
52
|
+
## timeBounded
|
|
53
|
+
Wraps a given function with `timeoutAround` with the specified arguments.
|
|
54
|
+
```ts
|
|
55
|
+
const timeBoundAction = timeBounded(action, options);
|
|
56
|
+
const result = await timeBoundAction();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
## sleep
|
|
46
61
|
```ts
|
|
47
62
|
// Pause execution for a specified amount of time
|
|
48
|
-
await sleep(10
|
|
63
|
+
await sleep(10);
|
|
64
|
+
await sleep(10, { units: TimeUnit.Seconds });
|
|
65
|
+
await sleep(10, { units: TimeUnit.Seconds, unref: true });
|
|
49
66
|
```
|
|
50
67
|
|
|
51
|
-
##
|
|
68
|
+
## stopwatch
|
|
52
69
|
```ts
|
|
53
70
|
// Measure time between actions
|
|
54
71
|
const elapsed = stopwatch();
|
|
@@ -62,11 +79,13 @@ const elapsed1 = elapsed(TimeUnit.Milliseconds);
|
|
|
62
79
|
const elapsed2 = elapsed(TimeUnit.Seconds);
|
|
63
80
|
```
|
|
64
81
|
|
|
65
|
-
##
|
|
82
|
+
## until / eventually
|
|
66
83
|
```ts
|
|
67
84
|
// Wait for a condition to become true
|
|
68
|
-
await until(condition, {
|
|
69
|
-
await
|
|
85
|
+
await until(condition, { deadline: 10000 });
|
|
86
|
+
await until(condition, { deadline: 10000, interval: 100 });
|
|
87
|
+
await until(condition, { deadline: 10000, interval: 100, units: TimeUnit.Milliseconds });
|
|
88
|
+
await until(condition, { deadline: 10000, interval: 100, units: TimeUnit.Milliseconds, unref: true });
|
|
70
89
|
```
|
|
71
90
|
|
|
72
91
|
## Retry
|
|
@@ -96,14 +115,14 @@ interval<sub>i</sub> = min(limit, (exponential<sup>i</sup> - 1) / 2)
|
|
|
96
115
|
const retryPolicy = exponentialBackoffRetryPolicy(/* count = */10, /* opts?: { exponential?: number, limit?: number, units?: TimeUnit }*/);
|
|
97
116
|
```
|
|
98
117
|
|
|
99
|
-
###
|
|
118
|
+
### retryAround
|
|
100
119
|
Executes the given function with retries based on the specified policy and *optional* predicate.
|
|
101
120
|
The predicate provides control over which errors we want to retry on.
|
|
102
121
|
```ts
|
|
103
122
|
const result = await retryAround(action, retryPolicy, predicate);
|
|
104
123
|
```
|
|
105
124
|
|
|
106
|
-
###
|
|
125
|
+
### retriable
|
|
107
126
|
Wraps a given function with `retryAround` with the specified arguments.
|
|
108
127
|
```ts
|
|
109
128
|
const retriableAction = retriable(action, retryPolicy, predicate);
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.exponentialBackoffRetryPolicy = exports.simpleRetryPolicy = exports.fixedRetryPolicy = exports.retriable = exports.retryAround = exports.eventually = exports.until = exports.stopwatch = exports.delay = exports.sleep = exports.toMilliseconds = exports.TimeUnit = void 0;
|
|
3
|
+
exports.exponentialBackoffRetryPolicy = exports.simpleRetryPolicy = exports.fixedRetryPolicy = exports.retriable = exports.retryAround = exports.eventually = exports.until = exports.stopwatch = exports.delay = exports.sleep = exports.withTimeout = exports.toMilliseconds = exports.TimeUnit = void 0;
|
|
4
4
|
var timeunit_1 = require("./lib/timeunit");
|
|
5
5
|
Object.defineProperty(exports, "TimeUnit", { enumerable: true, get: function () { return timeunit_1.TimeUnit; } });
|
|
6
6
|
Object.defineProperty(exports, "toMilliseconds", { enumerable: true, get: function () { return timeunit_1.toMilliseconds; } });
|
|
7
7
|
var utilities_1 = require("./lib/utilities");
|
|
8
|
+
Object.defineProperty(exports, "withTimeout", { enumerable: true, get: function () { return utilities_1.withTimeout; } });
|
|
8
9
|
Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return utilities_1.sleep; } });
|
|
9
10
|
Object.defineProperty(exports, "delay", { enumerable: true, get: function () { return utilities_1.delay; } });
|
|
10
11
|
Object.defineProperty(exports, "stopwatch", { enumerable: true, get: function () { return utilities_1.stopwatch; } });
|
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);
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { TimeUnit, toMilliseconds } from './lib/timeunit';
|
|
2
|
-
export { sleep, delay, stopwatch, until, eventually } from './lib/utilities';
|
|
2
|
+
export { withTimeout, sleep, delay, stopwatch, until, eventually } from './lib/utilities';
|
|
3
3
|
export { RetryPolicy, retryAround, retriable, fixedRetryPolicy, simpleRetryPolicy, exponentialBackoffRetryPolicy } from './lib/retry';
|
|
@@ -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/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { TimeUnit, toMilliseconds } from './lib/timeunit';
|
|
2
|
-
export { sleep, delay, stopwatch, until, eventually } from './lib/utilities';
|
|
2
|
+
export { timeoutAround, timeBounded, sleep, delay, stopwatch, until, eventually } from './lib/utilities';
|
|
3
3
|
export {
|
|
4
4
|
RetryPolicy,
|
|
5
5
|
retryAround,
|
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
|
|
139
|
+
async function timeoutAround<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 => {
|
|
@@ -126,4 +169,10 @@ async function withTimeout<T>(action: () => T | Promise<T>, timeout: number, uni
|
|
|
126
169
|
return race;
|
|
127
170
|
}
|
|
128
171
|
|
|
129
|
-
|
|
172
|
+
function timeBounded<T>(action: () => T | Promise<T>, options: TimerOptions): () => Promise<T> {
|
|
173
|
+
return () => {
|
|
174
|
+
return timeoutAround(action, options);
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { timeoutAround, timeBounded, sleep, delay, stopwatch, until, eventually };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sha1n/about-time",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
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,39 @@
|
|
|
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",
|
|
28
26
|
"lint": "eslint --fix --ext .js,.ts .",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
27
|
+
"jest": "DEBUG='error:*' jest --coverage",
|
|
28
|
+
"test": "run jest && run lint",
|
|
29
|
+
"prepublish": "run build"
|
|
31
30
|
},
|
|
32
|
-
"dependencies": {},
|
|
33
31
|
"devDependencies": {
|
|
34
32
|
"@types/chance": "^1.1.3",
|
|
33
|
+
"@types/is-ci": "^3.0.0",
|
|
35
34
|
"@types/jest": "^27.4.0",
|
|
36
35
|
"@types/node": "^17.0.8",
|
|
37
36
|
"@typescript-eslint/eslint-plugin": "^5.1.0",
|
|
38
37
|
"@typescript-eslint/parser": "^5.1.0",
|
|
39
38
|
"chance": "^1.1.8",
|
|
40
|
-
"eslint": "^
|
|
39
|
+
"eslint": "^8.9.0",
|
|
41
40
|
"eslint-config-prettier": "^8.3.0",
|
|
42
41
|
"eslint-plugin-import": "^2.25.2",
|
|
43
|
-
"eslint-plugin-jest": "^
|
|
42
|
+
"eslint-plugin-jest": "^26.0.0",
|
|
44
43
|
"eslint-plugin-no-floating-promise": "^1.0.2",
|
|
45
44
|
"eslint-plugin-node": "^11.1.0",
|
|
46
45
|
"eslint-plugin-prettier": "^4.0.0",
|
|
47
|
-
"eslint-plugin-unused-imports": "^
|
|
48
|
-
"
|
|
46
|
+
"eslint-plugin-unused-imports": "^2.0.0",
|
|
47
|
+
"is-ci": "^3.0.1",
|
|
48
|
+
"jest": "^27.5.1",
|
|
49
49
|
"jest-environment-node": "^27.2.0",
|
|
50
|
-
"jest-extended": "^
|
|
51
|
-
"jest-html-reporters": "^3.0.
|
|
50
|
+
"jest-extended": "^2.0.0",
|
|
51
|
+
"jest-html-reporters": "^3.0.5",
|
|
52
52
|
"jest-mock-extended": "^2.0.4",
|
|
53
53
|
"jest-summary-reporter": "^0.0.2",
|
|
54
54
|
"prettier": "^2.4.1",
|
|
55
55
|
"ts-jest": "^27.1.3",
|
|
56
56
|
"ts-node": "^10.4.0",
|
|
57
57
|
"typescript": "^4.5.4"
|
|
58
|
-
}
|
|
58
|
+
},
|
|
59
|
+
"packageManager": "yarn@3.2.0"
|
|
59
60
|
}
|