timeout-flow 0.0.19 → 0.0.20

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/types/retry.d.ts CHANGED
@@ -1,31 +1,70 @@
1
- /**
2
- * Retries a promise-returning function with delays.
3
- *
4
- * @param {(attemptIndex: number) => Promise<any>} fn - async function
5
- * @param {Object} [options]
6
- * @param {number} [options.attempts=3] - total attempts (including the first)
7
- * @param {string|number} [options.delay='500ms'] - base delay between retries
8
- * @param {boolean} [options.backoff=false] - exponential backoff if true
9
- * @param {number} [options.factor=2] - backoff multiplier (only if backoff=true)
10
- * @param {string|number} [options.maxDelay] - clamp delay to this max
11
- * @param {boolean|'full'|'equal'|'decorrelated'} [options.jitter=false] - jitter strategy
12
- * @param {AbortSignal} [options.signal] - optional AbortSignal to cancel retries
13
- * @param {() => number} [options.random=Math.random] - injectable RNG for tests
14
- * @param {(err: any, attempt: number) => boolean | Promise<boolean>} [options.shouldRetry]
15
- * Predicate deciding whether to retry after a failure.
16
- * @param {(err: any, attempt: number, delayMs: number) => void | Promise<void>} [options.onRetry]
17
- * Hook invoked after deciding to retry and before waiting.
18
- * @returns {Promise<any>}
19
- */
20
- export function retry(fn: (attemptIndex: number) => Promise<any>, { attempts, delay, backoff, factor, maxDelay, jitter, signal, random, shouldRetry, onRetry, }?: {
21
- attempts?: number | undefined;
22
- delay?: string | number | undefined;
23
- backoff?: boolean | undefined;
24
- factor?: number | undefined;
25
- maxDelay?: string | number | undefined;
26
- jitter?: boolean | "full" | "equal" | "decorrelated" | undefined;
27
- signal?: AbortSignal | undefined;
28
- random?: (() => number) | undefined;
29
- shouldRetry?: ((err: any, attempt: number) => boolean | Promise<boolean>) | undefined;
30
- onRetry?: ((err: any, attempt: number, delayMs: number) => void | Promise<void>) | undefined;
31
- }): Promise<any>;
1
+ /**
2
+ * Retries a (possibly async) function with delays between attempts.
3
+ *
4
+ * Attempt numbers are 0-based everywhere (`fn`, hooks, predicates).
5
+ *
6
+ * Outcome:
7
+ * - Resolves with the first successful result.
8
+ * - When retries stop (attempts exhausted, `timeout` reached, or
9
+ * `shouldRetry` returned false) it rejects with the last failure:
10
+ * the last thrown error as-is, or, when the last attempt resolved with a
11
+ * result rejected by `isSuccess`, a `RetryError` (`name: 'RetryError'`)
12
+ * carrying `.result` and `.attempts`.
13
+ * - On abort it rejects with `signal.reason` (an AbortError by default).
14
+ *
15
+ * @template T
16
+ * @param {(attempt: number) => T | Promise<T>} fn - sync or async function
17
+ * @param {Object} [options]
18
+ * @param {number} [options.attempts=3] - total attempts including the first (`Infinity` allowed)
19
+ * @param {string|number} [options.delay='500ms'] - base delay between attempts
20
+ * @param {boolean|'fixed'|'linear'|'exponential'} [options.backoff=false]
21
+ * Delay growth per retry n (0-based): fixed (false) = delay,
22
+ * linear = delay*(n+1), exponential (true) = delay*factor^n.
23
+ * @param {number} [options.factor=2] - exponential multiplier
24
+ * @param {string|number} [options.maxDelay] - cap for each computed delay
25
+ * @param {boolean|number|'full'|'equal'|'decorrelated'} [options.jitter=false] - jitter strategy.
26
+ * A number j in [0, 1] applies proportional �j jitter:
27
+ * delay * (1 + (random()*2 - 1) * j), after the maxDelay cap and then
28
+ * re-capped by maxDelay, clamped to >= 0 and rounded. Other numbers
29
+ * (or NaN) reject with a RangeError before any attempt.
30
+ * `delayFirst` is never jittered.
31
+ * @param {boolean|string|number} [options.delayFirst=false]
32
+ * Wait before the first attempt (`true` = `delay`, or an explicit duration).
33
+ * @param {string|number} [options.timeout] - overall deadline measured from the call.
34
+ * A retry is skipped when its wait would end past the deadline; the first
35
+ * attempt always runs and an in-flight attempt is never interrupted.
36
+ * @param {AbortSignal} [options.signal] - cancels waiting and further attempts
37
+ * @param {(result: T, attempt: number) => boolean | Promise<boolean>} [options.isSuccess]
38
+ * Treat a resolved result as a failure (and retry) when this returns false.
39
+ * @param {(err: any, attempt: number) => boolean | Promise<boolean>} [options.shouldRetry]
40
+ * Return false to stop retrying after a failure.
41
+ * @param {(attempt: number, elapsedMs: number) => void | Promise<void>} [options.onAttempt]
42
+ * Called before every attempt.
43
+ * @param {(err: any, attempt: number, delayMs: number) => void | Promise<void>} [options.onRetry]
44
+ * Called after deciding to retry and before waiting.
45
+ * @param {(ms: number, options: { signal?: AbortSignal }) => Promise<void>} [options.sleep]
46
+ * Injectable sleep (e.g. for fake-timer tests).
47
+ * @param {() => number} [options.now] - injectable clock used for `timeout`
48
+ * @param {() => number} [options.random=Math.random] - injectable RNG for tests
49
+ * @returns {Promise<T>}
50
+ */
51
+ export function retry<T>(fn: (attempt: number) => T | Promise<T>, { attempts, delay, backoff, factor, maxDelay, jitter, delayFirst, timeout, signal, isSuccess, shouldRetry, onAttempt, onRetry, sleep, now, random, }?: {
52
+ attempts?: number | undefined;
53
+ delay?: string | number | undefined;
54
+ backoff?: boolean | "fixed" | "exponential" | "linear" | undefined;
55
+ factor?: number | undefined;
56
+ maxDelay?: string | number | undefined;
57
+ jitter?: number | boolean | "full" | "equal" | "decorrelated" | undefined;
58
+ delayFirst?: string | number | boolean | undefined;
59
+ timeout?: string | number | undefined;
60
+ signal?: AbortSignal | undefined;
61
+ isSuccess?: ((result: T, attempt: number) => boolean | Promise<boolean>) | undefined;
62
+ shouldRetry?: ((err: any, attempt: number) => boolean | Promise<boolean>) | undefined;
63
+ onAttempt?: ((attempt: number, elapsedMs: number) => void | Promise<void>) | undefined;
64
+ onRetry?: ((err: any, attempt: number, delayMs: number) => void | Promise<void>) | undefined;
65
+ sleep?: ((ms: number, options: {
66
+ signal?: AbortSignal;
67
+ }) => Promise<void>) | undefined;
68
+ now?: (() => number) | undefined;
69
+ random?: (() => number) | undefined;
70
+ }): Promise<T>;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Normalizes a duration to a setTimeout-safe delay in ms.
3
+ * - Strings are parsed ("500ms", "1s", "1.5m", "1h"); invalid strings throw.
4
+ * - Numbers behave like setTimeout: NaN and negative values clamp to 0.
5
+ * - Values above setTimeout's 32-bit limit (including Infinity) are capped at
6
+ * 2147483647 ms (~24.8 days) instead of overflowing and firing immediately.
7
+ *
8
+ * @param {string|number|null|undefined} duration
9
+ * @returns {number}
10
+ */
11
+ export function toDelay(duration: string | number | null | undefined): number;
12
+ /**
13
+ * Promise-based delay: `await sleep('500ms')`.
14
+ *
15
+ * - Accepts the same durations as the rest of TimeoutFlow ("500ms", "1s", 250).
16
+ * - Numbers behave like setTimeout: NaN / negative become 0; huge values
17
+ * (including Infinity) are capped at ~24.8 days rather than firing at once.
18
+ * - Invalid duration strings reject with an Error (never throw synchronously).
19
+ * - With `signal`, rejects with `signal.reason` (an AbortError by default)
20
+ * and clears its timer; an already-aborted signal rejects immediately.
21
+ *
22
+ * @param {string|number} [duration=0]
23
+ * @param {Object} [options]
24
+ * @param {AbortSignal} [options.signal]
25
+ * @returns {Promise<void>}
26
+ */
27
+ export function sleep(duration?: string | number, { signal }?: {
28
+ signal?: AbortSignal | undefined;
29
+ }): Promise<void>;
@@ -1,17 +1,28 @@
1
- /**
2
- * Waits for a condition to become true, polling at intervals.
3
- *
4
- * @param {Function} condition - returns truthy when satisfied
5
- * @param {Object} [options] - optional polling control
6
- * @param {string|number} [options.interval='250ms'] - poll frequency
7
- * @param {string|number} [options.timeout] - max wait time
8
- * @param {boolean} [options.immediate=false] - If true, evaluate condition immediately
9
- * @param {AbortSignal} [options.signal] - Optional AbortSignal to cancel waiting
10
- * @returns {Promise<void>}
11
- */
12
- export function waitFor(condition: Function, { interval, timeout, immediate, signal }?: {
13
- interval?: string | number | undefined;
14
- timeout?: string | number | undefined;
15
- immediate?: boolean | undefined;
16
- signal?: AbortSignal | undefined;
17
- }): Promise<void>;
1
+ /**
2
+ * Waits for a condition to become truthy, polling at intervals.
3
+ *
4
+ * Timing model:
5
+ * - With `immediate: false` (default) the first check runs after `interval`.
6
+ * - The next check is scheduled `interval` after the previous one finishes,
7
+ * so async conditions never overlap.
8
+ * - `timeout` is measured from the call and enforced by its own timer, so it
9
+ * is not rounded up to the next poll. At the deadline a sync condition gets
10
+ * one final check (success at exactly `timeout` counts, like `>=`); an async
11
+ * check still pending at the deadline is abandoned and the promise rejects.
12
+ *
13
+ * @template T
14
+ * @param {() => T | Promise<T>} condition - sync or async; truthy when satisfied
15
+ * @param {Object} [options] - optional polling control
16
+ * @param {string|number} [options.interval='250ms'] - delay between checks
17
+ * @param {string|number} [options.timeout] - max wait time; rejects with an
18
+ * Error named 'TimeoutError' (message 'waitFor timed out')
19
+ * @param {boolean} [options.immediate=false] - If true, evaluate condition immediately
20
+ * @param {AbortSignal} [options.signal] - rejects with `signal.reason` on abort
21
+ * @returns {Promise<T>} resolves with the condition's truthy value
22
+ */
23
+ export function waitFor<T>(condition: () => T | Promise<T>, { interval, timeout, immediate, signal }?: {
24
+ interval?: string | number | undefined;
25
+ timeout?: string | number | undefined;
26
+ immediate?: boolean | undefined;
27
+ signal?: AbortSignal | undefined;
28
+ }): Promise<T>;