@sha1n/about-time 0.0.11 → 0.0.12
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/dist/index.js +2 -1
- package/dist/types/index.d.ts +1 -1
- package/package.json +1 -1
- package/dist/lib/timeunit.js +0 -28
- package/dist/lib/utilities.js +0 -133
- package/dist/types/lib/timeunit.d.ts +0 -21
- package/dist/types/lib/utilities.d.ts +0 -76
package/dist/index.js
CHANGED
|
@@ -10,13 +10,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.exponentialBackoffRetryPolicy = exports.simpleRetryPolicy = exports.fixedRetryPolicy = exports.retriable = exports.retryAround = exports.eventually = exports.until = exports.stopwatch = exports.timeBounded = exports.timeoutAround = exports.delay = exports.sleep = exports.toMilliseconds = void 0;
|
|
13
|
+
exports.exponentialBackoffRetryPolicy = exports.simpleRetryPolicy = exports.fixedRetryPolicy = exports.retriable = exports.retryAround = exports.eventually = exports.until = exports.stopwatch = exports.timeBounded = exports.timeoutAround = exports.delayed = exports.delay = exports.sleep = exports.toMilliseconds = void 0;
|
|
14
14
|
__exportStar(require("./lib/types"), exports);
|
|
15
15
|
var toMilliseconds_1 = require("./lib/toMilliseconds");
|
|
16
16
|
Object.defineProperty(exports, "toMilliseconds", { enumerable: true, get: function () { return toMilliseconds_1.toMilliseconds; } });
|
|
17
17
|
var delay_1 = require("./lib/delay");
|
|
18
18
|
Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return delay_1.sleep; } });
|
|
19
19
|
Object.defineProperty(exports, "delay", { enumerable: true, get: function () { return delay_1.delay; } });
|
|
20
|
+
Object.defineProperty(exports, "delayed", { enumerable: true, get: function () { return delay_1.delayed; } });
|
|
20
21
|
var timeout_1 = require("./lib/timeout");
|
|
21
22
|
Object.defineProperty(exports, "timeoutAround", { enumerable: true, get: function () { return timeout_1.timeoutAround; } });
|
|
22
23
|
Object.defineProperty(exports, "timeBounded", { enumerable: true, get: function () { return timeout_1.timeBounded; } });
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from './lib/types';
|
|
2
2
|
export { toMilliseconds } from './lib/toMilliseconds';
|
|
3
|
-
export { sleep, delay } from './lib/delay';
|
|
3
|
+
export { sleep, delay, delayed } from './lib/delay';
|
|
4
4
|
export { timeoutAround, timeBounded } from './lib/timeout';
|
|
5
5
|
export { stopwatch } from './lib/stopwatch';
|
|
6
6
|
export { until, eventually } from './lib/eventually';
|
package/package.json
CHANGED
package/dist/lib/timeunit.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toMilliseconds = exports.TimeUnit = void 0;
|
|
4
|
-
var TimeUnit;
|
|
5
|
-
(function (TimeUnit) {
|
|
6
|
-
TimeUnit[TimeUnit["Milliseconds"] = 1] = "Milliseconds";
|
|
7
|
-
TimeUnit[TimeUnit["Millisecond"] = 1] = "Millisecond";
|
|
8
|
-
TimeUnit[TimeUnit["Seconds"] = 1000] = "Seconds";
|
|
9
|
-
TimeUnit[TimeUnit["Second"] = 1000] = "Second";
|
|
10
|
-
TimeUnit[TimeUnit["Minutes"] = 60000] = "Minutes";
|
|
11
|
-
TimeUnit[TimeUnit["Minute"] = 60000] = "Minute";
|
|
12
|
-
TimeUnit[TimeUnit["Hours"] = 3600000] = "Hours";
|
|
13
|
-
TimeUnit[TimeUnit["Hour"] = 3600000] = "Hour";
|
|
14
|
-
TimeUnit[TimeUnit["Days"] = 86400000] = "Days";
|
|
15
|
-
TimeUnit[TimeUnit["Day"] = 86400000] = "Day";
|
|
16
|
-
})(TimeUnit || (TimeUnit = {}));
|
|
17
|
-
exports.TimeUnit = TimeUnit;
|
|
18
|
-
/**
|
|
19
|
-
* Converts time value in other units to milliseconds.
|
|
20
|
-
*
|
|
21
|
-
* @param time a time value to be converted
|
|
22
|
-
* @param units the units of time
|
|
23
|
-
* @returns the time value in milliseconds
|
|
24
|
-
*/
|
|
25
|
-
function toMilliseconds(time, units) {
|
|
26
|
-
return time * (units ? units : 1);
|
|
27
|
-
}
|
|
28
|
-
exports.toMilliseconds = toMilliseconds;
|
package/dist/lib/utilities.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.eventually = exports.until = exports.stopwatch = exports.delay = exports.sleep = exports.timeBounded = exports.timeoutAround = void 0;
|
|
4
|
-
const timeunit_1 = require("./timeunit");
|
|
5
|
-
class TimeoutError extends Error {
|
|
6
|
-
constructor(message) {
|
|
7
|
-
super(message || 'Timeout');
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Zzzz...
|
|
12
|
-
*
|
|
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
|
-
* @returns a promise that resolves when the specified time has elapsed.
|
|
16
|
-
*/
|
|
17
|
-
function sleep(time, options) {
|
|
18
|
-
return new Promise(resolve => {
|
|
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
|
-
}
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
exports.sleep = sleep;
|
|
26
|
-
/**
|
|
27
|
-
* Delays the execution of the specified action and returns its value.
|
|
28
|
-
*
|
|
29
|
-
* @param action a function to execute with delay
|
|
30
|
-
* @param options timer options
|
|
31
|
-
* @returns a promise that resolves when the specified time has elapsed.
|
|
32
|
-
*/
|
|
33
|
-
function delay(action, options) {
|
|
34
|
-
const { time, units, unref } = options;
|
|
35
|
-
const delayMs = (0, timeunit_1.toMilliseconds)(time, units);
|
|
36
|
-
return new Promise((resolve, reject) => {
|
|
37
|
-
const timer = setTimeout(() => Promise.resolve(action()).then(resolve, reject), delayMs);
|
|
38
|
-
if (unref) {
|
|
39
|
-
timer.unref();
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
exports.delay = delay;
|
|
44
|
-
/**
|
|
45
|
-
* Return a function that returns the elapsed time relative to this call.
|
|
46
|
-
* @returns a function
|
|
47
|
-
*/
|
|
48
|
-
function stopwatch() {
|
|
49
|
-
const startTime = Date.now();
|
|
50
|
-
return (units) => {
|
|
51
|
-
return (Date.now() - startTime) / (units || timeunit_1.TimeUnit.Milliseconds);
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
exports.stopwatch = stopwatch;
|
|
55
|
-
/**
|
|
56
|
-
* Awaits a specified condition to evaluate to true with or without a timeout.
|
|
57
|
-
*
|
|
58
|
-
* @param condition the condition to wait for
|
|
59
|
-
* @param options poll-options
|
|
60
|
-
* @returns a promise that resolves when the condition becomes true, or rejects when a set timeout is crossed.
|
|
61
|
-
*/
|
|
62
|
-
async function until(condition, options) {
|
|
63
|
-
const defaultInterval = 50;
|
|
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;
|
|
66
|
-
return new Promise((resolve, reject) => {
|
|
67
|
-
const handle = setInterval(() => {
|
|
68
|
-
if (Date.now() > deadline) {
|
|
69
|
-
clearInterval(handle);
|
|
70
|
-
reject(new TimeoutError());
|
|
71
|
-
}
|
|
72
|
-
try {
|
|
73
|
-
if (condition()) {
|
|
74
|
-
clearInterval(handle);
|
|
75
|
-
resolve();
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
catch (e) {
|
|
79
|
-
clearInterval(handle);
|
|
80
|
-
reject(e);
|
|
81
|
-
}
|
|
82
|
-
}, interval);
|
|
83
|
-
if (options === null || options === void 0 ? void 0 : options.unref) {
|
|
84
|
-
handle.unref();
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
exports.until = until;
|
|
89
|
-
/**
|
|
90
|
-
* Alias to `until`
|
|
91
|
-
*/
|
|
92
|
-
const eventually = until;
|
|
93
|
-
exports.eventually = eventually;
|
|
94
|
-
/**
|
|
95
|
-
* Executes an action with a specified timeout. If the action times out, rejects with TimeoutError.
|
|
96
|
-
*
|
|
97
|
-
* @param action an action to execute with timeout
|
|
98
|
-
* @param options timer options
|
|
99
|
-
* @returns the action result
|
|
100
|
-
*/
|
|
101
|
-
async function timeoutAround(action, options) {
|
|
102
|
-
const promisedAction = new Promise((resolve, reject) => {
|
|
103
|
-
try {
|
|
104
|
-
resolve(action());
|
|
105
|
-
}
|
|
106
|
-
catch (e) {
|
|
107
|
-
reject(e);
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
const race = new Promise((resolve, reject) => {
|
|
111
|
-
const timer = setTimeout(() => {
|
|
112
|
-
reject(new TimeoutError());
|
|
113
|
-
}, (0, timeunit_1.toMilliseconds)(options.time, options.units));
|
|
114
|
-
if (options.unref) {
|
|
115
|
-
timer.unref();
|
|
116
|
-
}
|
|
117
|
-
return Promise.resolve(promisedAction).then(r => {
|
|
118
|
-
clearTimeout(timer);
|
|
119
|
-
resolve(r);
|
|
120
|
-
}, err => {
|
|
121
|
-
clearTimeout(timer);
|
|
122
|
-
reject(err);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
return race;
|
|
126
|
-
}
|
|
127
|
-
exports.timeoutAround = timeoutAround;
|
|
128
|
-
function timeBounded(action, options) {
|
|
129
|
-
return () => {
|
|
130
|
-
return timeoutAround(action, options);
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
exports.timeBounded = timeBounded;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
declare enum TimeUnit {
|
|
2
|
-
Milliseconds = 1,
|
|
3
|
-
Millisecond = 1,
|
|
4
|
-
Seconds = 1000,
|
|
5
|
-
Second = 1000,
|
|
6
|
-
Minutes = 60000,
|
|
7
|
-
Minute = 60000,
|
|
8
|
-
Hours = 3600000,
|
|
9
|
-
Hour = 3600000,
|
|
10
|
-
Days = 86400000,
|
|
11
|
-
Day = 86400000
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Converts time value in other units to milliseconds.
|
|
15
|
-
*
|
|
16
|
-
* @param time a time value to be converted
|
|
17
|
-
* @param units the units of time
|
|
18
|
-
* @returns the time value in milliseconds
|
|
19
|
-
*/
|
|
20
|
-
declare function toMilliseconds(time: number, units?: TimeUnit): number;
|
|
21
|
-
export { TimeUnit, toMilliseconds };
|
|
@@ -1,76 +0,0 @@
|
|
|
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
|
-
};
|
|
34
|
-
/**
|
|
35
|
-
* Zzzz...
|
|
36
|
-
*
|
|
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
|
|
39
|
-
* @returns a promise that resolves when the specified time has elapsed.
|
|
40
|
-
*/
|
|
41
|
-
declare function sleep(time: number, options?: Omit<TimerOptions, 'time'>): Promise<void>;
|
|
42
|
-
/**
|
|
43
|
-
* Delays the execution of the specified action and returns its value.
|
|
44
|
-
*
|
|
45
|
-
* @param action a function to execute with delay
|
|
46
|
-
* @param options timer options
|
|
47
|
-
* @returns a promise that resolves when the specified time has elapsed.
|
|
48
|
-
*/
|
|
49
|
-
declare function delay<T>(action: () => T | Promise<T>, options: TimerOptions): Promise<T>;
|
|
50
|
-
/**
|
|
51
|
-
* Return a function that returns the elapsed time relative to this call.
|
|
52
|
-
* @returns a function
|
|
53
|
-
*/
|
|
54
|
-
declare function stopwatch(): (units?: TimeUnit) => number;
|
|
55
|
-
/**
|
|
56
|
-
* Awaits a specified condition to evaluate to true with or without a timeout.
|
|
57
|
-
*
|
|
58
|
-
* @param condition the condition to wait for
|
|
59
|
-
* @param options poll-options
|
|
60
|
-
* @returns a promise that resolves when the condition becomes true, or rejects when a set timeout is crossed.
|
|
61
|
-
*/
|
|
62
|
-
declare function until(condition: () => boolean, options?: PollOptions): Promise<void>;
|
|
63
|
-
/**
|
|
64
|
-
* Alias to `until`
|
|
65
|
-
*/
|
|
66
|
-
declare const eventually: typeof until;
|
|
67
|
-
/**
|
|
68
|
-
* Executes an action with a specified timeout. If the action times out, rejects with TimeoutError.
|
|
69
|
-
*
|
|
70
|
-
* @param action an action to execute with timeout
|
|
71
|
-
* @param options timer options
|
|
72
|
-
* @returns the action result
|
|
73
|
-
*/
|
|
74
|
-
declare function timeoutAround<T>(action: () => T | Promise<T>, options: TimerOptions): Promise<T>;
|
|
75
|
-
declare function timeBounded<T>(action: () => T | Promise<T>, options: TimerOptions): () => Promise<T>;
|
|
76
|
-
export { timeoutAround, timeBounded, sleep, delay, stopwatch, until, eventually };
|