@sha1n/about-time 0.1.0 → 0.2.1

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];
package/dist/lib/delay.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.delayed = exports.delay = exports.sleep = void 0;
3
+ exports.sleep = sleep;
4
+ exports.delay = delay;
5
+ exports.delayed = delayed;
4
6
  const toMilliseconds_1 = require("./toMilliseconds");
5
7
  /**
6
8
  * Zzzz...
@@ -11,13 +13,12 @@ const toMilliseconds_1 = require("./toMilliseconds");
11
13
  */
12
14
  function sleep(time, options) {
13
15
  return new Promise(resolve => {
14
- const timeout = setTimeout(resolve, toMilliseconds_1.toMilliseconds(time, options === null || options === void 0 ? void 0 : options.units));
16
+ const timeout = setTimeout(resolve, (0, toMilliseconds_1.toMilliseconds)(time, options === null || options === void 0 ? void 0 : options.units));
15
17
  if (options === null || options === void 0 ? void 0 : options.unref) {
16
18
  timeout.unref();
17
19
  }
18
20
  });
19
21
  }
20
- exports.sleep = sleep;
21
22
  /**
22
23
  * Delays the execution of the specified action and returns its value.
23
24
  *
@@ -30,7 +31,6 @@ async function delay(action, options) {
30
31
  const result = await action();
31
32
  return result;
32
33
  }
33
- exports.delay = delay;
34
34
  /**
35
35
  * Returns a new function that executes the specified action with delay.
36
36
  *
@@ -43,4 +43,3 @@ function delayed(action, options) {
43
43
  return delay(action, options);
44
44
  };
45
45
  }
46
- exports.delayed = delayed;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.eventually = exports.until = void 0;
3
+ exports.eventually = void 0;
4
+ exports.until = until;
4
5
  const toMilliseconds_1 = require("./toMilliseconds");
5
6
  const types_1 = require("./types");
6
7
  /**
@@ -12,31 +13,33 @@ const types_1 = require("./types");
12
13
  */
13
14
  async function until(condition, options) {
14
15
  const defaultInterval = 50;
15
- const deadline = (options === null || options === void 0 ? void 0 : options.deadline) ? Date.now() + toMilliseconds_1.toMilliseconds(options.deadline, options.units) : Number.MAX_VALUE;
16
- const interval = (options === null || options === void 0 ? void 0 : options.interval) ? toMilliseconds_1.toMilliseconds(options.interval, options.units) : defaultInterval;
16
+ const deadline = (options === null || options === void 0 ? void 0 : options.deadline) ? Date.now() + (0, toMilliseconds_1.toMilliseconds)(options.deadline, options.units) : Number.MAX_VALUE;
17
+ const interval = (options === null || options === void 0 ? void 0 : options.interval) ? (0, toMilliseconds_1.toMilliseconds)(options.interval, options.units) : defaultInterval;
17
18
  return new Promise((resolve, reject) => {
18
- const handle = setInterval(() => {
19
+ let handle;
20
+ const poll = () => {
19
21
  if (Date.now() > deadline) {
20
- clearInterval(handle);
21
22
  reject(new types_1.TimeoutError());
23
+ return;
22
24
  }
23
25
  try {
24
26
  if (condition()) {
25
- clearInterval(handle);
26
27
  resolve();
27
28
  }
29
+ else {
30
+ handle = setTimeout(poll, interval);
31
+ if (options === null || options === void 0 ? void 0 : options.unref) {
32
+ handle.unref();
33
+ }
34
+ }
28
35
  }
29
36
  catch (e) {
30
- clearInterval(handle);
31
37
  reject(e);
32
38
  }
33
- }, interval);
34
- if (options === null || options === void 0 ? void 0 : options.unref) {
35
- handle.unref();
36
- }
39
+ };
40
+ poll();
37
41
  });
38
42
  }
39
- exports.until = until;
40
43
  /**
41
44
  * Alias to `until`
42
45
  */
package/dist/lib/retry.js CHANGED
@@ -1,13 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.exponentialBackoffRetryPolicy = exports.simpleRetryPolicy = exports.fixedRetryPolicy = exports.retriable = exports.retryAround = void 0;
3
+ exports.retryAround = retryAround;
4
+ exports.retriable = retriable;
5
+ exports.fixedRetryPolicy = fixedRetryPolicy;
6
+ exports.simpleRetryPolicy = simpleRetryPolicy;
7
+ exports.exponentialBackoffRetryPolicy = exponentialBackoffRetryPolicy;
4
8
  const toMilliseconds_1 = require("./toMilliseconds");
5
9
  const delay_1 = require("./delay");
6
10
  const types_1 = require("./types");
7
11
  class SimpleRetryPolicy {
8
12
  constructor(count, interval, units) {
9
13
  this.count = count;
10
- this.interval = toMilliseconds_1.toMilliseconds(interval, units);
14
+ this.interval = (0, toMilliseconds_1.toMilliseconds)(interval, units);
11
15
  }
12
16
  *intervals() {
13
17
  let count = this.count;
@@ -38,7 +42,7 @@ class ExponentialBackoffRetryPolicy {
38
42
  class FixedRetryPolicy {
39
43
  constructor(intervals, units) {
40
44
  this._intervals = intervals.reduceRight((result, v) => {
41
- result.push(toMilliseconds_1.toMilliseconds(v, units));
45
+ result.push((0, toMilliseconds_1.toMilliseconds)(v, units));
42
46
  return result;
43
47
  }, []);
44
48
  }
@@ -53,15 +57,12 @@ class FixedRetryPolicy {
53
57
  function simpleRetryPolicy(count, interval, opts) {
54
58
  return Object.freeze(new SimpleRetryPolicy(count, interval, opts === null || opts === void 0 ? void 0 : opts.units));
55
59
  }
56
- exports.simpleRetryPolicy = simpleRetryPolicy;
57
60
  function fixedRetryPolicy(intervals, opts) {
58
61
  return Object.freeze(new FixedRetryPolicy(intervals, opts === null || opts === void 0 ? void 0 : opts.units));
59
62
  }
60
- exports.fixedRetryPolicy = fixedRetryPolicy;
61
63
  function exponentialBackoffRetryPolicy(count, opts) {
62
64
  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));
63
65
  }
64
- exports.exponentialBackoffRetryPolicy = exponentialBackoffRetryPolicy;
65
66
  async function retryAround(action, policy, predicate = () => true) {
66
67
  let next;
67
68
  const intervals = policy.intervals()[Symbol.iterator]();
@@ -74,15 +75,13 @@ async function retryAround(action, policy, predicate = () => true) {
74
75
  if (next.done || !predicate(e)) {
75
76
  throw e;
76
77
  }
77
- await delay_1.sleep(next.value);
78
+ await (0, delay_1.sleep)(next.value);
78
79
  }
79
80
  } while (!next.done);
80
81
  throw new Error('Unexpected error. This is most likely a bug.');
81
82
  }
82
- exports.retryAround = retryAround;
83
83
  function retriable(action, policy, predicate = () => true) {
84
84
  return () => {
85
85
  return retryAround(action, policy, predicate);
86
86
  };
87
87
  }
88
- exports.retriable = retriable;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stopwatch = void 0;
3
+ exports.stopwatch = stopwatch;
4
4
  const types_1 = require("./types");
5
5
  /**
6
6
  * Return a function that returns the elapsed time relative to this call.
@@ -12,4 +12,3 @@ function stopwatch() {
12
12
  return (Date.now() - startTime) / (units || types_1.TimeUnit.Milliseconds);
13
13
  };
14
14
  }
15
- exports.stopwatch = stopwatch;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.timeoutAround = exports.timeBounded = void 0;
3
+ exports.timeBounded = timeBounded;
4
+ exports.timeoutAround = timeoutAround;
4
5
  const toMilliseconds_1 = require("./toMilliseconds");
5
6
  const types_1 = require("./types");
6
7
  /**
@@ -22,7 +23,7 @@ async function timeoutAround(action, options) {
22
23
  const race = new Promise((resolve, reject) => {
23
24
  const timer = setTimeout(() => {
24
25
  reject(new types_1.TimeoutError());
25
- }, toMilliseconds_1.toMilliseconds(options.time, options.units));
26
+ }, (0, toMilliseconds_1.toMilliseconds)(options.time, options.units));
26
27
  if (options.unref) {
27
28
  timer.unref();
28
29
  }
@@ -36,10 +37,8 @@ async function timeoutAround(action, options) {
36
37
  });
37
38
  return race;
38
39
  }
39
- exports.timeoutAround = timeoutAround;
40
40
  function timeBounded(action, options) {
41
41
  return () => {
42
42
  return timeoutAround(action, options);
43
43
  };
44
44
  }
45
- exports.timeBounded = timeBounded;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toMilliseconds = void 0;
3
+ exports.toMilliseconds = toMilliseconds;
4
4
  /**
5
5
  * Converts time value in other units to milliseconds.
6
6
  *
@@ -11,4 +11,3 @@ exports.toMilliseconds = void 0;
11
11
  function toMilliseconds(time, units) {
12
12
  return time * (units ? units : 1);
13
13
  }
14
- exports.toMilliseconds = toMilliseconds;
package/dist/lib/types.js CHANGED
@@ -13,8 +13,7 @@ var TimeUnit;
13
13
  TimeUnit[TimeUnit["Hour"] = 3600000] = "Hour";
14
14
  TimeUnit[TimeUnit["Days"] = 86400000] = "Days";
15
15
  TimeUnit[TimeUnit["Day"] = 86400000] = "Day";
16
- })(TimeUnit || (TimeUnit = {}));
17
- exports.TimeUnit = TimeUnit;
16
+ })(TimeUnit || (exports.TimeUnit = TimeUnit = {}));
18
17
  class TimeoutError extends Error {
19
18
  constructor(message) {
20
19
  super(message || 'Timeout');
@@ -13,7 +13,7 @@ declare enum TimeUnit {
13
13
  declare class TimeoutError extends Error {
14
14
  constructor(message?: string);
15
15
  }
16
- declare type TimerOptions = {
16
+ type TimerOptions = {
17
17
  /**
18
18
  * The time to set
19
19
  */
@@ -27,7 +27,7 @@ declare type TimerOptions = {
27
27
  */
28
28
  readonly unref?: boolean;
29
29
  };
30
- declare type PollOptions = {
30
+ type PollOptions = {
31
31
  /**
32
32
  * The poll interval to set
33
33
  */
package/lib/eventually.ts CHANGED
@@ -14,26 +14,29 @@ async function until(condition: () => boolean, options?: PollOptions): Promise<v
14
14
  const interval = options?.interval ? toMilliseconds(options.interval, options.units) : defaultInterval;
15
15
 
16
16
  return new Promise<void>((resolve, reject) => {
17
- const handle = setInterval(() => {
17
+ let handle: NodeJS.Timeout;
18
+
19
+ const poll = () => {
18
20
  if (Date.now() > deadline) {
19
- clearInterval(handle);
20
21
  reject(new TimeoutError());
22
+ return;
21
23
  }
22
24
 
23
25
  try {
24
26
  if (condition()) {
25
- clearInterval(handle);
26
27
  resolve();
28
+ } else {
29
+ handle = setTimeout(poll, interval);
30
+ if (options?.unref) {
31
+ handle.unref();
32
+ }
27
33
  }
28
34
  } catch (e) {
29
- clearInterval(handle);
30
35
  reject(e);
31
36
  }
32
- }, interval);
37
+ };
33
38
 
34
- if (options?.unref) {
35
- handle.unref();
36
- }
39
+ poll();
37
40
  });
38
41
  }
39
42
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sha1n/about-time",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "type": "commonjs",
5
5
  "description": "A set of essential time related utilities",
6
6
  "repository": "https://github.com/sha1n/about-time",
@@ -29,39 +29,53 @@
29
29
  "build": "tsc",
30
30
  "lint": "eslint --fix --ext .js,.ts .",
31
31
  "jest": "DEBUG='error:*' jest --coverage",
32
- "test": "run jest && run lint",
33
- "prepare": "npm run build"
32
+ "test": "pnpm jest && pnpm lint",
33
+ "prepare": "pnpm run build"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public",
37
+ "registry": "https://registry.npmjs.org/"
34
38
  },
35
39
  "devDependencies": {
36
40
  "@types/chance": "^1.1.3",
37
41
  "@types/is-ci": "^3.0.0",
38
- "@types/jest": "^27.4.0",
39
- "@types/node": "^17.0.8",
40
- "@typescript-eslint/eslint-plugin": "^5.1.0",
41
- "@typescript-eslint/parser": "^5.1.0",
42
+ "@types/jest": "^29.5.14",
43
+ "@types/node": "^22.0.0",
44
+ "@typescript-eslint/eslint-plugin": "^8.0.0",
45
+ "@typescript-eslint/parser": "^8.0.0",
42
46
  "chance": "^1.1.8",
43
- "eslint": "^8.13.0",
47
+ "eslint": "^8.57.0",
44
48
  "eslint-config-prettier": "^8.5.0",
45
49
  "eslint-plugin-import": "^2.26.0",
46
- "eslint-plugin-jest": "^26.1.4",
50
+ "eslint-plugin-jest": "^29.12.1",
47
51
  "eslint-plugin-no-floating-promise": "^1.0.2",
48
52
  "eslint-plugin-node": "^11.1.0",
49
53
  "eslint-plugin-prettier": "^4.0.0",
50
- "eslint-plugin-unused-imports": "^2.0.0",
54
+ "eslint-plugin-unused-imports": "^4.3.0",
51
55
  "is-ci": "^3.0.1",
52
- "jest": "^27.5.1",
53
- "jest-environment-node": "^27.5.1",
54
- "jest-extended": "^2.0.0",
56
+ "jest": "^29.7.0",
57
+ "jest-environment-node": "^29.7.0",
58
+ "jest-extended": "^7.0.0",
55
59
  "jest-html-reporters": "^3.0.6",
56
- "jest-mock-extended": "^2.0.5",
60
+ "jest-mock-extended": "^4.0.0",
57
61
  "jest-summary-reporter": "^0.0.2",
58
62
  "prettier": "^2.6.2",
59
- "ts-jest": "^27.1.4",
60
- "ts-node": "^10.7.0",
61
- "typescript": "~4.0.0"
63
+ "ts-jest": "^29.4.6",
64
+ "ts-node": "^10.9.2",
65
+ "typescript": "^5.5.0"
62
66
  },
63
- "packageManager": "yarn@3.2.0",
67
+ "packageManager": "pnpm@9.15.4",
64
68
  "dependencies": {
65
- "@typescript-eslint/typescript-estree": "^5.30.3"
69
+ "@typescript-eslint/typescript-estree": "^8.0.0"
70
+ },
71
+ "engines": {
72
+ "node": ">=24"
73
+ },
74
+ "resolutions": {
75
+ "json5": "^2.2.3",
76
+ "socks": "^2.8.3"
77
+ },
78
+ "pnpm": {
79
+ "nodeLinker": "node-modules"
66
80
  }
67
81
  }