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
package/src/throttle.js
CHANGED
|
@@ -1,43 +1,142 @@
|
|
|
1
1
|
// ./src/throttle.js
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { resolveDelayFnOptions } from './resolveDelayAndFn.js';
|
|
4
|
+
import { pendingAbort } from './abort.js';
|
|
5
|
+
import { now } from './now.js'
|
|
4
6
|
|
|
5
7
|
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {((...args: any[]) => void) & {
|
|
10
|
+
* cancel: () => void,
|
|
11
|
+
* flush: () => void
|
|
12
|
+
* }} ThrottledFunction
|
|
13
|
+
*/
|
|
14
|
+
|
|
6
15
|
/**
|
|
7
16
|
* Creates a throttled version of a function.
|
|
8
17
|
* Executes immediately, throttles further calls, optional trailing execution.
|
|
9
18
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
19
|
+
* Preferred:
|
|
20
|
+
* - throttle(fn, delay, [options])
|
|
21
|
+
*
|
|
22
|
+
* Also supported:
|
|
23
|
+
* - throttle(delay, fn, [options]) // legacy/alt ordering
|
|
24
|
+
*
|
|
25
|
+
* Notes:
|
|
26
|
+
* - Uses performance.now() (via `now()`) for elapsed timing.
|
|
27
|
+
* - If trailing is enabled, uses the latest arguments/context seen during the wait.
|
|
28
|
+
* - `.cancel()` clears any pending trailing invocation.
|
|
29
|
+
* - `.flush()` immediately invokes a pending trailing call (if scheduled).
|
|
30
|
+
* - Optional AbortSignal: abort cancels any pending trailing invocation.
|
|
31
|
+
*
|
|
32
|
+
* @param {Function|string|number} a
|
|
33
|
+
* @param {Function|string|number|Object} b
|
|
34
|
+
* @param {Object} [c]
|
|
35
|
+
* @param {boolean} [c.trailing=true]
|
|
36
|
+
* @param {AbortSignal} [c.signal]
|
|
37
|
+
* @returns {ThrottledFunction}
|
|
14
38
|
*/
|
|
15
|
-
export function throttle(a, b,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
39
|
+
export function throttle(a, b, c) {
|
|
40
|
+
// Require an explicit delay by passing `undefined` as defaultDelay.
|
|
41
|
+
const { fn, delay, options } = resolveDelayFnOptions(a, b, c, undefined);
|
|
42
|
+
|
|
43
|
+
const signal = options?.signal;
|
|
44
|
+
const trailing = options?.trailing ?? true;
|
|
45
|
+
|
|
46
|
+
const waitWindow = Math.max(0, delay);
|
|
47
|
+
|
|
48
|
+
let lastCallTime = -Infinity;
|
|
49
|
+
let timeoutId = 0;
|
|
50
|
+
|
|
51
|
+
/** @type {any[] | null} */
|
|
52
|
+
let lastArgs = null;
|
|
53
|
+
/** @type {any | null} */
|
|
54
|
+
let lastThis = null;
|
|
55
|
+
|
|
56
|
+
const abort = pendingAbort(signal, () => cancel());
|
|
57
|
+
|
|
58
|
+
const invoke = () => {
|
|
59
|
+
if (!lastArgs) return;
|
|
60
|
+
|
|
61
|
+
const args = lastArgs;
|
|
62
|
+
const ctx = lastThis;
|
|
63
|
+
|
|
64
|
+
// clear first (re-entrancy safe + GC friendly)
|
|
65
|
+
lastArgs = null;
|
|
66
|
+
lastThis = null;
|
|
67
|
+
|
|
68
|
+
abort.remove();
|
|
69
|
+
fn.apply(ctx, args);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const cancel = () => {
|
|
73
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
74
|
+
timeoutId = 0;
|
|
19
75
|
|
|
76
|
+
lastArgs = null;
|
|
77
|
+
lastThis = null;
|
|
78
|
+
|
|
79
|
+
abort.remove();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** @param {number} wait */
|
|
83
|
+
const scheduleTrailing = (wait) => {
|
|
84
|
+
abort.add();
|
|
85
|
+
|
|
86
|
+
timeoutId = setTimeout(() => {
|
|
87
|
+
timeoutId = 0;
|
|
88
|
+
lastCallTime = now();
|
|
89
|
+
invoke();
|
|
90
|
+
}, wait);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** @type {any} */
|
|
20
94
|
const throttled = function (...args) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
95
|
+
if (signal?.aborted) return;
|
|
96
|
+
|
|
97
|
+
const currentTime = now();
|
|
98
|
+
const timeSinceLast = currentTime - lastCallTime;
|
|
99
|
+
|
|
100
|
+
lastArgs = args;
|
|
101
|
+
lastThis = this;
|
|
102
|
+
|
|
103
|
+
if (timeSinceLast >= waitWindow) {
|
|
104
|
+
// Leading edge: run now
|
|
105
|
+
lastCallTime = currentTime;
|
|
106
|
+
|
|
107
|
+
// Clear any pending trailing call (and listener)
|
|
108
|
+
if (timeoutId) {
|
|
109
|
+
clearTimeout(timeoutId);
|
|
110
|
+
timeoutId = 0;
|
|
111
|
+
abort.remove();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
invoke();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!trailing) return;
|
|
119
|
+
|
|
120
|
+
// Trailing edge: schedule once, keep latest args
|
|
121
|
+
if (!timeoutId) {
|
|
122
|
+
const wait = Math.max(0, waitWindow - timeSinceLast);
|
|
123
|
+
scheduleTrailing(wait);
|
|
34
124
|
}
|
|
35
125
|
};
|
|
36
126
|
|
|
37
|
-
throttled.cancel =
|
|
127
|
+
throttled.cancel = cancel;
|
|
128
|
+
|
|
129
|
+
throttled.flush = () => {
|
|
130
|
+
if (!timeoutId) return;
|
|
38
131
|
clearTimeout(timeoutId);
|
|
39
|
-
timeoutId =
|
|
132
|
+
timeoutId = 0;
|
|
133
|
+
|
|
134
|
+
lastCallTime = now();
|
|
135
|
+
invoke();
|
|
40
136
|
};
|
|
41
137
|
|
|
138
|
+
// If already aborted at creation time, ensure clean slate.
|
|
139
|
+
if (signal?.aborted) cancel();
|
|
140
|
+
|
|
42
141
|
return throttled;
|
|
43
142
|
}
|
package/src/waitFor.js
CHANGED
|
@@ -1,33 +1,77 @@
|
|
|
1
|
-
|
|
2
1
|
// ./src/waitFor.js
|
|
3
2
|
|
|
4
3
|
import { parseDuration } from './parseDuration.js';
|
|
4
|
+
import { attachAbort, createAbortError } from './abort.js';
|
|
5
|
+
import { now } from './now.js'
|
|
6
|
+
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Waits for a condition to become true, polling at intervals.
|
|
10
|
+
*
|
|
8
11
|
* @param {Function} condition - returns truthy when satisfied
|
|
9
|
-
* @param {Object} options - optional polling control
|
|
10
|
-
* @param {string} [options.interval='250ms']
|
|
11
|
-
* @param {string} [options.timeout] - max wait time
|
|
12
|
+
* @param {Object} [options] - optional polling control
|
|
13
|
+
* @param {string|number} [options.interval='250ms'] - poll frequency
|
|
14
|
+
* @param {string|number} [options.timeout] - max wait time
|
|
15
|
+
* @param {boolean} [options.immediate=false] - If true, evaluate condition immediately
|
|
16
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal to cancel waiting
|
|
12
17
|
* @returns {Promise<void>}
|
|
13
18
|
*/
|
|
14
|
-
export function waitFor(condition, { interval = '250ms', timeout } = {}) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
19
|
+
export function waitFor(condition, { interval = '250ms', timeout, immediate = false, signal } = {}) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
if (signal?.aborted) {
|
|
22
|
+
reject(createAbortError());
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const start = now();
|
|
27
|
+
|
|
28
|
+
const intervalMs = typeof interval === 'string' ? parseDuration(interval) : interval;
|
|
29
|
+
const timeoutMs = timeout != null ? parseDuration(timeout) : null;
|
|
30
|
+
|
|
31
|
+
let poller = null;
|
|
32
|
+
|
|
33
|
+
const cleanup = () => {
|
|
34
|
+
if (poller != null) {
|
|
35
|
+
clearInterval(poller);
|
|
36
|
+
poller = null;
|
|
37
|
+
}
|
|
38
|
+
cleanupAbort();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const onAbort = () => {
|
|
42
|
+
cleanup();
|
|
43
|
+
reject(createAbortError());
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const cleanupAbort = attachAbort(signal, onAbort);
|
|
47
|
+
|
|
48
|
+
const check = () => {
|
|
49
|
+
let ok = false;
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
ok = !!condition();
|
|
53
|
+
} catch (err) {
|
|
54
|
+
cleanup();
|
|
55
|
+
reject(err);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (ok) {
|
|
60
|
+
cleanup();
|
|
61
|
+
resolve();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (timeoutMs != null && now() - start >= timeoutMs) {
|
|
66
|
+
cleanup();
|
|
67
|
+
reject(new Error('waitFor timed out'));
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// Only evaluate immediately if explicitly requested
|
|
72
|
+
if (immediate) check();
|
|
73
|
+
|
|
74
|
+
// Schedule exactly once
|
|
75
|
+
poller = setInterval(check, intervalMs);
|
|
76
|
+
});
|
|
77
|
+
}
|
package/types/AfterTimer.d.ts
CHANGED
|
@@ -5,8 +5,14 @@ export class AfterTimer extends TimerBase {
|
|
|
5
5
|
/**
|
|
6
6
|
* @param {string|number} duration - Delay before firing (e.g., '1s', 300)
|
|
7
7
|
* @param {Function} fn - Main function to execute
|
|
8
|
-
* @param {
|
|
8
|
+
* @param {Object} [options]
|
|
9
|
+
* @param {Function} [options.onFinish] - Optional callback when done
|
|
10
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel
|
|
9
11
|
*/
|
|
10
|
-
constructor(duration: string | number, fn: Function, onFinish?:
|
|
12
|
+
constructor(duration: string | number, fn: Function, { onFinish, signal }?: {
|
|
13
|
+
onFinish?: Function | undefined;
|
|
14
|
+
signal?: AbortSignal | undefined;
|
|
15
|
+
});
|
|
16
|
+
#private;
|
|
11
17
|
}
|
|
12
18
|
import { TimerBase } from './TimerBase.js';
|
package/types/EveryTimer.d.ts
CHANGED
|
@@ -5,18 +5,35 @@ export class EveryTimer {
|
|
|
5
5
|
/**
|
|
6
6
|
* @param {string|number} duration - Delay between calls (e.g., '1s', 200)
|
|
7
7
|
* @param {Function} fn - Function to call each tick
|
|
8
|
-
* @param {
|
|
9
|
-
* @param {
|
|
8
|
+
* @param {Object} [options]
|
|
9
|
+
* @param {number} [options.max=Infinity] - Max executions
|
|
10
|
+
* @param {boolean} [options.runImmediately=false] - Run `fn` once before first delay
|
|
11
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel
|
|
10
12
|
*/
|
|
11
|
-
constructor(duration: string | number, fn: Function, max
|
|
13
|
+
constructor(duration: string | number, fn: Function, { max, runImmediately, signal }?: {
|
|
14
|
+
max?: number | undefined;
|
|
15
|
+
runImmediately?: boolean | undefined;
|
|
16
|
+
signal?: AbortSignal | undefined;
|
|
17
|
+
});
|
|
12
18
|
/**
|
|
13
19
|
* Number of times `fn` has been executed.
|
|
14
20
|
*/
|
|
15
21
|
get count(): number;
|
|
16
22
|
/**
|
|
17
|
-
* Whether the timer is actively running.
|
|
23
|
+
* Whether the timer is actively running (waiting for next tick).
|
|
24
|
+
*
|
|
25
|
+
* Note: during the brief moment between a tick firing and the next schedule,
|
|
26
|
+
* this may be false — which reflects "no countdown currently pending".
|
|
18
27
|
*/
|
|
19
28
|
get isRunning(): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Whether the timer is paused (resumable).
|
|
31
|
+
*/
|
|
32
|
+
get isPaused(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Whether the timer has finished (hit max) or been canceled (terminal).
|
|
35
|
+
*/
|
|
36
|
+
get isFinished(): boolean;
|
|
20
37
|
pause(): void;
|
|
21
38
|
resume(): void;
|
|
22
39
|
cancel(): void;
|
package/types/TimeoutFlow.d.ts
CHANGED
|
@@ -1,22 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates a fluent scheduler.
|
|
3
3
|
* Returns a chainable timeline.
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} [options]
|
|
6
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel the flow
|
|
4
7
|
*/
|
|
5
|
-
export function flow(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
loop(n?: boolean): /*elided*/ any;
|
|
9
|
-
onFinish(cb: any): /*elided*/ any;
|
|
10
|
-
start(): /*elided*/ any;
|
|
11
|
-
pause(): void;
|
|
12
|
-
resume(): void;
|
|
13
|
-
cancel(): void;
|
|
14
|
-
reset(): /*elided*/ any;
|
|
15
|
-
readonly isPaused: boolean;
|
|
16
|
-
label(name: any): /*elided*/ any;
|
|
17
|
-
jumpTo(name: any): /*elided*/ any;
|
|
18
|
-
if(predicate: any): /*elided*/ any;
|
|
19
|
-
unless(predicate: any): /*elided*/ any;
|
|
20
|
-
while(predicate: any): /*elided*/ any;
|
|
21
|
-
doWhile(predicate: any): /*elided*/ any;
|
|
22
|
-
};
|
|
8
|
+
export function flow({ signal }?: {
|
|
9
|
+
signal?: AbortSignal | undefined;
|
|
10
|
+
}): any;
|
package/types/TimerBase.d.ts
CHANGED
|
@@ -8,26 +8,37 @@ export class TimerBase {
|
|
|
8
8
|
*/
|
|
9
9
|
constructor(duration: string | number);
|
|
10
10
|
/**
|
|
11
|
-
* Whether the timer is currently running.
|
|
11
|
+
* Whether the timer is currently running (actively counting down).
|
|
12
12
|
* @returns {boolean}
|
|
13
13
|
*/
|
|
14
14
|
get isRunning(): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Whether the timer is paused (resumable).
|
|
17
|
+
* @returns {boolean}
|
|
18
|
+
*/
|
|
19
|
+
get isPaused(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Whether the timer has finished or been canceled (terminal state).
|
|
22
|
+
* @returns {boolean}
|
|
23
|
+
*/
|
|
24
|
+
get isFinished(): boolean;
|
|
15
25
|
/**
|
|
16
26
|
* Pause the active timer, storing remaining time.
|
|
17
27
|
*/
|
|
18
28
|
pause(): void;
|
|
19
29
|
/**
|
|
20
|
-
* Resume the timer using the remaining time.
|
|
30
|
+
* Resume the timer using the remaining time (or an override delay).
|
|
21
31
|
* @param {Function} callback - Called when time completes
|
|
22
|
-
* @param {number|null} [overrideDelay] - Optional custom delay in ms
|
|
32
|
+
* @param {number|null} [overrideDelay=null] - Optional custom delay in ms
|
|
23
33
|
*/
|
|
24
34
|
resume(callback: Function, overrideDelay?: number | null): void;
|
|
25
35
|
/**
|
|
26
|
-
* Cancel the timer immediately.
|
|
36
|
+
* Cancel the timer immediately (terminal).
|
|
27
37
|
*/
|
|
28
38
|
cancel(): void;
|
|
29
39
|
/**
|
|
30
40
|
* Reset the timer to its original delay, cancelling if needed.
|
|
41
|
+
* After reset, the timer is not running.
|
|
31
42
|
*/
|
|
32
43
|
reset(): void;
|
|
33
44
|
#private;
|
package/types/abort.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attach an AbortSignal listener and return a cleanup function.
|
|
3
|
+
* - If `signal` is missing, returns a no-op cleanup.
|
|
4
|
+
* - If already aborted, calls `onAbort()` immediately and returns a no-op cleanup.
|
|
5
|
+
*
|
|
6
|
+
* @param {AbortSignal | null | undefined} signal
|
|
7
|
+
* @param {() => void} onAbort
|
|
8
|
+
* @returns {() => void} cleanup
|
|
9
|
+
*/
|
|
10
|
+
export function attachAbort(signal: AbortSignal | null | undefined, onAbort: () => void): () => void;
|
|
11
|
+
/**
|
|
12
|
+
* Manage AbortSignal listener lifecycle for "pending work" cases.
|
|
13
|
+
* Call `add()` when work becomes pending and `remove()` when it’s resolved/canceled.
|
|
14
|
+
*
|
|
15
|
+
* @param {AbortSignal | null | undefined} signal
|
|
16
|
+
* @param {() => void} onAbort
|
|
17
|
+
* @returns {{ add: () => void, remove: () => void, reset: () => void }}
|
|
18
|
+
*/
|
|
19
|
+
export function pendingAbort(signal: AbortSignal | null | undefined, onAbort: () => void): {
|
|
20
|
+
add: () => void;
|
|
21
|
+
remove: () => void;
|
|
22
|
+
reset: () => void;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Standardized AbortError factory.
|
|
26
|
+
* Works in browsers and Node runtimes that may not have DOMException.
|
|
27
|
+
*
|
|
28
|
+
* We resolve the best implementation once at module load time.
|
|
29
|
+
* This avoids feature detection in the hot path.
|
|
30
|
+
*/
|
|
31
|
+
export let createAbortError: any;
|
package/types/after.d.ts
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Schedules a one-time delayed function.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* }
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
pause(): void;
|
|
16
|
-
resume(): void;
|
|
17
|
-
cancel(): void;
|
|
18
|
-
isRunning: boolean;
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Schedules a one-time delayed function.
|
|
22
|
-
* @param {string} duration - e.g. "5s", "500ms", "2m"
|
|
23
|
-
* @param {Function} fn - callback to run
|
|
4
|
+
* Preferred:
|
|
5
|
+
* - after(fn, duration, [options])
|
|
6
|
+
*
|
|
7
|
+
* Also supported (legacy/alt):
|
|
8
|
+
* - after(duration, fn, [options])
|
|
9
|
+
*
|
|
10
|
+
* @param {Function|string|number} a - Function or duration (see overloads)
|
|
11
|
+
* @param {Function|string|number|Object} b - Duration, function, or options
|
|
12
|
+
* @param {Object} [c] - Options
|
|
13
|
+
* @param {Function} [c.onFinish] - Optional callback after `fn` runs
|
|
14
|
+
* @param {AbortSignal} [c.signal] - Optional AbortSignal to auto-cancel
|
|
24
15
|
* @returns {{
|
|
25
16
|
* pause(): void,
|
|
26
17
|
* resume(): void,
|
|
27
18
|
* cancel(): void,
|
|
19
|
+
* reset(): void,
|
|
28
20
|
* readonly isRunning: boolean,
|
|
21
|
+
* readonly isPaused: boolean,
|
|
22
|
+
* readonly isFinished: boolean
|
|
29
23
|
* }}
|
|
30
|
-
|
|
31
|
-
export function
|
|
24
|
+
*/
|
|
25
|
+
export function after(a: Function | string | number, b: Function | string | number | Object, c?: {
|
|
26
|
+
onFinish?: Function | undefined;
|
|
27
|
+
signal?: AbortSignal | undefined;
|
|
28
|
+
}): {
|
|
32
29
|
pause(): void;
|
|
33
30
|
resume(): void;
|
|
34
31
|
cancel(): void;
|
|
32
|
+
reset(): void;
|
|
35
33
|
readonly isRunning: boolean;
|
|
34
|
+
readonly isPaused: boolean;
|
|
35
|
+
readonly isFinished: boolean;
|
|
36
36
|
};
|
package/types/debounce.d.ts
CHANGED
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @typedef {((...args: any[]) => void) & {
|
|
2
|
+
* @typedef {((...args: any[]) => void) & {
|
|
3
|
+
* cancel: () => void,
|
|
4
|
+
* flush: () => void
|
|
5
|
+
* }} DebouncedFunction
|
|
3
6
|
*/
|
|
4
7
|
/**
|
|
5
|
-
* Creates a debounced function that delays invoking `fn`
|
|
8
|
+
* Creates a debounced function that delays invoking `fn`
|
|
9
|
+
* until after `delay` ms of inactivity.
|
|
6
10
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
11
|
+
* Preferred:
|
|
12
|
+
* - debounce(fn, [delay], [options]) // delay defaults to 0
|
|
13
|
+
*
|
|
14
|
+
* Also supported:
|
|
15
|
+
* - debounce(delay, fn, [options]) // legacy/alt ordering
|
|
16
|
+
* - debounce(fn, [options])
|
|
17
|
+
*
|
|
18
|
+
* Notes:
|
|
19
|
+
* - Always invokes with the latest arguments/context.
|
|
20
|
+
* - `.cancel()` clears any pending invocation.
|
|
21
|
+
* - `.flush()` immediately invokes if pending.
|
|
22
|
+
* - Optional AbortSignal: abort cancels pending invocation.
|
|
23
|
+
*
|
|
24
|
+
* @param {Function|string|number} a - Function or delay (see overloads)
|
|
25
|
+
* @param {Function|string|number|Object} [b] - Delay, function, or options
|
|
26
|
+
* @param {Object} [c] - Options (3-arg form)
|
|
27
|
+
* @param {AbortSignal} [c.signal] - Optional AbortSignal to cancel pending work
|
|
28
|
+
* @returns {DebouncedFunction}
|
|
10
29
|
*/
|
|
11
|
-
export function debounce(a:
|
|
30
|
+
export function debounce(a: Function | string | number, b?: Function | string | number | Object, c?: {
|
|
31
|
+
signal?: AbortSignal | undefined;
|
|
32
|
+
}): DebouncedFunction;
|
|
12
33
|
export type DebouncedFunction = ((...args: any[]) => void) & {
|
|
13
34
|
cancel: () => void;
|
|
35
|
+
flush: () => void;
|
|
14
36
|
};
|
package/types/every.d.ts
CHANGED
|
@@ -1,48 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Runs a function every N ms with optional execution limit.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* pause(): void,
|
|
10
|
-
* resume(): void,
|
|
11
|
-
* cancel(): void,
|
|
12
|
-
* reset(restart?: boolean): void,
|
|
13
|
-
* readonly isRunning: boolean,
|
|
14
|
-
* readonly count: number
|
|
15
|
-
* }}
|
|
16
|
-
*/
|
|
17
|
-
export function every(duration: string | number, fn: Function, max?: number, runImmediately?: boolean): {
|
|
18
|
-
pause(): void;
|
|
19
|
-
resume(): void;
|
|
20
|
-
cancel(): void;
|
|
21
|
-
reset(restart?: boolean): void;
|
|
22
|
-
readonly isRunning: boolean;
|
|
23
|
-
readonly count: number;
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Runs a function every N milliseconds with optional execution limit.
|
|
27
|
-
* Returns control methods: pause, resume, cancel, reset, and status flags.
|
|
4
|
+
* Preferred:
|
|
5
|
+
* - every(fn, duration, [options])
|
|
6
|
+
*
|
|
7
|
+
* Also supported (legacy/alt):
|
|
8
|
+
* - every(duration, fn, [options])
|
|
28
9
|
*
|
|
29
|
-
* @param {string|number}
|
|
30
|
-
* @param {Function}
|
|
31
|
-
* @param {
|
|
10
|
+
* @param {Function|string|number} a - Function or duration (see overloads)
|
|
11
|
+
* @param {Function|string|number|Object} b - Duration, function, or options
|
|
12
|
+
* @param {Object} [c] - Options
|
|
13
|
+
* @param {number} [c.max=Infinity] - Max times to run
|
|
14
|
+
* @param {boolean} [c.runImmediately=false] - Run immediately on first tick
|
|
15
|
+
* @param {AbortSignal} [c.signal] - Optional AbortSignal to auto-cancel
|
|
32
16
|
* @returns {{
|
|
33
17
|
* pause(): void,
|
|
34
18
|
* resume(): void,
|
|
35
19
|
* cancel(): void,
|
|
36
20
|
* reset(restart?: boolean): void,
|
|
37
21
|
* readonly isRunning: boolean,
|
|
22
|
+
* readonly isPaused: boolean,
|
|
23
|
+
* readonly isFinished: boolean,
|
|
38
24
|
* readonly count: number
|
|
39
25
|
* }}
|
|
40
26
|
*/
|
|
41
|
-
export function
|
|
27
|
+
export function every(a: Function | string | number, b: Function | string | number | Object, c?: {
|
|
28
|
+
max?: number | undefined;
|
|
29
|
+
runImmediately?: boolean | undefined;
|
|
30
|
+
signal?: AbortSignal | undefined;
|
|
31
|
+
}): {
|
|
42
32
|
pause(): void;
|
|
43
33
|
resume(): void;
|
|
44
34
|
cancel(): void;
|
|
45
35
|
reset(restart?: boolean): void;
|
|
46
36
|
readonly isRunning: boolean;
|
|
37
|
+
readonly isPaused: boolean;
|
|
38
|
+
readonly isFinished: boolean;
|
|
47
39
|
readonly count: number;
|
|
48
40
|
};
|
package/types/index.d.ts
CHANGED
|
@@ -6,4 +6,4 @@ export { after } from "./after.js";
|
|
|
6
6
|
export { every } from "./every.js";
|
|
7
7
|
export { waitFor } from "./waitFor.js";
|
|
8
8
|
export { retry } from "./retry.js";
|
|
9
|
-
export
|
|
9
|
+
export { afterRaf, everyRaf, debounceRaf, throttleRaf, waitForRaf } from "./raf/index.js";
|
package/types/now.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export let now: any;
|
package/types/raf/afterRaf.d.ts
CHANGED
|
@@ -1,8 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Schedules a one-time delayed function using requestAnimationFrame.
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* Preferred:
|
|
5
|
+
* - afterRaf(fn, duration, [options])
|
|
6
|
+
*
|
|
7
|
+
* Also supported (legacy):
|
|
8
|
+
* - afterRaf(duration, fn, [onFinish], [options])
|
|
9
|
+
*
|
|
10
|
+
* Timing model:
|
|
11
|
+
* - Uses frame timestamps for progression.
|
|
12
|
+
* - Paused time does NOT count toward elapsed time (pause freezes remaining).
|
|
13
|
+
*
|
|
14
|
+
* @param {Function|string|number} a
|
|
15
|
+
* @param {Function|string|number|Object} b
|
|
16
|
+
* @param {Function|Object} [c] - legacy onFinish OR options
|
|
17
|
+
* @param {Object} [d] - legacy options (when using 4 args)
|
|
18
|
+
* @param {Function} [c.onFinish] - optional callback after finishing (preferred: in options)
|
|
19
|
+
* @param {AbortSignal} [c.signal] - optional AbortSignal to auto-cancel
|
|
20
|
+
* @returns {{
|
|
21
|
+
* pause(): void,
|
|
22
|
+
* resume(): void,
|
|
23
|
+
* cancel(): void,
|
|
24
|
+
* reset(): void,
|
|
25
|
+
* readonly isRunning: boolean,
|
|
26
|
+
* readonly isPaused: boolean,
|
|
27
|
+
* readonly isFinished: boolean
|
|
28
|
+
* }}
|
|
7
29
|
*/
|
|
8
|
-
export function afterRaf(
|
|
30
|
+
export function afterRaf(a: Function | string | number, b: Function | string | number | Object, c?: Function | Object, d?: Object): {
|
|
31
|
+
pause(): void;
|
|
32
|
+
resume(): void;
|
|
33
|
+
cancel(): void;
|
|
34
|
+
reset(): void;
|
|
35
|
+
readonly isRunning: boolean;
|
|
36
|
+
readonly isPaused: boolean;
|
|
37
|
+
readonly isFinished: boolean;
|
|
38
|
+
};
|