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.
@@ -1,204 +1,204 @@
1
- // ./raf/afterRaf.js
2
- import { resolveDelayFnOptions } from '../resolveDelayAndFn.js';
3
- import { attachAbort } from '../abort.js';
4
- import { now } from '../now.js';
5
-
6
- /**
7
- * Schedules a one-time delayed function using requestAnimationFrame.
8
- *
9
- * Preferred:
10
- * - afterRaf(fn, duration, [options])
11
- *
12
- * Also supported (legacy):
13
- * - afterRaf(duration, fn, [onFinish], [options])
14
- *
15
- * Timing model:
16
- * - Uses frame timestamps for progression.
17
- * - Paused time does NOT count toward elapsed time (pause freezes remaining).
18
- *
19
- * @param {Function|string|number} a
20
- * @param {Function|string|number|Object} b
21
- * @param {Function|Object} [c] - legacy onFinish OR options
22
- * @param {Object} [d] - legacy options (when using 4 args)
23
- * @param {Function} [c.onFinish] - optional callback after finishing (preferred: in options)
24
- * @param {AbortSignal} [c.signal] - optional AbortSignal to auto-cancel
25
- * @returns {{
26
- * pause(): void,
27
- * resume(): void,
28
- * cancel(): void,
29
- * reset(): void,
30
- * readonly isRunning: boolean,
31
- * readonly isPaused: boolean,
32
- * readonly isFinished: boolean
33
- * }}
34
- */
35
- export function afterRaf(a, b, c, d) {
36
- const legacyOnFinish = typeof c === 'function' ? c : undefined;
37
- const maybeOptions = legacyOnFinish ? d : c;
38
-
39
- // Require explicit duration: defaultDelay = undefined
40
- const { fn, delay, options } = resolveDelayFnOptions(a, b, maybeOptions, undefined);
41
-
42
- const { signal, onFinish } = legacyOnFinish
43
- ? { ...options, onFinish: legacyOnFinish }
44
- : options;
45
-
46
- const totalMs = Math.max(0, delay);
47
-
48
- let rafId = 0;
49
-
50
- // remaining time until fire (pause-safe)
51
- let remaining = totalMs;
52
-
53
- // start timestamp for the current running segment
54
- let segmentStart = null; // number | null
55
-
56
- let running = false;
57
- let paused = false;
58
- let finished = false;
59
-
60
- const cancelFrame = () => {
61
- if (rafId) cancelAnimationFrame(rafId);
62
- rafId = 0;
63
- };
64
-
65
- let cleanupAbort = () => { };
66
-
67
- const cleanupAbortListener = () => {
68
- cleanupAbort();
69
- cleanupAbort = () => { };
70
- };
71
-
72
- const finish = () => {
73
- cancelFrame();
74
- running = false;
75
- paused = false;
76
- finished = true;
77
- segmentStart = null;
78
- remaining = 0;
79
- cleanupAbortListener();
80
- };
81
-
82
- const execFinish = () => {
83
- finish();
84
- fn?.();
85
- onFinish?.();
86
- };
87
-
88
- const cancel = () => {
89
- if (finished) return;
90
- finish();
91
- };
92
-
93
- const attachAbortListener = () => {
94
- cleanupAbortListener();
95
-
96
- // Preserve semantics: if already aborted, become terminal.
97
- if (signal?.aborted) {
98
- finished = true;
99
- remaining = 0;
100
- return;
101
- }
102
-
103
- cleanupAbort = attachAbort(signal, cancel);
104
- };
105
-
106
- // Attach abort for the lifetime of this timer instance
107
- attachAbortListener();
108
-
109
- const tick = (ts) => {
110
- if (!running || finished) return;
111
-
112
- if (segmentStart == null) segmentStart = ts;
113
-
114
- const elapsed = ts - segmentStart;
115
-
116
- if (elapsed >= remaining) {
117
- execFinish();
118
- return;
119
- }
120
-
121
- rafId = requestAnimationFrame(tick);
122
- };
123
-
124
- const pause = () => {
125
- if (!running || finished) return;
126
-
127
- cancelFrame();
128
-
129
- // Freeze remaining based on how long we ran in this segment
130
- const currentTime = now();
131
- if (segmentStart != null) {
132
- const elapsed = currentTime - segmentStart;
133
- remaining = Math.max(0, remaining - elapsed);
134
- }
135
-
136
- segmentStart = null;
137
- running = false;
138
- paused = true;
139
- };
140
-
141
- const resume = () => {
142
- if (finished || running || !paused) return;
143
- if (signal?.aborted) return;
144
-
145
- // If nothing remains, finish synchronously
146
- if (!(remaining > 0)) {
147
- execFinish();
148
- return;
149
- }
150
-
151
- paused = false;
152
- running = true;
153
- segmentStart = null; // will be set on first tick
154
- rafId = requestAnimationFrame(tick);
155
- };
156
-
157
- const reset = () => {
158
- cancelFrame();
159
-
160
- segmentStart = null;
161
- remaining = totalMs;
162
-
163
- running = false;
164
- paused = false;
165
- finished = false;
166
-
167
- // Re-attach abort on reset (more consistent with non-RAF timers)
168
- attachAbortListener();
169
-
170
- if (finished) {
171
- // signal was aborted during attachAbortListener()
172
- return;
173
- }
174
- };
175
-
176
- // Start immediately unless already aborted/finished
177
- if (!finished) {
178
- // Support 0ms: fire on next frame (consistent with RAF timing)
179
- if (!(remaining > 0)) {
180
- execFinish();
181
- } else {
182
- running = true;
183
- rafId = requestAnimationFrame(tick);
184
- }
185
- } else {
186
- cleanupAbortListener();
187
- }
188
-
189
- return {
190
- pause,
191
- resume,
192
- cancel,
193
- reset,
194
- get isRunning() {
195
- return running;
196
- },
197
- get isPaused() {
198
- return paused;
199
- },
200
- get isFinished() {
201
- return finished;
202
- },
203
- };
1
+ // ./raf/afterRaf.js
2
+ import { resolveDelayFnOptions } from '../resolveDelayAndFn.js';
3
+ import { attachAbort } from '../abort.js';
4
+ import { now } from '../now.js';
5
+
6
+ /**
7
+ * Schedules a one-time delayed function using requestAnimationFrame.
8
+ *
9
+ * Preferred:
10
+ * - afterRaf(fn, duration, [options])
11
+ *
12
+ * Also supported (legacy):
13
+ * - afterRaf(duration, fn, [onFinish], [options])
14
+ *
15
+ * Timing model:
16
+ * - Uses frame timestamps for progression.
17
+ * - Paused time does NOT count toward elapsed time (pause freezes remaining).
18
+ *
19
+ * @param {Function|string|number} a
20
+ * @param {Function|string|number|Object} b
21
+ * @param {Function|Object} [c] - legacy onFinish OR options
22
+ * @param {Object} [d] - legacy options (when using 4 args)
23
+ * @param {Function} [c.onFinish] - optional callback after finishing (preferred: in options)
24
+ * @param {AbortSignal} [c.signal] - optional AbortSignal to auto-cancel
25
+ * @returns {{
26
+ * pause(): void,
27
+ * resume(): void,
28
+ * cancel(): void,
29
+ * reset(): void,
30
+ * readonly isRunning: boolean,
31
+ * readonly isPaused: boolean,
32
+ * readonly isFinished: boolean
33
+ * }}
34
+ */
35
+ export function afterRaf(a, b, c, d) {
36
+ const legacyOnFinish = typeof c === 'function' ? c : undefined;
37
+ const maybeOptions = legacyOnFinish ? d : c;
38
+
39
+ // Require explicit duration: defaultDelay = undefined
40
+ const { fn, delay, options } = resolveDelayFnOptions(a, b, maybeOptions, undefined);
41
+
42
+ const { signal, onFinish } = legacyOnFinish
43
+ ? { ...options, onFinish: legacyOnFinish }
44
+ : options;
45
+
46
+ const totalMs = Math.max(0, delay);
47
+
48
+ let rafId = 0;
49
+
50
+ // remaining time until fire (pause-safe)
51
+ let remaining = totalMs;
52
+
53
+ // start timestamp for the current running segment
54
+ let segmentStart = null; // number | null
55
+
56
+ let running = false;
57
+ let paused = false;
58
+ let finished = false;
59
+
60
+ const cancelFrame = () => {
61
+ if (rafId) cancelAnimationFrame(rafId);
62
+ rafId = 0;
63
+ };
64
+
65
+ let cleanupAbort = () => { };
66
+
67
+ const cleanupAbortListener = () => {
68
+ cleanupAbort();
69
+ cleanupAbort = () => { };
70
+ };
71
+
72
+ const finish = () => {
73
+ cancelFrame();
74
+ running = false;
75
+ paused = false;
76
+ finished = true;
77
+ segmentStart = null;
78
+ remaining = 0;
79
+ cleanupAbortListener();
80
+ };
81
+
82
+ const execFinish = () => {
83
+ finish();
84
+ fn?.();
85
+ onFinish?.();
86
+ };
87
+
88
+ const cancel = () => {
89
+ if (finished) return;
90
+ finish();
91
+ };
92
+
93
+ const attachAbortListener = () => {
94
+ cleanupAbortListener();
95
+
96
+ // Preserve semantics: if already aborted, become terminal.
97
+ if (signal?.aborted) {
98
+ finished = true;
99
+ remaining = 0;
100
+ return;
101
+ }
102
+
103
+ cleanupAbort = attachAbort(signal, cancel);
104
+ };
105
+
106
+ // Attach abort for the lifetime of this timer instance
107
+ attachAbortListener();
108
+
109
+ const tick = (ts) => {
110
+ if (!running || finished) return;
111
+
112
+ if (segmentStart == null) segmentStart = ts;
113
+
114
+ const elapsed = ts - segmentStart;
115
+
116
+ if (elapsed >= remaining) {
117
+ execFinish();
118
+ return;
119
+ }
120
+
121
+ rafId = requestAnimationFrame(tick);
122
+ };
123
+
124
+ const pause = () => {
125
+ if (!running || finished) return;
126
+
127
+ cancelFrame();
128
+
129
+ // Freeze remaining based on how long we ran in this segment
130
+ const currentTime = now();
131
+ if (segmentStart != null) {
132
+ const elapsed = currentTime - segmentStart;
133
+ remaining = Math.max(0, remaining - elapsed);
134
+ }
135
+
136
+ segmentStart = null;
137
+ running = false;
138
+ paused = true;
139
+ };
140
+
141
+ const resume = () => {
142
+ if (finished || running || !paused) return;
143
+ if (signal?.aborted) return;
144
+
145
+ // If nothing remains, finish synchronously
146
+ if (!(remaining > 0)) {
147
+ execFinish();
148
+ return;
149
+ }
150
+
151
+ paused = false;
152
+ running = true;
153
+ segmentStart = null; // will be set on first tick
154
+ rafId = requestAnimationFrame(tick);
155
+ };
156
+
157
+ const reset = () => {
158
+ cancelFrame();
159
+
160
+ segmentStart = null;
161
+ remaining = totalMs;
162
+
163
+ running = false;
164
+ paused = false;
165
+ finished = false;
166
+
167
+ // Re-attach abort on reset (more consistent with non-RAF timers)
168
+ attachAbortListener();
169
+
170
+ if (finished) {
171
+ // signal was aborted during attachAbortListener()
172
+ return;
173
+ }
174
+ };
175
+
176
+ // Start immediately unless already aborted/finished
177
+ if (!finished) {
178
+ // Support 0ms: fire on next frame (consistent with RAF timing)
179
+ if (!(remaining > 0)) {
180
+ execFinish();
181
+ } else {
182
+ running = true;
183
+ rafId = requestAnimationFrame(tick);
184
+ }
185
+ } else {
186
+ cleanupAbortListener();
187
+ }
188
+
189
+ return {
190
+ pause,
191
+ resume,
192
+ cancel,
193
+ reset,
194
+ get isRunning() {
195
+ return running;
196
+ },
197
+ get isPaused() {
198
+ return paused;
199
+ },
200
+ get isFinished() {
201
+ return finished;
202
+ },
203
+ };
204
204
  }
@@ -1,127 +1,127 @@
1
- // ./raf/debounceRaf.js
2
- import { resolveDelayFnOptions } from '../resolveDelayAndFn.js';
3
- import { pendingAbort } from '../abort.js';
4
- import { now } from '../now.js';
5
-
6
- /**
7
- * Debounces a function using requestAnimationFrame.
8
- *
9
- * Preferred:
10
- * - debounceRaf(fn, [duration], [options]) // duration defaults to 0 (next frame)
11
- *
12
- * Also supported:
13
- * - debounceRaf(duration, fn, [options])
14
- * - debounceRaf(fn, [options])
15
- *
16
- * Timing model:
17
- * - Waits until `ms` of inactivity have passed (measured using frame timestamps).
18
- * - `ms = 0` means "run on the next animation frame" (still debounced).
19
- *
20
- * Supports:
21
- * - `.cancel()` clears any pending invocation.
22
- * - `.flush()` immediately invokes if pending.
23
- * - Optional AbortSignal: abort cancels any pending invocation.
24
- *
25
- * @param {Function|string|number} a - function or duration
26
- * @param {Function|string|number|Object} [b] - duration, fn, or options
27
- * @param {Object} [c] - options (3-arg form)
28
- * @param {AbortSignal} [c.signal] - AbortSignal to auto-cancel pending work
29
- * @returns {((...args: any[]) => void) & { cancel: () => void, flush: () => void }}
30
- */
31
- export function debounceRaf(a, b, c) {
32
- // duration is optional for debounceRaf (defaults to 0)
33
- const { fn, delay, options } = resolveDelayFnOptions(a, b, c, 0);
34
-
35
- const ms = Math.max(0, delay);
36
- const signal = options?.signal;
37
-
38
- let rafId = 0;
39
-
40
- /** @type {any[] | null} */
41
- let lastArgs = null;
42
- /** @type {any | null} */
43
- let lastThis = null;
44
-
45
- // last time the debounced function was called (used for inactivity measurement)
46
- let lastCallTime = 0;
47
-
48
- const clearRaf = () => {
49
- if (rafId) cancelAnimationFrame(rafId);
50
- rafId = 0;
51
- };
52
-
53
- const abort = pendingAbort(signal, () => cancel());
54
-
55
- const cancel = () => {
56
- clearRaf();
57
- lastArgs = null;
58
- lastThis = null;
59
- lastCallTime = 0;
60
- abort.remove();
61
- };
62
-
63
- const invoke = () => {
64
- if (!lastArgs) return;
65
-
66
- const args = lastArgs;
67
- const ctx = lastThis;
68
-
69
- // Clear first (re-entrancy safe + GC friendly)
70
- lastArgs = null;
71
- lastThis = null;
72
-
73
- abort.remove();
74
- fn.apply(ctx, args);
75
- };
76
-
77
- const tick = (ts) => {
78
- if (!rafId) return; // canceled
79
-
80
- if (!lastArgs) {
81
- // Nothing pending; clean slate
82
- cancel();
83
- return;
84
- }
85
-
86
- const elapsed = ts - lastCallTime;
87
-
88
- if (elapsed >= ms) {
89
- // Stop scheduling first, then invoke
90
- clearRaf();
91
- invoke();
92
- return;
93
- }
94
-
95
- rafId = requestAnimationFrame(tick);
96
- };
97
-
98
- /** @type {any} */
99
- const debounced = function (...args) {
100
- if (signal?.aborted) return;
101
-
102
- lastArgs = args;
103
- lastThis = this;
104
-
105
- // Use monotonic clock for call timestamp; RAF tick uses its own timestamp.
106
- lastCallTime = now();
107
-
108
- // Ensure abort listener exists only while pending work exists
109
- abort.add();
110
-
111
- clearRaf();
112
- rafId = requestAnimationFrame(tick);
113
- };
114
-
115
- debounced.cancel = cancel;
116
-
117
- debounced.flush = () => {
118
- if (!rafId || !lastArgs) return;
119
- clearRaf();
120
- invoke();
121
- };
122
-
123
- // If already aborted at creation time, ensure clean slate.
124
- if (signal?.aborted) cancel();
125
-
126
- return debounced;
1
+ // ./raf/debounceRaf.js
2
+ import { resolveDelayFnOptions } from '../resolveDelayAndFn.js';
3
+ import { pendingAbort } from '../abort.js';
4
+ import { now } from '../now.js';
5
+
6
+ /**
7
+ * Debounces a function using requestAnimationFrame.
8
+ *
9
+ * Preferred:
10
+ * - debounceRaf(fn, [duration], [options]) // duration defaults to 0 (next frame)
11
+ *
12
+ * Also supported:
13
+ * - debounceRaf(duration, fn, [options])
14
+ * - debounceRaf(fn, [options])
15
+ *
16
+ * Timing model:
17
+ * - Waits until `ms` of inactivity have passed (measured using frame timestamps).
18
+ * - `ms = 0` means "run on the next animation frame" (still debounced).
19
+ *
20
+ * Supports:
21
+ * - `.cancel()` clears any pending invocation.
22
+ * - `.flush()` immediately invokes if pending.
23
+ * - Optional AbortSignal: abort cancels any pending invocation.
24
+ *
25
+ * @param {Function|string|number} a - function or duration
26
+ * @param {Function|string|number|Object} [b] - duration, fn, or options
27
+ * @param {Object} [c] - options (3-arg form)
28
+ * @param {AbortSignal} [c.signal] - AbortSignal to auto-cancel pending work
29
+ * @returns {((...args: any[]) => void) & { cancel: () => void, flush: () => void }}
30
+ */
31
+ export function debounceRaf(a, b, c) {
32
+ // duration is optional for debounceRaf (defaults to 0)
33
+ const { fn, delay, options } = resolveDelayFnOptions(a, b, c, 0);
34
+
35
+ const ms = Math.max(0, delay);
36
+ const signal = options?.signal;
37
+
38
+ let rafId = 0;
39
+
40
+ /** @type {any[] | null} */
41
+ let lastArgs = null;
42
+ /** @type {any | null} */
43
+ let lastThis = null;
44
+
45
+ // last time the debounced function was called (used for inactivity measurement)
46
+ let lastCallTime = 0;
47
+
48
+ const clearRaf = () => {
49
+ if (rafId) cancelAnimationFrame(rafId);
50
+ rafId = 0;
51
+ };
52
+
53
+ const abort = pendingAbort(signal, () => cancel());
54
+
55
+ const cancel = () => {
56
+ clearRaf();
57
+ lastArgs = null;
58
+ lastThis = null;
59
+ lastCallTime = 0;
60
+ abort.remove();
61
+ };
62
+
63
+ const invoke = () => {
64
+ if (!lastArgs) return;
65
+
66
+ const args = lastArgs;
67
+ const ctx = lastThis;
68
+
69
+ // Clear first (re-entrancy safe + GC friendly)
70
+ lastArgs = null;
71
+ lastThis = null;
72
+
73
+ abort.remove();
74
+ fn.apply(ctx, args);
75
+ };
76
+
77
+ const tick = (ts) => {
78
+ if (!rafId) return; // canceled
79
+
80
+ if (!lastArgs) {
81
+ // Nothing pending; clean slate
82
+ cancel();
83
+ return;
84
+ }
85
+
86
+ const elapsed = ts - lastCallTime;
87
+
88
+ if (elapsed >= ms) {
89
+ // Stop scheduling first, then invoke
90
+ clearRaf();
91
+ invoke();
92
+ return;
93
+ }
94
+
95
+ rafId = requestAnimationFrame(tick);
96
+ };
97
+
98
+ /** @type {any} */
99
+ const debounced = function (...args) {
100
+ if (signal?.aborted) return;
101
+
102
+ lastArgs = args;
103
+ lastThis = this;
104
+
105
+ // Use monotonic clock for call timestamp; RAF tick uses its own timestamp.
106
+ lastCallTime = now();
107
+
108
+ // Ensure abort listener exists only while pending work exists
109
+ abort.add();
110
+
111
+ clearRaf();
112
+ rafId = requestAnimationFrame(tick);
113
+ };
114
+
115
+ debounced.cancel = cancel;
116
+
117
+ debounced.flush = () => {
118
+ if (!rafId || !lastArgs) return;
119
+ clearRaf();
120
+ invoke();
121
+ };
122
+
123
+ // If already aborted at creation time, ensure clean slate.
124
+ if (signal?.aborted) cancel();
125
+
126
+ return debounced;
127
127
  }