jrx 0.3.0-alpha.4 → 0.3.0-alpha.6
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 +48 -30
- package/index.d.ts +6 -6
- package/index.js +6 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -28,15 +28,33 @@ npm i jrx
|
|
|
28
28
|
- [`makeReset()`](#makereset) - Create a resettable `DisposableStack`
|
|
29
29
|
- [`makeAsyncReset()`](#makeasyncreset) - Create a resettable `AsyncDisposableStack`
|
|
30
30
|
- [`makeRenderLoop()`](#makerenderloop) - Render loops with automatic cleanup
|
|
31
|
-
- [`
|
|
32
|
-
- [`
|
|
33
|
-
- [`
|
|
34
|
-
- [`
|
|
35
|
-
- [`
|
|
36
|
-
- [`
|
|
31
|
+
- [`createInterval(cb, ms)`](#createIntervalcb-ms) - Repeating intervals with cleanup
|
|
32
|
+
- [`createIntervalAsync(cb, ms)`](#createIntervalasynccb-ms) - Async intervals with cancellation
|
|
33
|
+
- [`createAnimationFrame(cb)`](#createAnimationFramecb) - Single animation frame with cleanup
|
|
34
|
+
- [`createAnimationFrameLoop(cb)`](#createAnimationFrameloopcb) - Animation frame loops
|
|
35
|
+
- [`createTimeout(cb, ms)`](#createTimeoutcb-ms) - Timeouts with cleanup
|
|
36
|
+
- [`createTransition(cb, durationMs)`](#createTransitioncb-durationms) - Progress-based animations
|
|
37
37
|
- [`computed(fn, getDeps?)`](#computedfn-getdeps) - Memoized computed values
|
|
38
38
|
- [`retry(cb, backoffSec?)`](#retrycb-backoffsec) - Retry with exponential backoff
|
|
39
39
|
|
|
40
|
+
## Naming convention
|
|
41
|
+
|
|
42
|
+
Functions prefixed with `create*` return a `Disposable` object. The returned object can be passed to [`DisposableStack.use()`](https://github.com/tc39/proposal-explicit-resource-management) or bound with the `using` keyword for automatic cleanup.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import {createInterval, createTimeout} from 'jrx'
|
|
46
|
+
|
|
47
|
+
// With DisposableStack.use()
|
|
48
|
+
using stack = new DisposableStack()
|
|
49
|
+
stack.use(createInterval(() => console.log('tick'), 1000))
|
|
50
|
+
stack.use(createTimeout(() => console.log('done'), 5000))
|
|
51
|
+
// Both are disposed when `stack` goes out of scope
|
|
52
|
+
|
|
53
|
+
// Or directly with `using`
|
|
54
|
+
using interval = createInterval(() => console.log('tick'), 1000)
|
|
55
|
+
// Disposed when the enclosing block exits
|
|
56
|
+
```
|
|
57
|
+
|
|
40
58
|
## API
|
|
41
59
|
|
|
42
60
|
### `makeReset()`
|
|
@@ -95,16 +113,16 @@ requestAnimationFrame(loop)
|
|
|
95
113
|
handle[Symbol.dispose]()
|
|
96
114
|
```
|
|
97
115
|
|
|
98
|
-
### `
|
|
116
|
+
### `createInterval(cb, ms)`
|
|
99
117
|
|
|
100
118
|
Creates a repeating interval with cleanup. The callback can optionally return a `Disposable` that is disposed before the next invocation. Returns a `Disposable`.
|
|
101
119
|
|
|
102
120
|
**Note:** The callback fires **immediately** on first call, then waits `ms` milliseconds **after** the previous callback completes. This is not a fixed-rate timer.
|
|
103
121
|
|
|
104
122
|
```typescript
|
|
105
|
-
import {
|
|
123
|
+
import {createInterval} from 'jrx'
|
|
106
124
|
|
|
107
|
-
const handle =
|
|
125
|
+
const handle = createInterval(() => {
|
|
108
126
|
console.log('Tick') // Called immediately, then every 1000ms after completion
|
|
109
127
|
|
|
110
128
|
// Optional: return a Disposable for cleanup
|
|
@@ -119,16 +137,16 @@ const handle = makeInterval(() => {
|
|
|
119
137
|
handle[Symbol.dispose]()
|
|
120
138
|
```
|
|
121
139
|
|
|
122
|
-
### `
|
|
140
|
+
### `createIntervalAsync(cb, ms)`
|
|
123
141
|
|
|
124
|
-
Async version of `
|
|
142
|
+
Async version of `createInterval`. Waits for the callback to complete before scheduling the next invocation. Returns a `Disposable`.
|
|
125
143
|
|
|
126
144
|
**Note:** The callback fires **immediately** on first call, then waits `ms` milliseconds **after** the previous async callback completes.
|
|
127
145
|
|
|
128
146
|
```typescript
|
|
129
|
-
import {
|
|
147
|
+
import {createIntervalAsync} from 'jrx'
|
|
130
148
|
|
|
131
|
-
const handle =
|
|
149
|
+
const handle = createIntervalAsync(async () => {
|
|
132
150
|
// Called immediately, then 5000ms after each completion
|
|
133
151
|
await fetchData()
|
|
134
152
|
processData()
|
|
@@ -137,14 +155,14 @@ const handle = makeIntervalAsync(async () => {
|
|
|
137
155
|
handle[Symbol.dispose]()
|
|
138
156
|
```
|
|
139
157
|
|
|
140
|
-
### `
|
|
158
|
+
### `createAnimationFrame(cb)`
|
|
141
159
|
|
|
142
160
|
Executes a callback on the next animation frame with cleanup. Returns a `Disposable`.
|
|
143
161
|
|
|
144
162
|
```typescript
|
|
145
|
-
import {
|
|
163
|
+
import {createAnimationFrame} from 'jrx'
|
|
146
164
|
|
|
147
|
-
const handle =
|
|
165
|
+
const handle = createAnimationFrame((now) => {
|
|
148
166
|
updateAnimation(now)
|
|
149
167
|
|
|
150
168
|
// Optional: return a Disposable for cleanup
|
|
@@ -159,14 +177,14 @@ const handle = makeAnimationFrame((now) => {
|
|
|
159
177
|
handle[Symbol.dispose]()
|
|
160
178
|
```
|
|
161
179
|
|
|
162
|
-
### `
|
|
180
|
+
### `createAnimationFrameLoop(cb)`
|
|
163
181
|
|
|
164
182
|
Creates a continuous `requestAnimationFrame` loop with cleanup. Returns a `Disposable`.
|
|
165
183
|
|
|
166
184
|
```typescript
|
|
167
|
-
import {
|
|
185
|
+
import {createAnimationFrameLoop} from 'jrx'
|
|
168
186
|
|
|
169
|
-
const handle =
|
|
187
|
+
const handle = createAnimationFrameLoop((now) => {
|
|
170
188
|
updateAnimation(now)
|
|
171
189
|
|
|
172
190
|
// Optional: return a Disposable for cleanup
|
|
@@ -181,14 +199,14 @@ const handle = makeAnimationFrameLoop((now) => {
|
|
|
181
199
|
handle[Symbol.dispose]()
|
|
182
200
|
```
|
|
183
201
|
|
|
184
|
-
### `
|
|
202
|
+
### `createTimeout(cb, ms)`
|
|
185
203
|
|
|
186
204
|
Creates a timeout with cleanup. Returns a `Disposable`.
|
|
187
205
|
|
|
188
206
|
```typescript
|
|
189
|
-
import {
|
|
207
|
+
import {createTimeout} from 'jrx'
|
|
190
208
|
|
|
191
|
-
const handle =
|
|
209
|
+
const handle = createTimeout(() => {
|
|
192
210
|
console.log('Timeout fired')
|
|
193
211
|
}, 1000)
|
|
194
212
|
|
|
@@ -196,14 +214,14 @@ const handle = makeTimeout(() => {
|
|
|
196
214
|
handle[Symbol.dispose]()
|
|
197
215
|
```
|
|
198
216
|
|
|
199
|
-
### `
|
|
217
|
+
### `createTransition(cb, durationMs)`
|
|
200
218
|
|
|
201
219
|
Creates an animation transition with progress tracking (0 to 1). Returns a `Disposable`.
|
|
202
220
|
|
|
203
221
|
```typescript
|
|
204
|
-
import {
|
|
222
|
+
import {createTransition} from 'jrx'
|
|
205
223
|
|
|
206
|
-
const handle =
|
|
224
|
+
const handle = createTransition((progress) => {
|
|
207
225
|
element.style.opacity = progress.toString()
|
|
208
226
|
|
|
209
227
|
// Optional: return a Disposable for cleanup
|
|
@@ -292,13 +310,13 @@ const data = await r // undefined
|
|
|
292
310
|
All effect functions return a `Disposable` object that stops the effect and runs any pending cleanup:
|
|
293
311
|
|
|
294
312
|
```typescript
|
|
295
|
-
import {
|
|
313
|
+
import {createInterval, createTimeout, createAnimationFrame, createTransition} from 'jrx'
|
|
296
314
|
|
|
297
315
|
// Each function returns a Disposable
|
|
298
|
-
const interval =
|
|
299
|
-
const timeout =
|
|
300
|
-
const raf =
|
|
301
|
-
const transition =
|
|
316
|
+
const interval = createInterval(() => console.log('tick'), 1000)
|
|
317
|
+
const timeout = createTimeout(() => console.log('timeout'), 5000)
|
|
318
|
+
const raf = createAnimationFrame((now) => render(now))
|
|
319
|
+
const transition = createTransition((p) => console.log(p), 1000)
|
|
302
320
|
|
|
303
321
|
// Call [Symbol.dispose]() to stop the effect
|
|
304
322
|
interval[Symbol.dispose]()
|
package/index.d.ts
CHANGED
|
@@ -9,21 +9,21 @@ export declare function makeRenderLoop(): {
|
|
|
9
9
|
[Symbol.dispose](): void;
|
|
10
10
|
};
|
|
11
11
|
};
|
|
12
|
-
export declare function
|
|
12
|
+
export declare function createInterval(cb: () => undefined | Disposable, ms: number): {
|
|
13
13
|
[Symbol.dispose](): void;
|
|
14
14
|
};
|
|
15
|
-
export declare function
|
|
15
|
+
export declare function createIntervalAsync(cb: () => void | Disposable | Promise<void>, ms: number): {
|
|
16
16
|
[Symbol.dispose](): void;
|
|
17
17
|
};
|
|
18
|
-
export declare function
|
|
18
|
+
export declare function createAnimationFrame(cb: (now: DOMHighResTimeStamp) => undefined | Disposable): {
|
|
19
19
|
[Symbol.dispose](): void;
|
|
20
20
|
};
|
|
21
|
-
export declare function
|
|
21
|
+
export declare function createAnimationFrameLoop(cb: (now: DOMHighResTimeStamp) => undefined | Disposable): {
|
|
22
22
|
[Symbol.dispose](): void;
|
|
23
23
|
};
|
|
24
|
-
export declare function
|
|
24
|
+
export declare function createTimeout(cb: () => void, ms: number): {
|
|
25
25
|
[Symbol.dispose](): void;
|
|
26
26
|
};
|
|
27
|
-
export declare function
|
|
27
|
+
export declare function createTransition(cb: (progress: number) => undefined | Disposable, durationMs: number): {
|
|
28
28
|
[Symbol.dispose](): void;
|
|
29
29
|
};
|
package/index.js
CHANGED
|
@@ -33,7 +33,7 @@ export function makeRenderLoop() {
|
|
|
33
33
|
},
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
-
export function
|
|
36
|
+
export function createInterval(cb, ms) {
|
|
37
37
|
const reset = makeReset();
|
|
38
38
|
let timeout;
|
|
39
39
|
wrapper();
|
|
@@ -48,7 +48,7 @@ export function makeInterval(cb, ms) {
|
|
|
48
48
|
timeout = setTimeout(wrapper, ms);
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
-
export function
|
|
51
|
+
export function createIntervalAsync(cb, ms) {
|
|
52
52
|
const reset = makeReset();
|
|
53
53
|
let timeout;
|
|
54
54
|
void wrapper();
|
|
@@ -65,7 +65,7 @@ export function makeIntervalAsync(cb, ms) {
|
|
|
65
65
|
timeout = setTimeout(wrapper, ms);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
export function
|
|
68
|
+
export function createAnimationFrame(cb) {
|
|
69
69
|
const stack = new DisposableStack();
|
|
70
70
|
const raf = requestAnimationFrame(now => {
|
|
71
71
|
if (stack.disposed)
|
|
@@ -79,7 +79,7 @@ export function makeAnimationFrame(cb) {
|
|
|
79
79
|
},
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
|
-
export function
|
|
82
|
+
export function createAnimationFrameLoop(cb) {
|
|
83
83
|
const reset = makeReset();
|
|
84
84
|
let raf = requestAnimationFrame(wrapper);
|
|
85
85
|
return {
|
|
@@ -93,7 +93,7 @@ export function makeAnimationFrameLoop(cb) {
|
|
|
93
93
|
raf = requestAnimationFrame(wrapper);
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
-
export function
|
|
96
|
+
export function createTimeout(cb, ms) {
|
|
97
97
|
const timeout = setTimeout(cb, ms);
|
|
98
98
|
return {
|
|
99
99
|
[Symbol.dispose]() {
|
|
@@ -101,7 +101,7 @@ export function makeTimeout(cb, ms) {
|
|
|
101
101
|
},
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
|
-
export function
|
|
104
|
+
export function createTransition(cb, durationMs) {
|
|
105
105
|
const reset = makeReset();
|
|
106
106
|
let start;
|
|
107
107
|
let raf = requestAnimationFrame(wrapper);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jrx",
|
|
3
|
-
"version": "0.3.0-alpha.
|
|
3
|
+
"version": "0.3.0-alpha.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"prepublishOnly": "npx tsc",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
12
|
"types": "./index.d.ts",
|
|
13
|
-
"
|
|
13
|
+
"default": "./index.js"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|