@sha1n/about-time 0.0.2 → 0.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sha1n/about-time",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "A set of essential time related utilities",
5
5
  "repository": "https://github.com/sha1n/about-time",
6
6
  "author": "Shai Nagar",
@@ -8,6 +8,18 @@
8
8
  "types": "./dist/types",
9
9
  "main": "./dist",
10
10
  "exports": "./dist/index.js",
11
+ "keywords": [
12
+ "time",
13
+ "wait",
14
+ "retry",
15
+ "sleep",
16
+ "delay",
17
+ "eventually",
18
+ "exponential-backoff",
19
+ "utility",
20
+ "typescript",
21
+ "timeout"
22
+ ],
11
23
  "scripts": {
12
24
  "clean": "rm -rf ./dist",
13
25
  "prebuild": "yarn run clean",
package/dist/index.js DELETED
@@ -1,15 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./lib/timeunit"), exports);
14
- __exportStar(require("./lib/utilities"), exports);
15
- __exportStar(require("./lib/retry"), exports);
package/dist/lib/retry.js DELETED
@@ -1,87 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.exponentialBackoffRetryPolicy = exports.simpleRetryPolicy = exports.fixedRetryPolicy = exports.retriable = exports.retryAround = void 0;
4
- const timeunit_1 = require("./timeunit");
5
- const utilities_1 = require("./utilities");
6
- class SimpleRetryPolicy {
7
- constructor(count, interval, units) {
8
- this.count = count;
9
- this.interval = (0, timeunit_1.toMilliseconds)(interval, units);
10
- }
11
- *intervals() {
12
- let count = this.count;
13
- while (count > 0) {
14
- count -= 1;
15
- yield this.interval;
16
- }
17
- return;
18
- }
19
- }
20
- class ExponentialBackoffRetryPolicy {
21
- constructor(count, exponential = 2, limit = Infinity, units = timeunit_1.TimeUnit.Milliseconds) {
22
- this.count = count;
23
- this.exponential = exponential;
24
- this.limit = limit;
25
- this.units = units;
26
- }
27
- *intervals() {
28
- let index = 0;
29
- while (index < this.count) {
30
- const interval = Math.round((Math.pow(this.exponential, index) - 1) / 2);
31
- yield Math.min(this.limit, interval) * this.units;
32
- index += 1;
33
- }
34
- return;
35
- }
36
- }
37
- class FixedRetryPolicy {
38
- constructor(intervals, units) {
39
- this._intervals = intervals.reduceRight((result, v) => {
40
- result.push((0, timeunit_1.toMilliseconds)(v, units));
41
- return result;
42
- }, []);
43
- }
44
- *intervals() {
45
- const intervals = [...this._intervals];
46
- while (intervals.length > 0) {
47
- yield intervals.pop();
48
- }
49
- return;
50
- }
51
- }
52
- function simpleRetryPolicy(count, interval, opts) {
53
- return Object.freeze(new SimpleRetryPolicy(count, interval, opts === null || opts === void 0 ? void 0 : opts.units));
54
- }
55
- exports.simpleRetryPolicy = simpleRetryPolicy;
56
- function fixedRetryPolicy(intervals, opts) {
57
- return Object.freeze(new FixedRetryPolicy(intervals, opts === null || opts === void 0 ? void 0 : opts.units));
58
- }
59
- exports.fixedRetryPolicy = fixedRetryPolicy;
60
- function exponentialBackoffRetryPolicy(count, opts) {
61
- return Object.freeze(new ExponentialBackoffRetryPolicy(count, opts === null || opts === void 0 ? void 0 : opts.exponential, opts === null || opts === void 0 ? void 0 : opts.limit, opts === null || opts === void 0 ? void 0 : opts.units));
62
- }
63
- exports.exponentialBackoffRetryPolicy = exponentialBackoffRetryPolicy;
64
- async function retryAround(action, policy, predicate = () => true) {
65
- let next;
66
- const intervals = policy.intervals()[Symbol.iterator]();
67
- do {
68
- try {
69
- return await action();
70
- }
71
- catch (e) {
72
- next = intervals.next();
73
- if (next.done || !predicate(e)) {
74
- throw e;
75
- }
76
- await (0, utilities_1.sleep)(next.value);
77
- }
78
- } while (!next.done);
79
- throw new Error('Unexpected error. This is most likely a bug.');
80
- }
81
- exports.retryAround = retryAround;
82
- function retriable(action, policy, predicate = () => true) {
83
- return () => {
84
- return retryAround(action, policy, predicate);
85
- };
86
- }
87
- exports.retriable = retriable;
@@ -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;
@@ -1,76 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.eventually = exports.until = exports.stopwatch = exports.delay = exports.sleep = void 0;
4
- const timeunit_1 = require("./timeunit");
5
- /**
6
- * Zzzz...
7
- *
8
- * @param time time to sleep
9
- * @param units the units on time
10
- * @returns a promise that resolves when the specified time has elapsed.
11
- */
12
- function sleep(time, units) {
13
- return new Promise(resolve => {
14
- setTimeout(resolve, (0, timeunit_1.toMilliseconds)(time, units));
15
- });
16
- }
17
- exports.sleep = sleep;
18
- /**
19
- * Delays the execution of the specified action and returns its value.
20
- *
21
- * @param action a function to execute with delay
22
- * @param time time to sleep
23
- * @param units the units on time
24
- * @returns a promise that resolves when the specified time has elapsed.
25
- */
26
- function delay(action, time, units) {
27
- const delayMs = (0, timeunit_1.toMilliseconds)(time, units);
28
- return new Promise((resolve, reject) => {
29
- setTimeout(() => Promise.resolve(action()).then(resolve, reject), delayMs);
30
- });
31
- }
32
- exports.delay = delay;
33
- /**
34
- * Return a function that returns the elapsed time relative to this call.
35
- * @returns a function
36
- */
37
- function stopwatch() {
38
- const startTime = Date.now();
39
- return (units) => {
40
- return (Date.now() - startTime) / (units || timeunit_1.TimeUnit.Milliseconds);
41
- };
42
- }
43
- exports.stopwatch = stopwatch;
44
- /**
45
- * Awaits a specified condition to evaluate to true with or without a timeout.
46
- *
47
- * @param condition the condition to wait for
48
- * @param opts options that control evaluation intervals and timeout
49
- * @returns a promise that resolves when the condition becomes true, or rejects when a set timeout is crossed.
50
- */
51
- async function until(condition, opts) {
52
- if (condition()) {
53
- return;
54
- }
55
- const defaultInterval = 50;
56
- const deadline = opts ? Date.now() + (0, timeunit_1.toMilliseconds)(opts.timeout, opts.units) : Number.MAX_VALUE;
57
- const interval = (opts === null || opts === void 0 ? void 0 : opts.interval) ? (0, timeunit_1.toMilliseconds)(opts.interval, opts.units) : defaultInterval;
58
- return new Promise((resolve, reject) => {
59
- const handle = setInterval(() => {
60
- if (Date.now() > deadline) {
61
- clearInterval(handle);
62
- reject(new Error('Timeout'));
63
- }
64
- if (condition()) {
65
- clearInterval(handle);
66
- resolve();
67
- }
68
- }, interval);
69
- });
70
- }
71
- exports.until = until;
72
- /**
73
- * Alias to `until`
74
- */
75
- const eventually = until;
76
- exports.eventually = eventually;
@@ -1,3 +0,0 @@
1
- export * from './lib/timeunit';
2
- export * from './lib/utilities';
3
- export * from './lib/retry';
@@ -1,18 +0,0 @@
1
- import { TimeUnit } from './timeunit';
2
- interface RetryPolicy {
3
- intervals(): Iterable<number>;
4
- }
5
- declare function simpleRetryPolicy(count: number, interval: number, opts?: {
6
- units?: TimeUnit;
7
- }): RetryPolicy;
8
- declare function fixedRetryPolicy(intervals: number[], opts?: {
9
- units?: TimeUnit;
10
- }): RetryPolicy;
11
- declare function exponentialBackoffRetryPolicy(count: number, opts?: {
12
- exponential?: number;
13
- limit?: number;
14
- units?: TimeUnit;
15
- }): RetryPolicy;
16
- declare function retryAround<T>(action: () => T | Promise<T>, policy: RetryPolicy, predicate?: (e: Error) => boolean): Promise<T>;
17
- declare function retriable<T>(action: () => T | Promise<T>, policy: RetryPolicy, predicate?: (e: Error) => boolean): () => Promise<T>;
18
- export { RetryPolicy, retryAround, retriable, fixedRetryPolicy, simpleRetryPolicy, exponentialBackoffRetryPolicy };
@@ -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,40 +0,0 @@
1
- import { TimeUnit } from './timeunit';
2
- /**
3
- * Zzzz...
4
- *
5
- * @param time time to sleep
6
- * @param units the units on time
7
- * @returns a promise that resolves when the specified time has elapsed.
8
- */
9
- declare function sleep(time: number, units?: TimeUnit): Promise<void>;
10
- /**
11
- * Delays the execution of the specified action and returns its value.
12
- *
13
- * @param action a function to execute with delay
14
- * @param time time to sleep
15
- * @param units the units on time
16
- * @returns a promise that resolves when the specified time has elapsed.
17
- */
18
- declare function delay<T>(action: () => T | Promise<T>, time: number, units?: TimeUnit): Promise<T>;
19
- /**
20
- * Return a function that returns the elapsed time relative to this call.
21
- * @returns a function
22
- */
23
- declare function stopwatch(): (units?: TimeUnit) => number;
24
- /**
25
- * Awaits a specified condition to evaluate to true with or without a timeout.
26
- *
27
- * @param condition the condition to wait for
28
- * @param opts options that control evaluation intervals and timeout
29
- * @returns a promise that resolves when the condition becomes true, or rejects when a set timeout is crossed.
30
- */
31
- declare function until(condition: () => boolean, opts?: {
32
- interval?: number;
33
- timeout: number;
34
- units?: TimeUnit;
35
- }): Promise<void>;
36
- /**
37
- * Alias to `until`
38
- */
39
- declare const eventually: typeof until;
40
- export { sleep, delay, stopwatch, until, eventually };