jrx 0.3.0-alpha.2 → 0.3.0-alpha.3
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 +19 -21
- package/index.d.ts +12 -4
- package/index.js +20 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -121,30 +121,30 @@ handle[Symbol.dispose]()
|
|
|
121
121
|
|
|
122
122
|
### `makeIntervalAsync(cb, ms)`
|
|
123
123
|
|
|
124
|
-
Async version of `makeInterval`. Waits for the callback to complete before scheduling the next invocation.
|
|
124
|
+
Async version of `makeInterval`. Waits for the callback to complete before scheduling the next invocation. Returns a `Disposable`.
|
|
125
125
|
|
|
126
126
|
**Note:** The callback fires **immediately** on first call, then waits `ms` milliseconds **after** the previous async callback completes.
|
|
127
127
|
|
|
128
128
|
```typescript
|
|
129
129
|
import {makeIntervalAsync} from 'jrx'
|
|
130
130
|
|
|
131
|
-
const
|
|
131
|
+
const handle = makeIntervalAsync(async () => {
|
|
132
132
|
// Called immediately, then 5000ms after each completion
|
|
133
133
|
await fetchData()
|
|
134
134
|
processData()
|
|
135
135
|
}, 5000)
|
|
136
136
|
|
|
137
|
-
dispose()
|
|
137
|
+
handle[Symbol.dispose]()
|
|
138
138
|
```
|
|
139
139
|
|
|
140
140
|
### `makeAnimationFrame(cb)`
|
|
141
141
|
|
|
142
|
-
Executes a callback on the next animation frame with cleanup.
|
|
142
|
+
Executes a callback on the next animation frame with cleanup. Returns a `Disposable`.
|
|
143
143
|
|
|
144
144
|
```typescript
|
|
145
145
|
import {makeAnimationFrame} from 'jrx'
|
|
146
146
|
|
|
147
|
-
const
|
|
147
|
+
const handle = makeAnimationFrame((now) => {
|
|
148
148
|
updateAnimation(now)
|
|
149
149
|
|
|
150
150
|
// Optional: return a Disposable for cleanup
|
|
@@ -156,17 +156,17 @@ const dispose = makeAnimationFrame((now) => {
|
|
|
156
156
|
})
|
|
157
157
|
|
|
158
158
|
// Cancel if needed before the frame fires
|
|
159
|
-
dispose()
|
|
159
|
+
handle[Symbol.dispose]()
|
|
160
160
|
```
|
|
161
161
|
|
|
162
162
|
### `makeAnimationFrameLoop(cb)`
|
|
163
163
|
|
|
164
|
-
Creates a continuous `requestAnimationFrame` loop with cleanup.
|
|
164
|
+
Creates a continuous `requestAnimationFrame` loop with cleanup. Returns a `Disposable`.
|
|
165
165
|
|
|
166
166
|
```typescript
|
|
167
167
|
import {makeAnimationFrameLoop} from 'jrx'
|
|
168
168
|
|
|
169
|
-
const
|
|
169
|
+
const handle = makeAnimationFrameLoop((now) => {
|
|
170
170
|
updateAnimation(now)
|
|
171
171
|
|
|
172
172
|
// Optional: return a Disposable for cleanup
|
|
@@ -178,22 +178,22 @@ const dispose = makeAnimationFrameLoop((now) => {
|
|
|
178
178
|
})
|
|
179
179
|
|
|
180
180
|
// Stop the loop
|
|
181
|
-
dispose()
|
|
181
|
+
handle[Symbol.dispose]()
|
|
182
182
|
```
|
|
183
183
|
|
|
184
184
|
### `makeTimeout(cb, ms)`
|
|
185
185
|
|
|
186
|
-
Creates a timeout with cleanup.
|
|
186
|
+
Creates a timeout with cleanup. Returns a `Disposable`.
|
|
187
187
|
|
|
188
188
|
```typescript
|
|
189
189
|
import {makeTimeout} from 'jrx'
|
|
190
190
|
|
|
191
|
-
const
|
|
191
|
+
const handle = makeTimeout(() => {
|
|
192
192
|
console.log('Timeout fired')
|
|
193
193
|
}, 1000)
|
|
194
194
|
|
|
195
195
|
// Cancel if needed
|
|
196
|
-
|
|
196
|
+
handle[Symbol.dispose]()
|
|
197
197
|
```
|
|
198
198
|
|
|
199
199
|
### `makeTransition(cb, durationMs)`
|
|
@@ -289,24 +289,22 @@ const data = await r // undefined
|
|
|
289
289
|
|
|
290
290
|
## Cleanup Pattern
|
|
291
291
|
|
|
292
|
-
|
|
292
|
+
All effect functions return a `Disposable` object that stops the effect and runs any pending cleanup:
|
|
293
293
|
|
|
294
294
|
```typescript
|
|
295
295
|
import {makeInterval, makeTimeout, makeAnimationFrame, makeTransition} from 'jrx'
|
|
296
296
|
|
|
297
|
-
//
|
|
297
|
+
// Each function returns a Disposable
|
|
298
298
|
const interval = makeInterval(() => console.log('tick'), 1000)
|
|
299
|
+
const timeout = makeTimeout(() => console.log('timeout'), 5000)
|
|
300
|
+
const raf = makeAnimationFrame((now) => render(now))
|
|
299
301
|
const transition = makeTransition((p) => console.log(p), 1000)
|
|
300
302
|
|
|
303
|
+
// Call [Symbol.dispose]() to stop the effect
|
|
301
304
|
interval[Symbol.dispose]()
|
|
305
|
+
timeout[Symbol.dispose]()
|
|
306
|
+
raf[Symbol.dispose]()
|
|
302
307
|
transition[Symbol.dispose]()
|
|
303
|
-
|
|
304
|
-
// Functions returning dispose functions (call directly)
|
|
305
|
-
const disposeTimeout = makeTimeout(() => console.log('timeout'), 5000)
|
|
306
|
-
const disposeRaf = makeAnimationFrame((now) => render(now))
|
|
307
|
-
|
|
308
|
-
disposeTimeout()
|
|
309
|
-
disposeRaf()
|
|
310
308
|
```
|
|
311
309
|
|
|
312
310
|
## TypeScript
|
package/index.d.ts
CHANGED
|
@@ -12,10 +12,18 @@ export declare function makeRenderLoop(): {
|
|
|
12
12
|
export declare function makeInterval(cb: () => undefined | Disposable, ms: number): {
|
|
13
13
|
[Symbol.dispose](): void;
|
|
14
14
|
};
|
|
15
|
-
export declare function makeIntervalAsync(cb: () => void | Disposable | Promise<void>, ms: number):
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export declare function
|
|
15
|
+
export declare function makeIntervalAsync(cb: () => void | Disposable | Promise<void>, ms: number): {
|
|
16
|
+
[Symbol.dispose](): void;
|
|
17
|
+
};
|
|
18
|
+
export declare function makeAnimationFrame(cb: (now: DOMHighResTimeStamp) => undefined | Disposable): {
|
|
19
|
+
[Symbol.dispose](): void;
|
|
20
|
+
};
|
|
21
|
+
export declare function makeAnimationFrameLoop(cb: (now: DOMHighResTimeStamp) => undefined | Disposable): {
|
|
22
|
+
[Symbol.dispose](): void;
|
|
23
|
+
};
|
|
24
|
+
export declare function makeTimeout(cb: () => void, ms: number): {
|
|
25
|
+
[Symbol.dispose](): void;
|
|
26
|
+
};
|
|
19
27
|
export declare function makeTransition(cb: (progress: number) => undefined | Disposable, durationMs: number): {
|
|
20
28
|
[Symbol.dispose](): void;
|
|
21
29
|
};
|
package/index.js
CHANGED
|
@@ -52,9 +52,11 @@ export function makeIntervalAsync(cb, ms) {
|
|
|
52
52
|
const reset = makeReset();
|
|
53
53
|
let timeout;
|
|
54
54
|
void wrapper();
|
|
55
|
-
return
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
return {
|
|
56
|
+
[Symbol.dispose]() {
|
|
57
|
+
reset();
|
|
58
|
+
clearTimeout(timeout);
|
|
59
|
+
},
|
|
58
60
|
};
|
|
59
61
|
async function wrapper() {
|
|
60
62
|
const stack = reset();
|
|
@@ -70,17 +72,21 @@ export function makeAnimationFrame(cb) {
|
|
|
70
72
|
return;
|
|
71
73
|
stack.use(cb(now));
|
|
72
74
|
});
|
|
73
|
-
return
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
return {
|
|
76
|
+
[Symbol.dispose]() {
|
|
77
|
+
stack.dispose();
|
|
78
|
+
cancelAnimationFrame(raf);
|
|
79
|
+
},
|
|
76
80
|
};
|
|
77
81
|
}
|
|
78
82
|
export function makeAnimationFrameLoop(cb) {
|
|
79
83
|
const reset = makeReset();
|
|
80
84
|
let raf = requestAnimationFrame(wrapper);
|
|
81
|
-
return
|
|
82
|
-
|
|
83
|
-
|
|
85
|
+
return {
|
|
86
|
+
[Symbol.dispose]() {
|
|
87
|
+
reset();
|
|
88
|
+
cancelAnimationFrame(raf);
|
|
89
|
+
},
|
|
84
90
|
};
|
|
85
91
|
function wrapper(now) {
|
|
86
92
|
reset().use(cb(now));
|
|
@@ -89,7 +95,11 @@ export function makeAnimationFrameLoop(cb) {
|
|
|
89
95
|
}
|
|
90
96
|
export function makeTimeout(cb, ms) {
|
|
91
97
|
const timeout = setTimeout(cb, ms);
|
|
92
|
-
return
|
|
98
|
+
return {
|
|
99
|
+
[Symbol.dispose]() {
|
|
100
|
+
clearTimeout(timeout);
|
|
101
|
+
},
|
|
102
|
+
};
|
|
93
103
|
}
|
|
94
104
|
export function makeTransition(cb, durationMs) {
|
|
95
105
|
const reset = makeReset();
|