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,228 +1,401 @@
1
-
2
- // ./src/TimeoutFlow.js
3
-
4
- import { after } from './after.js';
5
- import { every } from './every.js';
6
-
7
- /**
8
- * Creates a fluent scheduler.
9
- * Returns a chainable timeline.
10
- */
11
- export function flow() {
12
- const steps = [];
13
- let currentIndex = 0;
14
- let isPaused = false;
15
- let isCancelled = false;
16
- let loopEnabled = false;
17
- let loopLimit = Infinity;
18
- let loopCount = 0;
19
- let onFinishCallback = null;
20
- const labelMap = new Map();
21
- let jumpTarget = null;
22
- let conditionStack = [];
23
- let skipMode = false;
24
- let whileCondition = null;
25
- let doWhileCondition = null;
26
-
27
- const runner = {
28
-
29
- after(duration, fn) {
30
- steps.push({ type: 'after', duration, fn });
31
- return runner;
32
- },
33
-
34
- every(duration, fn, times = Infinity) {
35
- steps.push({
36
- type: 'every',
37
- duration,
38
- fn,
39
- times,
40
- whileCondition,
41
- doWhileCondition
42
- });
43
- whileCondition = null;
44
- doWhileCondition = null;
45
- return runner;
46
- },
47
-
48
- loop(n = true) {
49
- loopEnabled = true;
50
- loopLimit = n === true ? Infinity : n;
51
- return runner;
52
- },
53
-
54
- onFinish(cb) {
55
- onFinishCallback = cb;
56
- return runner;
57
- },
58
-
59
- start() {
60
- isCancelled = false;
61
- isPaused = false;
62
- currentIndex = 0;
63
- loopCount = 0;
64
- executeNext();
65
- return runner;
66
- },
67
-
68
- pause() {
69
- if (isPaused || isCancelled) return;
70
- isPaused = true;
71
- steps[currentIndex]?.controller?.pause?.();
72
- },
73
-
74
- resume() {
75
- if (!isPaused || isCancelled) return;
76
- isPaused = false;
77
- steps[currentIndex]?.controller?.resume?.();
78
- },
79
-
80
- cancel() {
81
- isCancelled = true;
82
- const step = steps[currentIndex];
83
- step?.controller?.cancel?.();
84
- step.controller = null;
85
- },
86
-
87
- reset() {
88
- runner.cancel();
89
- steps.length = 0;
90
- currentIndex = 0;
91
- loopCount = 0;
92
- onFinishCallback = null;
93
- return runner;
94
- },
95
-
96
- get isPaused() {
97
- return isPaused;
98
- },
99
-
100
- label(name) {
101
- labelMap.set(name, steps.length); // point to next step index
102
- return runner;
103
- },
104
-
105
- jumpTo(name) {
106
- jumpTarget = name;
107
- return runner;
108
- },
109
-
110
- if(predicate) {
111
- conditionStack.push(() => (typeof predicate === 'function' ? predicate() : !!predicate));
112
- return runner;
113
- },
114
-
115
- unless(predicate) {
116
- conditionStack.push(() => !(typeof predicate === 'function' ? predicate() : !!predicate));
117
- return runner;
118
- },
119
-
120
- while(predicate) {
121
- whileCondition = () => (typeof predicate === 'function' ? predicate() : !!predicate);
122
- return runner;
123
- },
124
-
125
- doWhile(predicate) {
126
- doWhileCondition = () => (typeof predicate === 'function' ? predicate() : !!predicate);
127
- return runner;
128
- }
129
-
130
- };
131
-
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
- return runner;
1
+ // ./src/TimeoutFlow.js
2
+
3
+ import { after } from './after.js';
4
+ import { every } from './every.js';
5
+ import { pendingAbort } from './abort.js';
6
+
7
+ /**
8
+ * Creates a fluent scheduler.
9
+ * Returns a chainable timeline.
10
+ *
11
+ * @param {Object} [options]
12
+ * @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel the flow
13
+ */
14
+ export function flow({ signal } = {}) {
15
+ const steps = [];
16
+ const labelMap = new Map();
17
+
18
+ let currentIndex = 0;
19
+
20
+ let started = false;
21
+ let isPaused = false;
22
+ let isCancelled = false;
23
+ let isFinished = false;
24
+
25
+ let loopEnabled = false;
26
+ let loopLimit = Infinity;
27
+ let loopCount = 0;
28
+
29
+ /** @type {Function | null} */
30
+ let onFinishCallback = null;
31
+ /** @type {Function | null} */
32
+ let onErrorCallback = null;
33
+
34
+ let jumpTarget = null;
35
+
36
+ /** @type {Array<() => boolean>} */
37
+ let conditionStack = [];
38
+
39
+ let skipMode = false;
40
+
41
+ let whileCondition = null;
42
+ let doWhileCondition = null;
43
+
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
+ }
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 = {
230
+ after(duration, fn) {
231
+ steps.push({ type: 'after', duration, fn, controller: null });
232
+ return runner;
233
+ },
234
+
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
+
244
+ steps.push({
245
+ type: 'every',
246
+ duration,
247
+ fn,
248
+ times,
249
+ whileCondition,
250
+ doWhileCondition,
251
+ controller: null,
252
+ });
253
+
254
+ whileCondition = null;
255
+ doWhileCondition = null;
256
+ return runner;
257
+ },
258
+
259
+ loop(n = true) {
260
+ loopEnabled = true;
261
+ loopLimit = n === true ? Infinity : n;
262
+ return runner;
263
+ },
264
+
265
+ onFinish(cb) {
266
+ onFinishCallback = cb;
267
+ return runner;
268
+ },
269
+
270
+ onError(cb) {
271
+ onErrorCallback = cb;
272
+ return runner;
273
+ },
274
+
275
+ start() {
276
+ // restart hygiene: cancel any active controller from a previous run
277
+ cleanupCurrentController();
278
+
279
+ isCancelled = false;
280
+ isFinished = false;
281
+ isPaused = false;
282
+ started = true;
283
+
284
+ currentIndex = 0;
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
+
296
+ executeNext();
297
+ return runner;
298
+ },
299
+
300
+ pause() {
301
+ if (isPaused || isCancelled || isFinished) return;
302
+ isPaused = true;
303
+ steps[currentIndex]?.controller?.pause?.();
304
+ },
305
+
306
+ resume() {
307
+ if (!isPaused || isCancelled || isFinished) return;
308
+ if (signal?.aborted) {
309
+ cancelFlow();
310
+ return;
311
+ }
312
+ isPaused = false;
313
+ steps[currentIndex]?.controller?.resume?.();
314
+ },
315
+
316
+ cancel() {
317
+ cancelFlow(); // ✅ never calls onFinish
318
+ },
319
+
320
+ reset() {
321
+ cancelFlow();
322
+
323
+ steps.length = 0;
324
+ labelMap.clear();
325
+
326
+ currentIndex = 0;
327
+ loopCount = 0;
328
+
329
+ onFinishCallback = null;
330
+ onErrorCallback = null;
331
+
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;
346
+ },
347
+
348
+ label(name) {
349
+ labelMap.set(name, steps.length); // point to next step index
350
+ steps.push({ type: 'label', name, controller: null });
351
+ return runner;
352
+ },
353
+
354
+ jumpTo(name) {
355
+ jumpTarget = name;
356
+ return runner;
357
+ },
358
+
359
+ if(predicate) {
360
+ conditionStack.push(() =>
361
+ typeof predicate === 'function' ? !!predicate() : !!predicate
362
+ );
363
+ return runner;
364
+ },
365
+
366
+ unless(predicate) {
367
+ conditionStack.push(() =>
368
+ typeof predicate === 'function' ? !predicate() : !predicate
369
+ );
370
+ return runner;
371
+ },
372
+
373
+ while(predicate) {
374
+ whileCondition = () =>
375
+ typeof predicate === 'function' ? !!predicate() : !!predicate;
376
+ return runner;
377
+ },
378
+
379
+ doWhile(predicate) {
380
+ doWhileCondition = () =>
381
+ typeof predicate === 'function' ? !!predicate() : !!predicate;
382
+ return runner;
383
+ },
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
+ },
398
+ };
399
+
400
+ return runner;
228
401
  }