timeout-flow 0.0.19 → 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.
- package/ADDITIONAL_TERMS.md +122 -122
- package/LICENSE +694 -694
- package/README.md +475 -307
- package/dist/timeout-flow.min.js +1 -1
- package/package.json +79 -68
- 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 +137 -132
- package/src/abort.js +87 -75
- package/src/after.js +32 -32
- 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 +43 -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 +314 -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 +65 -89
- package/src/retry.js +205 -164
- package/src/sleep.js +58 -0
- package/src/throttle.js +141 -141
- package/src/waitFor.js +99 -77
- package/types/TimerBase.d.ts +2 -2
- package/types/abort.d.ts +9 -0
- package/types/index.d.ts +1 -0
- package/types/raf/waitForRaf.d.ts +1 -1
- package/types/resolveDelayAndFn.d.ts +1 -15
- package/types/retry.d.ts +57 -18
- package/types/sleep.d.ts +29 -0
- package/types/waitFor.d.ts +19 -8
package/src/raf/index.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
// ./raf/index.js
|
|
2
|
-
|
|
3
|
-
import { afterRaf as _afterRaf } from './afterRaf.js';
|
|
4
|
-
import { everyRaf as _everyRaf } from './everyRaf.js';
|
|
5
|
-
import { debounceRaf as _debounceRaf } from './debounceRaf.js';
|
|
6
|
-
import { throttleRaf as _throttleRaf } from './throttleRaf.js';
|
|
7
|
-
import { waitForRaf as _waitForRaf } from './waitForRaf.js';
|
|
8
|
-
|
|
9
|
-
export function assertRafAvailable() {
|
|
10
|
-
if (typeof requestAnimationFrame !== 'function') {
|
|
11
|
-
throw new Error(
|
|
12
|
-
'requestAnimationFrame is not available in this environment. ' +
|
|
13
|
-
'Use the non-RAF timers (after/every/waitFor) in Node, or provide a RAF polyfill.'
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const guard = (fn) =>
|
|
19
|
-
(...args) => {
|
|
20
|
-
assertRafAvailable();
|
|
21
|
-
return fn(...args);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export const afterRaf = guard(_afterRaf);
|
|
25
|
-
export const everyRaf = guard(_everyRaf);
|
|
26
|
-
export const debounceRaf = guard(_debounceRaf);
|
|
27
|
-
export const throttleRaf = guard(_throttleRaf);
|
|
1
|
+
// ./raf/index.js
|
|
2
|
+
|
|
3
|
+
import { afterRaf as _afterRaf } from './afterRaf.js';
|
|
4
|
+
import { everyRaf as _everyRaf } from './everyRaf.js';
|
|
5
|
+
import { debounceRaf as _debounceRaf } from './debounceRaf.js';
|
|
6
|
+
import { throttleRaf as _throttleRaf } from './throttleRaf.js';
|
|
7
|
+
import { waitForRaf as _waitForRaf } from './waitForRaf.js';
|
|
8
|
+
|
|
9
|
+
export function assertRafAvailable() {
|
|
10
|
+
if (typeof requestAnimationFrame !== 'function') {
|
|
11
|
+
throw new Error(
|
|
12
|
+
'requestAnimationFrame is not available in this environment. ' +
|
|
13
|
+
'Use the non-RAF timers (after/every/waitFor) in Node, or provide a RAF polyfill.'
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const guard = (fn) =>
|
|
19
|
+
(...args) => {
|
|
20
|
+
assertRafAvailable();
|
|
21
|
+
return fn(...args);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const afterRaf = guard(_afterRaf);
|
|
25
|
+
export const everyRaf = guard(_everyRaf);
|
|
26
|
+
export const debounceRaf = guard(_debounceRaf);
|
|
27
|
+
export const throttleRaf = guard(_throttleRaf);
|
|
28
28
|
export const waitForRaf = guard(_waitForRaf);
|
package/src/raf/throttleRaf.js
CHANGED
|
@@ -1,182 +1,182 @@
|
|
|
1
|
-
// ./raf/throttleRaf.js
|
|
2
|
-
import { pendingAbort } from '../abort.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Throttles a function using `requestAnimationFrame`, with optional frame skipping.
|
|
8
|
-
*
|
|
9
|
-
* Preferred:
|
|
10
|
-
* - throttleRaf(fn, [frameSkip], [options])
|
|
11
|
-
*
|
|
12
|
-
* Also supported:
|
|
13
|
-
* - throttleRaf(fn, [options]) // frameSkip defaults to 0
|
|
14
|
-
*
|
|
15
|
-
* Executes at most once every `(frameSkip + 1)` frames.
|
|
16
|
-
*
|
|
17
|
-
* Argument behavior while throttled:
|
|
18
|
-
* - trailing=true (default): uses the latest args/this seen while waiting
|
|
19
|
-
* - trailing=false: keeps the first args/this and ignores later calls until it fires
|
|
20
|
-
*
|
|
21
|
-
* AbortSignal:
|
|
22
|
-
* - If `signal` is provided, abort will cancel any pending scheduled execution.
|
|
23
|
-
* - Listener is attached only while a call is pending.
|
|
24
|
-
*
|
|
25
|
-
* @template {(...args: any[]) => any} T
|
|
26
|
-
* @param {T} fn
|
|
27
|
-
* @param {number|{signal?: AbortSignal, trailing?: boolean}} [frameSkipOrOptions=0]
|
|
28
|
-
* @param {{signal?: AbortSignal, trailing?: boolean}} [maybeOptions]
|
|
29
|
-
* @returns {T & { cancel: () => void, flush: () => void }}
|
|
30
|
-
*/
|
|
31
|
-
export function throttleRaf(fn, frameSkipOrOptions = 0, maybeOptions) {
|
|
32
|
-
if (typeof fn !== 'function') {
|
|
33
|
-
throw new TypeError(`Expected a function, got: ${typeof fn}`);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const frameSkip = typeof frameSkipOrOptions === 'number' ? frameSkipOrOptions : 0;
|
|
37
|
-
|
|
38
|
-
const options = isPlainOptions(frameSkipOrOptions)
|
|
39
|
-
? frameSkipOrOptions
|
|
40
|
-
: isPlainOptions(maybeOptions)
|
|
41
|
-
? maybeOptions
|
|
42
|
-
: {};
|
|
43
|
-
|
|
44
|
-
const signal = options.signal;
|
|
45
|
-
const trailing = options.trailing ?? true;
|
|
46
|
-
|
|
47
|
-
// Normalize once: int >= 0
|
|
48
|
-
const skip = Math.max(0, frameSkip | 0);
|
|
49
|
-
|
|
50
|
-
let scheduled = false;
|
|
51
|
-
let rafId = 0;
|
|
52
|
-
|
|
53
|
-
/** @type {any[] | null} */
|
|
54
|
-
let lastArgs = null;
|
|
55
|
-
/** @type {any | null} */
|
|
56
|
-
let lastThis = null;
|
|
57
|
-
|
|
58
|
-
// countdown for skip-path only
|
|
59
|
-
let remaining = skip;
|
|
60
|
-
|
|
61
|
-
const abort = pendingAbort(signal, () => cancel());
|
|
62
|
-
|
|
63
|
-
const clearState = () => {
|
|
64
|
-
scheduled = false;
|
|
65
|
-
|
|
66
|
-
if (rafId) cancelAnimationFrame(rafId);
|
|
67
|
-
rafId = 0;
|
|
68
|
-
|
|
69
|
-
remaining = skip;
|
|
70
|
-
|
|
71
|
-
lastArgs = null;
|
|
72
|
-
lastThis = null;
|
|
73
|
-
|
|
74
|
-
abort.remove();
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const cancel = () => {
|
|
78
|
-
clearState();
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const invoke = () => {
|
|
82
|
-
if (!lastArgs) return;
|
|
83
|
-
|
|
84
|
-
const args = lastArgs;
|
|
85
|
-
const ctx = lastThis;
|
|
86
|
-
|
|
87
|
-
// Clear first to aid GC + prevent re-entrancy oddities
|
|
88
|
-
lastArgs = null;
|
|
89
|
-
lastThis = null;
|
|
90
|
-
|
|
91
|
-
abort.remove();
|
|
92
|
-
fn.apply(ctx, args);
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const fire = () => {
|
|
96
|
-
scheduled = false;
|
|
97
|
-
rafId = 0;
|
|
98
|
-
remaining = skip;
|
|
99
|
-
invoke();
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const tickSkip = () => {
|
|
103
|
-
if (!scheduled) return;
|
|
104
|
-
|
|
105
|
-
if (signal?.aborted) {
|
|
106
|
-
cancel();
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (remaining === 0) {
|
|
111
|
-
fire();
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
remaining -= 1;
|
|
116
|
-
rafId = requestAnimationFrame(tickSkip);
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
// Fast path for skip === 0
|
|
120
|
-
const tick0 = () => {
|
|
121
|
-
if (!scheduled) return;
|
|
122
|
-
|
|
123
|
-
if (signal?.aborted) {
|
|
124
|
-
cancel();
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
fire();
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const schedule =
|
|
132
|
-
skip === 0
|
|
133
|
-
? () => {
|
|
134
|
-
scheduled = true;
|
|
135
|
-
abort.add();
|
|
136
|
-
rafId = requestAnimationFrame(tick0);
|
|
137
|
-
}
|
|
138
|
-
: () => {
|
|
139
|
-
scheduled = true;
|
|
140
|
-
remaining = skip;
|
|
141
|
-
abort.add();
|
|
142
|
-
rafId = requestAnimationFrame(tickSkip);
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
/** @type {any} */
|
|
146
|
-
const throttled = function (...args) {
|
|
147
|
-
if (signal?.aborted) return;
|
|
148
|
-
|
|
149
|
-
// While waiting, keep latest args only if trailing=true
|
|
150
|
-
if (!scheduled || trailing) {
|
|
151
|
-
lastArgs = args;
|
|
152
|
-
lastThis = this;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (scheduled) return;
|
|
156
|
-
schedule();
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
throttled.cancel = cancel;
|
|
160
|
-
|
|
161
|
-
throttled.flush = () => {
|
|
162
|
-
if (!scheduled) return;
|
|
163
|
-
|
|
164
|
-
if (signal?.aborted) {
|
|
165
|
-
cancel();
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Cancel RAF and invoke immediately
|
|
170
|
-
scheduled = false;
|
|
171
|
-
if (rafId) cancelAnimationFrame(rafId);
|
|
172
|
-
rafId = 0;
|
|
173
|
-
remaining = skip;
|
|
174
|
-
|
|
175
|
-
invoke();
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// If already aborted at creation time, ensure clean slate.
|
|
179
|
-
if (signal?.aborted) cancel();
|
|
180
|
-
|
|
181
|
-
return throttled;
|
|
1
|
+
// ./raf/throttleRaf.js
|
|
2
|
+
import { pendingAbort } from '../abort.js';
|
|
3
|
+
import { isPlainOptions } from '../resolveDelayAndFn.js';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Throttles a function using `requestAnimationFrame`, with optional frame skipping.
|
|
8
|
+
*
|
|
9
|
+
* Preferred:
|
|
10
|
+
* - throttleRaf(fn, [frameSkip], [options])
|
|
11
|
+
*
|
|
12
|
+
* Also supported:
|
|
13
|
+
* - throttleRaf(fn, [options]) // frameSkip defaults to 0
|
|
14
|
+
*
|
|
15
|
+
* Executes at most once every `(frameSkip + 1)` frames.
|
|
16
|
+
*
|
|
17
|
+
* Argument behavior while throttled:
|
|
18
|
+
* - trailing=true (default): uses the latest args/this seen while waiting
|
|
19
|
+
* - trailing=false: keeps the first args/this and ignores later calls until it fires
|
|
20
|
+
*
|
|
21
|
+
* AbortSignal:
|
|
22
|
+
* - If `signal` is provided, abort will cancel any pending scheduled execution.
|
|
23
|
+
* - Listener is attached only while a call is pending.
|
|
24
|
+
*
|
|
25
|
+
* @template {(...args: any[]) => any} T
|
|
26
|
+
* @param {T} fn
|
|
27
|
+
* @param {number|{signal?: AbortSignal, trailing?: boolean}} [frameSkipOrOptions=0]
|
|
28
|
+
* @param {{signal?: AbortSignal, trailing?: boolean}} [maybeOptions]
|
|
29
|
+
* @returns {T & { cancel: () => void, flush: () => void }}
|
|
30
|
+
*/
|
|
31
|
+
export function throttleRaf(fn, frameSkipOrOptions = 0, maybeOptions) {
|
|
32
|
+
if (typeof fn !== 'function') {
|
|
33
|
+
throw new TypeError(`Expected a function, got: ${typeof fn}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const frameSkip = typeof frameSkipOrOptions === 'number' ? frameSkipOrOptions : 0;
|
|
37
|
+
|
|
38
|
+
const options = isPlainOptions(frameSkipOrOptions)
|
|
39
|
+
? frameSkipOrOptions
|
|
40
|
+
: isPlainOptions(maybeOptions)
|
|
41
|
+
? maybeOptions
|
|
42
|
+
: {};
|
|
43
|
+
|
|
44
|
+
const signal = options.signal;
|
|
45
|
+
const trailing = options.trailing ?? true;
|
|
46
|
+
|
|
47
|
+
// Normalize once: int >= 0
|
|
48
|
+
const skip = Math.max(0, frameSkip | 0);
|
|
49
|
+
|
|
50
|
+
let scheduled = false;
|
|
51
|
+
let rafId = 0;
|
|
52
|
+
|
|
53
|
+
/** @type {any[] | null} */
|
|
54
|
+
let lastArgs = null;
|
|
55
|
+
/** @type {any | null} */
|
|
56
|
+
let lastThis = null;
|
|
57
|
+
|
|
58
|
+
// countdown for skip-path only
|
|
59
|
+
let remaining = skip;
|
|
60
|
+
|
|
61
|
+
const abort = pendingAbort(signal, () => cancel());
|
|
62
|
+
|
|
63
|
+
const clearState = () => {
|
|
64
|
+
scheduled = false;
|
|
65
|
+
|
|
66
|
+
if (rafId) cancelAnimationFrame(rafId);
|
|
67
|
+
rafId = 0;
|
|
68
|
+
|
|
69
|
+
remaining = skip;
|
|
70
|
+
|
|
71
|
+
lastArgs = null;
|
|
72
|
+
lastThis = null;
|
|
73
|
+
|
|
74
|
+
abort.remove();
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const cancel = () => {
|
|
78
|
+
clearState();
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const invoke = () => {
|
|
82
|
+
if (!lastArgs) return;
|
|
83
|
+
|
|
84
|
+
const args = lastArgs;
|
|
85
|
+
const ctx = lastThis;
|
|
86
|
+
|
|
87
|
+
// Clear first to aid GC + prevent re-entrancy oddities
|
|
88
|
+
lastArgs = null;
|
|
89
|
+
lastThis = null;
|
|
90
|
+
|
|
91
|
+
abort.remove();
|
|
92
|
+
fn.apply(ctx, args);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const fire = () => {
|
|
96
|
+
scheduled = false;
|
|
97
|
+
rafId = 0;
|
|
98
|
+
remaining = skip;
|
|
99
|
+
invoke();
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const tickSkip = () => {
|
|
103
|
+
if (!scheduled) return;
|
|
104
|
+
|
|
105
|
+
if (signal?.aborted) {
|
|
106
|
+
cancel();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (remaining === 0) {
|
|
111
|
+
fire();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
remaining -= 1;
|
|
116
|
+
rafId = requestAnimationFrame(tickSkip);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Fast path for skip === 0
|
|
120
|
+
const tick0 = () => {
|
|
121
|
+
if (!scheduled) return;
|
|
122
|
+
|
|
123
|
+
if (signal?.aborted) {
|
|
124
|
+
cancel();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
fire();
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const schedule =
|
|
132
|
+
skip === 0
|
|
133
|
+
? () => {
|
|
134
|
+
scheduled = true;
|
|
135
|
+
abort.add();
|
|
136
|
+
rafId = requestAnimationFrame(tick0);
|
|
137
|
+
}
|
|
138
|
+
: () => {
|
|
139
|
+
scheduled = true;
|
|
140
|
+
remaining = skip;
|
|
141
|
+
abort.add();
|
|
142
|
+
rafId = requestAnimationFrame(tickSkip);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/** @type {any} */
|
|
146
|
+
const throttled = function (...args) {
|
|
147
|
+
if (signal?.aborted) return;
|
|
148
|
+
|
|
149
|
+
// While waiting, keep latest args only if trailing=true
|
|
150
|
+
if (!scheduled || trailing) {
|
|
151
|
+
lastArgs = args;
|
|
152
|
+
lastThis = this;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (scheduled) return;
|
|
156
|
+
schedule();
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
throttled.cancel = cancel;
|
|
160
|
+
|
|
161
|
+
throttled.flush = () => {
|
|
162
|
+
if (!scheduled) return;
|
|
163
|
+
|
|
164
|
+
if (signal?.aborted) {
|
|
165
|
+
cancel();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Cancel RAF and invoke immediately
|
|
170
|
+
scheduled = false;
|
|
171
|
+
if (rafId) cancelAnimationFrame(rafId);
|
|
172
|
+
rafId = 0;
|
|
173
|
+
remaining = skip;
|
|
174
|
+
|
|
175
|
+
invoke();
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// If already aborted at creation time, ensure clean slate.
|
|
179
|
+
if (signal?.aborted) cancel();
|
|
180
|
+
|
|
181
|
+
return throttled;
|
|
182
182
|
}
|
package/src/raf/waitForRaf.js
CHANGED
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
// ./raf/waitForRaf.js
|
|
2
|
-
import { parseDuration } from '../parseDuration.js';
|
|
3
|
-
import { attachAbort,
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Waits for a condition to return truthy, polling each frame.
|
|
7
|
-
*
|
|
8
|
-
* Timing model:
|
|
9
|
-
* - Evaluates `condition()` once per animation frame until it returns truthy.
|
|
10
|
-
* - If `timeout` is provided, timeout is measured in RAF time (frame timestamps).
|
|
11
|
-
*
|
|
12
|
-
* @param {Function} condition - Function returning truthy when complete
|
|
13
|
-
* @param {Object} [options]
|
|
14
|
-
* @param {string|number} [options.timeout] - Optional max time to wait
|
|
15
|
-
* @param {AbortSignal} [options.signal] - Optional AbortSignal
|
|
16
|
-
* @param {boolean} [options.immediate=false] - If true, evaluate condition immediately before first frame
|
|
17
|
-
* @returns {Promise<void>} Resolves when condition is met, rejects on timeout/abort
|
|
18
|
-
*/
|
|
19
|
-
export function waitForRaf(condition, { timeout, signal, immediate = false } = {}) {
|
|
20
|
-
return new Promise((resolve, reject) => {
|
|
21
|
-
if (typeof condition !== 'function') {
|
|
22
|
-
reject(new TypeError(`waitForRaf: condition must be a function, got: ${typeof condition}`));
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (signal?.aborted) {
|
|
27
|
-
reject(
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const timeoutMs = timeout != null ? Math.max(0, parseDuration(timeout)) : null;
|
|
32
|
-
|
|
33
|
-
let rafId = 0;
|
|
34
|
-
let startedAt = null; // RAF timestamp of first tick (or immediate tick reference)
|
|
35
|
-
|
|
36
|
-
const cleanup = () => {
|
|
37
|
-
if (rafId) cancelAnimationFrame(rafId);
|
|
38
|
-
rafId = 0;
|
|
39
|
-
cleanupAbort();
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const onAbort = () => {
|
|
43
|
-
cleanup();
|
|
44
|
-
reject(
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const cleanupAbort = attachAbort(signal, onAbort);
|
|
48
|
-
|
|
49
|
-
const evaluateCondition = () => {
|
|
50
|
-
try {
|
|
51
|
-
return !!condition();
|
|
52
|
-
} catch (err) {
|
|
53
|
-
cleanup();
|
|
54
|
-
reject(err);
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// Optional immediate evaluation (before any frames)
|
|
60
|
-
if (immediate) {
|
|
61
|
-
const ok = evaluateCondition();
|
|
62
|
-
if (ok === null) return;
|
|
63
|
-
if (ok) {
|
|
64
|
-
cleanup();
|
|
65
|
-
resolve();
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// If timeout is explicitly 0, and immediate failed, we can fail fast.
|
|
70
|
-
if (timeoutMs === 0) {
|
|
71
|
-
cleanup();
|
|
72
|
-
reject(new Error('waitForRaf timed out'));
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const tick = (ts) => {
|
|
78
|
-
// Initialize start time using the same clock as `ts`
|
|
79
|
-
if (startedAt == null) startedAt = ts;
|
|
80
|
-
|
|
81
|
-
const ok = evaluateCondition();
|
|
82
|
-
if (ok === null) return;
|
|
83
|
-
|
|
84
|
-
if (ok) {
|
|
85
|
-
cleanup();
|
|
86
|
-
resolve();
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (timeoutMs != null && ts - startedAt >= timeoutMs) {
|
|
91
|
-
cleanup();
|
|
92
|
-
reject(new Error('waitForRaf timed out'));
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
rafId = requestAnimationFrame(tick);
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
rafId = requestAnimationFrame(tick);
|
|
100
|
-
});
|
|
1
|
+
// ./raf/waitForRaf.js
|
|
2
|
+
import { parseDuration } from '../parseDuration.js';
|
|
3
|
+
import { attachAbort, abortReason } from '../abort.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Waits for a condition to return truthy, polling each frame.
|
|
7
|
+
*
|
|
8
|
+
* Timing model:
|
|
9
|
+
* - Evaluates `condition()` once per animation frame until it returns truthy.
|
|
10
|
+
* - If `timeout` is provided, timeout is measured in RAF time (frame timestamps).
|
|
11
|
+
*
|
|
12
|
+
* @param {Function} condition - Function returning truthy when complete
|
|
13
|
+
* @param {Object} [options]
|
|
14
|
+
* @param {string|number} [options.timeout] - Optional max time to wait
|
|
15
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal; rejects with `signal.reason`
|
|
16
|
+
* @param {boolean} [options.immediate=false] - If true, evaluate condition immediately before first frame
|
|
17
|
+
* @returns {Promise<void>} Resolves when condition is met, rejects on timeout/abort
|
|
18
|
+
*/
|
|
19
|
+
export function waitForRaf(condition, { timeout, signal, immediate = false } = {}) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
if (typeof condition !== 'function') {
|
|
22
|
+
reject(new TypeError(`waitForRaf: condition must be a function, got: ${typeof condition}`));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (signal?.aborted) {
|
|
27
|
+
reject(abortReason(signal));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const timeoutMs = timeout != null ? Math.max(0, parseDuration(timeout)) : null;
|
|
32
|
+
|
|
33
|
+
let rafId = 0;
|
|
34
|
+
let startedAt = null; // RAF timestamp of first tick (or immediate tick reference)
|
|
35
|
+
|
|
36
|
+
const cleanup = () => {
|
|
37
|
+
if (rafId) cancelAnimationFrame(rafId);
|
|
38
|
+
rafId = 0;
|
|
39
|
+
cleanupAbort();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const onAbort = () => {
|
|
43
|
+
cleanup();
|
|
44
|
+
reject(abortReason(signal));
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const cleanupAbort = attachAbort(signal, onAbort);
|
|
48
|
+
|
|
49
|
+
const evaluateCondition = () => {
|
|
50
|
+
try {
|
|
51
|
+
return !!condition();
|
|
52
|
+
} catch (err) {
|
|
53
|
+
cleanup();
|
|
54
|
+
reject(err);
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// Optional immediate evaluation (before any frames)
|
|
60
|
+
if (immediate) {
|
|
61
|
+
const ok = evaluateCondition();
|
|
62
|
+
if (ok === null) return;
|
|
63
|
+
if (ok) {
|
|
64
|
+
cleanup();
|
|
65
|
+
resolve();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// If timeout is explicitly 0, and immediate failed, we can fail fast.
|
|
70
|
+
if (timeoutMs === 0) {
|
|
71
|
+
cleanup();
|
|
72
|
+
reject(new Error('waitForRaf timed out'));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const tick = (ts) => {
|
|
78
|
+
// Initialize start time using the same clock as `ts`
|
|
79
|
+
if (startedAt == null) startedAt = ts;
|
|
80
|
+
|
|
81
|
+
const ok = evaluateCondition();
|
|
82
|
+
if (ok === null) return;
|
|
83
|
+
|
|
84
|
+
if (ok) {
|
|
85
|
+
cleanup();
|
|
86
|
+
resolve();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (timeoutMs != null && ts - startedAt >= timeoutMs) {
|
|
91
|
+
cleanup();
|
|
92
|
+
reject(new Error('waitForRaf timed out'));
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
rafId = requestAnimationFrame(tick);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
rafId = requestAnimationFrame(tick);
|
|
100
|
+
});
|
|
101
101
|
}
|