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/README.md CHANGED
@@ -1,332 +1,307 @@
1
- # TimeoutFlow
2
-
3
- [![npm](https://img.shields.io/npm/v/timeout-flow)](https://www.npmjs.com/package/timeout-flow)
4
- [![gzip size](https://img.shields.io/bundlephobia/minzip/timeout-flow)](https://bundlephobia.com/package/timeout-flow)
5
- [![downloads](https://img.shields.io/npm/dw/timeout-flow)](https://www.npmjs.com/package/timeout-flow)
6
- [![GitHub stars](https://img.shields.io/github/stars/iWhatty/TimeoutFlow-js?style=social)](https://github.com/iWhatty/TimeoutFlow-js)
7
-
8
- **Fluent, human-readable time control for JavaScript.**
9
-
10
- TimeoutFlow makes working with time-based logic intuitive — think of it as a modern, composable upgrade to `setTimeout` and `setInterval`, with chaining, conditional logic, pause/resume control, retries, RAF utilities, and more.
11
-
12
- * Minified: ~6–7 KB
13
- * Gzipped: ~2–3 KB
14
- * Zero dependencies
15
- * ESM-first, tree-shakeable
16
-
17
- ---
18
-
19
- # Philosophy
20
-
21
- TimeoutFlow is not just a wrapper around timers.
22
- It’s a composable toolkit for expressing time as readable logic.
23
-
24
- Temporal behavior should be:
25
-
26
- * **Readable** — durations like `"1s"` and `"500ms"` beat magic numbers.
27
- * **Composable** — sequencing should be declarative.
28
- * **Controllable** — timers should pause, resume, cancel.
29
- * **Branchable** — real flows need `if`, `while`, `label`, `jumpTo()`.
30
- * **Tiny** — no runtime bloat.
31
-
32
- > Think of TimeoutFlow as `setTimeout()` with superpowers.
33
-
34
- ---
35
-
36
- # Clean Signatures
37
-
38
- TimeoutFlow supports **both** natural-language ordering and function-first ordering.
39
-
40
- You can write:
41
-
42
- ```js
43
- after('1s', done);
44
- ```
45
-
46
- Or:
47
-
48
- ```js
49
- after(done, '1s');
50
- ```
51
-
52
- Both are valid.
53
-
54
- This applies to:
55
-
56
- * `after()`
57
- * `every()`
58
- * `debounce()`
59
- * `throttle()`
60
-
61
- Use whichever reads best in your codebase.
62
-
63
- ---
64
-
65
- # Installation
66
-
67
- ```bash
68
- npm install timeout-flow
69
- ```
70
-
71
- ---
72
-
73
- # Unified Control Surface
74
-
75
- Most time-based primitives return a **controller object**:
76
-
77
- ```js
78
- import { after } from 'timeout-flow';
79
-
80
- function done() {
81
- console.log('done');
82
- }
83
-
84
- const ctrl = after('1s', done);
85
-
86
- ctrl.pause();
87
- ctrl.resume();
88
- ctrl.cancel();
89
- ctrl.reset?.();
90
-
91
- console.log(ctrl.isRunning);
92
- console.log(ctrl.isPaused);
93
- console.log(ctrl.isFinished);
94
- ```
95
-
96
- Shared by:
97
-
98
- * `after()`
99
- * `every()`
100
- * `afterRaf()`
101
- * `everyRaf()`
102
-
103
- All controllers are:
104
-
105
- * Pause-safe (paused time does not count)
106
- * Cancel-safe
107
- * Monotonic-time based
108
- * AbortSignal-aware (where supported)
109
-
110
- ---
111
-
112
- # Core Primitives
113
-
114
- ## after()
115
-
116
- Run once after a delay.
117
-
118
- ```js
119
- import { after } from 'timeout-flow';
120
-
121
- function greet() {
122
- console.log('Hello');
123
- }
124
-
125
- after('2s', greet);
126
- ```
127
-
128
- ---
129
-
130
- ## every()
131
-
132
- Repeat execution with optional limit.
133
-
134
- ```js
135
- import { every } from 'timeout-flow';
136
-
137
- function tick(i) {
138
- console.log('Tick', i);
139
- }
140
-
141
- const ticker = every('1s', tick, { max: 5 });
142
- ```
143
-
144
- ---
145
-
146
- ## debounce()
147
-
148
- Delay execution until inactivity.
149
-
150
- ```js
151
- import { debounce } from 'timeout-flow';
152
-
153
- function search(event) {
154
- console.log('Searching for:', event.target.value);
155
- }
156
-
157
- const debouncedSearch = debounce('300ms', search);
158
-
159
- debouncedSearch.cancel();
160
- debouncedSearch.flush();
161
- ```
162
-
163
- ---
164
-
165
- ## throttle()
166
-
167
- Limit execution frequency.
168
-
169
- ```js
170
- import { throttle } from 'timeout-flow';
171
-
172
- function handleScroll() {
173
- console.log('scroll');
174
- }
175
-
176
- const onScroll = throttle('250ms', handleScroll);
177
-
178
- onScroll.cancel();
179
- onScroll.flush();
180
- ```
181
-
182
- ---
183
-
184
- ## retry()
185
-
186
- Retry async operations with backoff + jitter.
187
-
188
- ```js
189
- import { retry } from 'timeout-flow';
190
-
191
- async function fetchData() {
192
- return fetch('/api/data');
193
- }
194
-
195
- await retry(fetchData, {
196
- attempts: 5,
197
- delay: '500ms',
198
- backoff: true,
199
- factor: 2,
200
- maxDelay: '5s',
201
- jitter: 'decorrelated'
202
- });
203
- ```
204
-
205
- ---
206
-
207
- ## waitFor()
208
-
209
- Wait until a condition becomes true.
210
-
211
- ```js
212
- import { waitFor } from 'timeout-flow';
213
-
214
- await waitFor(function checkLoaded() {
215
- return document.querySelector('#loaded');
216
- }, {
217
- interval: '250ms',
218
- timeout: '5s',
219
- immediate: true
220
- });
221
- ```
222
-
223
- ---
224
-
225
- # Fluent Timeline
226
-
227
- Build declarative time flows.
228
-
229
- ```js
230
- import { flow } from 'timeout-flow';
231
-
232
- function stepOne() {
233
- console.log('Step 1');
234
- }
235
-
236
- function tick(i) {
237
- console.log('Tick', i);
238
- }
239
-
240
- function finalStep() {
241
- console.log('Final Step');
242
- }
243
-
244
- flow()
245
- .after('1s', stepOne)
246
- .every('500ms', tick, { max: 3 })
247
- .after('1s', finalStep)
248
- .start();
249
- ```
250
-
251
- Flow reads like a timeline and keeps duration-first ordering for readability.
252
-
253
- ---
254
-
255
- # AbortSignal Support
256
-
257
- Many utilities accept `{ signal }` for automatic cancellation.
258
-
259
- ```js
260
- import { after } from 'timeout-flow';
261
-
262
- const ac = new AbortController();
263
-
264
- function doWork() {
265
- console.log('Will not run if aborted');
266
- }
267
-
268
- after('2s', doWork, { signal: ac.signal });
269
-
270
- ac.abort();
271
- ```
272
-
273
- If already aborted at creation time, no work is scheduled.
274
-
275
- ---
276
-
277
- # RAF Utilities
278
-
279
- Frame-based timing powered by `requestAnimationFrame`.
280
-
281
- Ideal for visual updates, scroll handlers, layout checks, and animation loops.
282
-
283
- ## Available
284
-
285
- * `afterRaf()`
286
- * `everyRaf()`
287
- * `debounceRaf()`
288
- * `throttleRaf()`
289
- * `waitForRaf()`
290
-
291
- ## Example
292
-
293
- ```js
294
- import { throttleRaf } from 'timeout-flow';
295
-
296
- function drawFrame() {
297
- console.log('draw');
298
- }
299
-
300
- const onScroll = throttleRaf(drawFrame);
301
-
302
- onScroll.cancel();
303
- onScroll.flush();
304
- ```
305
-
306
- ---
307
-
308
- # Public API
309
-
310
- ```js
311
- import {
312
- after,
313
- every,
314
- debounce,
315
- throttle,
316
- retry,
317
- waitFor,
318
- flow,
319
-
320
- afterRaf,
321
- everyRaf,
322
- debounceRaf,
323
- throttleRaf,
324
- waitForRaf
325
- } from 'timeout-flow';
326
- ```
327
-
328
- ---
329
-
330
- # License
331
-
332
- --{DR.WATT v3.0}--
1
+ # timeout-flow
2
+
3
+ [![npm](https://img.shields.io/npm/v/timeout-flow)](https://www.npmjs.com/package/timeout-flow)
4
+ [![downloads](https://img.shields.io/npm/dm/timeout-flow)](https://www.npmjs.com/package/timeout-flow)
5
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/timeout-flow)](https://bundlephobia.com/package/timeout-flow)
6
+ [![license](https://img.shields.io/npm/l/timeout-flow)](https://github.com/iWhatty/TimeoutFlow-js/blob/main/LICENSE)
7
+ [![stars](https://img.shields.io/github/stars/iWhatty/TimeoutFlow-js?style=social)](https://github.com/iWhatty/TimeoutFlow-js)
8
+
9
+ Fluent, human-readable time control for JavaScript. A modern, composable upgrade to `setTimeout` and `setInterval` with chaining, conditional logic, pause/resume control, retries, and RAF utilities.
10
+
11
+ ## Features
12
+
13
+ - Readable durations: `"1s"`, `"500ms"`, plain numbers
14
+ - Pause / resume / cancel controllers for `after()`, `every()`, RAF variants
15
+ - `flow()` builds declarative timelines with branching and labels
16
+ - `retry()` with backoff, jitter, and `AbortSignal` cancellation
17
+ - `waitFor()` polls a predicate with timeout
18
+ - RAF variants (`afterRaf`, `everyRaf`, `debounceRaf`, `throttleRaf`, `waitForRaf`) for frame-aligned timing
19
+ - ESM-first, tree-shakeable. Per-file subpath exports for size-sensitive consumers (~1.2 KB gzipped for a single primitive)
20
+ - Zero dependencies
21
+ - ~6–7 KB minified for the full kit (~2–3 KB gzipped)
22
+
23
+ ---
24
+
25
+ ## Install
26
+
27
+ ```sh
28
+ pnpm add timeout-flow
29
+ ```
30
+
31
+ ---
32
+
33
+ ## Quick start
34
+
35
+ ```js
36
+ import { after } from 'timeout-flow';
37
+
38
+ function greet() {
39
+ console.log('Hello');
40
+ }
41
+
42
+ const ctrl = after('2s', greet);
43
+
44
+ ctrl.pause();
45
+ ctrl.resume();
46
+ ctrl.cancel();
47
+ ```
48
+
49
+ ---
50
+
51
+ ## API
52
+
53
+ ### `after(duration, fn, options?)`
54
+
55
+ Run once after a delay.
56
+
57
+ ```js
58
+ import { after } from 'timeout-flow';
59
+
60
+ after('2s', function greet() {
61
+ console.log('Hello');
62
+ });
63
+ ```
64
+
65
+ ### `every(duration, fn, options?)`
66
+
67
+ Repeat execution with optional limit.
68
+
69
+ ```js
70
+ import { every } from 'timeout-flow';
71
+
72
+ const ticker = every('1s', function tick(i) {
73
+ console.log('Tick', i);
74
+ }, { max: 5 });
75
+ ```
76
+
77
+ ### `debounce(duration, fn, options?)`
78
+
79
+ Delay execution until inactivity.
80
+
81
+ ```js
82
+ import { debounce } from 'timeout-flow';
83
+
84
+ const debouncedSearch = debounce('300ms', function search(event) {
85
+ console.log('Searching for:', event.target.value);
86
+ });
87
+
88
+ debouncedSearch.cancel();
89
+ debouncedSearch.flush();
90
+ ```
91
+
92
+ ### `throttle(duration, fn, options?)`
93
+
94
+ Limit execution frequency.
95
+
96
+ ```js
97
+ import { throttle } from 'timeout-flow';
98
+
99
+ const onScroll = throttle('250ms', function handleScroll() {
100
+ console.log('scroll');
101
+ });
102
+
103
+ onScroll.cancel();
104
+ onScroll.flush();
105
+ ```
106
+
107
+ ### `retry(fn, options?)`
108
+
109
+ Retry async operations with backoff and jitter.
110
+
111
+ ```js
112
+ import { retry } from 'timeout-flow';
113
+
114
+ await retry(async function fetchData() {
115
+ return fetch('/api/data');
116
+ }, {
117
+ attempts: 5,
118
+ delay: '500ms',
119
+ backoff: true,
120
+ factor: 2,
121
+ maxDelay: '5s',
122
+ jitter: 'decorrelated'
123
+ });
124
+ ```
125
+
126
+ ### `waitFor(predicate, options?)`
127
+
128
+ Wait until a condition becomes true.
129
+
130
+ ```js
131
+ import { waitFor } from 'timeout-flow';
132
+
133
+ await waitFor(function checkLoaded() {
134
+ return document.querySelector('#loaded');
135
+ }, {
136
+ interval: '250ms',
137
+ timeout: '5s',
138
+ immediate: true
139
+ });
140
+ ```
141
+
142
+ ### `flow()`
143
+
144
+ Build declarative time flows.
145
+
146
+ ```js
147
+ import { flow } from 'timeout-flow';
148
+
149
+ flow()
150
+ .after('1s', function stepOne() {
151
+ console.log('Step 1');
152
+ })
153
+ .every('500ms', function tick(i) {
154
+ console.log('Tick', i);
155
+ }, { max: 3 })
156
+ .after('1s', function finalStep() {
157
+ console.log('Final Step');
158
+ })
159
+ .start();
160
+ ```
161
+
162
+ Flow reads like a timeline and keeps duration-first ordering for readability.
163
+
164
+ ### RAF utilities
165
+
166
+ Frame-based timing powered by `requestAnimationFrame`. Ideal for visual updates, scroll handlers, layout checks, and animation loops.
167
+
168
+ Available: `afterRaf()`, `everyRaf()`, `debounceRaf()`, `throttleRaf()`, `waitForRaf()`.
169
+
170
+ ```js
171
+ import { throttleRaf } from 'timeout-flow';
172
+
173
+ const onScroll = throttleRaf(function drawFrame() {
174
+ console.log('draw');
175
+ });
176
+
177
+ onScroll.cancel();
178
+ onScroll.flush();
179
+ ```
180
+
181
+ ### Controllers
182
+
183
+ Most time-based primitives return a controller object:
184
+
185
+ ```js
186
+ const ctrl = after('1s', () => console.log('done'));
187
+
188
+ ctrl.pause();
189
+ ctrl.resume();
190
+ ctrl.cancel();
191
+ ctrl.reset?.();
192
+
193
+ console.log(ctrl.isRunning);
194
+ console.log(ctrl.isPaused);
195
+ console.log(ctrl.isFinished);
196
+ ```
197
+
198
+ Shared by `after()`, `every()`, `afterRaf()`, `everyRaf()`. All controllers are pause-safe (paused time does not count), cancel-safe, monotonic-time based, and `AbortSignal`-aware where supported.
199
+
200
+ ### AbortSignal support
201
+
202
+ Many utilities accept `{ signal }` for automatic cancellation.
203
+
204
+ ```js
205
+ import { after } from 'timeout-flow';
206
+
207
+ const ac = new AbortController();
208
+
209
+ after('2s', function doWork() {
210
+ console.log('Will not run if aborted');
211
+ }, { signal: ac.signal });
212
+
213
+ ac.abort();
214
+ ```
215
+
216
+ If already aborted at creation time, no work is scheduled.
217
+
218
+ ### Public surface
219
+
220
+ ```js
221
+ import {
222
+ after,
223
+ every,
224
+ debounce,
225
+ throttle,
226
+ retry,
227
+ waitFor,
228
+ flow,
229
+
230
+ afterRaf,
231
+ everyRaf,
232
+ debounceRaf,
233
+ throttleRaf,
234
+ waitForRaf
235
+ } from 'timeout-flow';
236
+ ```
237
+
238
+ ---
239
+
240
+ ## Notes
241
+
242
+ ### Tree-shaking and per-primitive imports
243
+
244
+ Two import styles, same package, choose by bundle-size sensitivity:
245
+
246
+ ```js
247
+ // Full kit. Every primitive available on one import.
248
+ // Modern bundlers (esbuild, vite, rollup, webpack 5) will tree-shake
249
+ // unused exports because the package ships `sideEffects: false` and the
250
+ // `.` entrypoint points at ESM source instead of a pre-minified blob.
251
+ import { after, debounce, retry } from 'timeout-flow';
252
+ ```
253
+
254
+ ```js
255
+ // Per-primitive subpath. Explicit minimum surface, useful when you know
256
+ // you only need one primitive and want the bundle to reflect that
257
+ // without relying on the bundler's tree-shaker. Same source either way.
258
+ import { after } from 'timeout-flow/after';
259
+ import { every } from 'timeout-flow/every';
260
+ import { debounce } from 'timeout-flow/debounce';
261
+ import { throttle } from 'timeout-flow/throttle';
262
+ import { retry } from 'timeout-flow/retry';
263
+ import { waitFor } from 'timeout-flow/wait-for';
264
+ import { flow } from 'timeout-flow/flow';
265
+ import { afterRaf, everyRaf, debounceRaf, throttleRaf, waitForRaf } from 'timeout-flow/raf';
266
+ import { parseDuration } from 'timeout-flow/parse-duration';
267
+ ```
268
+
269
+ Measured for a single `after()` call in a typical browser bundler (esbuild, minified + gzip):
270
+
271
+ | Import style | Gzip |
272
+ |---|---:|
273
+ | pre-0.0.19 default (full minified bundle) | 5098 bytes |
274
+ | 0.0.19 default (`import { after } from 'timeout-flow'`) | 1199 bytes |
275
+ | 0.0.19 subpath (`import { after } from 'timeout-flow/after'`) | 1199 bytes |
276
+
277
+ For unbundled `<script type="module">` consumption of the full kit, use the **`timeout-flow/min`** subpath, which points at the pre-built minified IIFE.
278
+
279
+ ### Clean signatures
280
+
281
+ timeout-flow supports both natural-language and function-first argument ordering:
282
+
283
+ ```js
284
+ after('1s', done);
285
+ // or
286
+ after(done, '1s');
287
+ ```
288
+
289
+ This applies to `after()`, `every()`, `debounce()`, `throttle()`. Use whichever reads best in your codebase.
290
+
291
+ ### Philosophy
292
+
293
+ timeout-flow is not just a wrapper around timers, it's a composable toolkit for expressing time as readable logic. Temporal behavior should be:
294
+
295
+ - **Readable.** Durations like `"1s"` and `"500ms"` beat magic numbers.
296
+ - **Composable.** Sequencing should be declarative.
297
+ - **Controllable.** Timers should pause, resume, cancel.
298
+ - **Branchable.** Real flows need `if`, `while`, `label`, `jumpTo()`.
299
+ - **Tiny.** No runtime bloat.
300
+
301
+ > Think of timeout-flow as `setTimeout()` with superpowers.
302
+
303
+ ---
304
+
305
+ ## License
306
+
307
+ Licensed under AGPL-3.0 with WATT3D Additional Terms. See [LICENSE](./LICENSE) and [ADDITIONAL_TERMS.md](./ADDITIONAL_TERMS.md). Commercial AI/model-training use requires compliance with those terms or a separate WATT3D license. © WATT3D.