timeout-flow 0.0.2 → 0.0.4
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 +234 -0
- package/package.json +1 -1
- package/ReadMe.md +0 -152
package/README.md
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# TimeoutFlow
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/timeout-flow)
|
|
4
|
+
[](https://bundlephobia.com/package//timeout-flow)
|
|
5
|
+
[](https://www.npmjs.com/package/timeout-flow)
|
|
6
|
+
[](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 added powers like chaining, conditional logic, pause/resume, retries, and more.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Philosophy
|
|
15
|
+
|
|
16
|
+
**TimeoutFlow is not just a wrapper for `setTimeout`.**
|
|
17
|
+
It's a composable mini-framework for expressing time as fluent logic.
|
|
18
|
+
|
|
19
|
+
We believe temporal behavior in JavaScript should be:
|
|
20
|
+
|
|
21
|
+
* **Readable** – durations like "1s" and "500ms" are easier to reason about than magic numbers.
|
|
22
|
+
* **Composable** – sequencing events should be declarative, not a tangle of nested callbacks or timers.
|
|
23
|
+
* **Controllable** – any timer should be pauseable, resumable, and cancelable at any moment.
|
|
24
|
+
* **Branchable** – real flows require `if`, `while`, `label`, and `jumpTo()` — not just repetition.
|
|
25
|
+
* **Tiny** – no dependencies, no bloat, and no reactivity engine required.
|
|
26
|
+
|
|
27
|
+
TimeoutFlow gives you **atomic time primitives** (`after`, `every`, `debounce`, `retry`)
|
|
28
|
+
and a fluent builder (`flow()`) to script rich behavior over time — like a timeline you can control.
|
|
29
|
+
|
|
30
|
+
### In Other Words:
|
|
31
|
+
|
|
32
|
+
> Think of TimeoutFlow as **setTimeout() with superpowers.**
|
|
33
|
+
> But more importantly, think of it as a way to **write time** like you write logic.
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
flow()
|
|
37
|
+
.after('1s', () => console.log('Start'))
|
|
38
|
+
.every('500ms', (i) => console.log(`Tick ${i}`), 3)
|
|
39
|
+
.after('1s', () => console.log('Done'))
|
|
40
|
+
.start();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
This isn’t about wrapping timers.
|
|
44
|
+
It’s about **orchestrating intent** — clearly, fluently, and with full control.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm install timeout-flow
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Features
|
|
57
|
+
|
|
58
|
+
* `after("1s", fn)` — delay execution (via `AfterTimer`)
|
|
59
|
+
* `every("500ms", fn, count?)` — repeat execution with optional limit (via `EveryTimer`)
|
|
60
|
+
* `flow()` — create fluent, chainable timelines with:
|
|
61
|
+
|
|
62
|
+
* `.after()`, `.every()`, `.loop(n)`
|
|
63
|
+
* `.if()`, `.unless()`, `.label()`, `.jumpTo()`
|
|
64
|
+
* `.while()`, `.doWhile()`
|
|
65
|
+
* Utilities: `debounce()`, `throttle()`, `retry()`, `waitFor()`
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Usage Examples
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
// 1. Delayed Execution
|
|
73
|
+
import { after } from 'timeout-flow';
|
|
74
|
+
after('2s', () => console.log('Waited 2 seconds...'));
|
|
75
|
+
|
|
76
|
+
// 2. Repeating with Pause & Resume
|
|
77
|
+
import { every, after as wait } from 'timeout-flow';
|
|
78
|
+
const ticker = every('1s', i => console.log(`Tick ${i}`), 5);
|
|
79
|
+
wait('2.5s', () => ticker.pause());
|
|
80
|
+
wait('4s', () => ticker.resume());
|
|
81
|
+
|
|
82
|
+
// 3. Debounced Input
|
|
83
|
+
import { debounce } from 'timeout-flow';
|
|
84
|
+
const search = debounce('300ms', (e) => {
|
|
85
|
+
console.log('Searching for:', e.target.value);
|
|
86
|
+
});
|
|
87
|
+
document.querySelector('input').addEventListener('input', search);
|
|
88
|
+
|
|
89
|
+
// 4. Retry a Failing Request
|
|
90
|
+
import { retry } from 'timeout-flow';
|
|
91
|
+
await retry(() => fetch('/api/data'), {
|
|
92
|
+
attempts: 4,
|
|
93
|
+
delay: '1s',
|
|
94
|
+
backoff: true
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// 5. Wait for DOM Change
|
|
98
|
+
import { waitFor } from 'timeout-flow';
|
|
99
|
+
await waitFor(() => document.querySelector('#loaded'), {
|
|
100
|
+
interval: '250ms',
|
|
101
|
+
timeout: '5s'
|
|
102
|
+
});
|
|
103
|
+
console.log('Element loaded!');
|
|
104
|
+
|
|
105
|
+
// 6. Fluent Timeline
|
|
106
|
+
import { flow } from 'timeout-flow';
|
|
107
|
+
flow()
|
|
108
|
+
.after('1s', () => console.log('Step 1'))
|
|
109
|
+
.every('500ms', (i) => console.log(`Tick ${i}`), 3)
|
|
110
|
+
.after('1s', () => console.log('Final Step'))
|
|
111
|
+
.start();
|
|
112
|
+
|
|
113
|
+
// 7. Conditional & Labeled Logic
|
|
114
|
+
let debug = true;
|
|
115
|
+
flow()
|
|
116
|
+
.after('1s', () => console.log('Boot sequence'))
|
|
117
|
+
.if(() => debug)
|
|
118
|
+
.after('500ms', () => console.log('Debug logs enabled'))
|
|
119
|
+
.label('loop')
|
|
120
|
+
.every('1s', i => console.log(`Frame ${i}`), 3)
|
|
121
|
+
.after('500ms', () => console.log('Restarting...'))
|
|
122
|
+
.jumpTo('loop')
|
|
123
|
+
.start();
|
|
124
|
+
|
|
125
|
+
// 8. Controlled Loop
|
|
126
|
+
let energy = 3;
|
|
127
|
+
flow()
|
|
128
|
+
.doWhile(() => energy-- > 0)
|
|
129
|
+
.every('400ms', () => console.log(`Blast (${energy})`))
|
|
130
|
+
.after('1s', () => console.log('Energy depleted'))
|
|
131
|
+
.start();
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Utilities
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
import { debounce, throttle, retry, waitFor } from 'timeout-flow';
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
* `debounce('300ms', fn)` — Run only after silence
|
|
143
|
+
* `throttle('1s', fn)` — Run at most once per time window
|
|
144
|
+
* `retry(fn, { attempts, delay, backoff })` — Resilient retry for async calls
|
|
145
|
+
* `waitFor(() => condition, { timeout, interval })` — Await condition change
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## Frame-Based Timing (RAF Utilities)
|
|
150
|
+
|
|
151
|
+
These helpers use `requestAnimationFrame` under the hood to provide smooth, energy-efficient timing. Ideal for visual UI flows, canvas apps, scroll/resize behavior, and performance-sensitive interactions.
|
|
152
|
+
|
|
153
|
+
All `raf` utilities automatically **pause in background tabs**, unlike timers.
|
|
154
|
+
|
|
155
|
+
### API Summary
|
|
156
|
+
|
|
157
|
+
| Function | Purpose | Best For |
|
|
158
|
+
| -------------------------- | ------------------------------------------------------------------- | -------------------------------------------------- |
|
|
159
|
+
| `afterRaf()` | Runs a function **once after N ms**, using `requestAnimationFrame`. | Idle effects, UI post-load, paint batching |
|
|
160
|
+
| `everyRaf()` | Repeats a function **every N ms**, throttled via frames. | Sanity loops, smooth polling, visual checks |
|
|
161
|
+
| `debounceRaf()` | Debounces a function using **frames instead of timeouts**. | Drag/move handlers, visual updates |
|
|
162
|
+
| `debounceRaf('300ms', fn)` | Debounces like traditional debounce, but frame-aware. | Resize events, paused background flows |
|
|
163
|
+
| `throttleRaf()` | Throttles execution to **at most once per frame**. | Scroll events, pointermove, paint-heavy flows |
|
|
164
|
+
| `throttleRaf(fn, 2)` | Throttles to once every 3 frames (`frameSkip = 2`). | Advanced visuals, slower sync without timers |
|
|
165
|
+
| `waitForRaf()` | Waits for a condition to become true using a frame-based loop. | DOM readiness, layout stability, visibility checks |
|
|
166
|
+
|
|
167
|
+
### Key Advantages
|
|
168
|
+
|
|
169
|
+
* **Frame-sync**: Triggered in sync with visual updates (60Hz or higher)
|
|
170
|
+
* **Background-tab safe**: No CPU use when inactive
|
|
171
|
+
* **Energy efficient**: Great for battery-conscious apps
|
|
172
|
+
* **Smoother UX**: Especially under load or heavy visuals
|
|
173
|
+
|
|
174
|
+
### 🛠 Example Usage
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
import { debounceRaf } from 'timeout-flow';
|
|
178
|
+
|
|
179
|
+
const onMouseMove = debounceRaf(() => {
|
|
180
|
+
drawPreview();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const onResize = debounceRaf('250ms', () => {
|
|
184
|
+
updateLayout();
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
import { afterRaf } from 'timeout-flow';
|
|
190
|
+
|
|
191
|
+
afterRaf('2s', () => {
|
|
192
|
+
showIntroAnimation();
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
```js
|
|
197
|
+
import { everyRaf } from 'timeout-flow';
|
|
198
|
+
|
|
199
|
+
const loop = everyRaf('1s', () => {
|
|
200
|
+
console.log('heartbeat');
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
```js
|
|
205
|
+
import { throttleRaf } from 'timeout-flow';
|
|
206
|
+
|
|
207
|
+
const onScroll = throttleRaf((e) => {
|
|
208
|
+
handleScroll(e);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const onDrag = throttleRaf(drawFrame, 2);
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
import { waitForRaf } from 'timeout-flow';
|
|
216
|
+
|
|
217
|
+
await waitForRaf(() => document.querySelector('#panel')?.offsetHeight > 0);
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### File Locations
|
|
221
|
+
|
|
222
|
+
| File | Description |
|
|
223
|
+
| -------------------- | ------------------------------------------------------ |
|
|
224
|
+
| `raf/afterRaf.js` | One-time timer with frame pause support |
|
|
225
|
+
| `raf/everyRaf.js` | Interval timer using `requestAnimationFrame` |
|
|
226
|
+
| `raf/debounceRaf.js` | Smart debounce with optional duration and frame pause |
|
|
227
|
+
| `raf/throttleRaf.js` | Input-event throttle using frame-skip control |
|
|
228
|
+
| `raf/waitForRaf.js` | Waits for truthy condition using passive frame polling |
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## License
|
|
233
|
+
|
|
234
|
+
DR.WATT 2.0
|
package/package.json
CHANGED
package/ReadMe.md
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
# TimeoutFlow
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/timeout-flow)
|
|
4
|
-
[](https://bundlephobia.com/package//timeout-flow)
|
|
5
|
-
[](https://www.npmjs.com/package/timeout-flow)
|
|
6
|
-
[](https://github.com/iWhatty/TimeoutFlow-js)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
**Fluent, human-readable time control for JavaScript.**
|
|
10
|
-
|
|
11
|
-
TimeoutFlow makes working with time-based logic intuitive — think of it as a modern, composable upgrade to `setTimeout` and `setInterval`, with added powers like chaining, conditional logic, pause/resume, retries, and more.
|
|
12
|
-
|
|
13
|
-
---
|
|
14
|
-
|
|
15
|
-
## Philosophy
|
|
16
|
-
|
|
17
|
-
**TimeoutFlow is not just a wrapper for `setTimeout`.**
|
|
18
|
-
It's a composable mini-framework for expressing time as fluent logic.
|
|
19
|
-
|
|
20
|
-
We believe temporal behavior in JavaScript should be:
|
|
21
|
-
|
|
22
|
-
- **Readable** – durations like "1s" and "500ms" are easier to reason about than magic numbers.
|
|
23
|
-
- **Composable** – sequencing events should be declarative, not a tangle of nested callbacks or timers.
|
|
24
|
-
- **Controllable** – any timer should be pauseable, resumable, and cancelable at any moment.
|
|
25
|
-
- **Branchable** – real flows require `if`, `while`, `label`, and `jumpTo()` — not just repetition.
|
|
26
|
-
- **Tiny** – no dependencies, no bloat, and no reactivity engine required.
|
|
27
|
-
|
|
28
|
-
TimeoutFlow gives you **atomic time primitives** (`after`, `every`, `debounce`, `retry`)
|
|
29
|
-
and a fluent builder (`flow()`) to script rich behavior over time — like a timeline you can control.
|
|
30
|
-
|
|
31
|
-
### In Other Words:
|
|
32
|
-
|
|
33
|
-
> Think of TimeoutFlow as **setTimeout() with superpowers.**
|
|
34
|
-
> But more importantly, think of it as a way to **write time** like you write logic.
|
|
35
|
-
|
|
36
|
-
```js
|
|
37
|
-
flow()
|
|
38
|
-
.after('1s', () => console.log('Start'))
|
|
39
|
-
.every('500ms', (i) => console.log(`Tick ${i}`), 3)
|
|
40
|
-
.after('1s', () => console.log('Done'))
|
|
41
|
-
.start();
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
This isn’t about wrapping timers.
|
|
45
|
-
It’s about **orchestrating intent** — clearly, fluently, and with full control.
|
|
46
|
-
|
|
47
|
-
---
|
|
48
|
-
|
|
49
|
-
## Installation
|
|
50
|
-
|
|
51
|
-
```bash
|
|
52
|
-
npm install timeout-flow
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
---
|
|
56
|
-
|
|
57
|
-
## Features
|
|
58
|
-
|
|
59
|
-
- `after("1s", fn)` — delay execution (via `AfterTimer`)
|
|
60
|
-
- `every("500ms", fn, count?)` — repeat execution with optional limit (via `EveryTimer`)
|
|
61
|
-
- `flow()` — create fluent, chainable timelines with:
|
|
62
|
-
- `.after()`, `.every()`, `.loop(n)`
|
|
63
|
-
- `.if()`, `.unless()`, `.label()`, `.jumpTo()`
|
|
64
|
-
- `.while()`, `.doWhile()`
|
|
65
|
-
- Utilities: `debounce()`, `throttle()`, `retry()`, `waitFor()`
|
|
66
|
-
|
|
67
|
-
---
|
|
68
|
-
|
|
69
|
-
## Usage Examples
|
|
70
|
-
|
|
71
|
-
```js
|
|
72
|
-
// 1. Delayed Execution
|
|
73
|
-
import { after } from 'timeout-flow';
|
|
74
|
-
after('2s', () => console.log('Waited 2 seconds...'));
|
|
75
|
-
|
|
76
|
-
// 2. Repeating with Pause & Resume
|
|
77
|
-
import { every, after as wait } from 'timeout-flow';
|
|
78
|
-
const ticker = every('1s', i => console.log(`Tick ${i}`), 5);
|
|
79
|
-
wait('2.5s', () => ticker.pause());
|
|
80
|
-
wait('4s', () => ticker.resume());
|
|
81
|
-
|
|
82
|
-
// 3. Debounced Input
|
|
83
|
-
import { debounce } from 'timeout-flow';
|
|
84
|
-
const search = debounce('300ms', (e) => {
|
|
85
|
-
console.log('Searching for:', e.target.value);
|
|
86
|
-
});
|
|
87
|
-
document.querySelector('input').addEventListener('input', search);
|
|
88
|
-
|
|
89
|
-
// 4. Retry a Failing Request
|
|
90
|
-
import { retry } from 'timeout-flow';
|
|
91
|
-
await retry(() => fetch('/api/data'), {
|
|
92
|
-
attempts: 4,
|
|
93
|
-
delay: '1s',
|
|
94
|
-
backoff: true
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// 5. Wait for DOM Change
|
|
98
|
-
import { waitFor } from 'timeout-flow';
|
|
99
|
-
await waitFor(() => document.querySelector('#loaded'), {
|
|
100
|
-
interval: '250ms',
|
|
101
|
-
timeout: '5s'
|
|
102
|
-
});
|
|
103
|
-
console.log('Element loaded!');
|
|
104
|
-
|
|
105
|
-
// 6. Fluent Timeline
|
|
106
|
-
import { flow } from 'timeout-flow';
|
|
107
|
-
flow()
|
|
108
|
-
.after('1s', () => console.log('Step 1'))
|
|
109
|
-
.every('500ms', (i) => console.log(`Tick ${i}`), 3)
|
|
110
|
-
.after('1s', () => console.log('Final Step'))
|
|
111
|
-
.start();
|
|
112
|
-
|
|
113
|
-
// 7. Conditional & Labeled Logic
|
|
114
|
-
let debug = true;
|
|
115
|
-
flow()
|
|
116
|
-
.after('1s', () => console.log('Boot sequence'))
|
|
117
|
-
.if(() => debug)
|
|
118
|
-
.after('500ms', () => console.log('Debug logs enabled'))
|
|
119
|
-
.label('loop')
|
|
120
|
-
.every('1s', i => console.log(`Frame ${i}`), 3)
|
|
121
|
-
.after('500ms', () => console.log('Restarting...'))
|
|
122
|
-
.jumpTo('loop')
|
|
123
|
-
.start();
|
|
124
|
-
|
|
125
|
-
// 8. Controlled Loop
|
|
126
|
-
let energy = 3;
|
|
127
|
-
flow()
|
|
128
|
-
.doWhile(() => energy-- > 0)
|
|
129
|
-
.every('400ms', () => console.log(`Blast (${energy})`))
|
|
130
|
-
.after('1s', () => console.log('Energy depleted'))
|
|
131
|
-
.start();
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
---
|
|
135
|
-
|
|
136
|
-
## Utilities
|
|
137
|
-
|
|
138
|
-
```js
|
|
139
|
-
import { debounce, throttle, retry, waitFor } from 'timeout-flow';
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
- `debounce('300ms', fn)` — Run only after silence
|
|
143
|
-
- `throttle('1s', fn)` — Run at most once per time window
|
|
144
|
-
- `retry(fn, { attempts, delay, backoff })` — Resilient retry for async calls
|
|
145
|
-
- `waitFor(() => condition, { timeout, interval })` — Await condition change
|
|
146
|
-
|
|
147
|
-
---
|
|
148
|
-
|
|
149
|
-
## License
|
|
150
|
-
|
|
151
|
-
DR.WATT 2.0
|
|
152
|
-
|