@peerbit/time 1.0.4 → 2.0.0

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.
@@ -1,23 +1,21 @@
1
1
  export declare class TimeoutError extends Error {
2
2
  constructor(message: string);
3
3
  }
4
+ export declare class AbortError extends Error {
5
+ constructor();
6
+ }
4
7
  export declare const delay: (ms: number, options?: {
5
- stopperCallback?: ((stopper: () => void) => void) | undefined;
6
- } | undefined) => Promise<void>;
7
- export declare const waitFor: <T>(fn: () => T, options?: {
8
- timeout: number;
9
- stopperCallback?: ((stopper: () => void) => void) | undefined;
10
- delayInterval: number;
11
- timeoutMessage?: string | undefined;
8
+ signal?: AbortSignal;
9
+ }) => Promise<void>;
10
+ export declare const waitFor: <T>(fn: () => T | Promise<T>, options?: {
11
+ timeout?: number;
12
+ signal?: AbortSignal;
13
+ delayInterval?: number;
14
+ timeoutMessage?: string;
12
15
  }) => Promise<T | undefined>;
13
16
  export declare const waitForResolved: <T>(fn: () => T | Promise<T>, options?: {
14
- timeout: number;
15
- delayInterval: number;
17
+ timeout?: number;
18
+ signal?: AbortSignal;
19
+ delayInterval?: number;
16
20
  timeoutMessage?: string;
17
21
  }) => Promise<T | undefined>;
18
- export declare const waitForAsync: <T>(fn: () => Promise<T>, options?: {
19
- timeout: number;
20
- stopperCallback?: ((stopper: () => void) => void) | undefined;
21
- delayInterval: number;
22
- timeoutMessage?: string | undefined;
23
- }) => Promise<T | undefined>;
package/lib/esm/index.js CHANGED
@@ -3,78 +3,73 @@ export class TimeoutError extends Error {
3
3
  super(message);
4
4
  }
5
5
  }
6
+ export class AbortError extends Error {
7
+ constructor() {
8
+ super();
9
+ }
10
+ }
6
11
  export const delay = (ms, options) => {
7
- return new Promise((res) => {
8
- const timer = setTimeout(res, ms);
9
- if (options?.stopperCallback)
10
- options?.stopperCallback(() => {
11
- clearTimeout(timer);
12
- res();
13
- });
12
+ return new Promise((res, rej) => {
13
+ function handleAbort() {
14
+ clearTimeout(timer);
15
+ rej(new AbortError());
16
+ }
17
+ options?.signal?.addEventListener("abort", handleAbort);
18
+ const timer = setTimeout(() => {
19
+ options?.signal?.removeEventListener("abort", handleAbort);
20
+ res();
21
+ }, ms);
14
22
  });
15
23
  };
16
- export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
24
+ export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval: 100 }) => {
25
+ const delayInterval = options.delayInterval || 100;
26
+ const timeout = options.timeout || 10 * 1000;
17
27
  const startTime = +new Date();
18
28
  let stop = false;
19
- if (options.stopperCallback) {
20
- const stopper = () => {
21
- stop = true;
22
- };
23
- options.stopperCallback(stopper);
24
- }
25
- while (+new Date() - startTime < options.timeout) {
26
- if (stop) {
27
- return;
28
- }
29
- const result = fn();
29
+ const handleAbort = () => {
30
+ stop = true;
31
+ options.signal?.removeEventListener("abort", handleAbort);
32
+ };
33
+ options.signal?.addEventListener("abort", handleAbort);
34
+ while (!stop && +new Date() - startTime < timeout) {
35
+ const result = await fn();
30
36
  if (result) {
37
+ options.signal?.removeEventListener("abort", handleAbort);
31
38
  return result;
32
39
  }
33
- await delay(options.delayInterval, options);
40
+ await delay(delayInterval, options);
34
41
  }
35
42
  throw new TimeoutError(options.timeoutMessage
36
43
  ? "Timed out: " + options.timeoutMessage
37
44
  : "Timed out");
38
45
  };
39
46
  export const waitForResolved = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
47
+ const delayInterval = options.delayInterval || 50;
48
+ const timeout = options.timeout || 10 * 1000;
40
49
  const startTime = +new Date();
41
- const stop = false;
50
+ let stop = false;
42
51
  let lastError;
43
- while (+new Date() - startTime < options.timeout) {
44
- if (stop) {
45
- return;
46
- }
52
+ const handleAbort = () => {
53
+ stop = true;
54
+ options.signal?.removeEventListener("abort", handleAbort);
55
+ };
56
+ options.signal?.addEventListener("abort", handleAbort);
57
+ while (!stop && +new Date() - startTime < timeout) {
47
58
  try {
48
- return await fn();
59
+ const result = await fn();
60
+ options.signal?.removeEventListener("abort", handleAbort);
61
+ return result;
49
62
  }
50
63
  catch (error) {
51
- lastError = error;
64
+ if (error instanceof AbortError === false) {
65
+ lastError = error;
66
+ }
67
+ else {
68
+ throw error;
69
+ }
52
70
  }
53
- await delay(options.delayInterval);
71
+ await delay(delayInterval, options);
54
72
  }
55
73
  throw lastError;
56
74
  };
57
- export const waitForAsync = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
58
- const startTime = +new Date();
59
- let stop = false;
60
- if (options.stopperCallback) {
61
- const stopper = () => {
62
- stop = true;
63
- };
64
- options.stopperCallback(stopper);
65
- }
66
- while (+new Date() - startTime < options.timeout) {
67
- if (stop) {
68
- return;
69
- }
70
- const result = await fn();
71
- if (result) {
72
- return result;
73
- }
74
- await delay(options.delayInterval, options);
75
- }
76
- throw new TimeoutError(options.timeoutMessage
77
- ? "Timed out: " + options.timeoutMessage
78
- : "Timed out");
79
- };
80
75
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;IACtC,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;IAChB,CAAC;CACD;AACD,MAAM,CAAC,MAAM,KAAK,GAAG,CACpB,EAAU,EACV,OAA6D,EAC5D,EAAE;IACH,OAAO,IAAI,OAAO,CAAO,CAAC,GAAG,EAAE,EAAE;QAChC,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QAClC,IAAI,OAAO,EAAE,eAAe;YAC3B,OAAO,EAAE,eAAe,CAAC,GAAG,EAAE;gBAC7B,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,GAAG,EAAE,CAAC;YACP,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,OAAO,GAAG,KAA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CAAC;YAC1B,OAAO,CAAC,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;YAC1D,OAAO,MAAM,CAAC;QACf,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YACrB,IAAI,KAAK,YAAY,UAAU,KAAK,KAAK,EAAE,CAAC;gBAC3C,SAAS,GAAG,KAAK,CAAC;YACnB,CAAC;iBAAM,CAAC;gBACP,MAAM,KAAK,CAAC;YACb,CAAC;QACF,CAAC;QACD,MAAM,KAAK,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;IACrC,CAAC;IAED,MAAM,SAAS,CAAC;AACjB,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerbit/time",
3
- "version": "1.0.4",
3
+ "version": "2.0.0",
4
4
  "description": "Utility functions for time",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -29,5 +29,5 @@
29
29
  },
30
30
  "author": "dao.xyz",
31
31
  "license": "MIT",
32
- "gitHead": "6263a10a6236346d1c45d02dc0bf18eed1dc2995"
32
+ "gitHead": "c48cb37d237a25b0bcc849482b43f6941d53e3d5"
33
33
  }
package/src/index.ts CHANGED
@@ -3,46 +3,54 @@ export class TimeoutError extends Error {
3
3
  super(message);
4
4
  }
5
5
  }
6
- export const delay = (
7
- ms: number,
8
- options?: { stopperCallback?: (stopper: () => void) => void }
9
- ) => {
10
- return new Promise<void>((res) => {
11
- const timer = setTimeout(res, ms);
12
- if (options?.stopperCallback)
13
- options?.stopperCallback(() => {
14
- clearTimeout(timer);
15
- res();
16
- });
6
+
7
+ export class AbortError extends Error {
8
+ constructor() {
9
+ super();
10
+ }
11
+ }
12
+ export const delay = (ms: number, options?: { signal?: AbortSignal }) => {
13
+ return new Promise<void>((res, rej) => {
14
+ function handleAbort() {
15
+ clearTimeout(timer);
16
+ rej(new AbortError());
17
+ }
18
+ options?.signal?.addEventListener("abort", handleAbort);
19
+ const timer = setTimeout(() => {
20
+ options?.signal?.removeEventListener("abort", handleAbort);
21
+ res();
22
+ }, ms);
17
23
  });
18
24
  };
19
25
 
20
26
  export const waitFor = async <T>(
21
- fn: () => T,
27
+ fn: () => T | Promise<T>,
22
28
  options: {
23
- timeout: number;
24
- stopperCallback?: (stopper: () => void) => void;
25
- delayInterval: number;
29
+ timeout?: number;
30
+ signal?: AbortSignal;
31
+ delayInterval?: number;
26
32
  timeoutMessage?: string;
27
- } = { timeout: 10 * 1000, delayInterval: 50 }
33
+ } = { timeout: 10 * 1000, delayInterval: 100 }
28
34
  ): Promise<T | undefined> => {
35
+ const delayInterval = options.delayInterval || 100;
36
+ const timeout = options.timeout || 10 * 1000;
29
37
  const startTime = +new Date();
30
38
  let stop = false;
31
- if (options.stopperCallback) {
32
- const stopper = () => {
33
- stop = true;
34
- };
35
- options.stopperCallback(stopper);
36
- }
37
- while (+new Date() - startTime < options.timeout) {
38
- if (stop) {
39
- return;
40
- }
41
- const result = fn();
39
+
40
+ const handleAbort = () => {
41
+ stop = true;
42
+ options.signal?.removeEventListener("abort", handleAbort);
43
+ };
44
+
45
+ options.signal?.addEventListener("abort", handleAbort);
46
+ while (!stop && +new Date() - startTime < timeout) {
47
+ const result = await fn();
42
48
  if (result) {
49
+ options.signal?.removeEventListener("abort", handleAbort);
43
50
  return result;
44
51
  }
45
- await delay(options.delayInterval, options);
52
+
53
+ await delay(delayInterval, options);
46
54
  }
47
55
  throw new TimeoutError(
48
56
  options.timeoutMessage
@@ -54,58 +62,39 @@ export const waitFor = async <T>(
54
62
  export const waitForResolved = async <T>(
55
63
  fn: () => T | Promise<T>,
56
64
  options: {
57
- timeout: number;
58
- delayInterval: number;
65
+ timeout?: number;
66
+ signal?: AbortSignal;
67
+ delayInterval?: number;
59
68
  timeoutMessage?: string;
60
69
  } = { timeout: 10 * 1000, delayInterval: 50 }
61
70
  ): Promise<T | undefined> => {
71
+ const delayInterval = options.delayInterval || 50;
72
+ const timeout = options.timeout || 10 * 1000;
73
+
62
74
  const startTime = +new Date();
63
- const stop = false;
75
+ let stop = false;
64
76
  let lastError: Error | undefined;
65
- while (+new Date() - startTime < options.timeout) {
66
- if (stop) {
67
- return;
68
- }
77
+
78
+ const handleAbort = () => {
79
+ stop = true;
80
+ options.signal?.removeEventListener("abort", handleAbort);
81
+ };
82
+
83
+ options.signal?.addEventListener("abort", handleAbort);
84
+ while (!stop && +new Date() - startTime < timeout) {
69
85
  try {
70
- return await fn();
86
+ const result = await fn();
87
+ options.signal?.removeEventListener("abort", handleAbort);
88
+ return result;
71
89
  } catch (error: any) {
72
- lastError = error;
90
+ if (error instanceof AbortError === false) {
91
+ lastError = error;
92
+ } else {
93
+ throw error;
94
+ }
73
95
  }
74
- await delay(options.delayInterval);
96
+ await delay(delayInterval, options);
75
97
  }
76
- throw lastError;
77
- };
78
98
 
79
- export const waitForAsync = async <T>(
80
- fn: () => Promise<T>,
81
- options: {
82
- timeout: number;
83
- stopperCallback?: (stopper: () => void) => void;
84
- delayInterval: number;
85
- timeoutMessage?: string;
86
- } = { timeout: 10 * 1000, delayInterval: 50 }
87
- ): Promise<T | undefined> => {
88
- const startTime = +new Date();
89
- let stop = false;
90
- if (options.stopperCallback) {
91
- const stopper = () => {
92
- stop = true;
93
- };
94
- options.stopperCallback(stopper);
95
- }
96
- while (+new Date() - startTime < options.timeout) {
97
- if (stop) {
98
- return;
99
- }
100
- const result = await fn();
101
- if (result) {
102
- return result;
103
- }
104
- await delay(options.delayInterval, options);
105
- }
106
- throw new TimeoutError(
107
- options.timeoutMessage
108
- ? "Timed out: " + options.timeoutMessage
109
- : "Timed out"
110
- );
99
+ throw lastError;
111
100
  };