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.
- package/README.md +239 -144
- package/dist/timeout-flow.min.js +1 -6
- package/package.json +2 -1
- package/src/AfterTimer.js +64 -3
- package/src/EveryTimer.js +139 -18
- package/src/TimeoutFlow.js +291 -118
- package/src/TimerBase.js +81 -23
- package/src/abort.js +75 -0
- package/src/after.js +20 -71
- package/src/context-js.bat +58 -0
- package/src/context.txt +2604 -0
- package/src/debounce.js +90 -24
- package/src/every.js +19 -105
- package/src/index.js +7 -2
- package/src/now.js +23 -0
- package/src/parseDuration.js +22 -20
- package/src/raf/afterRaf.js +198 -66
- package/src/raf/debounceRaf.js +108 -26
- package/src/raf/everyRaf.js +278 -67
- package/src/raf/index.js +26 -6
- package/src/raf/throttleRaf.js +175 -31
- package/src/raf/waitForRaf.js +82 -14
- package/src/resolveDelayAndFn.js +69 -7
- package/src/retry.js +152 -12
- package/src/throttle.js +123 -24
- package/src/waitFor.js +68 -24
- package/types/AfterTimer.d.ts +8 -2
- package/types/EveryTimer.d.ts +21 -4
- package/types/TimeoutFlow.d.ts +6 -18
- package/types/TimerBase.d.ts +15 -4
- package/types/abort.d.ts +31 -0
- package/types/after.d.ts +22 -22
- package/types/debounce.d.ts +28 -6
- package/types/every.d.ts +20 -28
- package/types/index.d.ts +1 -1
- package/types/now.d.ts +1 -0
- package/types/raf/afterRaf.d.ts +35 -5
- package/types/raf/debounceRaf.d.ts +27 -6
- package/types/raf/everyRaf.d.ts +39 -9
- package/types/raf/index.d.ts +6 -5
- package/types/raf/throttleRaf.d.ts +33 -10
- package/types/raf/waitForRaf.d.ts +10 -4
- package/types/resolveDelayAndFn.d.ts +27 -0
- package/types/retry.d.ts +25 -8
- package/types/throttle.d.ts +33 -5
- 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
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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(
|
|
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
|
+
};
|
package/types/raf/everyRaf.d.ts
CHANGED
|
@@ -1,11 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Repeats a function every N milliseconds using requestAnimationFrame
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
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(
|
|
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
|
+
};
|
package/types/raf/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
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
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* - frameSkip
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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:
|
|
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
|
-
*
|
|
4
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
4
|
-
* @param {
|
|
5
|
-
* @param {
|
|
6
|
-
* @param {
|
|
7
|
-
* @param {
|
|
8
|
-
* @
|
|
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:
|
|
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>;
|
package/types/throttle.d.ts
CHANGED
|
@@ -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
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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,
|
|
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
|
+
};
|
package/types/waitFor.d.ts
CHANGED
|
@@ -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>;
|