timeout-flow 0.0.18 → 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.
package/src/EveryTimer.js CHANGED
@@ -1,209 +1,209 @@
1
- // ./src/EveryTimer.js
2
- import { attachAbort } from './abort.js';
3
- import { TimerBase } from './TimerBase.js';
4
- import { parseDuration } from './parseDuration.js';
5
-
6
- /**
7
- * Repeatedly runs a function every N ms, with optional max count.
8
- */
9
- export class EveryTimer {
10
- #interval;
11
- #fn;
12
- #max;
13
-
14
- #count = 0;
15
- #finished = false;
16
-
17
- /** @type {TimerBase | null} */
18
- #controller = null;
19
-
20
- /** @type {AbortSignal | null} */
21
- #signal = null;
22
-
23
- /** @type {() => void} */
24
- #cleanupAbort = () => {};
25
-
26
- /**
27
- * @param {string|number} duration - Delay between calls (e.g., '1s', 200)
28
- * @param {Function} fn - Function to call each tick
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
33
- */
34
- constructor(duration, fn, { max = Infinity, runImmediately = false, signal } = {}) {
35
- this.#interval = parseDuration(duration);
36
- this.#fn = fn;
37
- this.#max = max;
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
-
59
- if (runImmediately) {
60
- this.#tick();
61
- } else {
62
- this.#schedule();
63
- }
64
- }
65
-
66
- /**
67
- * Number of times `fn` has been executed.
68
- */
69
- get count() {
70
- return this.#count;
71
- }
72
-
73
- /**
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".
78
- */
79
- get isRunning() {
80
- return !this.#finished && !!this.#controller && this.#controller.isRunning;
81
- }
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
-
108
- #tick = () => {
109
- if (this.#finished) return;
110
-
111
- if (this.#signal?.aborted) {
112
- this.#finish();
113
- return;
114
- }
115
-
116
- if (this.#count >= this.#max) {
117
- this.#finish();
118
- return;
119
- }
120
-
121
- this.#fn?.(this.#count++);
122
- if (this.#count >= this.#max) {
123
- this.#finish();
124
- return;
125
- }
126
-
127
- this.#schedule();
128
- };
129
-
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
139
- this.#controller = new TimerBase(this.#interval);
140
- this.#controller.resume(this.#tick);
141
- }
142
-
143
- pause() {
144
- if (this.#finished) return;
145
- this.#controller?.pause();
146
- }
147
-
148
- resume() {
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;
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();
173
- }
174
-
175
- cancel() {
176
- if (this.#finished) return;
177
- this.#finish();
178
- }
179
-
180
- reset(restart = false) {
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
-
186
- this.#count = 0;
187
- this.#finished = false;
188
-
189
- if (this.#signal?.aborted) {
190
- this.#finished = true;
191
- this.#cleanupAbort();
192
- this.#cleanupAbort = () => {};
193
- this.#signal = null;
194
- return;
195
- }
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
- }
1
+ // ./src/EveryTimer.js
2
+ import { attachAbort } from './abort.js';
3
+ import { TimerBase } from './TimerBase.js';
4
+ import { parseDuration } from './parseDuration.js';
5
+
6
+ /**
7
+ * Repeatedly runs a function every N ms, with optional max count.
8
+ */
9
+ export class EveryTimer {
10
+ #interval;
11
+ #fn;
12
+ #max;
13
+
14
+ #count = 0;
15
+ #finished = false;
16
+
17
+ /** @type {TimerBase | null} */
18
+ #controller = null;
19
+
20
+ /** @type {AbortSignal | null} */
21
+ #signal = null;
22
+
23
+ /** @type {() => void} */
24
+ #cleanupAbort = () => {};
25
+
26
+ /**
27
+ * @param {string|number} duration - Delay between calls (e.g., '1s', 200)
28
+ * @param {Function} fn - Function to call each tick
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
33
+ */
34
+ constructor(duration, fn, { max = Infinity, runImmediately = false, signal } = {}) {
35
+ this.#interval = parseDuration(duration);
36
+ this.#fn = fn;
37
+ this.#max = max;
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
+
59
+ if (runImmediately) {
60
+ this.#tick();
61
+ } else {
62
+ this.#schedule();
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Number of times `fn` has been executed.
68
+ */
69
+ get count() {
70
+ return this.#count;
71
+ }
72
+
73
+ /**
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".
78
+ */
79
+ get isRunning() {
80
+ return !this.#finished && !!this.#controller && this.#controller.isRunning;
81
+ }
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
+
108
+ #tick = () => {
109
+ if (this.#finished) return;
110
+
111
+ if (this.#signal?.aborted) {
112
+ this.#finish();
113
+ return;
114
+ }
115
+
116
+ if (this.#count >= this.#max) {
117
+ this.#finish();
118
+ return;
119
+ }
120
+
121
+ this.#fn?.(this.#count++);
122
+ if (this.#count >= this.#max) {
123
+ this.#finish();
124
+ return;
125
+ }
126
+
127
+ this.#schedule();
128
+ };
129
+
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
139
+ this.#controller = new TimerBase(this.#interval);
140
+ this.#controller.resume(this.#tick);
141
+ }
142
+
143
+ pause() {
144
+ if (this.#finished) return;
145
+ this.#controller?.pause();
146
+ }
147
+
148
+ resume() {
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;
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();
173
+ }
174
+
175
+ cancel() {
176
+ if (this.#finished) return;
177
+ this.#finish();
178
+ }
179
+
180
+ reset(restart = false) {
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
+
186
+ this.#count = 0;
187
+ this.#finished = false;
188
+
189
+ if (this.#signal?.aborted) {
190
+ this.#finished = true;
191
+ this.#cleanupAbort();
192
+ this.#cleanupAbort = () => {};
193
+ this.#signal = null;
194
+ return;
195
+ }
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
209
  }