timeout-flow 0.0.18 → 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/src/context.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  ===== Context dump for folder: C:\Users\PC User\GitHub\TimeoutFlow-js\src =====
2
2
  Extension: .js
3
- Generated on: 26/02/23 11:37:00.98
3
+ Generated on: 26/02/23 13:56:31.70
4
4
 
5
5
 
6
6
  ===== FILE: abort.js =====
@@ -87,28 +87,36 @@ export function pendingAbort(signal, onAbort) {
87
87
 
88
88
  // ./src/after.js
89
89
  import { AfterTimer } from './AfterTimer.js';
90
-
90
+ import { resolveDelayFnOptions } from './resolveDelayAndFn.js';
91
91
 
92
92
  /**
93
93
  * Schedules a one-time delayed function.
94
94
  *
95
- * @param {string|number} duration - e.g. '1s', 500, '2m'
96
- * @param {Function} fn - Main function to run
97
- * @param {Object} [options]
98
- * @param {Function} [options.onFinish] - Optional post-run callback
99
- * @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel
95
+ * Preferred:
96
+ * - after(fn, duration, [options])
97
+ *
98
+ * Also supported (legacy/alt):
99
+ * - after(duration, fn, [options])
100
+ *
101
+ * @param {Function|string|number} a - Function or duration (see overloads)
102
+ * @param {Function|string|number|Object} b - Duration, function, or options
103
+ * @param {Object} [c] - Options
104
+ * @param {Function} [c.onFinish] - Optional callback after `fn` runs
105
+ * @param {AbortSignal} [c.signal] - Optional AbortSignal to auto-cancel
100
106
  * @returns {{
101
- * pause(): void,
102
- * resume(): void,
103
- * cancel(): void,
104
- * reset(): void,
105
- * readonly isRunning: boolean,
106
- * readonly isPaused: boolean,
107
- * readonly isFinished: boolean
108
- * }}
109
- */
110
- export function after(duration, fn, options) {
111
- return new AfterTimer(duration, fn, options);
107
+ * pause(): void,
108
+ * resume(): void,
109
+ * cancel(): void,
110
+ * reset(): void,
111
+ * readonly isRunning: boolean,
112
+ * readonly isPaused: boolean,
113
+ * readonly isFinished: boolean
114
+ * }}
115
+ */
116
+ export function after(a, b, c) {
117
+ // require explicit duration: defaultDelay = undefined
118
+ const { fn, delay, options } = resolveDelayFnOptions(a, b, c, undefined);
119
+ return new AfterTimer(delay, fn, options);
112
120
  }
113
121
  ===== END FILE: after.js =====
114
122
 
@@ -218,20 +226,23 @@ import { pendingAbort } from './abort.js';
218
226
  * Creates a debounced function that delays invoking `fn`
219
227
  * until after `delay` ms of inactivity.
220
228
  *
229
+ * Preferred:
230
+ * - debounce(fn, [delay], [options]) // delay defaults to 0
231
+ *
232
+ * Also supported:
233
+ * - debounce(delay, fn, [options]) // legacy/alt ordering
234
+ * - debounce(fn, [options])
235
+ *
221
236
  * Notes:
222
237
  * - Always invokes with the latest arguments/context.
223
238
  * - `.cancel()` clears any pending invocation.
224
239
  * - `.flush()` immediately invokes if pending.
225
240
  * - Optional AbortSignal: abort cancels pending invocation.
226
241
  *
227
- * Overloads:
228
- * - debounce(fn, delay, [options])
229
- * - debounce(delay, fn, [options])
230
- * - debounce(fn, [options]) // delay defaults to 0
231
- *
232
- * @param {string|number|Function} a - Delay in ms or the function to debounce.
233
- * @param {string|number|Function|Object} [b] - The fn, delay, or options.
234
- * @param {Object} [c] - Options (when using 3 args).
242
+ * @param {Function|string|number} a - Function or delay (see overloads)
243
+ * @param {Function|string|number|Object} [b] - Delay, function, or options
244
+ * @param {Object} [c] - Options (3-arg form)
245
+ * @param {AbortSignal} [c.signal] - Optional AbortSignal to cancel pending work
235
246
  * @returns {DebouncedFunction}
236
247
  */
237
248
  export function debounce(a, b, c) {
@@ -312,16 +323,23 @@ export function debounce(a, b, c) {
312
323
 
313
324
  // ./src/every.js
314
325
  import { EveryTimer } from './EveryTimer.js';
326
+ import { resolveDelayFnOptions } from './resolveDelayAndFn.js';
315
327
 
316
328
  /**
317
329
  * Runs a function every N ms with optional execution limit.
318
330
  *
319
- * @param {string|number} duration - e.g. '500ms', 1000
320
- * @param {Function} fn - Function to execute
321
- * @param {Object} [options]
322
- * @param {number} [options.max=Infinity] - Max times to run
323
- * @param {boolean} [options.runImmediately=false] - Run immediately on first tick
324
- * @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel
331
+ * Preferred:
332
+ * - every(fn, duration, [options])
333
+ *
334
+ * Also supported (legacy/alt):
335
+ * - every(duration, fn, [options])
336
+ *
337
+ * @param {Function|string|number} a - Function or duration (see overloads)
338
+ * @param {Function|string|number|Object} b - Duration, function, or options
339
+ * @param {Object} [c] - Options
340
+ * @param {number} [c.max=Infinity] - Max times to run
341
+ * @param {boolean} [c.runImmediately=false] - Run immediately on first tick
342
+ * @param {AbortSignal} [c.signal] - Optional AbortSignal to auto-cancel
325
343
  * @returns {{
326
344
  * pause(): void,
327
345
  * resume(): void,
@@ -333,11 +351,11 @@ import { EveryTimer } from './EveryTimer.js';
333
351
  * readonly count: number
334
352
  * }}
335
353
  */
336
- export function every(duration, fn, options) {
337
- return new EveryTimer(duration, fn, options);
354
+ export function every(a, b, c) {
355
+ // require explicit duration: defaultDelay = undefined
356
+ const { fn, delay, options } = resolveDelayFnOptions(a, b, c, undefined);
357
+ return new EveryTimer(delay, fn, options);
338
358
  }
339
-
340
-
341
359
  ===== END FILE: every.js =====
342
360
 
343
361
 
@@ -948,22 +966,24 @@ import { now } from './now.js'
948
966
  * Creates a throttled version of a function.
949
967
  * Executes immediately, throttles further calls, optional trailing execution.
950
968
  *
969
+ * Preferred:
970
+ * - throttle(fn, delay, [options])
971
+ *
972
+ * Also supported:
973
+ * - throttle(delay, fn, [options]) // legacy/alt ordering
974
+ *
951
975
  * Notes:
952
- * - Uses performance.now() for elapsed timing.
976
+ * - Uses performance.now() (via `now()`) for elapsed timing.
953
977
  * - If trailing is enabled, uses the latest arguments/context seen during the wait.
954
978
  * - `.cancel()` clears any pending trailing invocation.
955
979
  * - `.flush()` immediately invokes a pending trailing call (if scheduled).
956
980
  * - Optional AbortSignal: abort cancels any pending trailing invocation.
957
981
  *
958
- * Supported forms:
959
- * - throttle(fn, delay, [options])
960
- * - throttle(delay, fn, [options])
961
- *
962
- * @param {Function|string|number} a - Function or delay
963
- * @param {Function|string|number|Object} b - Delay, function, or options (only when delay defaults are used)
964
- * @param {Object} [c] - options
965
- * @param {boolean} [c.trailing=true] - If true, fire once at trailing edge
966
- * @param {AbortSignal} [c.signal] - Optional AbortSignal to auto-cancel
982
+ * @param {Function|string|number} a
983
+ * @param {Function|string|number|Object} b
984
+ * @param {Object} [c]
985
+ * @param {boolean} [c.trailing=true]
986
+ * @param {AbortSignal} [c.signal]
967
987
  * @returns {ThrottledFunction}
968
988
  */
969
989
  export function throttle(a, b, c) {
@@ -1253,18 +1273,22 @@ export function flow({ signal } = {}) {
1253
1273
 
1254
1274
  if (step.type === 'after') {
1255
1275
  step.controller = after(
1256
- step.duration,
1257
1276
  () => safeCall(step.fn, step),
1258
- () => incrementStep(step)
1277
+ step.duration,
1278
+ { signal, onFinish: () => incrementStep(step) }
1259
1279
  );
1260
1280
  return;
1261
1281
  }
1262
1282
 
1263
1283
  if (step.type === 'every') {
1264
- let count = 0;
1265
1284
 
1285
+ // No ticks will ever fire => advance immediately.
1286
+ if (!(step.times > 0) && step.times !== Infinity) {
1287
+ return incrementStep(step);
1288
+ }
1289
+
1290
+ let count = 0;
1266
1291
  const ctrl = every(
1267
- step.duration,
1268
1292
  () => {
1269
1293
  if (isPaused || isCancelled || isFinished) return;
1270
1294
  if (signal?.aborted) {
@@ -1291,7 +1315,8 @@ export function flow({ signal } = {}) {
1291
1315
  return incrementStep(step);
1292
1316
  }
1293
1317
  },
1294
- step.times
1318
+ step.duration,
1319
+ { max: step.times, signal }
1295
1320
  );
1296
1321
 
1297
1322
  step.controller = ctrl;
@@ -1304,7 +1329,15 @@ export function flow({ signal } = {}) {
1304
1329
  return runner;
1305
1330
  },
1306
1331
 
1307
- every(duration, fn, times = Infinity) {
1332
+ every(duration, fn, timesOrOptions = Infinity) {
1333
+ const raw =
1334
+ typeof timesOrOptions === 'number'
1335
+ ? timesOrOptions
1336
+ : timesOrOptions?.max ?? Infinity;
1337
+
1338
+ const times =
1339
+ raw === Infinity ? Infinity : Math.max(0, raw | 0); // int >= 0
1340
+
1308
1341
  steps.push({
1309
1342
  type: 'every',
1310
1343
  duration,
@@ -1314,6 +1347,7 @@ export function flow({ signal } = {}) {
1314
1347
  doWhileCondition,
1315
1348
  controller: null,
1316
1349
  });
1350
+
1317
1351
  whileCondition = null;
1318
1352
  doWhileCondition = null;
1319
1353
  return runner;
@@ -1688,22 +1722,29 @@ export function waitFor(condition, { interval = '250ms', timeout, immediate = fa
1688
1722
  ===== FILE: raf\afterRaf.js =====
1689
1723
 
1690
1724
  // ./raf/afterRaf.js
1691
- import { parseDuration } from '../parseDuration.js';
1725
+ import { resolveDelayFnOptions } from '../resolveDelayAndFn.js';
1692
1726
  import { attachAbort } from '../abort.js';
1693
- import { now } from '../now.js'
1727
+ import { now } from '../now.js';
1694
1728
 
1695
1729
  /**
1696
1730
  * Schedules a one-time delayed function using requestAnimationFrame.
1697
1731
  *
1732
+ * Preferred:
1733
+ * - afterRaf(fn, duration, [options])
1734
+ *
1735
+ * Also supported (legacy):
1736
+ * - afterRaf(duration, fn, [onFinish], [options])
1737
+ *
1698
1738
  * Timing model:
1699
1739
  * - Uses frame timestamps for progression.
1700
1740
  * - Paused time does NOT count toward elapsed time (pause freezes remaining).
1701
1741
  *
1702
- * @param {string|number} duration - e.g. "5s", "500ms", 2000
1703
- * @param {Function} fn - callback to run
1704
- * @param {Function} [onFinish] - optional callback after finishing
1705
- * @param {Object} [options]
1706
- * @param {AbortSignal} [options.signal] - optional AbortSignal to auto-cancel
1742
+ * @param {Function|string|number} a
1743
+ * @param {Function|string|number|Object} b
1744
+ * @param {Function|Object} [c] - legacy onFinish OR options
1745
+ * @param {Object} [d] - legacy options (when using 4 args)
1746
+ * @param {Function} [c.onFinish] - optional callback after finishing (preferred: in options)
1747
+ * @param {AbortSignal} [c.signal] - optional AbortSignal to auto-cancel
1707
1748
  * @returns {{
1708
1749
  * pause(): void,
1709
1750
  * resume(): void,
@@ -1714,8 +1755,18 @@ import { now } from '../now.js'
1714
1755
  * readonly isFinished: boolean
1715
1756
  * }}
1716
1757
  */
1717
- export function afterRaf(duration, fn, onFinish, { signal } = {}) {
1718
- const totalMs = parseDuration(duration);
1758
+ export function afterRaf(a, b, c, d) {
1759
+ const legacyOnFinish = typeof c === 'function' ? c : undefined;
1760
+ const maybeOptions = legacyOnFinish ? d : c;
1761
+
1762
+ // Require explicit duration: defaultDelay = undefined
1763
+ const { fn, delay, options } = resolveDelayFnOptions(a, b, maybeOptions, undefined);
1764
+
1765
+ const { signal, onFinish } = legacyOnFinish
1766
+ ? { ...options, onFinish: legacyOnFinish }
1767
+ : options;
1768
+
1769
+ const totalMs = Math.max(0, delay);
1719
1770
 
1720
1771
  let rafId = 0;
1721
1772
 
@@ -1734,33 +1785,49 @@ export function afterRaf(duration, fn, onFinish, { signal } = {}) {
1734
1785
  rafId = 0;
1735
1786
  };
1736
1787
 
1737
- // Will be replaced by attachAbort() if signal is provided.
1738
1788
  let cleanupAbort = () => { };
1739
1789
 
1740
- function finish() {
1790
+ const cleanupAbortListener = () => {
1791
+ cleanupAbort();
1792
+ cleanupAbort = () => { };
1793
+ };
1794
+
1795
+ const finish = () => {
1741
1796
  cancelFrame();
1742
1797
  running = false;
1743
1798
  paused = false;
1744
1799
  finished = true;
1745
1800
  segmentStart = null;
1746
1801
  remaining = 0;
1747
- cleanupAbort();
1748
- }
1802
+ cleanupAbortListener();
1803
+ };
1749
1804
 
1750
- function execFinsh(){
1805
+ const execFinish = () => {
1751
1806
  finish();
1752
1807
  fn?.();
1753
1808
  onFinish?.();
1754
- }
1809
+ };
1755
1810
 
1756
- function cancel() {
1811
+ const cancel = () => {
1757
1812
  if (finished) return;
1758
1813
  finish();
1759
- }
1814
+ };
1815
+
1816
+ const attachAbortListener = () => {
1817
+ cleanupAbortListener();
1818
+
1819
+ // Preserve semantics: if already aborted, become terminal.
1820
+ if (signal?.aborted) {
1821
+ finished = true;
1822
+ remaining = 0;
1823
+ return;
1824
+ }
1825
+
1826
+ cleanupAbort = attachAbort(signal, cancel);
1827
+ };
1760
1828
 
1761
- // Attach abort once for this run.
1762
- // (Matches existing behavior: reset() does not reattach.)
1763
- cleanupAbort = attachAbort(signal, cancel);
1829
+ // Attach abort for the lifetime of this timer instance
1830
+ attachAbortListener();
1764
1831
 
1765
1832
  const tick = (ts) => {
1766
1833
  if (!running || finished) return;
@@ -1770,7 +1837,7 @@ export function afterRaf(duration, fn, onFinish, { signal } = {}) {
1770
1837
  const elapsed = ts - segmentStart;
1771
1838
 
1772
1839
  if (elapsed >= remaining) {
1773
- execFinsh();
1840
+ execFinish();
1774
1841
  return;
1775
1842
  }
1776
1843
 
@@ -1800,7 +1867,7 @@ export function afterRaf(duration, fn, onFinish, { signal } = {}) {
1800
1867
 
1801
1868
  // If nothing remains, finish synchronously
1802
1869
  if (!(remaining > 0)) {
1803
- execFinsh();
1870
+ execFinish();
1804
1871
  return;
1805
1872
  }
1806
1873
 
@@ -1820,12 +1887,12 @@ export function afterRaf(duration, fn, onFinish, { signal } = {}) {
1820
1887
  paused = false;
1821
1888
  finished = false;
1822
1889
 
1823
- // Keep the same AbortSignal semantics:
1824
- // if the signal is already aborted, remain terminal.
1825
- if (signal?.aborted) {
1826
- finished = true;
1827
- remaining = 0;
1828
- cleanupAbort(); // no-op if nothing attached
1890
+ // Re-attach abort on reset (more consistent with non-RAF timers)
1891
+ attachAbortListener();
1892
+
1893
+ if (finished) {
1894
+ // signal was aborted during attachAbortListener()
1895
+ return;
1829
1896
  }
1830
1897
  };
1831
1898
 
@@ -1833,13 +1900,13 @@ export function afterRaf(duration, fn, onFinish, { signal } = {}) {
1833
1900
  if (!finished) {
1834
1901
  // Support 0ms: fire on next frame (consistent with RAF timing)
1835
1902
  if (!(remaining > 0)) {
1836
- execFinsh();
1903
+ execFinish();
1837
1904
  } else {
1838
1905
  running = true;
1839
1906
  rafId = requestAnimationFrame(tick);
1840
1907
  }
1841
1908
  } else {
1842
- cleanupAbort();
1909
+ cleanupAbortListener();
1843
1910
  }
1844
1911
 
1845
1912
  return {
@@ -1864,13 +1931,20 @@ export function afterRaf(duration, fn, onFinish, { signal } = {}) {
1864
1931
  ===== FILE: raf\debounceRaf.js =====
1865
1932
 
1866
1933
  // ./raf/debounceRaf.js
1867
- import { parseDuration } from '../parseDuration.js';
1934
+ import { resolveDelayFnOptions } from '../resolveDelayAndFn.js';
1868
1935
  import { pendingAbort } from '../abort.js';
1869
- import { now } from '../now.js'
1936
+ import { now } from '../now.js';
1870
1937
 
1871
1938
  /**
1872
1939
  * Debounces a function using requestAnimationFrame.
1873
1940
  *
1941
+ * Preferred:
1942
+ * - debounceRaf(fn, [duration], [options]) // duration defaults to 0 (next frame)
1943
+ *
1944
+ * Also supported:
1945
+ * - debounceRaf(duration, fn, [options])
1946
+ * - debounceRaf(fn, [options])
1947
+ *
1874
1948
  * Timing model:
1875
1949
  * - Waits until `ms` of inactivity have passed (measured using frame timestamps).
1876
1950
  * - `ms = 0` means "run on the next animation frame" (still debounced).
@@ -1880,120 +1954,108 @@ import { now } from '../now.js'
1880
1954
  * - `.flush()` immediately invokes if pending.
1881
1955
  * - Optional AbortSignal: abort cancels any pending invocation.
1882
1956
  *
1883
- * Overloads:
1884
- * - debounceRaf(fn, [options]) // ms defaults to 0 (next frame)
1885
- * - debounceRaf(duration, fn, [options])
1886
- *
1887
- * @param {string|number|Function} durationOrFn - Duration (e.g., "300ms") or fn directly
1888
- * @param {Function|Object} [callbackFnOrOptions] - Callback (if duration first) OR options (if fn first)
1889
- * @param {Object} [options]
1890
- * @param {AbortSignal} [options.signal] - AbortSignal to auto-cancel pending work
1957
+ * @param {Function|string|number} a - function or duration
1958
+ * @param {Function|string|number|Object} [b] - duration, fn, or options
1959
+ * @param {Object} [c] - options (3-arg form)
1960
+ * @param {AbortSignal} [c.signal] - AbortSignal to auto-cancel pending work
1891
1961
  * @returns {((...args: any[]) => void) & { cancel: () => void, flush: () => void }}
1892
1962
  */
1893
- export function debounceRaf(durationOrFn, callbackFnOrOptions, options) {
1894
- const durationFirst = typeof durationOrFn !== 'function';
1895
-
1896
- const msRaw = durationFirst ? parseDuration(durationOrFn) : 0;
1897
- const ms = Math.max(0, msRaw);
1963
+ export function debounceRaf(a, b, c) {
1964
+ // duration is optional for debounceRaf (defaults to 0)
1965
+ const { fn, delay, options } = resolveDelayFnOptions(a, b, c, 0);
1898
1966
 
1899
- const fn = durationFirst ? callbackFnOrOptions : durationOrFn;
1967
+ const ms = Math.max(0, delay);
1968
+ const signal = options?.signal;
1900
1969
 
1901
- const opts = durationFirst ? (options ?? {}) : (callbackFnOrOptions ?? {});
1902
- const signal = opts?.signal;
1903
-
1904
- if (typeof fn !== 'function') {
1905
- throw new TypeError(`Expected a function, got: ${typeof fn}`);
1906
- }
1970
+ let rafId = 0;
1907
1971
 
1908
- let rafId = 0;
1972
+ /** @type {any[] | null} */
1973
+ let lastArgs = null;
1974
+ /** @type {any | null} */
1975
+ let lastThis = null;
1909
1976
 
1910
- /** @type {any[] | null} */
1911
- let lastArgs = null;
1912
- /** @type {any | null} */
1913
- let lastThis = null;
1977
+ // last time the debounced function was called (used for inactivity measurement)
1978
+ let lastCallTime = 0;
1914
1979
 
1915
- // last time the debounced function was called (used for inactivity measurement)
1916
- let lastCallTime = 0;
1980
+ const clearRaf = () => {
1981
+ if (rafId) cancelAnimationFrame(rafId);
1982
+ rafId = 0;
1983
+ };
1917
1984
 
1918
- const clearRaf = () => {
1919
- if (rafId) cancelAnimationFrame(rafId);
1920
- rafId = 0;
1921
- };
1985
+ const abort = pendingAbort(signal, () => cancel());
1922
1986
 
1923
- const cancel = () => {
1924
- clearRaf();
1925
- lastArgs = null;
1926
- lastThis = null;
1927
- lastCallTime = 0;
1928
- abort.remove();
1929
- };
1987
+ const cancel = () => {
1988
+ clearRaf();
1989
+ lastArgs = null;
1990
+ lastThis = null;
1991
+ lastCallTime = 0;
1992
+ abort.remove();
1993
+ };
1930
1994
 
1931
- const abort = pendingAbort(signal, () => cancel());
1995
+ const invoke = () => {
1996
+ if (!lastArgs) return;
1932
1997
 
1933
- const invoke = () => {
1934
- if (!lastArgs) return;
1998
+ const args = lastArgs;
1999
+ const ctx = lastThis;
1935
2000
 
1936
- const args = lastArgs;
1937
- const ctx = lastThis;
2001
+ // Clear first (re-entrancy safe + GC friendly)
2002
+ lastArgs = null;
2003
+ lastThis = null;
1938
2004
 
1939
- // Clear first (re-entrancy safe + GC friendly)
1940
- lastArgs = null;
1941
- lastThis = null;
2005
+ abort.remove();
2006
+ fn.apply(ctx, args);
2007
+ };
1942
2008
 
1943
- abort.remove();
1944
- fn.apply(ctx, args);
1945
- };
2009
+ const tick = (ts) => {
2010
+ if (!rafId) return; // canceled
1946
2011
 
1947
- const tick = (ts) => {
1948
- if (!rafId) return; // canceled
1949
- if (!lastArgs) {
1950
- // Nothing pending; clean slate
1951
- cancel();
1952
- return;
1953
- }
2012
+ if (!lastArgs) {
2013
+ // Nothing pending; clean slate
2014
+ cancel();
2015
+ return;
2016
+ }
1954
2017
 
1955
- const elapsed = ts - lastCallTime;
2018
+ const elapsed = ts - lastCallTime;
1956
2019
 
1957
- if (elapsed >= ms) {
1958
- // Stop scheduling first, then invoke
1959
- clearRaf();
1960
- invoke();
1961
- return;
1962
- }
2020
+ if (elapsed >= ms) {
2021
+ // Stop scheduling first, then invoke
2022
+ clearRaf();
2023
+ invoke();
2024
+ return;
2025
+ }
1963
2026
 
1964
- rafId = requestAnimationFrame(tick);
1965
- };
2027
+ rafId = requestAnimationFrame(tick);
2028
+ };
1966
2029
 
1967
- /** @type {any} */
1968
- const debounced = function (...args) {
1969
- if (signal?.aborted) return;
2030
+ /** @type {any} */
2031
+ const debounced = function (...args) {
2032
+ if (signal?.aborted) return;
1970
2033
 
1971
- lastArgs = args;
1972
- lastThis = this;
2034
+ lastArgs = args;
2035
+ lastThis = this;
1973
2036
 
1974
- // Use perf.now for call timestamp; RAF tick uses its own timestamp,
1975
- // both are monotonic in modern browsers.
1976
- lastCallTime = now();
2037
+ // Use monotonic clock for call timestamp; RAF tick uses its own timestamp.
2038
+ lastCallTime = now();
1977
2039
 
1978
- // Ensure abort listener exists only while pending work exists
1979
- abort.add();
2040
+ // Ensure abort listener exists only while pending work exists
2041
+ abort.add();
1980
2042
 
1981
- clearRaf();
1982
- rafId = requestAnimationFrame(tick);
1983
- };
2043
+ clearRaf();
2044
+ rafId = requestAnimationFrame(tick);
2045
+ };
1984
2046
 
1985
- debounced.cancel = cancel;
2047
+ debounced.cancel = cancel;
1986
2048
 
1987
- debounced.flush = () => {
1988
- if (!rafId || !lastArgs) return;
1989
- clearRaf();
1990
- invoke();
1991
- };
2049
+ debounced.flush = () => {
2050
+ if (!rafId || !lastArgs) return;
2051
+ clearRaf();
2052
+ invoke();
2053
+ };
1992
2054
 
1993
- // If already aborted at creation time, ensure clean slate.
1994
- if (signal?.aborted) cancel();
2055
+ // If already aborted at creation time, ensure clean slate.
2056
+ if (signal?.aborted) cancel();
1995
2057
 
1996
- return debounced;
2058
+ return debounced;
1997
2059
  }
1998
2060
  ===== END FILE: raf\debounceRaf.js =====
1999
2061
 
@@ -2001,25 +2063,32 @@ export function debounceRaf(durationOrFn, callbackFnOrOptions, options) {
2001
2063
  ===== FILE: raf\everyRaf.js =====
2002
2064
 
2003
2065
  // ./raf/everyRaf.js
2004
- import { parseDuration } from '../parseDuration.js';
2066
+ import { resolveDelayFnOptions } from '../resolveDelayAndFn.js';
2005
2067
  import { attachAbort } from '../abort.js';
2006
- import { now } from '../now.js'
2068
+ import { now } from '../now.js';
2069
+
2070
+ const isPlainOptions = (v) => v != null && typeof v === 'object' && !Array.isArray(v);
2007
2071
 
2008
2072
  /**
2009
2073
  * Repeats a function every N milliseconds using requestAnimationFrame.
2010
2074
  *
2075
+ * Preferred:
2076
+ * - everyRaf(fn, duration, [options])
2077
+ *
2078
+ * Also supported (legacy):
2079
+ * - everyRaf(duration, fn, [maxTimes], [runImmediately], [options])
2080
+ *
2011
2081
  * Timing model:
2012
2082
  * - Counts down using frame deltas.
2013
2083
  * - Paused time does NOT count (pause freezes remaining-to-next-tick).
2014
2084
  * - On large frame gaps, may "catch up" by running multiple ticks in one frame,
2015
- * but will never exceed maxTimes.
2085
+ * but will never exceed max.
2016
2086
  *
2017
- * @param {string|number} duration - Interval duration ("1s", "500ms", 500, etc.)
2018
- * @param {Function} fn - Function to run (receives count index)
2019
- * @param {number} [maxTimes=Infinity] - Max number of executions
2020
- * @param {boolean} [runImmediately=false] - Whether to run immediately once
2021
- * @param {Object} [options]
2022
- * @param {AbortSignal} [options.signal] - optional AbortSignal to auto-cancel
2087
+ * @param {Function|string|number} a
2088
+ * @param {Function|string|number|Object} b
2089
+ * @param {number|boolean|Object} [c] - legacy maxTimes OR legacy runImmediately OR options
2090
+ * @param {boolean|Object} [d] - legacy runImmediately OR options
2091
+ * @param {Object} [e] - legacy options
2023
2092
  * @returns {{
2024
2093
  * pause(): void,
2025
2094
  * resume(): void,
@@ -2031,17 +2100,59 @@ import { now } from '../now.js'
2031
2100
  * readonly count: number
2032
2101
  * }}
2033
2102
  */
2034
- export function everyRaf(
2035
- duration,
2036
- fn,
2037
- maxTimes = Infinity,
2038
- runImmediately = false,
2039
- { signal } = {}
2040
- ) {
2041
- const intervalMs = parseDuration(duration);
2103
+ export function everyRaf(a, b, c, d, e) {
2104
+ const durationFirst = typeof a !== 'function';
2042
2105
 
2043
- let rafId = 0;
2106
+ // ---- Normalize legacy positional args into options (without breaking legacy behavior) ----
2107
+ let legacyMax;
2108
+ let legacyRunImmediately;
2109
+
2110
+ /** @type {any} */
2111
+ let optionsCandidate;
2112
+
2113
+ if (durationFirst) {
2114
+ // Legacy or duration-first:
2115
+ // - everyRaf(duration, fn, options)
2116
+ // - everyRaf(duration, fn, maxTimes, runImmediately, options)
2117
+ // - everyRaf(duration, fn, maxTimes, options)
2118
+ // - everyRaf(duration, fn, runImmediately, options) (extra leniency)
2119
+ if (isPlainOptions(c)) {
2120
+ optionsCandidate = c;
2121
+ } else if (isPlainOptions(d)) {
2122
+ optionsCandidate = d;
2123
+ } else if (isPlainOptions(e)) {
2124
+ optionsCandidate = e;
2125
+ } else {
2126
+ optionsCandidate = {};
2127
+ }
2128
+
2129
+ if (typeof c === 'number') legacyMax = c;
2130
+ if (typeof d === 'boolean') legacyRunImmediately = d;
2131
+
2132
+ // Lenient support: (duration, fn, true, options) => runImmediately=true
2133
+ if (legacyRunImmediately == null && typeof c === 'boolean') legacyRunImmediately = c;
2134
+ } else {
2135
+ // Preferred fn-first:
2136
+ // - everyRaf(fn, duration, options?)
2137
+ optionsCandidate = isPlainOptions(c) ? c : {};
2138
+ }
2139
+
2140
+ // Parse fn + duration using shared helper (duration is REQUIRED)
2141
+ const { fn, delay, options } = resolveDelayFnOptions(a, b, optionsCandidate, undefined);
2142
+
2143
+ const signal = options?.signal;
2144
+
2145
+ const maxFromOptions = options?.max ?? Infinity;
2146
+ const runImmediatelyFromOptions = options?.runImmediately ?? false;
2044
2147
 
2148
+ // Legacy positional should win (to preserve prior behavior)
2149
+ const max = legacyMax != null ? legacyMax : maxFromOptions;
2150
+ const runImmediately =
2151
+ legacyRunImmediately != null ? legacyRunImmediately : runImmediatelyFromOptions;
2152
+
2153
+ const intervalMs = Math.max(0, delay);
2154
+
2155
+ let rafId = 0;
2045
2156
  let count = 0;
2046
2157
 
2047
2158
  // remaining time until next tick (pause-safe)
@@ -2059,9 +2170,13 @@ export function everyRaf(
2059
2170
  rafId = 0;
2060
2171
  };
2061
2172
 
2062
- // Abort listener cleanup (attached once per construction run)
2063
2173
  let cleanupAbort = () => { };
2064
2174
 
2175
+ const cleanupAbortListener = () => {
2176
+ cleanupAbort();
2177
+ cleanupAbort = () => { };
2178
+ };
2179
+
2065
2180
  const finish = () => {
2066
2181
  cancelFrame();
2067
2182
  running = false;
@@ -2071,7 +2186,7 @@ export function everyRaf(
2071
2186
  lastTs = null;
2072
2187
  remainingToNext = 0;
2073
2188
 
2074
- cleanupAbort();
2189
+ cleanupAbortListener();
2075
2190
  };
2076
2191
 
2077
2192
  const cancel = () => {
@@ -2079,17 +2194,22 @@ export function everyRaf(
2079
2194
  finish();
2080
2195
  };
2081
2196
 
2082
- // Preserve existing semantics:
2083
- // - If already aborted at creation, just mark finished/zero remaining (do not attach).
2084
- // - Otherwise attach abort listener once; removed on finish().
2085
- if (signal?.aborted) {
2086
- finished = true;
2087
- remainingToNext = 0;
2088
- } else {
2197
+ const attachAbortListener = () => {
2198
+ cleanupAbortListener();
2199
+
2200
+ // Preserve semantics: if already aborted, become terminal (and do not attach).
2201
+ if (signal?.aborted) {
2202
+ finished = true;
2203
+ remainingToNext = 0;
2204
+ return;
2205
+ }
2206
+
2089
2207
  cleanupAbort = attachAbort(signal, cancel);
2090
- }
2208
+ };
2091
2209
 
2092
- const canRunMore = () => count < maxTimes;
2210
+ attachAbortListener();
2211
+
2212
+ const canRunMore = () => count < max;
2093
2213
 
2094
2214
  const fire = () => {
2095
2215
  fn?.(count++);
@@ -2124,6 +2244,7 @@ export function everyRaf(
2124
2244
  // Catch-up loop: if we missed multiple intervals, fire multiple times.
2125
2245
  // Protect against infinite loops: intervalMs could be 0.
2126
2246
  const step = Math.max(0, intervalMs);
2247
+
2127
2248
  if (step === 0) {
2128
2249
  // If interval is 0ms, treat as "once per frame"
2129
2250
  remainingToNext = 0;
@@ -2133,11 +2254,10 @@ export function everyRaf(
2133
2254
  }
2134
2255
 
2135
2256
  while (remainingToNext <= 0 && !finished) {
2136
- // We reached the next tick boundary
2137
2257
  const ok = fire();
2138
2258
  if (!ok) return;
2139
2259
 
2140
- // Schedule next boundary: carry over overshoot
2260
+ // carry over overshoot
2141
2261
  remainingToNext += step;
2142
2262
  }
2143
2263
 
@@ -2146,10 +2266,12 @@ export function everyRaf(
2146
2266
 
2147
2267
  const startLoop = () => {
2148
2268
  if (finished) return;
2269
+
2149
2270
  if (signal?.aborted) {
2150
2271
  finish();
2151
2272
  return;
2152
2273
  }
2274
+
2153
2275
  if (!canRunMore()) {
2154
2276
  finish();
2155
2277
  return;
@@ -2167,9 +2289,9 @@ export function everyRaf(
2167
2289
  cancelFrame();
2168
2290
 
2169
2291
  // Freeze remainingToNext based on time since lastTs
2170
- const now = now();
2292
+ const currentTime = now(); // ✅ fixed: no shadowing / TDZ
2171
2293
  if (lastTs != null) {
2172
- const delta = now - lastTs;
2294
+ const delta = currentTime - lastTs;
2173
2295
  remainingToNext = Math.max(0, remainingToNext - delta);
2174
2296
  }
2175
2297
 
@@ -2201,18 +2323,13 @@ export function everyRaf(
2201
2323
  paused = false;
2202
2324
  finished = false;
2203
2325
 
2204
- if (signal?.aborted) {
2205
- finished = true;
2206
- remainingToNext = 0;
2207
- cleanupAbort(); // no-op if nothing attached; mirrors prior removeAbortListener path
2208
- return;
2209
- }
2326
+ // Re-attach abort on reset (consistent with non-RAF timers)
2327
+ attachAbortListener();
2328
+ if (finished) return;
2210
2329
 
2211
- // If maxTimes is already satisfied by definition (<=0 but not Infinity), finish.
2212
- if (!(maxTimes > 0) && maxTimes !== Infinity) {
2213
- finished = true;
2214
- remainingToNext = 0;
2215
- cleanupAbort();
2330
+ // If max is already satisfied by definition (<=0 but not Infinity), finish.
2331
+ if (!(max > 0) && max !== Infinity) {
2332
+ finish();
2216
2333
  return;
2217
2334
  }
2218
2335
 
@@ -2228,21 +2345,19 @@ export function everyRaf(
2228
2345
 
2229
2346
  // Initial launch
2230
2347
  if (!finished) {
2231
- // If maxTimes is "zero-ish", finish immediately.
2232
- if (!(maxTimes > 0) && maxTimes !== Infinity) {
2348
+ if (!(max > 0) && max !== Infinity) {
2233
2349
  finish();
2234
2350
  } else {
2235
2351
  if (runImmediately && canRunMore()) {
2236
2352
  fire();
2237
2353
  }
2238
2354
  if (!finished) {
2239
- // If interval is 0, treat as "once per frame"
2240
2355
  remainingToNext = Math.max(0, intervalMs);
2241
2356
  startLoop();
2242
2357
  }
2243
2358
  }
2244
2359
  } else {
2245
- cleanupAbort();
2360
+ cleanupAbortListener();
2246
2361
  }
2247
2362
 
2248
2363
  return {
@@ -2269,7 +2384,6 @@ export function everyRaf(
2269
2384
 
2270
2385
  ===== FILE: raf\index.js =====
2271
2386
 
2272
-
2273
2387
  // ./raf/index.js
2274
2388
 
2275
2389
  import { afterRaf as _afterRaf } from './afterRaf.js';
@@ -2278,31 +2392,6 @@ import { debounceRaf as _debounceRaf } from './debounceRaf.js';
2278
2392
  import { throttleRaf as _throttleRaf } from './throttleRaf.js';
2279
2393
  import { waitForRaf as _waitForRaf } from './waitForRaf.js';
2280
2394
 
2281
- export function afterRaf(...args) {
2282
- assertRafAvailable();
2283
- return _afterRaf(...args);
2284
- }
2285
-
2286
- export function everyRaf(...args) {
2287
- assertRafAvailable();
2288
- return _everyRaf(...args);
2289
- }
2290
-
2291
- export function debounceRaf(...args) {
2292
- assertRafAvailable();
2293
- return _debounceRaf(...args);
2294
- }
2295
-
2296
- export function throttleRaf(...args) {
2297
- assertRafAvailable();
2298
- return _throttleRaf(...args);
2299
- }
2300
-
2301
- export function waitForRaf(...args) {
2302
- assertRafAvailable();
2303
- return _waitForRaf(...args);
2304
- }
2305
-
2306
2395
  export function assertRafAvailable() {
2307
2396
  if (typeof requestAnimationFrame !== 'function') {
2308
2397
  throw new Error(
@@ -2311,18 +2400,37 @@ export function assertRafAvailable() {
2311
2400
  );
2312
2401
  }
2313
2402
  }
2403
+
2404
+ const guard = (fn) =>
2405
+ (...args) => {
2406
+ assertRafAvailable();
2407
+ return fn(...args);
2408
+ };
2409
+
2410
+ export const afterRaf = guard(_afterRaf);
2411
+ export const everyRaf = guard(_everyRaf);
2412
+ export const debounceRaf = guard(_debounceRaf);
2413
+ export const throttleRaf = guard(_throttleRaf);
2414
+ export const waitForRaf = guard(_waitForRaf);
2314
2415
  ===== END FILE: raf\index.js =====
2315
2416
 
2316
2417
 
2317
2418
  ===== FILE: raf\throttleRaf.js =====
2318
2419
 
2319
2420
  // ./raf/throttleRaf.js
2320
-
2321
2421
  import { pendingAbort } from '../abort.js';
2322
2422
 
2423
+ const isPlainOptions = (v) => v != null && typeof v === 'object' && !Array.isArray(v);
2424
+
2323
2425
  /**
2324
2426
  * Throttles a function using `requestAnimationFrame`, with optional frame skipping.
2325
2427
  *
2428
+ * Preferred:
2429
+ * - throttleRaf(fn, [frameSkip], [options])
2430
+ *
2431
+ * Also supported:
2432
+ * - throttleRaf(fn, [options]) // frameSkip defaults to 0
2433
+ *
2326
2434
  * Executes at most once every `(frameSkip + 1)` frames.
2327
2435
  *
2328
2436
  * Argument behavior while throttled:
@@ -2334,13 +2442,10 @@ import { pendingAbort } from '../abort.js';
2334
2442
  * - Listener is attached only while a call is pending.
2335
2443
  *
2336
2444
  * @template {(...args: any[]) => any} T
2337
- * @param {T} fn - Function to throttle.
2445
+ * @param {T} fn
2338
2446
  * @param {number|{signal?: AbortSignal, trailing?: boolean}} [frameSkipOrOptions=0]
2339
2447
  * @param {{signal?: AbortSignal, trailing?: boolean}} [maybeOptions]
2340
- * @returns {T & {
2341
- * cancel: () => void,
2342
- * flush: () => void
2343
- * }}
2448
+ * @returns {T & { cancel: () => void, flush: () => void }}
2344
2449
  */
2345
2450
  export function throttleRaf(fn, frameSkipOrOptions = 0, maybeOptions) {
2346
2451
  if (typeof fn !== 'function') {
@@ -2349,13 +2454,14 @@ export function throttleRaf(fn, frameSkipOrOptions = 0, maybeOptions) {
2349
2454
 
2350
2455
  const frameSkip = typeof frameSkipOrOptions === 'number' ? frameSkipOrOptions : 0;
2351
2456
 
2352
- const options =
2353
- typeof frameSkipOrOptions === 'object' && frameSkipOrOptions != null
2354
- ? frameSkipOrOptions
2355
- : maybeOptions ?? {};
2457
+ const options = isPlainOptions(frameSkipOrOptions)
2458
+ ? frameSkipOrOptions
2459
+ : isPlainOptions(maybeOptions)
2460
+ ? maybeOptions
2461
+ : {};
2356
2462
 
2357
- const signal = options?.signal;
2358
- const trailing = options?.trailing ?? true;
2463
+ const signal = options.signal;
2464
+ const trailing = options.trailing ?? true;
2359
2465
 
2360
2466
  // Normalize once: int >= 0
2361
2467
  const skip = Math.max(0, frameSkip | 0);
@@ -2371,6 +2477,8 @@ export function throttleRaf(fn, frameSkipOrOptions = 0, maybeOptions) {
2371
2477
  // countdown for skip-path only
2372
2478
  let remaining = skip;
2373
2479
 
2480
+ const abort = pendingAbort(signal, () => cancel());
2481
+
2374
2482
  const clearState = () => {
2375
2483
  scheduled = false;
2376
2484
 
@@ -2389,10 +2497,7 @@ export function throttleRaf(fn, frameSkipOrOptions = 0, maybeOptions) {
2389
2497
  clearState();
2390
2498
  };
2391
2499
 
2392
- const abort = pendingAbort(signal, () => cancel());
2393
-
2394
2500
  const invoke = () => {
2395
- // if canceled, lastArgs is null
2396
2501
  if (!lastArgs) return;
2397
2502
 
2398
2503
  const args = lastArgs;
@@ -2414,7 +2519,6 @@ export function throttleRaf(fn, frameSkipOrOptions = 0, maybeOptions) {
2414
2519
  };
2415
2520
 
2416
2521
  const tickSkip = () => {
2417
- // scheduled might have been canceled since RAF was queued
2418
2522
  if (!scheduled) return;
2419
2523
 
2420
2524
  if (signal?.aborted) {
@@ -2475,6 +2579,7 @@ export function throttleRaf(fn, frameSkipOrOptions = 0, maybeOptions) {
2475
2579
 
2476
2580
  throttled.flush = () => {
2477
2581
  if (!scheduled) return;
2582
+
2478
2583
  if (signal?.aborted) {
2479
2584
  cancel();
2480
2585
  return;