timeout-flow 0.0.17 → 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 +120 -26
- 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 +28 -6
- package/types/waitFor.d.ts +11 -6
package/src/debounce.js
CHANGED
|
@@ -1,40 +1,106 @@
|
|
|
1
|
-
|
|
2
1
|
// ./src/debounce.js
|
|
3
2
|
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import { resolveDelayFnOptions } from './resolveDelayAndFn.js';
|
|
4
|
+
import { pendingAbort } from './abort.js';
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
|
-
* @typedef {((...args: any[]) => void) & {
|
|
7
|
+
* @typedef {((...args: any[]) => void) & {
|
|
8
|
+
* cancel: () => void,
|
|
9
|
+
* flush: () => void
|
|
10
|
+
* }} DebouncedFunction
|
|
9
11
|
*/
|
|
10
12
|
|
|
11
|
-
|
|
12
13
|
/**
|
|
13
|
-
* Creates a debounced function that delays invoking `fn`
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
14
|
+
* Creates a debounced function that delays invoking `fn`
|
|
15
|
+
* until after `delay` ms of inactivity.
|
|
16
|
+
*
|
|
17
|
+
* Preferred:
|
|
18
|
+
* - debounce(fn, [delay], [options]) // delay defaults to 0
|
|
19
|
+
*
|
|
20
|
+
* Also supported:
|
|
21
|
+
* - debounce(delay, fn, [options]) // legacy/alt ordering
|
|
22
|
+
* - debounce(fn, [options])
|
|
23
|
+
*
|
|
24
|
+
* Notes:
|
|
25
|
+
* - Always invokes with the latest arguments/context.
|
|
26
|
+
* - `.cancel()` clears any pending invocation.
|
|
27
|
+
* - `.flush()` immediately invokes if pending.
|
|
28
|
+
* - Optional AbortSignal: abort cancels pending invocation.
|
|
29
|
+
*
|
|
30
|
+
* @param {Function|string|number} a - Function or delay (see overloads)
|
|
31
|
+
* @param {Function|string|number|Object} [b] - Delay, function, or options
|
|
32
|
+
* @param {Object} [c] - Options (3-arg form)
|
|
33
|
+
* @param {AbortSignal} [c.signal] - Optional AbortSignal to cancel pending work
|
|
34
|
+
* @returns {DebouncedFunction}
|
|
18
35
|
*/
|
|
19
|
-
export function debounce(a, b) {
|
|
20
|
-
const { fn, delay } =
|
|
21
|
-
|
|
36
|
+
export function debounce(a, b, c) {
|
|
37
|
+
const { fn, delay, options } = resolveDelayFnOptions(a, b, c, 0);
|
|
38
|
+
const signal = options?.signal;
|
|
39
|
+
|
|
40
|
+
const wait = Math.max(0, delay);
|
|
41
|
+
|
|
42
|
+
let timeoutId = 0;
|
|
43
|
+
|
|
44
|
+
/** @type {any[] | null} */
|
|
45
|
+
let lastArgs = null;
|
|
46
|
+
/** @type {any | null} */
|
|
47
|
+
let lastThis = null;
|
|
48
|
+
|
|
49
|
+
const abort = pendingAbort(signal, () => cancel());
|
|
50
|
+
|
|
51
|
+
const invoke = () => {
|
|
52
|
+
if (!lastArgs) return;
|
|
53
|
+
|
|
54
|
+
const args = lastArgs;
|
|
55
|
+
const ctx = lastThis;
|
|
56
|
+
|
|
57
|
+
// clear first (re-entrancy safe + GC friendly)
|
|
58
|
+
lastArgs = null;
|
|
59
|
+
lastThis = null;
|
|
60
|
+
|
|
61
|
+
abort.remove();
|
|
62
|
+
fn.apply(ctx, args);
|
|
63
|
+
};
|
|
22
64
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
65
|
+
const cancel = () => {
|
|
66
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
67
|
+
timeoutId = 0;
|
|
68
|
+
|
|
69
|
+
lastArgs = null;
|
|
70
|
+
lastThis = null;
|
|
71
|
+
|
|
72
|
+
abort.remove();
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** @type {any} */
|
|
26
76
|
const debounced = function (...args) {
|
|
27
|
-
|
|
28
|
-
|
|
77
|
+
if (signal?.aborted) return;
|
|
78
|
+
|
|
79
|
+
lastArgs = args;
|
|
80
|
+
lastThis = this;
|
|
81
|
+
|
|
82
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
83
|
+
|
|
84
|
+
// Listener should exist only while a call is pending
|
|
85
|
+
abort.add();
|
|
86
|
+
|
|
87
|
+
timeoutId = setTimeout(() => {
|
|
88
|
+
timeoutId = 0;
|
|
89
|
+
invoke();
|
|
90
|
+
}, wait);
|
|
29
91
|
};
|
|
30
92
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
93
|
+
debounced.cancel = cancel;
|
|
94
|
+
|
|
95
|
+
debounced.flush = () => {
|
|
96
|
+
if (!timeoutId) return;
|
|
35
97
|
clearTimeout(timeoutId);
|
|
36
|
-
timeoutId =
|
|
98
|
+
timeoutId = 0;
|
|
99
|
+
invoke();
|
|
37
100
|
};
|
|
38
101
|
|
|
102
|
+
// If already aborted at creation time, ensure clean slate.
|
|
103
|
+
if (signal?.aborted) cancel();
|
|
104
|
+
|
|
39
105
|
return debounced;
|
|
40
|
-
}
|
|
106
|
+
}
|
package/src/every.js
CHANGED
|
@@ -1,121 +1,35 @@
|
|
|
1
|
-
|
|
2
1
|
// ./src/every.js
|
|
3
|
-
import { parseDuration } from './parseDuration.js';
|
|
4
|
-
|
|
5
2
|
import { EveryTimer } from './EveryTimer.js';
|
|
3
|
+
import { resolveDelayFnOptions } from './resolveDelayAndFn.js';
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
6
|
* Runs a function every N ms with optional execution limit.
|
|
9
7
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* }
|
|
22
|
-
*/
|
|
23
|
-
export function every(duration, fn, max = Infinity, runImmediately = false) {
|
|
24
|
-
return new EveryTimer(duration, fn, max, runImmediately);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Runs a function every N milliseconds with optional execution limit.
|
|
31
|
-
* Returns control methods: pause, resume, cancel, reset, and status flags.
|
|
32
|
-
*
|
|
33
|
-
* @param {string|number} duration - e.g. "1s", "500ms", "2m" or raw ms
|
|
34
|
-
* @param {Function} fn - function to execute
|
|
35
|
-
* @param {number} [maxTimes] - optional number of times to run
|
|
8
|
+
* Preferred:
|
|
9
|
+
* - every(fn, duration, [options])
|
|
10
|
+
*
|
|
11
|
+
* Also supported (legacy/alt):
|
|
12
|
+
* - every(duration, fn, [options])
|
|
13
|
+
*
|
|
14
|
+
* @param {Function|string|number} a - Function or duration (see overloads)
|
|
15
|
+
* @param {Function|string|number|Object} b - Duration, function, or options
|
|
16
|
+
* @param {Object} [c] - Options
|
|
17
|
+
* @param {number} [c.max=Infinity] - Max times to run
|
|
18
|
+
* @param {boolean} [c.runImmediately=false] - Run immediately on first tick
|
|
19
|
+
* @param {AbortSignal} [c.signal] - Optional AbortSignal to auto-cancel
|
|
36
20
|
* @returns {{
|
|
37
21
|
* pause(): void,
|
|
38
22
|
* resume(): void,
|
|
39
23
|
* cancel(): void,
|
|
40
24
|
* reset(restart?: boolean): void,
|
|
41
25
|
* readonly isRunning: boolean,
|
|
26
|
+
* readonly isPaused: boolean,
|
|
27
|
+
* readonly isFinished: boolean,
|
|
42
28
|
* readonly count: number
|
|
43
29
|
* }}
|
|
44
30
|
*/
|
|
45
|
-
export function
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
let running = false;
|
|
50
|
-
let timer = null;
|
|
51
|
-
|
|
52
|
-
let count = 0;
|
|
53
|
-
|
|
54
|
-
const tick = () => {
|
|
55
|
-
if (count >= maxTimes) {
|
|
56
|
-
cancel();
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
fn?.(); // Main Func to exec after delay.
|
|
60
|
-
count++
|
|
61
|
-
scheduleNext(); // keep going
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const scheduleNext = () => {
|
|
65
|
-
timer = setTimeout(tick, ms);
|
|
66
|
-
startTime = Date.now();
|
|
67
|
-
remaining = ms;
|
|
68
|
-
running = true;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const pause = () => {
|
|
72
|
-
if (running) {
|
|
73
|
-
clearTimeout(timer);
|
|
74
|
-
remaining -= Date.now() - startTime;
|
|
75
|
-
running = false;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const resume = () => {
|
|
80
|
-
if (!running && count < maxTimes && remaining > 0) {
|
|
81
|
-
timer = setTimeout(tick, remaining);
|
|
82
|
-
startTime = Date.now();
|
|
83
|
-
running = true;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const cancel = () => {
|
|
88
|
-
clearTimeout(timer);
|
|
89
|
-
running = false;
|
|
90
|
-
timer = null;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const reset = (restart = false) => {
|
|
94
|
-
cancel();
|
|
95
|
-
count = 0;
|
|
96
|
-
remaining = ms;
|
|
97
|
-
if (restart) start();
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const start = () => {
|
|
101
|
-
if (runImmediately) {
|
|
102
|
-
tick();
|
|
103
|
-
} else {
|
|
104
|
-
scheduleNext();
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// Initial launch
|
|
110
|
-
start();
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
pause,
|
|
114
|
-
resume,
|
|
115
|
-
cancel,
|
|
116
|
-
reset,
|
|
117
|
-
get isRunning() { return running; },
|
|
118
|
-
get count() { return count; }
|
|
119
|
-
};
|
|
120
|
-
|
|
31
|
+
export function every(a, b, c) {
|
|
32
|
+
// require explicit duration: defaultDelay = undefined
|
|
33
|
+
const { fn, delay, options } = resolveDelayFnOptions(a, b, c, undefined);
|
|
34
|
+
return new EveryTimer(delay, fn, options);
|
|
121
35
|
}
|
package/src/index.js
CHANGED
package/src/now.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// ./src/now.js
|
|
2
|
+
|
|
3
|
+
// We resolve the best available clock implementation *once* at module load time.
|
|
4
|
+
// This avoids doing feature detection on every call to `now()`,
|
|
5
|
+
// keeping the hot path branch-free and fast.
|
|
6
|
+
|
|
7
|
+
let now;
|
|
8
|
+
|
|
9
|
+
// Prefer high-resolution, monotonic clock when available.
|
|
10
|
+
// - Browsers: performance.now()
|
|
11
|
+
// - Modern Node: performance.now()
|
|
12
|
+
// This is ideal for measuring elapsed time.
|
|
13
|
+
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
|
|
14
|
+
now = () => performance.now();
|
|
15
|
+
} else {
|
|
16
|
+
// Fallback for older Node or minimal runtimes.
|
|
17
|
+
// Date.now() is not monotonic, but ensures compatibility.
|
|
18
|
+
now = () => Date.now();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Export the resolved implementation.
|
|
22
|
+
// Consumers simply call `now()` without worrying about environment differences.
|
|
23
|
+
export { now };
|
package/src/parseDuration.js
CHANGED
|
@@ -1,19 +1,5 @@
|
|
|
1
1
|
// ./src/parseDuration.js
|
|
2
2
|
|
|
3
|
-
// const timeMultipliers = Object.freeze({
|
|
4
|
-
// "ms": 1,
|
|
5
|
-
// "s": 1000,
|
|
6
|
-
// "m": 60_000,
|
|
7
|
-
// "h": 3_600_000,
|
|
8
|
-
// });
|
|
9
|
-
|
|
10
|
-
/** @const {!Object<string, number>} */
|
|
11
|
-
const timeMultipliers = Object.freeze(JSON.parse(`{
|
|
12
|
-
"ms": 1,
|
|
13
|
-
"s": 1000,
|
|
14
|
-
"m": 60000,
|
|
15
|
-
"h": 3600000
|
|
16
|
-
}`));
|
|
17
3
|
|
|
18
4
|
/**
|
|
19
5
|
* Parses a duration string like "2s", "1.5m", "500ms" into milliseconds.
|
|
@@ -22,7 +8,10 @@ const timeMultipliers = Object.freeze(JSON.parse(`{
|
|
|
22
8
|
* @returns {number}
|
|
23
9
|
*/
|
|
24
10
|
export function parseDuration(input) {
|
|
25
|
-
if (typeof input === 'number'
|
|
11
|
+
if (typeof input === 'number') {
|
|
12
|
+
if (!Number.isFinite(input)) {
|
|
13
|
+
throw new TypeError(`Expected a finite number of ms, got: ${input}`);
|
|
14
|
+
}
|
|
26
15
|
return input;
|
|
27
16
|
}
|
|
28
17
|
|
|
@@ -30,9 +19,8 @@ export function parseDuration(input) {
|
|
|
30
19
|
throw new TypeError(`Expected a duration string or number, got ${typeof input}`);
|
|
31
20
|
}
|
|
32
21
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const match = input.match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h)$/);
|
|
22
|
+
const str = input.toLowerCase().trim();
|
|
23
|
+
const match = str.match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h)$/);
|
|
36
24
|
|
|
37
25
|
if (!match) {
|
|
38
26
|
throw new Error(
|
|
@@ -40,6 +28,20 @@ export function parseDuration(input) {
|
|
|
40
28
|
);
|
|
41
29
|
}
|
|
42
30
|
|
|
43
|
-
const
|
|
44
|
-
|
|
31
|
+
const value = Number(match[1]);
|
|
32
|
+
if (!Number.isFinite(value)) {
|
|
33
|
+
throw new TypeError(`Invalid duration value: "${match[1]}"`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const unit = match[2];
|
|
37
|
+
|
|
38
|
+
switch (unit) {
|
|
39
|
+
case 'ms': return value;
|
|
40
|
+
case 's': return value * 1000;
|
|
41
|
+
case 'm': return value * 60_000;
|
|
42
|
+
case 'h': return value * 3_600_000;
|
|
43
|
+
default:
|
|
44
|
+
// Should be unreachable due to regex, but keeps function total.
|
|
45
|
+
throw new Error(`Unsupported duration unit: "${unit}"`);
|
|
46
|
+
}
|
|
45
47
|
}
|
package/src/raf/afterRaf.js
CHANGED
|
@@ -1,72 +1,204 @@
|
|
|
1
1
|
// ./raf/afterRaf.js
|
|
2
|
-
|
|
3
|
-
import {
|
|
2
|
+
import { resolveDelayFnOptions } from '../resolveDelayAndFn.js';
|
|
3
|
+
import { attachAbort } from '../abort.js';
|
|
4
|
+
import { now } from '../now.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Schedules a one-time delayed function using requestAnimationFrame.
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
+
* }}
|
|
11
34
|
*/
|
|
12
|
-
export function afterRaf(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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();
|
|
71
187
|
}
|
|
72
|
-
|
|
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
|
+
}
|