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/ADDITIONAL_TERMS.md +122 -0
- package/LICENSE +694 -0
- package/README.md +307 -332
- package/package.json +68 -59
- package/robots.txt +79 -3
- package/src/AfterTimer.js +81 -81
- package/src/EveryTimer.js +208 -208
- package/src/TimeoutFlow.js +400 -400
- package/src/TimerBase.js +132 -132
- package/src/abort.js +74 -74
- package/src/after.js +32 -32
- package/src/debounce.js +105 -105
- package/src/every.js +34 -34
- package/src/index.js +23 -23
- package/src/now.js +22 -22
- package/src/parseDuration.js +46 -46
- package/src/raf/ReadMe-Raf.md +94 -94
- package/src/raf/afterRaf.js +203 -203
- package/src/raf/debounceRaf.js +126 -126
- package/src/raf/everyRaf.js +316 -316
- package/src/raf/index.js +27 -27
- package/src/raf/throttleRaf.js +181 -181
- package/src/raf/waitForRaf.js +100 -100
- package/src/resolveDelayAndFn.js +89 -89
- package/src/retry.js +163 -163
- package/src/throttle.js +141 -141
- package/src/waitFor.js +76 -76
- package/LICENSE.MD +0 -285
- package/src/context-js.bat +0 -58
- package/src/context.txt +0 -2604
package/README.md
CHANGED
|
@@ -1,332 +1,307 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/timeout-flow)
|
|
4
|
-
[](https://www.npmjs.com/package/timeout-flow)
|
|
4
|
+
[](https://www.npmjs.com/package/timeout-flow)
|
|
5
|
+
[](https://bundlephobia.com/package/timeout-flow)
|
|
6
|
+
[](https://github.com/iWhatty/TimeoutFlow-js/blob/main/LICENSE)
|
|
7
|
+
[](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.
|