timeout-flow 0.0.16 → 0.0.18

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.
Files changed (46) hide show
  1. package/README.md +239 -144
  2. package/dist/timeout-flow.min.js +1 -6
  3. package/package.json +2 -1
  4. package/src/AfterTimer.js +64 -3
  5. package/src/EveryTimer.js +139 -18
  6. package/src/TimeoutFlow.js +291 -118
  7. package/src/TimerBase.js +81 -23
  8. package/src/abort.js +75 -0
  9. package/src/after.js +20 -71
  10. package/src/context-js.bat +58 -0
  11. package/src/context.txt +2604 -0
  12. package/src/debounce.js +90 -24
  13. package/src/every.js +19 -105
  14. package/src/index.js +7 -2
  15. package/src/now.js +23 -0
  16. package/src/parseDuration.js +22 -20
  17. package/src/raf/afterRaf.js +198 -66
  18. package/src/raf/debounceRaf.js +108 -26
  19. package/src/raf/everyRaf.js +278 -67
  20. package/src/raf/index.js +26 -6
  21. package/src/raf/throttleRaf.js +175 -31
  22. package/src/raf/waitForRaf.js +82 -14
  23. package/src/resolveDelayAndFn.js +69 -7
  24. package/src/retry.js +152 -12
  25. package/src/throttle.js +123 -24
  26. package/src/waitFor.js +68 -24
  27. package/types/AfterTimer.d.ts +8 -2
  28. package/types/EveryTimer.d.ts +21 -4
  29. package/types/TimeoutFlow.d.ts +6 -18
  30. package/types/TimerBase.d.ts +15 -4
  31. package/types/abort.d.ts +31 -0
  32. package/types/after.d.ts +22 -22
  33. package/types/debounce.d.ts +28 -6
  34. package/types/every.d.ts +20 -28
  35. package/types/index.d.ts +1 -1
  36. package/types/now.d.ts +1 -0
  37. package/types/raf/afterRaf.d.ts +35 -5
  38. package/types/raf/debounceRaf.d.ts +27 -6
  39. package/types/raf/everyRaf.d.ts +39 -9
  40. package/types/raf/index.d.ts +6 -5
  41. package/types/raf/throttleRaf.d.ts +33 -10
  42. package/types/raf/waitForRaf.d.ts +10 -4
  43. package/types/resolveDelayAndFn.d.ts +27 -0
  44. package/types/retry.d.ts +25 -8
  45. package/types/throttle.d.ts +33 -5
  46. package/types/waitFor.d.ts +11 -6
@@ -1,10 +1,31 @@
1
1
  /**
2
2
  * Debounces a function using requestAnimationFrame.
3
- * - If no duration is passed, triggers on the next frame.
4
- * - If duration is passed, waits until N ms of inactivity using frame-based timing.
5
3
  *
6
- * @param {string|number|Function} durationOrFn - Duration (e.g., "300ms") or fn directly
7
- * @param {Function} [callbackFn] - Callback (if duration is passed as first arg)
8
- * @returns {Function} debounced function
4
+ * Preferred:
5
+ * - debounceRaf(fn, [duration], [options]) // duration defaults to 0 (next frame)
6
+ *
7
+ * Also supported:
8
+ * - debounceRaf(duration, fn, [options])
9
+ * - debounceRaf(fn, [options])
10
+ *
11
+ * Timing model:
12
+ * - Waits until `ms` of inactivity have passed (measured using frame timestamps).
13
+ * - `ms = 0` means "run on the next animation frame" (still debounced).
14
+ *
15
+ * Supports:
16
+ * - `.cancel()` clears any pending invocation.
17
+ * - `.flush()` immediately invokes if pending.
18
+ * - Optional AbortSignal: abort cancels any pending invocation.
19
+ *
20
+ * @param {Function|string|number} a - function or duration
21
+ * @param {Function|string|number|Object} [b] - duration, fn, or options
22
+ * @param {Object} [c] - options (3-arg form)
23
+ * @param {AbortSignal} [c.signal] - AbortSignal to auto-cancel pending work
24
+ * @returns {((...args: any[]) => void) & { cancel: () => void, flush: () => void }}
9
25
  */
10
- export function debounceRaf(durationOrFn: string | number | Function, callbackFn?: Function): Function;
26
+ export function debounceRaf(a: Function | string | number, b?: Function | string | number | Object, c?: {
27
+ signal?: AbortSignal | undefined;
28
+ }): ((...args: any[]) => void) & {
29
+ cancel: () => void;
30
+ flush: () => void;
31
+ };
@@ -1,11 +1,41 @@
1
1
  /**
2
- * Repeats a function every N milliseconds using requestAnimationFrame
3
- * (instead of setTimeout/setInterval).
4
- *
5
- * @param {string|number} duration - Interval duration ("1s", "500ms", 500, etc.)
6
- * @param {Function} fn - Function to run
7
- * @param {number} [maxTimes=Infinity] - Max number of executions
8
- * @param {boolean} [runImmediately=false] - Whether to run immediately
9
- * @returns {Object} - Control methods and flags
2
+ * Repeats a function every N milliseconds using requestAnimationFrame.
3
+ *
4
+ * Preferred:
5
+ * - everyRaf(fn, duration, [options])
6
+ *
7
+ * Also supported (legacy):
8
+ * - everyRaf(duration, fn, [maxTimes], [runImmediately], [options])
9
+ *
10
+ * Timing model:
11
+ * - Counts down using frame deltas.
12
+ * - Paused time does NOT count (pause freezes remaining-to-next-tick).
13
+ * - On large frame gaps, may "catch up" by running multiple ticks in one frame,
14
+ * but will never exceed max.
15
+ *
16
+ * @param {Function|string|number} a
17
+ * @param {Function|string|number|Object} b
18
+ * @param {number|boolean|Object} [c] - legacy maxTimes OR legacy runImmediately OR options
19
+ * @param {boolean|Object} [d] - legacy runImmediately OR options
20
+ * @param {Object} [e] - legacy options
21
+ * @returns {{
22
+ * pause(): void,
23
+ * resume(): void,
24
+ * cancel(): void,
25
+ * reset(restart?: boolean): void,
26
+ * readonly isRunning: boolean,
27
+ * readonly isPaused: boolean,
28
+ * readonly isFinished: boolean,
29
+ * readonly count: number
30
+ * }}
10
31
  */
11
- export function everyRaf(duration: string | number, fn: Function, maxTimes?: number, runImmediately?: boolean): Object;
32
+ export function everyRaf(a: Function | string | number, b: Function | string | number | Object, c?: number | boolean | Object, d?: boolean | Object, e?: Object): {
33
+ pause(): void;
34
+ resume(): void;
35
+ cancel(): void;
36
+ reset(restart?: boolean): void;
37
+ readonly isRunning: boolean;
38
+ readonly isPaused: boolean;
39
+ readonly isFinished: boolean;
40
+ readonly count: number;
41
+ };
@@ -1,5 +1,6 @@
1
- export { afterRaf } from "./afterRaf.js";
2
- export { everyRaf } from "./everyRaf.js";
3
- export { debounceRaf } from "./debounceRaf.js";
4
- export { throttleRaf } from "./throttleRaf.js";
5
- export { waitForRaf } from "./waitForRaf.js";
1
+ export function assertRafAvailable(): void;
2
+ export function afterRaf(...args: any[]): any;
3
+ export function everyRaf(...args: any[]): any;
4
+ export function debounceRaf(...args: any[]): any;
5
+ export function throttleRaf(...args: any[]): any;
6
+ export function waitForRaf(...args: any[]): any;
@@ -1,12 +1,35 @@
1
1
  /**
2
- * Throttles a function using requestAnimationFrame with optional frame skipping.
3
- * Ensures the function runs at most once per N-frames.
4
- * - frameSkip = 0 → run every frame
5
- * - frameSkip = 1 → run every 2nd frame
6
- * - frameSkip = 2 → run every 3rd frame, etc.
7
- *
8
- * @param {Function} fn - Function to throttle
9
- * @param {number} [frameSkip=0] - Number of frames to skip between calls
10
- * @returns {Function} throttled function
2
+ * Throttles a function using `requestAnimationFrame`, with optional frame skipping.
3
+ *
4
+ * Preferred:
5
+ * - throttleRaf(fn, [frameSkip], [options])
6
+ *
7
+ * Also supported:
8
+ * - throttleRaf(fn, [options]) // frameSkip defaults to 0
9
+ *
10
+ * Executes at most once every `(frameSkip + 1)` frames.
11
+ *
12
+ * Argument behavior while throttled:
13
+ * - trailing=true (default): uses the latest args/this seen while waiting
14
+ * - trailing=false: keeps the first args/this and ignores later calls until it fires
15
+ *
16
+ * AbortSignal:
17
+ * - If `signal` is provided, abort will cancel any pending scheduled execution.
18
+ * - Listener is attached only while a call is pending.
19
+ *
20
+ * @template {(...args: any[]) => any} T
21
+ * @param {T} fn
22
+ * @param {number|{signal?: AbortSignal, trailing?: boolean}} [frameSkipOrOptions=0]
23
+ * @param {{signal?: AbortSignal, trailing?: boolean}} [maybeOptions]
24
+ * @returns {T & { cancel: () => void, flush: () => void }}
11
25
  */
12
- export function throttleRaf(fn: Function, frameSkip?: number): Function;
26
+ export function throttleRaf<T extends (...args: any[]) => any>(fn: T, frameSkipOrOptions?: number | {
27
+ signal?: AbortSignal;
28
+ trailing?: boolean;
29
+ }, maybeOptions?: {
30
+ signal?: AbortSignal;
31
+ trailing?: boolean;
32
+ }): T & {
33
+ cancel: () => void;
34
+ flush: () => void;
35
+ };
@@ -1,13 +1,19 @@
1
1
  /**
2
2
  * Waits for a condition to return truthy, polling each frame.
3
- * - Ideal for visual state: DOM settling, visibility, layout changes.
4
- * - Respects tab inactivity (pause-safe).
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).
5
7
  *
6
8
  * @param {Function} condition - Function returning truthy when complete
7
9
  * @param {Object} [options]
8
10
  * @param {string|number} [options.timeout] - Optional max time to wait
9
- * @returns {Promise<void>} Resolves when condition is met, rejects on timeout
11
+ * @param {AbortSignal} [options.signal] - Optional AbortSignal to cancel waiting
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
10
14
  */
11
- export function waitForRaf(condition: Function, { timeout }?: {
15
+ export function waitForRaf(condition: Function, { timeout, signal, immediate }?: {
12
16
  timeout?: string | number | undefined;
17
+ signal?: AbortSignal | undefined;
18
+ immediate?: boolean | undefined;
13
19
  }): Promise<void>;
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Resolves flexible (fn, delay) or (delay, fn) inputs.
3
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
+ *
4
8
  * @param {Function|string|number} a - function or duration
5
9
  * @param {Function|string|number} b - function or duration
6
10
  * @returns {{ fn: Function, delay: number }}
@@ -9,3 +13,26 @@ export function resolveDelayAndFn(a: Function | string | number, b: Function | s
9
13
  fn: Function;
10
14
  delay: number;
11
15
  };
16
+ /**
17
+ * Resolves flexible overloads and returns `{ fn, delay, options }`.
18
+ *
19
+ * Supported forms:
20
+ * - (fn, delay, [options])
21
+ * - (delay, fn, [options])
22
+ * - (fn, [options]) // delay defaults
23
+ *
24
+ * This is intentionally strict about what counts as "options" to avoid
25
+ * treating random values as configuration.
26
+ *
27
+ * @template {Record<string, any>} O
28
+ * @param {Function|string|number} a
29
+ * @param {Function|string|number|O} b
30
+ * @param {O} [c]
31
+ * @param {string|number} [defaultDelay=0]
32
+ * @returns {{ fn: Function, delay: number, options: O }}
33
+ */
34
+ export function resolveDelayFnOptions<O extends Record<string, any>>(a: Function | string | number, b: Function | string | number | O, c?: O, defaultDelay?: string | number): {
35
+ fn: Function;
36
+ delay: number;
37
+ options: O;
38
+ };
package/types/retry.d.ts CHANGED
@@ -1,14 +1,31 @@
1
1
  /**
2
2
  * Retries a promise-returning function with delays.
3
- * @param {Function} fn - async function
4
- * @param {Object} options
5
- * @param {number} [options.attempts=3]
6
- * @param {string} [options.delay='500ms']
7
- * @param {boolean} [options.backoff=false] - use exponential backoff
8
- * @returns {Promise}
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>}
9
19
  */
10
- export function retry(fn: Function, { attempts, delay, backoff }?: {
20
+ export function retry(fn: (attemptIndex: number) => Promise<any>, { attempts, delay, backoff, factor, maxDelay, jitter, signal, random, shouldRetry, onRetry, }?: {
11
21
  attempts?: number | undefined;
12
- delay?: string | undefined;
22
+ delay?: string | number | undefined;
13
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;
14
31
  }): Promise<any>;
@@ -1,10 +1,38 @@
1
+ /**
2
+ * @typedef {((...args: any[]) => void) & {
3
+ * cancel: () => void,
4
+ * flush: () => void
5
+ * }} ThrottledFunction
6
+ */
1
7
  /**
2
8
  * Creates a throttled version of a function.
3
9
  * Executes immediately, throttles further calls, optional trailing execution.
4
10
  *
5
- * @param {Function|string|number} a - Function or delay
6
- * @param {Function|string|number} b - The other parameter
7
- * @param {boolean} [trailing=true] - If true, fire once at trailing edge
8
- * @returns {Function} Throttled function with a .cancel() method
11
+ * Preferred:
12
+ * - throttle(fn, delay, [options])
13
+ *
14
+ * Also supported:
15
+ * - throttle(delay, fn, [options]) // legacy/alt ordering
16
+ *
17
+ * Notes:
18
+ * - Uses performance.now() (via `now()`) for elapsed timing.
19
+ * - If trailing is enabled, uses the latest arguments/context seen during the wait.
20
+ * - `.cancel()` clears any pending trailing invocation.
21
+ * - `.flush()` immediately invokes a pending trailing call (if scheduled).
22
+ * - Optional AbortSignal: abort cancels any pending trailing invocation.
23
+ *
24
+ * @param {Function|string|number} a
25
+ * @param {Function|string|number|Object} b
26
+ * @param {Object} [c]
27
+ * @param {boolean} [c.trailing=true]
28
+ * @param {AbortSignal} [c.signal]
29
+ * @returns {ThrottledFunction}
9
30
  */
10
- export function throttle(a: Function | string | number, b: Function | string | number, trailing?: boolean): Function;
31
+ export function throttle(a: Function | string | number, b: Function | string | number | Object, c?: {
32
+ trailing?: boolean | undefined;
33
+ signal?: AbortSignal | undefined;
34
+ }): ThrottledFunction;
35
+ export type ThrottledFunction = ((...args: any[]) => void) & {
36
+ cancel: () => void;
37
+ flush: () => void;
38
+ };
@@ -1,12 +1,17 @@
1
1
  /**
2
2
  * Waits for a condition to become true, polling at intervals.
3
+ *
3
4
  * @param {Function} condition - returns truthy when satisfied
4
- * @param {Object} options - optional polling control
5
- * @param {string} [options.interval='250ms']
6
- * @param {string} [options.timeout] - max wait time
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
7
10
  * @returns {Promise<void>}
8
11
  */
9
- export function waitFor(condition: Function, { interval, timeout }?: {
10
- interval?: string | undefined;
11
- timeout?: string | undefined;
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;
12
17
  }): Promise<void>;