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/ADDITIONAL_TERMS.md +122 -122
- package/LICENSE +694 -694
- package/README.md +453 -307
- package/dist/timeout-flow.min.js +1 -1
- package/package.json +12 -6
- package/robots.txt +79 -79
- package/src/AfterTimer.js +81 -81
- package/src/EveryTimer.js +208 -208
- package/src/TimeoutFlow.js +400 -400
- package/src/TimerBase.js +133 -132
- package/src/abort.js +87 -75
- package/src/after.js +32 -32
- package/src/context-js.bat +58 -0
- package/src/context.txt +2709 -0
- package/src/debounce.js +105 -105
- package/src/every.js +34 -34
- package/src/index.js +24 -23
- package/src/now.js +22 -22
- package/src/parseDuration.js +46 -46
- package/src/raf/ReadMe-Raf.md +94 -94
- package/src/raf/afterRaf.js +203 -203
- package/src/raf/debounceRaf.js +126 -126
- package/src/raf/everyRaf.js +316 -316
- package/src/raf/index.js +27 -27
- package/src/raf/throttleRaf.js +181 -181
- package/src/raf/waitForRaf.js +100 -100
- package/src/resolveDelayAndFn.js +89 -89
- package/src/retry.js +205 -164
- package/src/sleep.js +58 -0
- package/src/throttle.js +141 -141
- package/src/waitFor.js +96 -77
- package/types/abort.d.ts +40 -31
- package/types/index.d.ts +10 -9
- package/types/raf/waitForRaf.d.ts +19 -19
- package/types/retry.d.ts +70 -31
- package/types/sleep.d.ts +29 -0
- package/types/waitFor.d.ts +28 -17
package/types/retry.d.ts
CHANGED
|
@@ -1,31 +1,70 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retries a
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @param {(
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
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
ADDED
|
@@ -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>;
|
package/types/waitFor.d.ts
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Waits for a condition to become
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
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>;
|