timeout-flow 0.0.17 → 0.0.19

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.
Files changed (49) hide show
  1. package/ADDITIONAL_TERMS.md +122 -0
  2. package/LICENSE +694 -0
  3. package/README.md +307 -237
  4. package/dist/timeout-flow.min.js +1 -6
  5. package/package.json +68 -58
  6. package/robots.txt +79 -3
  7. package/src/AfterTimer.js +82 -21
  8. package/src/EveryTimer.js +209 -88
  9. package/src/TimeoutFlow.js +400 -227
  10. package/src/TimerBase.js +133 -75
  11. package/src/abort.js +75 -0
  12. package/src/after.js +32 -83
  13. package/src/debounce.js +106 -40
  14. package/src/every.js +34 -120
  15. package/src/index.js +24 -19
  16. package/src/now.js +23 -0
  17. package/src/parseDuration.js +46 -44
  18. package/src/raf/ReadMe-Raf.md +94 -94
  19. package/src/raf/afterRaf.js +204 -72
  20. package/src/raf/debounceRaf.js +127 -45
  21. package/src/raf/everyRaf.js +317 -106
  22. package/src/raf/index.js +28 -8
  23. package/src/raf/throttleRaf.js +182 -38
  24. package/src/raf/waitForRaf.js +101 -33
  25. package/src/resolveDelayAndFn.js +90 -28
  26. package/src/retry.js +164 -24
  27. package/src/throttle.js +141 -47
  28. package/src/waitFor.js +77 -33
  29. package/types/AfterTimer.d.ts +8 -2
  30. package/types/EveryTimer.d.ts +21 -4
  31. package/types/TimeoutFlow.d.ts +6 -18
  32. package/types/TimerBase.d.ts +15 -4
  33. package/types/abort.d.ts +31 -0
  34. package/types/after.d.ts +22 -22
  35. package/types/debounce.d.ts +28 -6
  36. package/types/every.d.ts +20 -28
  37. package/types/index.d.ts +1 -1
  38. package/types/now.d.ts +1 -0
  39. package/types/raf/afterRaf.d.ts +35 -5
  40. package/types/raf/debounceRaf.d.ts +27 -6
  41. package/types/raf/everyRaf.d.ts +39 -9
  42. package/types/raf/index.d.ts +6 -5
  43. package/types/raf/throttleRaf.d.ts +33 -10
  44. package/types/raf/waitForRaf.d.ts +10 -4
  45. package/types/resolveDelayAndFn.d.ts +27 -0
  46. package/types/retry.d.ts +25 -8
  47. package/types/throttle.d.ts +28 -6
  48. package/types/waitFor.d.ts +11 -6
  49. package/LICENSE.MD +0 -285
@@ -1,72 +1,204 @@
1
- // ./raf/afterRaf.js
2
-
3
- import { parseDuration } from '../parseDuration.js';
4
-
5
- /**
6
- * Schedules a one-time delayed function using requestAnimationFrame.
7
- * @param {string|number} duration - e.g. "5s", "500ms", 2000
8
- * @param {Function} fn - callback to run
9
- * @param {Function} [onFinish] - optional callback after finishing
10
- * @returns {Object} - Control methods and status flags
11
- */
12
- export function afterRaf(duration, fn, onFinish) {
13
- const ms = typeof duration === 'string' ? parseDuration(duration) : duration;
14
- let handle = null;
15
- let startTime = null;
16
- let pausedAt = null;
17
- let elapsedPaused = 0;
18
- let running = true;
19
-
20
- const tick = (timestamp) => {
21
- if (!running) return;
22
-
23
- if (startTime == null) {
24
- startTime = timestamp;
25
- }
26
-
27
- const elapsed = timestamp - startTime + elapsedPaused;
28
-
29
- if (elapsed >= ms) {
30
- running = false;
31
- fn?.();
32
- onFinish?.();
33
- } else {
34
- handle = requestAnimationFrame(tick);
35
- }
36
- };
37
-
38
- const pause = () => {
39
- if (running && handle !== null) {
40
- cancelAnimationFrame(handle);
41
- pausedAt = performance.now();
42
- running = false;
43
- }
44
- };
45
-
46
- const resume = () => {
47
- if (!running && pausedAt !== null) {
48
- elapsedPaused += performance.now() - pausedAt;
49
- running = true;
50
- handle = requestAnimationFrame(tick);
51
- }
52
- };
53
-
54
- const cancel = () => {
55
- if (handle !== null) {
56
- cancelAnimationFrame(handle);
57
- handle = null;
58
- }
59
- running = false;
60
- };
61
-
62
- // Start immediately
63
- handle = requestAnimationFrame(tick);
64
-
65
- return {
66
- pause,
67
- resume,
68
- cancel,
69
- get isRunning() { return running; }
70
- };
71
- }
72
-
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
+ }
@@ -1,45 +1,127 @@
1
- // ./raf/debounceRaf.js
2
-
3
- import { parseDuration } from '../parseDuration.js';
4
-
5
- /**
6
- * Debounces a function using requestAnimationFrame.
7
- * - If no duration is passed, triggers on the next frame.
8
- * - If duration is passed, waits until N ms of inactivity using frame-based timing.
9
- *
10
- * @param {string|number|Function} durationOrFn - Duration (e.g., "300ms") or fn directly
11
- * @param {Function} [callbackFn] - Callback (if duration is passed as first arg)
12
- * @returns {Function} debounced function
13
- */
14
- export function debounceRaf(durationOrFn, callbackFn) {
15
- const [ms, fn] =
16
- typeof durationOrFn === 'function'
17
- ? [0, durationOrFn] // ← next frame, no time threshold
18
- : [parseDuration(durationOrFn), callbackFn];
19
-
20
- let frame = null;
21
- let lastArgs = null;
22
- let lastCallTime = 0;
23
-
24
- const tick = (timestamp) => {
25
- const elapsed = timestamp - lastCallTime;
26
-
27
- if (elapsed >= ms) {
28
- fn(...lastArgs);
29
- frame = null;
30
- } else {
31
- frame = requestAnimationFrame(tick);
32
- }
33
- };
34
-
35
- return (...args) => {
36
- lastArgs = args;
37
- lastCallTime = performance.now();
38
-
39
- if (frame !== null) {
40
- cancelAnimationFrame(frame);
41
- }
42
-
43
- frame = requestAnimationFrame(tick);
44
- };
45
- }
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
+ }