@sha1n/about-time 0.0.8 → 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 +26 -17
- package/index.ts +1 -1
- package/lib/utilities.ts +8 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -12,18 +12,19 @@ A collection of essential time related utilities.
|
|
|
12
12
|
- [About-Time](#about-time)
|
|
13
13
|
- [Install](#install)
|
|
14
14
|
- [Utilities & Features](#utilities--features)
|
|
15
|
-
- [
|
|
16
|
-
- [
|
|
17
|
-
- [
|
|
18
|
-
- [
|
|
19
|
-
- [
|
|
15
|
+
- [delay](#delay)
|
|
16
|
+
- [timeoutAround](#timeoutaround)
|
|
17
|
+
- [timeBounded](#timebounded)
|
|
18
|
+
- [sleep](#sleep)
|
|
19
|
+
- [stopwatch](#stopwatch)
|
|
20
|
+
- [until / eventually](#until--eventually)
|
|
20
21
|
- [Retry](#retry)
|
|
21
22
|
- [RetryPolicy](#retrypolicy)
|
|
22
23
|
- [Simple retry policy](#simple-retry-policy)
|
|
23
24
|
- [Fixed retry policy](#fixed-retry-policy)
|
|
24
25
|
- [Exponential backoff retry policy](#exponential-backoff-retry-policy)
|
|
25
|
-
- [
|
|
26
|
-
- [
|
|
26
|
+
- [retryAround](#retryaround)
|
|
27
|
+
- [retriable](#retriable)
|
|
27
28
|
|
|
28
29
|
|
|
29
30
|
# Install
|
|
@@ -32,7 +33,7 @@ npm i @sha1n/about-time
|
|
|
32
33
|
```
|
|
33
34
|
|
|
34
35
|
# Utilities & Features
|
|
35
|
-
##
|
|
36
|
+
## delay
|
|
36
37
|
```ts
|
|
37
38
|
// Execute a function with delay and return it's value
|
|
38
39
|
await delay(action, { time: 10 });
|
|
@@ -40,15 +41,23 @@ await delay(action, { time: 10, units: TimeUnit.Milliseconds });
|
|
|
40
41
|
await delay(action, { time: 10, units: TimeUnit.Milliseconds, unref: true });
|
|
41
42
|
```
|
|
42
43
|
|
|
43
|
-
##
|
|
44
|
+
## timeoutAround
|
|
44
45
|
```ts
|
|
45
46
|
// Execute a function and guards it with a specified timeout
|
|
46
|
-
await
|
|
47
|
-
await
|
|
48
|
-
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 });
|
|
49
50
|
```
|
|
50
51
|
|
|
51
|
-
##
|
|
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
|
|
52
61
|
```ts
|
|
53
62
|
// Pause execution for a specified amount of time
|
|
54
63
|
await sleep(10);
|
|
@@ -56,7 +65,7 @@ await sleep(10, { units: TimeUnit.Seconds });
|
|
|
56
65
|
await sleep(10, { units: TimeUnit.Seconds, unref: true });
|
|
57
66
|
```
|
|
58
67
|
|
|
59
|
-
##
|
|
68
|
+
## stopwatch
|
|
60
69
|
```ts
|
|
61
70
|
// Measure time between actions
|
|
62
71
|
const elapsed = stopwatch();
|
|
@@ -70,7 +79,7 @@ const elapsed1 = elapsed(TimeUnit.Milliseconds);
|
|
|
70
79
|
const elapsed2 = elapsed(TimeUnit.Seconds);
|
|
71
80
|
```
|
|
72
81
|
|
|
73
|
-
##
|
|
82
|
+
## until / eventually
|
|
74
83
|
```ts
|
|
75
84
|
// Wait for a condition to become true
|
|
76
85
|
await until(condition, { deadline: 10000 });
|
|
@@ -106,14 +115,14 @@ interval<sub>i</sub> = min(limit, (exponential<sup>i</sup> - 1) / 2)
|
|
|
106
115
|
const retryPolicy = exponentialBackoffRetryPolicy(/* count = */10, /* opts?: { exponential?: number, limit?: number, units?: TimeUnit }*/);
|
|
107
116
|
```
|
|
108
117
|
|
|
109
|
-
###
|
|
118
|
+
### retryAround
|
|
110
119
|
Executes the given function with retries based on the specified policy and *optional* predicate.
|
|
111
120
|
The predicate provides control over which errors we want to retry on.
|
|
112
121
|
```ts
|
|
113
122
|
const result = await retryAround(action, retryPolicy, predicate);
|
|
114
123
|
```
|
|
115
124
|
|
|
116
|
-
###
|
|
125
|
+
### retriable
|
|
117
126
|
Wraps a given function with `retryAround` with the specified arguments.
|
|
118
127
|
```ts
|
|
119
128
|
const retriableAction = retriable(action, retryPolicy, predicate);
|
package/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { TimeUnit, toMilliseconds } from './lib/timeunit';
|
|
2
|
-
export {
|
|
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
|
@@ -136,7 +136,7 @@ const eventually = until;
|
|
|
136
136
|
* @param options timer options
|
|
137
137
|
* @returns the action result
|
|
138
138
|
*/
|
|
139
|
-
async function
|
|
139
|
+
async function timeoutAround<T>(action: () => T | Promise<T>, options: TimerOptions): Promise<T> {
|
|
140
140
|
const promisedAction = new Promise<T>((resolve, reject) => {
|
|
141
141
|
try {
|
|
142
142
|
resolve(action());
|
|
@@ -169,4 +169,10 @@ async function withTimeout<T>(action: () => T | Promise<T>, options: TimerOption
|
|
|
169
169
|
return race;
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
|
|
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",
|
|
@@ -23,8 +23,9 @@
|
|
|
23
23
|
"scripts": {
|
|
24
24
|
"clean": "rm -rf ./dist",
|
|
25
25
|
"build": "tsc",
|
|
26
|
-
"test": "DEBUG='error:*' jest --coverage && run lint",
|
|
27
26
|
"lint": "eslint --fix --ext .js,.ts .",
|
|
27
|
+
"jest": "DEBUG='error:*' jest --coverage",
|
|
28
|
+
"test": "run jest && run lint",
|
|
28
29
|
"prepublish": "run build"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|