timeout-flow 0.0.20 → 0.1.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,19 +1,19 @@
1
- /**
2
- * Waits for a condition to return truthy, polling each frame.
3
- *
4
- * Timing model:
5
- * - Evaluates `condition()` once per animation frame until it returns truthy.
6
- * - If `timeout` is provided, timeout is measured in RAF time (frame timestamps).
7
- *
8
- * @param {Function} condition - Function returning truthy when complete
9
- * @param {Object} [options]
10
- * @param {string|number} [options.timeout] - Optional max time to wait
11
- * @param {AbortSignal} [options.signal] - Optional AbortSignal; rejects with `signal.reason`
12
- * @param {boolean} [options.immediate=false] - If true, evaluate condition immediately before first frame
13
- * @returns {Promise<void>} Resolves when condition is met, rejects on timeout/abort
14
- */
15
- export function waitForRaf(condition: Function, { timeout, signal, immediate }?: {
16
- timeout?: string | number | undefined;
17
- signal?: AbortSignal | undefined;
18
- immediate?: boolean | undefined;
19
- }): Promise<void>;
1
+ /**
2
+ * Waits for a condition to return truthy, polling each frame.
3
+ *
4
+ * Timing model:
5
+ * - Evaluates `condition()` once per animation frame until it returns truthy.
6
+ * - If `timeout` is provided, timeout is measured in RAF time (frame timestamps).
7
+ *
8
+ * @param {Function} condition - Function returning truthy when complete
9
+ * @param {Object} [options]
10
+ * @param {string|number} [options.timeout] - Optional max time to wait
11
+ * @param {AbortSignal} [options.signal] - Optional AbortSignal; rejects with `signal.reason`
12
+ * @param {boolean} [options.immediate=false] - If true, evaluate condition immediately before first frame
13
+ * @returns {Promise<void>} Resolves when condition is met, rejects on timeout/abort
14
+ */
15
+ export function waitForRaf(condition: Function, { timeout, signal, immediate }?: {
16
+ timeout?: string | number | undefined;
17
+ signal?: AbortSignal | undefined;
18
+ immediate?: boolean | undefined;
19
+ }): Promise<void>;
@@ -1,18 +1,3 @@
1
- /**
2
- * Resolves flexible (fn, delay) or (delay, fn) inputs.
3
- *
4
- * NOTE: This helper is intentionally strict:
5
- * - It requires an explicit delay (2-arg form).
6
- * - Use `resolveDelayFnOptions()` for overloads that allow `(fn, options)` or default delays.
7
- *
8
- * @param {Function|string|number} a - function or duration
9
- * @param {Function|string|number} b - function or duration
10
- * @returns {{ fn: Function, delay: number }}
11
- */
12
- export function resolveDelayAndFn(a: Function | string | number, b: Function | string | number): {
13
- fn: Function;
14
- delay: number;
15
- };
16
1
  /**
17
2
  * Resolves flexible overloads and returns `{ fn, delay, options }`.
18
3
  *
@@ -36,3 +21,4 @@ export function resolveDelayFnOptions<O extends Record<string, any>>(a: Function
36
21
  delay: number;
37
22
  options: O;
38
23
  };
24
+ export function isPlainOptions(v: any): boolean;
package/types/retry.d.ts CHANGED
@@ -1,70 +1,70 @@
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>;
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>;
package/types/sleep.d.ts CHANGED
@@ -1,29 +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
+ /**
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,28 +1,28 @@
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>;
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>;
@@ -1,58 +0,0 @@
1
- @echo off
2
- setlocal EnableExtensions EnableDelayedExpansion
3
-
4
- REM ===================== CONFIG =====================
5
- set "EXT=.js"
6
- set "OUT=context.txt"
7
- REM ==================================================
8
-
9
- REM Switch to UTF-8 (optional but nice for code)
10
- chcp 65001 >nul
11
-
12
- REM Root directory (absolute)
13
- set "ROOTDIR=%CD%"
14
-
15
- REM Clear old output if it exists
16
- if exist "%OUT%" del "%OUT%"
17
-
18
- REM Count matching files (recursive)
19
- set "COUNT=0"
20
- for /r "%ROOTDIR%" %%F in (*%EXT%) do set /a COUNT+=1
21
-
22
- if "%COUNT%"=="0" (
23
- echo No files with extension "%EXT%" found under "%ROOTDIR%".
24
- goto :eof
25
- )
26
-
27
- echo Building "%OUT%" from %COUNT% file(s) with extension "%EXT%"...
28
- echo.
29
-
30
- REM Preamble
31
- >>"%OUT%" echo ===== Context dump for folder: %ROOTDIR% =====
32
- >>"%OUT%" echo Extension: %EXT%
33
- >>"%OUT%" echo Generated on: %DATE% %TIME%
34
- >>"%OUT%" echo.
35
-
36
- REM Iterate recursively in a stable sorted order via DIR
37
- for /f "delims=" %%F in ('dir /b /s /a-d "*%EXT%"') do (
38
- REM Compute relative path
39
- set "FULL=%%F"
40
- set "REL=!FULL:%ROOTDIR%\=!"
41
-
42
- echo Adding !REL!
43
-
44
- >>"%OUT%" echo.
45
- >>"%OUT%" echo ===== FILE: !REL! =====
46
- >>"%OUT%" echo.
47
-
48
- REM Concatenate file contents
49
- type "%%F" >>"%OUT%"
50
-
51
- >>"%OUT%" echo.
52
- >>"%OUT%" echo ===== END FILE: !REL! =====
53
- >>"%OUT%" echo.
54
- )
55
-
56
- echo.
57
- echo Done. Output: "%OUT%"
58
- endlocal