@sha1n/about-time 0.0.10 → 0.0.13

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 CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -10,13 +14,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
15
  };
12
16
  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;
17
+ 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
18
  __exportStar(require("./lib/types"), exports);
15
19
  var toMilliseconds_1 = require("./lib/toMilliseconds");
16
20
  Object.defineProperty(exports, "toMilliseconds", { enumerable: true, get: function () { return toMilliseconds_1.toMilliseconds; } });
17
21
  var delay_1 = require("./lib/delay");
18
22
  Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return delay_1.sleep; } });
19
23
  Object.defineProperty(exports, "delay", { enumerable: true, get: function () { return delay_1.delay; } });
24
+ Object.defineProperty(exports, "delayed", { enumerable: true, get: function () { return delay_1.delayed; } });
20
25
  var timeout_1 = require("./lib/timeout");
21
26
  Object.defineProperty(exports, "timeoutAround", { enumerable: true, get: function () { return timeout_1.timeoutAround; } });
22
27
  Object.defineProperty(exports, "timeBounded", { enumerable: true, get: function () { return timeout_1.timeBounded; } });
@@ -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/index.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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sha1n/about-time",
3
- "version": "0.0.10",
3
+ "version": "0.0.13",
4
4
  "description": "A set of essential time related utilities",
5
5
  "repository": "https://github.com/sha1n/about-time",
6
6
  "author": "Shai Nagar",
@@ -26,7 +26,7 @@
26
26
  "lint": "eslint --fix --ext .js,.ts .",
27
27
  "jest": "DEBUG='error:*' jest --coverage",
28
28
  "test": "run jest && run lint",
29
- "prepublish": "run build"
29
+ "prepare": "npm run build"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/chance": "^1.1.3",
@@ -36,25 +36,25 @@
36
36
  "@typescript-eslint/eslint-plugin": "^5.1.0",
37
37
  "@typescript-eslint/parser": "^5.1.0",
38
38
  "chance": "^1.1.8",
39
- "eslint": "^8.9.0",
40
- "eslint-config-prettier": "^8.3.0",
41
- "eslint-plugin-import": "^2.25.2",
42
- "eslint-plugin-jest": "^26.0.0",
39
+ "eslint": "^8.13.0",
40
+ "eslint-config-prettier": "^8.5.0",
41
+ "eslint-plugin-import": "^2.26.0",
42
+ "eslint-plugin-jest": "^26.1.4",
43
43
  "eslint-plugin-no-floating-promise": "^1.0.2",
44
44
  "eslint-plugin-node": "^11.1.0",
45
45
  "eslint-plugin-prettier": "^4.0.0",
46
46
  "eslint-plugin-unused-imports": "^2.0.0",
47
47
  "is-ci": "^3.0.1",
48
48
  "jest": "^27.5.1",
49
- "jest-environment-node": "^27.2.0",
49
+ "jest-environment-node": "^27.5.1",
50
50
  "jest-extended": "^2.0.0",
51
- "jest-html-reporters": "^3.0.5",
52
- "jest-mock-extended": "^2.0.4",
51
+ "jest-html-reporters": "^3.0.6",
52
+ "jest-mock-extended": "^2.0.5",
53
53
  "jest-summary-reporter": "^0.0.2",
54
- "prettier": "^2.4.1",
55
- "ts-jest": "^27.1.3",
56
- "ts-node": "^10.4.0",
57
- "typescript": "^4.5.4"
54
+ "prettier": "^2.6.2",
55
+ "ts-jest": "^27.1.4",
56
+ "ts-node": "^10.7.0",
57
+ "typescript": "^4.6.3"
58
58
  },
59
59
  "packageManager": "yarn@3.2.0"
60
60
  }
@@ -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,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 };