timeout-flow 0.0.16 → 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.
Files changed (46) hide show
  1. package/README.md +239 -144
  2. package/dist/timeout-flow.min.js +1 -6
  3. package/package.json +2 -1
  4. package/src/AfterTimer.js +64 -3
  5. package/src/EveryTimer.js +139 -18
  6. package/src/TimeoutFlow.js +291 -118
  7. package/src/TimerBase.js +81 -23
  8. package/src/abort.js +75 -0
  9. package/src/after.js +20 -71
  10. package/src/context-js.bat +58 -0
  11. package/src/context.txt +2604 -0
  12. package/src/debounce.js +90 -24
  13. package/src/every.js +19 -105
  14. package/src/index.js +7 -2
  15. package/src/now.js +23 -0
  16. package/src/parseDuration.js +22 -20
  17. package/src/raf/afterRaf.js +198 -66
  18. package/src/raf/debounceRaf.js +108 -26
  19. package/src/raf/everyRaf.js +278 -67
  20. package/src/raf/index.js +26 -6
  21. package/src/raf/throttleRaf.js +175 -31
  22. package/src/raf/waitForRaf.js +82 -14
  23. package/src/resolveDelayAndFn.js +69 -7
  24. package/src/retry.js +152 -12
  25. package/src/throttle.js +123 -24
  26. package/src/waitFor.js +68 -24
  27. package/types/AfterTimer.d.ts +8 -2
  28. package/types/EveryTimer.d.ts +21 -4
  29. package/types/TimeoutFlow.d.ts +6 -18
  30. package/types/TimerBase.d.ts +15 -4
  31. package/types/abort.d.ts +31 -0
  32. package/types/after.d.ts +22 -22
  33. package/types/debounce.d.ts +28 -6
  34. package/types/every.d.ts +20 -28
  35. package/types/index.d.ts +1 -1
  36. package/types/now.d.ts +1 -0
  37. package/types/raf/afterRaf.d.ts +35 -5
  38. package/types/raf/debounceRaf.d.ts +27 -6
  39. package/types/raf/everyRaf.d.ts +39 -9
  40. package/types/raf/index.d.ts +6 -5
  41. package/types/raf/throttleRaf.d.ts +33 -10
  42. package/types/raf/waitForRaf.d.ts +10 -4
  43. package/types/resolveDelayAndFn.d.ts +27 -0
  44. package/types/retry.d.ts +25 -8
  45. package/types/throttle.d.ts +33 -5
  46. 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
- #running = false;
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 {number} [max=Infinity] - Max executions
20
- * @param {boolean} [runImmediately=false] - Run `fn` once before first delay
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.#running;
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.cancel();
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 (!this.#running && this.#count < this.#max) {
71
- this.#controller?.resume(this.#tick);
72
- this.#running = true;
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.#controller?.cancel();
78
- this.#running = false;
176
+ if (this.#finished) return;
177
+ this.#finish();
79
178
  }
80
179
 
81
180
  reset(restart = false) {
82
- this.cancel();
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
- if (restart) this.#schedule();
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
+ }