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/TimeoutFlow.js
CHANGED
|
@@ -1,45 +1,256 @@
|
|
|
1
|
-
|
|
2
1
|
// ./src/TimeoutFlow.js
|
|
3
2
|
|
|
4
3
|
import { after } from './after.js';
|
|
5
4
|
import { every } from './every.js';
|
|
5
|
+
import { pendingAbort } from './abort.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Creates a fluent scheduler.
|
|
9
9
|
* Returns a chainable timeline.
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} [options]
|
|
12
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel the flow
|
|
10
13
|
*/
|
|
11
|
-
export function flow() {
|
|
14
|
+
export function flow({ signal } = {}) {
|
|
12
15
|
const steps = [];
|
|
16
|
+
const labelMap = new Map();
|
|
17
|
+
|
|
13
18
|
let currentIndex = 0;
|
|
19
|
+
|
|
20
|
+
let started = false;
|
|
14
21
|
let isPaused = false;
|
|
15
22
|
let isCancelled = false;
|
|
23
|
+
let isFinished = false;
|
|
24
|
+
|
|
16
25
|
let loopEnabled = false;
|
|
17
26
|
let loopLimit = Infinity;
|
|
18
27
|
let loopCount = 0;
|
|
28
|
+
|
|
29
|
+
/** @type {Function | null} */
|
|
19
30
|
let onFinishCallback = null;
|
|
20
|
-
|
|
31
|
+
/** @type {Function | null} */
|
|
32
|
+
let onErrorCallback = null;
|
|
33
|
+
|
|
21
34
|
let jumpTarget = null;
|
|
35
|
+
|
|
36
|
+
/** @type {Array<() => boolean>} */
|
|
22
37
|
let conditionStack = [];
|
|
38
|
+
|
|
23
39
|
let skipMode = false;
|
|
40
|
+
|
|
24
41
|
let whileCondition = null;
|
|
25
42
|
let doWhileCondition = null;
|
|
26
43
|
|
|
27
|
-
|
|
44
|
+
/** @type {any} */
|
|
45
|
+
let runner;
|
|
46
|
+
|
|
47
|
+
// Abort wiring is per-run (so we don't retain references forever)
|
|
48
|
+
const abort = pendingAbort(signal, () => runner.cancel());
|
|
49
|
+
|
|
50
|
+
const clearController = (step) => {
|
|
51
|
+
if (!step) return;
|
|
52
|
+
if (step.controller) step.controller = null;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const cleanupCurrentController = () => {
|
|
56
|
+
const step = steps[currentIndex];
|
|
57
|
+
step?.controller?.cancel?.();
|
|
58
|
+
if (step) step.controller = null;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const finishFlow = () => {
|
|
62
|
+
if (isFinished || isCancelled) return;
|
|
63
|
+
|
|
64
|
+
started = false;
|
|
65
|
+
isPaused = false;
|
|
66
|
+
isFinished = true;
|
|
67
|
+
|
|
68
|
+
cleanupCurrentController();
|
|
69
|
+
abort.remove();
|
|
70
|
+
|
|
71
|
+
onFinishCallback?.();
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const cancelFlow = () => {
|
|
75
|
+
if (isCancelled) return;
|
|
76
|
+
|
|
77
|
+
started = false;
|
|
78
|
+
isPaused = false;
|
|
79
|
+
isFinished = false;
|
|
80
|
+
isCancelled = true;
|
|
81
|
+
|
|
82
|
+
cleanupCurrentController();
|
|
83
|
+
abort.remove();
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const handleError = (err, step) => {
|
|
87
|
+
// best hygiene: cancel current controller so nothing leaks
|
|
88
|
+
cancelFlow();
|
|
89
|
+
|
|
90
|
+
if (onErrorCallback) {
|
|
91
|
+
try {
|
|
92
|
+
onErrorCallback(err, {
|
|
93
|
+
step,
|
|
94
|
+
index: currentIndex,
|
|
95
|
+
});
|
|
96
|
+
} catch (_) {
|
|
97
|
+
// If the error handler itself throws, fall through to rethrow original.
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// No handler: preserve existing behavior (throw)
|
|
103
|
+
throw err;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const safeCall = (fn, stepForContext) => {
|
|
107
|
+
try {
|
|
108
|
+
return fn();
|
|
109
|
+
} catch (err) {
|
|
110
|
+
handleError(err, stepForContext);
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const incrementStep = (step) => {
|
|
116
|
+
clearController(step);
|
|
117
|
+
currentIndex += 1;
|
|
118
|
+
queueMicrotask(executeNext);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
function executeNext() {
|
|
122
|
+
if (isCancelled || isPaused || isFinished) return;
|
|
123
|
+
if (signal?.aborted) {
|
|
124
|
+
cancelFlow();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Jump handling
|
|
129
|
+
if (jumpTarget) {
|
|
130
|
+
const targetIndex = labelMap.get(jumpTarget);
|
|
131
|
+
if (typeof targetIndex === 'number') {
|
|
132
|
+
currentIndex = targetIndex;
|
|
133
|
+
} else {
|
|
134
|
+
// treat as a flow error
|
|
135
|
+
safeCall(() => {
|
|
136
|
+
throw new Error(`Label "${jumpTarget}" not found`);
|
|
137
|
+
}, steps[currentIndex]);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
jumpTarget = null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Handle if/unless conditions (applies to the next step only)
|
|
144
|
+
if (conditionStack.length > 0) {
|
|
145
|
+
const predicate = conditionStack.shift();
|
|
146
|
+
const shouldRun = safeCall(predicate, steps[currentIndex]);
|
|
147
|
+
if (!shouldRun) skipMode = true;
|
|
148
|
+
}
|
|
28
149
|
|
|
150
|
+
// Skip mode: keep skipping until we hit a label (or end)
|
|
151
|
+
if (skipMode) {
|
|
152
|
+
const next = steps[currentIndex];
|
|
153
|
+
if (!next || next.type === 'label') {
|
|
154
|
+
skipMode = false;
|
|
155
|
+
} else {
|
|
156
|
+
return incrementStep(next);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const step = steps[currentIndex];
|
|
161
|
+
|
|
162
|
+
// End of timeline
|
|
163
|
+
if (!step || currentIndex >= steps.length) {
|
|
164
|
+
if (loopEnabled && ++loopCount < loopLimit) {
|
|
165
|
+
currentIndex = 0;
|
|
166
|
+
executeNext();
|
|
167
|
+
} else {
|
|
168
|
+
finishFlow(); // ✅ only natural completion calls onFinish
|
|
169
|
+
}
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (step.type === 'label') {
|
|
174
|
+
return incrementStep(step);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (step.type === 'after') {
|
|
178
|
+
step.controller = after(
|
|
179
|
+
() => safeCall(step.fn, step),
|
|
180
|
+
step.duration,
|
|
181
|
+
{ signal, onFinish: () => incrementStep(step) }
|
|
182
|
+
);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (step.type === 'every') {
|
|
187
|
+
|
|
188
|
+
// No ticks will ever fire => advance immediately.
|
|
189
|
+
if (!(step.times > 0) && step.times !== Infinity) {
|
|
190
|
+
return incrementStep(step);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
let count = 0;
|
|
194
|
+
const ctrl = every(
|
|
195
|
+
() => {
|
|
196
|
+
if (isPaused || isCancelled || isFinished) return;
|
|
197
|
+
if (signal?.aborted) {
|
|
198
|
+
cancelFlow();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Pre-run while guard
|
|
203
|
+
if (step.whileCondition && !safeCall(step.whileCondition, step)) {
|
|
204
|
+
ctrl.cancel();
|
|
205
|
+
return incrementStep(step);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
safeCall(() => step.fn(count++), step);
|
|
209
|
+
|
|
210
|
+
// Post-run doWhile guard
|
|
211
|
+
if (step.doWhileCondition && !safeCall(step.doWhileCondition, step)) {
|
|
212
|
+
ctrl.cancel();
|
|
213
|
+
return incrementStep(step);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (count >= step.times) {
|
|
217
|
+
ctrl.cancel();
|
|
218
|
+
return incrementStep(step);
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
step.duration,
|
|
222
|
+
{ max: step.times, signal }
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
step.controller = ctrl;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
runner = {
|
|
29
230
|
after(duration, fn) {
|
|
30
|
-
steps.push({ type: 'after', duration, fn });
|
|
231
|
+
steps.push({ type: 'after', duration, fn, controller: null });
|
|
31
232
|
return runner;
|
|
32
233
|
},
|
|
33
234
|
|
|
34
|
-
every(duration, fn,
|
|
235
|
+
every(duration, fn, timesOrOptions = Infinity) {
|
|
236
|
+
const raw =
|
|
237
|
+
typeof timesOrOptions === 'number'
|
|
238
|
+
? timesOrOptions
|
|
239
|
+
: timesOrOptions?.max ?? Infinity;
|
|
240
|
+
|
|
241
|
+
const times =
|
|
242
|
+
raw === Infinity ? Infinity : Math.max(0, raw | 0); // int >= 0
|
|
243
|
+
|
|
35
244
|
steps.push({
|
|
36
245
|
type: 'every',
|
|
37
246
|
duration,
|
|
38
247
|
fn,
|
|
39
248
|
times,
|
|
40
249
|
whileCondition,
|
|
41
|
-
doWhileCondition
|
|
250
|
+
doWhileCondition,
|
|
251
|
+
controller: null,
|
|
42
252
|
});
|
|
253
|
+
|
|
43
254
|
whileCondition = null;
|
|
44
255
|
doWhileCondition = null;
|
|
45
256
|
return runner;
|
|
@@ -56,49 +267,87 @@ export function flow() {
|
|
|
56
267
|
return runner;
|
|
57
268
|
},
|
|
58
269
|
|
|
270
|
+
onError(cb) {
|
|
271
|
+
onErrorCallback = cb;
|
|
272
|
+
return runner;
|
|
273
|
+
},
|
|
274
|
+
|
|
59
275
|
start() {
|
|
276
|
+
// restart hygiene: cancel any active controller from a previous run
|
|
277
|
+
cleanupCurrentController();
|
|
278
|
+
|
|
60
279
|
isCancelled = false;
|
|
280
|
+
isFinished = false;
|
|
61
281
|
isPaused = false;
|
|
282
|
+
started = true;
|
|
283
|
+
|
|
62
284
|
currentIndex = 0;
|
|
63
285
|
loopCount = 0;
|
|
286
|
+
|
|
287
|
+
// Abort wiring is per-run
|
|
288
|
+
abort.reset();
|
|
289
|
+
if (!signal?.aborted) abort.add();
|
|
290
|
+
|
|
291
|
+
if (signal?.aborted) {
|
|
292
|
+
cancelFlow(); // no-op start if already aborted
|
|
293
|
+
return runner;
|
|
294
|
+
}
|
|
295
|
+
|
|
64
296
|
executeNext();
|
|
65
297
|
return runner;
|
|
66
298
|
},
|
|
67
299
|
|
|
68
300
|
pause() {
|
|
69
|
-
if (isPaused || isCancelled) return;
|
|
301
|
+
if (isPaused || isCancelled || isFinished) return;
|
|
70
302
|
isPaused = true;
|
|
71
303
|
steps[currentIndex]?.controller?.pause?.();
|
|
72
304
|
},
|
|
73
305
|
|
|
74
306
|
resume() {
|
|
75
|
-
if (!isPaused || isCancelled) return;
|
|
307
|
+
if (!isPaused || isCancelled || isFinished) return;
|
|
308
|
+
if (signal?.aborted) {
|
|
309
|
+
cancelFlow();
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
76
312
|
isPaused = false;
|
|
77
313
|
steps[currentIndex]?.controller?.resume?.();
|
|
78
314
|
},
|
|
79
315
|
|
|
80
316
|
cancel() {
|
|
81
|
-
|
|
82
|
-
const step = steps[currentIndex];
|
|
83
|
-
step?.controller?.cancel?.();
|
|
84
|
-
step.controller = null;
|
|
317
|
+
cancelFlow(); // ✅ never calls onFinish
|
|
85
318
|
},
|
|
86
319
|
|
|
87
320
|
reset() {
|
|
88
|
-
|
|
321
|
+
cancelFlow();
|
|
322
|
+
|
|
89
323
|
steps.length = 0;
|
|
324
|
+
labelMap.clear();
|
|
325
|
+
|
|
90
326
|
currentIndex = 0;
|
|
91
327
|
loopCount = 0;
|
|
328
|
+
|
|
92
329
|
onFinishCallback = null;
|
|
93
|
-
|
|
94
|
-
},
|
|
330
|
+
onErrorCallback = null;
|
|
95
331
|
|
|
96
|
-
|
|
97
|
-
|
|
332
|
+
jumpTarget = null;
|
|
333
|
+
conditionStack = [];
|
|
334
|
+
skipMode = false;
|
|
335
|
+
|
|
336
|
+
whileCondition = null;
|
|
337
|
+
doWhileCondition = null;
|
|
338
|
+
|
|
339
|
+
// allow reuse after reset (fresh state)
|
|
340
|
+
started = false;
|
|
341
|
+
isPaused = false;
|
|
342
|
+
isCancelled = false;
|
|
343
|
+
isFinished = false;
|
|
344
|
+
|
|
345
|
+
return runner;
|
|
98
346
|
},
|
|
99
347
|
|
|
100
348
|
label(name) {
|
|
101
349
|
labelMap.set(name, steps.length); // point to next step index
|
|
350
|
+
steps.push({ type: 'label', name, controller: null });
|
|
102
351
|
return runner;
|
|
103
352
|
},
|
|
104
353
|
|
|
@@ -108,121 +357,45 @@ export function flow() {
|
|
|
108
357
|
},
|
|
109
358
|
|
|
110
359
|
if(predicate) {
|
|
111
|
-
conditionStack.push(() =>
|
|
360
|
+
conditionStack.push(() =>
|
|
361
|
+
typeof predicate === 'function' ? !!predicate() : !!predicate
|
|
362
|
+
);
|
|
112
363
|
return runner;
|
|
113
364
|
},
|
|
114
365
|
|
|
115
366
|
unless(predicate) {
|
|
116
|
-
conditionStack.push(() =>
|
|
367
|
+
conditionStack.push(() =>
|
|
368
|
+
typeof predicate === 'function' ? !predicate() : !predicate
|
|
369
|
+
);
|
|
117
370
|
return runner;
|
|
118
371
|
},
|
|
119
372
|
|
|
120
373
|
while(predicate) {
|
|
121
|
-
whileCondition = () =>
|
|
374
|
+
whileCondition = () =>
|
|
375
|
+
typeof predicate === 'function' ? !!predicate() : !!predicate;
|
|
122
376
|
return runner;
|
|
123
377
|
},
|
|
124
378
|
|
|
125
379
|
doWhile(predicate) {
|
|
126
|
-
doWhileCondition = () =>
|
|
380
|
+
doWhileCondition = () =>
|
|
381
|
+
typeof predicate === 'function' ? !!predicate() : !!predicate;
|
|
127
382
|
return runner;
|
|
128
|
-
}
|
|
383
|
+
},
|
|
129
384
|
|
|
385
|
+
// unified lifecycle surface
|
|
386
|
+
get isRunning() {
|
|
387
|
+
return started && !isPaused && !isCancelled && !isFinished;
|
|
388
|
+
},
|
|
389
|
+
get isPaused() {
|
|
390
|
+
return isPaused;
|
|
391
|
+
},
|
|
392
|
+
get isCancelled() {
|
|
393
|
+
return isCancelled;
|
|
394
|
+
},
|
|
395
|
+
get isFinished() {
|
|
396
|
+
return isFinished;
|
|
397
|
+
},
|
|
130
398
|
};
|
|
131
399
|
|
|
132
|
-
function increment_step(step = null) {
|
|
133
|
-
if (step?.controller) step.controller = null;
|
|
134
|
-
currentIndex++;
|
|
135
|
-
executeNext();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function executeNext() {
|
|
139
|
-
if (isCancelled || isPaused) return;
|
|
140
|
-
|
|
141
|
-
if (jumpTarget) {
|
|
142
|
-
const targetIndex = labelMap.get(jumpTarget);
|
|
143
|
-
if (typeof targetIndex === 'number') {
|
|
144
|
-
currentIndex = targetIndex;
|
|
145
|
-
} else {
|
|
146
|
-
throw new Error(`Label "${jumpTarget}" not found`);
|
|
147
|
-
}
|
|
148
|
-
jumpTarget = null; // reset after jump
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Handle if conditions
|
|
152
|
-
if (conditionStack.length > 0) {
|
|
153
|
-
const shouldRun = conditionStack.shift()(); // run & remove
|
|
154
|
-
if (!shouldRun) {
|
|
155
|
-
skipMode = true;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Skip logic
|
|
160
|
-
if (skipMode) {
|
|
161
|
-
const next = steps[currentIndex];
|
|
162
|
-
if (!next || next.type === 'label') {
|
|
163
|
-
skipMode = false; // stop skipping at label
|
|
164
|
-
} else {
|
|
165
|
-
return increment_step(step);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// End of timeline
|
|
170
|
-
const step = steps[currentIndex];
|
|
171
|
-
|
|
172
|
-
if (!step || currentIndex >= steps.length) {
|
|
173
|
-
if (loopEnabled && ++loopCount < loopLimit) {
|
|
174
|
-
currentIndex = 0;
|
|
175
|
-
executeNext();
|
|
176
|
-
} else {
|
|
177
|
-
onFinishCallback?.();
|
|
178
|
-
}
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
if (step.type === 'after') {
|
|
184
|
-
step.controller = after(step.duration, step.fn, () => increment_step(step));
|
|
185
|
-
return
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
if (step.type === 'every') {
|
|
189
|
-
let count = 0;
|
|
190
|
-
let firstRun = true;
|
|
191
|
-
|
|
192
|
-
const ctrl = every(step.duration, () => {
|
|
193
|
-
if (isPaused || isCancelled) return;
|
|
194
|
-
|
|
195
|
-
// Handle .while (skip when fails before run)
|
|
196
|
-
if (step.whileCondition && !step.whileCondition()) {
|
|
197
|
-
ctrl.cancel();
|
|
198
|
-
return increment_step(step);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// Run fn first
|
|
202
|
-
step.fn(count++);
|
|
203
|
-
firstRun = false;
|
|
204
|
-
|
|
205
|
-
// Check doWhile after first execution
|
|
206
|
-
if (!firstRun && step.doWhileCondition && !step.doWhileCondition()) {
|
|
207
|
-
ctrl.cancel();
|
|
208
|
-
return increment_step(step);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (count >= step.times) {
|
|
212
|
-
ctrl.cancel();
|
|
213
|
-
return increment_step(step);
|
|
214
|
-
}
|
|
215
|
-
}, step.times);
|
|
216
|
-
|
|
217
|
-
step.controller = ctrl;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (step.type === 'label') {
|
|
221
|
-
return increment_step(step);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
|
|
227
400
|
return runner;
|
|
228
401
|
}
|
package/src/TimerBase.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// ./src/TimerBase.js
|
|
2
2
|
import { parseDuration } from './parseDuration.js';
|
|
3
|
+
import { now } from './now.js'
|
|
4
|
+
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Base timer class to manage pause/resume/cancel behavior.
|
|
@@ -7,10 +9,13 @@ import { parseDuration } from './parseDuration.js';
|
|
|
7
9
|
*/
|
|
8
10
|
export class TimerBase {
|
|
9
11
|
#ms;
|
|
10
|
-
#startTime = null;
|
|
12
|
+
#startTime = null; // performance.now()
|
|
11
13
|
#remaining;
|
|
12
14
|
#timer = null;
|
|
15
|
+
|
|
13
16
|
#running = false;
|
|
17
|
+
#paused = false;
|
|
18
|
+
#finished = false;
|
|
14
19
|
|
|
15
20
|
/**
|
|
16
21
|
* @param {string|number} duration - The duration to wait in ms or shorthand format
|
|
@@ -21,55 +26,108 @@ export class TimerBase {
|
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
/**
|
|
24
|
-
* Whether the timer is currently running.
|
|
29
|
+
* Whether the timer is currently running (actively counting down).
|
|
25
30
|
* @returns {boolean}
|
|
26
31
|
*/
|
|
27
32
|
get isRunning() {
|
|
28
33
|
return this.#running;
|
|
29
34
|
}
|
|
30
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Whether the timer is paused (resumable).
|
|
38
|
+
* @returns {boolean}
|
|
39
|
+
*/
|
|
40
|
+
get isPaused() {
|
|
41
|
+
return this.#paused;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Whether the timer has finished or been canceled (terminal state).
|
|
46
|
+
* @returns {boolean}
|
|
47
|
+
*/
|
|
48
|
+
get isFinished() {
|
|
49
|
+
return this.#finished;
|
|
50
|
+
}
|
|
51
|
+
|
|
31
52
|
/**
|
|
32
53
|
* Pause the active timer, storing remaining time.
|
|
33
54
|
*/
|
|
34
55
|
pause() {
|
|
35
|
-
if (this.#running)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
56
|
+
if (!this.#running) return;
|
|
57
|
+
|
|
58
|
+
clearTimeout(this.#timer);
|
|
59
|
+
this.#timer = null;
|
|
60
|
+
|
|
61
|
+
const elapsed = now() - this.#startTime;
|
|
62
|
+
this.#remaining = Math.max(0, this.#remaining - elapsed);
|
|
63
|
+
|
|
64
|
+
this.#running = false;
|
|
65
|
+
this.#paused = true;
|
|
66
|
+
// not finished; still resumable
|
|
40
67
|
}
|
|
41
68
|
|
|
42
69
|
/**
|
|
43
|
-
* Resume the timer using the remaining time.
|
|
70
|
+
* Resume the timer using the remaining time (or an override delay).
|
|
44
71
|
* @param {Function} callback - Called when time completes
|
|
45
|
-
* @param {number|null} [overrideDelay] - Optional custom delay in ms
|
|
72
|
+
* @param {number|null} [overrideDelay=null] - Optional custom delay in ms
|
|
46
73
|
*/
|
|
47
74
|
resume(callback, overrideDelay = null) {
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
75
|
+
if (this.#finished) return;
|
|
76
|
+
if (this.#running) return;
|
|
77
|
+
|
|
78
|
+
const rawDelay = overrideDelay ?? this.#remaining;
|
|
79
|
+
|
|
80
|
+
// Reject invalid delays (avoid scheduling with NaN/Infinity/etc.)
|
|
81
|
+
if (!Number.isFinite(rawDelay) || rawDelay < 0) return;
|
|
82
|
+
|
|
83
|
+
// Support 0ms timers (schedule next macrotask)
|
|
84
|
+
const delay = Math.max(0, rawDelay);
|
|
85
|
+
|
|
86
|
+
// Important: remaining must reflect what we actually scheduled,
|
|
87
|
+
// otherwise pause() math will be wrong when overrideDelay is used.
|
|
88
|
+
this.#remaining = delay;
|
|
89
|
+
|
|
90
|
+
this.#startTime = now();
|
|
91
|
+
this.#running = true;
|
|
92
|
+
this.#paused = false;
|
|
93
|
+
|
|
94
|
+
this.#timer = setTimeout(() => {
|
|
95
|
+
this.#timer = null;
|
|
96
|
+
this.#running = false;
|
|
97
|
+
this.#paused = false;
|
|
98
|
+
this.#finished = true;
|
|
99
|
+
this.#remaining = 0;
|
|
100
|
+
callback?.();
|
|
101
|
+
}, delay);
|
|
56
102
|
}
|
|
57
103
|
|
|
58
104
|
/**
|
|
59
|
-
* Cancel the timer immediately.
|
|
105
|
+
* Cancel the timer immediately (terminal).
|
|
60
106
|
*/
|
|
61
107
|
cancel() {
|
|
62
|
-
clearTimeout(this.#timer);
|
|
63
|
-
this.#running = false;
|
|
108
|
+
if (this.#timer) clearTimeout(this.#timer);
|
|
64
109
|
this.#timer = null;
|
|
110
|
+
|
|
111
|
+
this.#running = false;
|
|
112
|
+
this.#paused = false;
|
|
113
|
+
this.#finished = true;
|
|
114
|
+
// leave #remaining as-is or set to 0; prefer 0 for "terminal means done"
|
|
115
|
+
this.#remaining = 0;
|
|
65
116
|
}
|
|
66
117
|
|
|
67
118
|
/**
|
|
68
119
|
* Reset the timer to its original delay, cancelling if needed.
|
|
120
|
+
* After reset, the timer is not running.
|
|
69
121
|
*/
|
|
70
122
|
reset() {
|
|
71
|
-
this
|
|
123
|
+
if (this.#timer) clearTimeout(this.#timer);
|
|
124
|
+
this.#timer = null;
|
|
125
|
+
|
|
126
|
+
this.#startTime = null;
|
|
72
127
|
this.#remaining = this.#ms;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
128
|
|
|
129
|
+
this.#running = false;
|
|
130
|
+
this.#paused = false;
|
|
131
|
+
this.#finished = false;
|
|
132
|
+
}
|
|
133
|
+
}
|