@jucie.io/reactive 1.0.24 → 1.0.26
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 +38 -29
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -55,21 +55,42 @@ console.log(doubled()); // 10
|
|
|
55
55
|
|
|
56
56
|
### Surfaces
|
|
57
57
|
|
|
58
|
-
Declarative reactive components:
|
|
58
|
+
Declarative reactive components with automatic value unwrapping:
|
|
59
|
+
|
|
60
|
+
**Key Concepts:**
|
|
61
|
+
- **Unwrapped Values**: Returned signals and computed values are automatically unwrapped. Access them as properties (e.g., `counter.count`, `counter.doubled`) instead of calling them as functions.
|
|
62
|
+
- **Context (`ctx`)**: Inside computed values and actions, the `ctx` parameter provides access to the unwrapped values of **only what was returned** from the surface. This creates a clean API boundary between public and private state.
|
|
59
63
|
|
|
60
64
|
```javascript
|
|
61
65
|
import { defineSurface } from '@jucie.io/reactive';
|
|
62
66
|
|
|
63
|
-
const useCounter = defineSurface(({
|
|
64
|
-
count
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
const useCounter = defineSurface(({ signal, computed, action }) => {
|
|
68
|
+
const count = signal(0);
|
|
69
|
+
const updateCount = signal(0); // Private signal - NOT in ctx
|
|
70
|
+
|
|
71
|
+
const doubled = computed((ctx) => {
|
|
72
|
+
// ctx.count is available because 'count' is returned
|
|
73
|
+
// ctx.updateCount is NOT available because 'updateCount' is not returned
|
|
74
|
+
return ctx.count * 2
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
const increment = action((ctx) => {
|
|
78
|
+
// Inside actions, call signals as functions
|
|
79
|
+
updateCount(prevUpdateCount => prevUpdateCount + 1)
|
|
80
|
+
ctx.count++
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
count, // Exposed publicly
|
|
85
|
+
doubled, // Exposed publicly
|
|
86
|
+
increment // Exposed publicly
|
|
87
|
+
// updateCount is NOT returned, so it's private
|
|
88
|
+
}
|
|
89
|
+
});
|
|
69
90
|
|
|
70
91
|
const counter = useCounter();
|
|
71
|
-
console.log(counter.count); // 0
|
|
72
|
-
console.log(counter.doubled); // 0
|
|
92
|
+
console.log(counter.count); // 0 - accessed as property, not counter.count()
|
|
93
|
+
console.log(counter.doubled); // 0 - accessed as property, not counter.doubled()
|
|
73
94
|
|
|
74
95
|
counter.increment();
|
|
75
96
|
console.log(counter.count); // 1
|
|
@@ -163,24 +184,6 @@ count(8);
|
|
|
163
184
|
console.log(delta()); // 3 (8 - 5)
|
|
164
185
|
```
|
|
165
186
|
|
|
166
|
-
### Async Computed
|
|
167
|
-
|
|
168
|
-
Computed values also support async:
|
|
169
|
-
|
|
170
|
-
```javascript
|
|
171
|
-
import { createSignal, createComputed } from '@jucie.io/reactive';
|
|
172
|
-
|
|
173
|
-
const userId = createSignal(1);
|
|
174
|
-
|
|
175
|
-
const userData = createComputed(async () => {
|
|
176
|
-
const response = await fetch(`/api/users/${userId()}`);
|
|
177
|
-
return response.json();
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
// Returns a promise
|
|
181
|
-
const data = await userData();
|
|
182
|
-
```
|
|
183
|
-
|
|
184
187
|
### Surface with Object Configuration
|
|
185
188
|
|
|
186
189
|
```javascript
|
|
@@ -208,6 +211,12 @@ const useApp = defineSurface({
|
|
|
208
211
|
console.log('Surface destroyed');
|
|
209
212
|
}
|
|
210
213
|
});
|
|
214
|
+
|
|
215
|
+
const app = useApp();
|
|
216
|
+
|
|
217
|
+
app.count // 0
|
|
218
|
+
app.message // Hello
|
|
219
|
+
|
|
211
220
|
```
|
|
212
221
|
|
|
213
222
|
### Effects
|
|
@@ -268,7 +277,7 @@ count(1); // Logs: "Effect: 1"
|
|
|
268
277
|
|
|
269
278
|
## Configuration Options
|
|
270
279
|
|
|
271
|
-
### Signal
|
|
280
|
+
### Signal
|
|
272
281
|
|
|
273
282
|
```javascript
|
|
274
283
|
{
|
|
@@ -280,7 +289,7 @@ count(1); // Logs: "Effect: 1"
|
|
|
280
289
|
}
|
|
281
290
|
```
|
|
282
291
|
|
|
283
|
-
### Binding Config
|
|
292
|
+
### Binding/Computed Config
|
|
284
293
|
|
|
285
294
|
```javascript
|
|
286
295
|
{
|
package/package.json
CHANGED