@sohanemon/utils 5.0.9 → 5.1.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.
@@ -1,4 +1,5 @@
1
1
  export * from './cookie';
2
2
  export * from './object';
3
3
  export * from './shield';
4
+ export * from './poll';
4
5
  export * from './utils';
@@ -1,4 +1,5 @@
1
1
  export * from './cookie';
2
2
  export * from './object';
3
3
  export * from './shield';
4
+ export * from './poll';
4
5
  export * from './utils';
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Repeatedly polls an async `cond` function UNTIL it returns a TRUTHY value,
3
+ * or until the operation times out or is aborted.
4
+ *
5
+ * Designed for waiting on async jobs, external state, or delayed availability.
6
+ *
7
+ * @template T The type of the successful result.
8
+ *
9
+ * @param cond
10
+ * A function returning a Promise that resolves to:
11
+ * - a truthy value `T` → stop polling and return it
12
+ * - falsy/null/undefined → continue polling
13
+ *
14
+ * @param options
15
+ * Configuration options:
16
+ * - `interval` (number) — Time between polls in ms (default: 5000 ms)
17
+ * - `timeout` (number) — Max total duration before failing (default: 5 min)
18
+ * - `jitter` (boolean) — Add small random offset (±10%) to intervals to avoid sync bursts (default: true)
19
+ * - `signal` (AbortSignal) — Optional abort signal to cancel polling
20
+ *
21
+ * @returns
22
+ * Resolves with the truthy value `T` when successful.
23
+ * Throws `AbortError` if aborted
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const job = await poll(async () => {
28
+ * const status = await getJobStatus();
29
+ * return status === 'done' ? status : null;
30
+ * }, { interval: 3000, timeout: 60000 });
31
+ * ```
32
+ */
33
+ export declare function poll<T>(cond: () => Promise<T | null | false | undefined>, { interval, timeout, jitter, signal, }?: Partial<{
34
+ interval: number;
35
+ timeout: number;
36
+ signal: AbortSignal;
37
+ jitter: boolean;
38
+ }>): Promise<T>;
@@ -0,0 +1,66 @@
1
+ import { sleep } from './utils';
2
+ /**
3
+ * Repeatedly polls an async `cond` function UNTIL it returns a TRUTHY value,
4
+ * or until the operation times out or is aborted.
5
+ *
6
+ * Designed for waiting on async jobs, external state, or delayed availability.
7
+ *
8
+ * @template T The type of the successful result.
9
+ *
10
+ * @param cond
11
+ * A function returning a Promise that resolves to:
12
+ * - a truthy value `T` → stop polling and return it
13
+ * - falsy/null/undefined → continue polling
14
+ *
15
+ * @param options
16
+ * Configuration options:
17
+ * - `interval` (number) — Time between polls in ms (default: 5000 ms)
18
+ * - `timeout` (number) — Max total duration before failing (default: 5 min)
19
+ * - `jitter` (boolean) — Add small random offset (±10%) to intervals to avoid sync bursts (default: true)
20
+ * - `signal` (AbortSignal) — Optional abort signal to cancel polling
21
+ *
22
+ * @returns
23
+ * Resolves with the truthy value `T` when successful.
24
+ * Throws `AbortError` if aborted
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const job = await poll(async () => {
29
+ * const status = await getJobStatus();
30
+ * return status === 'done' ? status : null;
31
+ * }, { interval: 3000, timeout: 60000 });
32
+ * ```
33
+ */
34
+ export async function poll(cond, { interval = 5000, timeout = 5 * 60 * 1000, jitter = true, signal, } = {}) {
35
+ const start = Date.now();
36
+ let aborted = signal?.aborted ?? false;
37
+ // fast listener (avoids repeated property lookups)
38
+ const abortListener = () => {
39
+ aborted = true;
40
+ };
41
+ signal?.addEventListener('abort', abortListener, { once: true });
42
+ try {
43
+ for (let attempt = 0;; attempt++) {
44
+ // fast exit check
45
+ if (aborted)
46
+ throw new Error('Polling aborted');
47
+ const result = await cond();
48
+ if (result)
49
+ return result;
50
+ const elapsed = Date.now() - start;
51
+ if (elapsed >= timeout)
52
+ throw new Error('Polling timed out', {
53
+ cause: `Polling timed out after ${timeout}ms`,
54
+ });
55
+ // add jitter (±10%) for anti-sync
56
+ const delay = jitter
57
+ ? interval + (Math.random() - 0.5) * interval * 0.2
58
+ : interval;
59
+ await sleep(delay, signal);
60
+ }
61
+ }
62
+ finally {
63
+ // cleanup to avoid leaks
64
+ signal?.removeEventListener('abort', abortListener);
65
+ }
66
+ }
@@ -1,4 +1,3 @@
1
- import { isSSR } from './utils';
2
1
  export function shield(operation) {
3
2
  if (operation instanceof Promise) {
4
3
  return operation
@@ -10,12 +9,7 @@ export function shield(operation) {
10
9
  return [null, data];
11
10
  }
12
11
  catch (error) {
13
- if (isSSR) {
14
- console.info(`🛡 [shield] ${operation.name} failed →`, error);
15
- }
16
- else {
17
- console.info(`%c🛡 [shield] %c${operation.name}`, 'color:#f87171;font-weight:bold;', 'color:#aaa;', error);
18
- }
12
+ console.log(`\x1b[31m🛡 [shield]\x1b[0m ${operation.name} failed →`, error);
19
13
  return [error, null];
20
14
  }
21
15
  }
@@ -86,10 +86,13 @@ export declare const svgToBase64: (str: string) => string;
86
86
  /**
87
87
  * Pauses execution for the specified time.
88
88
  *
89
+ * `signal` allows cancelling the sleep via AbortSignal.
90
+ *
89
91
  * @param time - Time in milliseconds to sleep (default is 1000ms)
90
- * @returns - A Promise that resolves after the specified time
92
+ * @param signal - Optional AbortSignal to cancel the sleep early
93
+ * @returns - A Promise that resolves after the specified time or when aborted
91
94
  */
92
- export declare const sleep: (time?: number) => Promise<unknown>;
95
+ export declare const sleep: (time?: number, signal?: AbortSignal) => Promise<void>;
93
96
  type DebouncedFunction<F extends (...args: any[]) => any> = {
94
97
  (...args: Parameters<F>): ReturnType<F> | undefined;
95
98
  readonly isPending: boolean;
@@ -158,10 +158,32 @@ export const svgToBase64 = (str) => isSSR ? Buffer.from(str).toString('base64')
158
158
  /**
159
159
  * Pauses execution for the specified time.
160
160
  *
161
+ * `signal` allows cancelling the sleep via AbortSignal.
162
+ *
161
163
  * @param time - Time in milliseconds to sleep (default is 1000ms)
162
- * @returns - A Promise that resolves after the specified time
164
+ * @param signal - Optional AbortSignal to cancel the sleep early
165
+ * @returns - A Promise that resolves after the specified time or when aborted
163
166
  */
164
- export const sleep = (time = 1000) => new Promise((resolve) => setTimeout(resolve, time));
167
+ export const sleep = (time = 1000, signal) => new Promise((resolve) => {
168
+ if (signal?.aborted)
169
+ return resolve();
170
+ const id = setTimeout(() => {
171
+ cleanup();
172
+ resolve();
173
+ }, time);
174
+ function onAbort() {
175
+ clearTimeout(id);
176
+ cleanup();
177
+ resolve();
178
+ }
179
+ function cleanup() {
180
+ signal?.removeEventListener('abort', onAbort);
181
+ }
182
+ // only add listener if a signal was supplied
183
+ if (signal) {
184
+ signal.addEventListener('abort', onAbort, { once: true });
185
+ }
186
+ });
165
187
  /**
166
188
  * Creates a debounced function that delays invoking the provided function until
167
189
  * after the specified `wait` time has elapsed since the last invocation.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "5.0.9",
3
+ "version": "5.1.1",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",