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/EveryTimer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// ./src/EveryTimer.js
|
|
2
|
+
import { attachAbort } from './abort.js';
|
|
2
3
|
import { TimerBase } from './TimerBase.js';
|
|
3
4
|
import { parseDuration } from './parseDuration.js';
|
|
4
5
|
|
|
@@ -9,21 +10,52 @@ export class EveryTimer {
|
|
|
9
10
|
#interval;
|
|
10
11
|
#fn;
|
|
11
12
|
#max;
|
|
13
|
+
|
|
12
14
|
#count = 0;
|
|
13
|
-
#
|
|
15
|
+
#finished = false;
|
|
16
|
+
|
|
17
|
+
/** @type {TimerBase | null} */
|
|
14
18
|
#controller = null;
|
|
15
19
|
|
|
20
|
+
/** @type {AbortSignal | null} */
|
|
21
|
+
#signal = null;
|
|
22
|
+
|
|
23
|
+
/** @type {() => void} */
|
|
24
|
+
#cleanupAbort = () => {};
|
|
25
|
+
|
|
16
26
|
/**
|
|
17
27
|
* @param {string|number} duration - Delay between calls (e.g., '1s', 200)
|
|
18
28
|
* @param {Function} fn - Function to call each tick
|
|
19
|
-
* @param {
|
|
20
|
-
* @param {
|
|
29
|
+
* @param {Object} [options]
|
|
30
|
+
* @param {number} [options.max=Infinity] - Max executions
|
|
31
|
+
* @param {boolean} [options.runImmediately=false] - Run `fn` once before first delay
|
|
32
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel
|
|
21
33
|
*/
|
|
22
|
-
constructor(duration, fn, max = Infinity, runImmediately = false) {
|
|
34
|
+
constructor(duration, fn, { max = Infinity, runImmediately = false, signal } = {}) {
|
|
23
35
|
this.#interval = parseDuration(duration);
|
|
24
36
|
this.#fn = fn;
|
|
25
37
|
this.#max = max;
|
|
26
38
|
|
|
39
|
+
this.#signal = signal ?? null;
|
|
40
|
+
|
|
41
|
+
// If max is already satisfied, finish immediately.
|
|
42
|
+
if (!(this.#max > 0) && this.#max !== Infinity) {
|
|
43
|
+
this.#finished = true;
|
|
44
|
+
this.#cleanupAbort();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Abort semantics: if already aborted, immediately finish (terminal).
|
|
49
|
+
if (this.#signal?.aborted) {
|
|
50
|
+
this.#finished = true;
|
|
51
|
+
this.#cleanupAbort();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Attach abort listener for the lifetime of this timer.
|
|
56
|
+
// (Cleanup happens in #finish().)
|
|
57
|
+
this.#cleanupAbort = attachAbort(this.#signal, () => this.cancel());
|
|
58
|
+
|
|
27
59
|
if (runImmediately) {
|
|
28
60
|
this.#tick();
|
|
29
61
|
} else {
|
|
@@ -39,50 +71,139 @@ export class EveryTimer {
|
|
|
39
71
|
}
|
|
40
72
|
|
|
41
73
|
/**
|
|
42
|
-
* Whether the timer is actively running.
|
|
74
|
+
* Whether the timer is actively running (waiting for next tick).
|
|
75
|
+
*
|
|
76
|
+
* Note: during the brief moment between a tick firing and the next schedule,
|
|
77
|
+
* this may be false — which reflects "no countdown currently pending".
|
|
43
78
|
*/
|
|
44
79
|
get isRunning() {
|
|
45
|
-
return this.#
|
|
80
|
+
return !this.#finished && !!this.#controller && this.#controller.isRunning;
|
|
46
81
|
}
|
|
47
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Whether the timer is paused (resumable).
|
|
85
|
+
*/
|
|
86
|
+
get isPaused() {
|
|
87
|
+
return !this.#finished && !!this.#controller && this.#controller.isPaused;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Whether the timer has finished (hit max) or been canceled (terminal).
|
|
92
|
+
*/
|
|
93
|
+
get isFinished() {
|
|
94
|
+
return this.#finished;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#finish = () => {
|
|
98
|
+
this.#controller?.cancel();
|
|
99
|
+
this.#controller = null;
|
|
100
|
+
|
|
101
|
+
this.#finished = true;
|
|
102
|
+
|
|
103
|
+
this.#cleanupAbort();
|
|
104
|
+
this.#cleanupAbort = () => {};
|
|
105
|
+
this.#signal = null;
|
|
106
|
+
};
|
|
107
|
+
|
|
48
108
|
#tick = () => {
|
|
109
|
+
if (this.#finished) return;
|
|
110
|
+
|
|
111
|
+
if (this.#signal?.aborted) {
|
|
112
|
+
this.#finish();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
49
116
|
if (this.#count >= this.#max) {
|
|
50
|
-
this
|
|
117
|
+
this.#finish();
|
|
51
118
|
return;
|
|
52
119
|
}
|
|
53
120
|
|
|
54
121
|
this.#fn?.(this.#count++);
|
|
122
|
+
if (this.#count >= this.#max) {
|
|
123
|
+
this.#finish();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
55
127
|
this.#schedule();
|
|
56
128
|
};
|
|
57
129
|
|
|
58
130
|
#schedule() {
|
|
131
|
+
if (this.#finished) return;
|
|
132
|
+
|
|
133
|
+
if (this.#signal?.aborted) {
|
|
134
|
+
this.#finish();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Fresh controller per interval window
|
|
59
139
|
this.#controller = new TimerBase(this.#interval);
|
|
60
|
-
this.#running = true;
|
|
61
140
|
this.#controller.resume(this.#tick);
|
|
62
141
|
}
|
|
63
142
|
|
|
64
143
|
pause() {
|
|
144
|
+
if (this.#finished) return;
|
|
65
145
|
this.#controller?.pause();
|
|
66
|
-
this.#running = false;
|
|
67
146
|
}
|
|
68
147
|
|
|
69
148
|
resume() {
|
|
70
|
-
if (
|
|
71
|
-
|
|
72
|
-
|
|
149
|
+
if (this.#finished) return;
|
|
150
|
+
|
|
151
|
+
if (this.#signal?.aborted) {
|
|
152
|
+
this.#finish();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (this.#count >= this.#max) {
|
|
157
|
+
this.#finish();
|
|
158
|
+
return;
|
|
73
159
|
}
|
|
160
|
+
|
|
161
|
+
// Resume existing countdown if paused, otherwise schedule fresh.
|
|
162
|
+
if (this.#controller) {
|
|
163
|
+
if (this.#controller.isPaused) {
|
|
164
|
+
this.#controller.resume(this.#tick);
|
|
165
|
+
} else if (!this.#controller.isRunning) {
|
|
166
|
+
// Controller exists but isn't active (edge); schedule next window
|
|
167
|
+
this.#schedule();
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.#schedule();
|
|
74
173
|
}
|
|
75
174
|
|
|
76
175
|
cancel() {
|
|
77
|
-
this.#
|
|
78
|
-
this.#
|
|
176
|
+
if (this.#finished) return;
|
|
177
|
+
this.#finish();
|
|
79
178
|
}
|
|
80
179
|
|
|
81
180
|
reset(restart = false) {
|
|
82
|
-
|
|
181
|
+
// Reset is not terminal; keep signal semantics:
|
|
182
|
+
// - If signal exists and is aborted, reset should leave the timer finished.
|
|
183
|
+
this.#controller?.cancel();
|
|
184
|
+
this.#controller = null;
|
|
185
|
+
|
|
83
186
|
this.#count = 0;
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
}
|
|
187
|
+
this.#finished = false;
|
|
87
188
|
|
|
189
|
+
if (this.#signal?.aborted) {
|
|
190
|
+
this.#finished = true;
|
|
191
|
+
this.#cleanupAbort();
|
|
192
|
+
this.#cleanupAbort = () => {};
|
|
193
|
+
this.#signal = null;
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
88
196
|
|
|
197
|
+
if (restart) {
|
|
198
|
+
// If max is zero-ish, immediately finish (consistent with constructor)
|
|
199
|
+
if (!(this.#max > 0) && this.#max !== Infinity) {
|
|
200
|
+
this.#finished = true;
|
|
201
|
+
this.#cleanupAbort();
|
|
202
|
+
this.#cleanupAbort = () => {};
|
|
203
|
+
this.#signal = null;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
this.#schedule();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|