@xmachines/play-react 1.0.0-beta.5 → 1.0.0-beta.51
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/LICENSE +21 -0
- package/README.md +131 -296
- package/dist/ActorProvider.d.ts +82 -0
- package/dist/ActorProvider.d.ts.map +1 -0
- package/dist/ActorProvider.js +179 -0
- package/dist/ActorProvider.js.map +1 -0
- package/dist/PlayErrorBoundary.d.ts +55 -0
- package/dist/PlayErrorBoundary.d.ts.map +1 -0
- package/dist/PlayErrorBoundary.js +45 -0
- package/dist/PlayErrorBoundary.js.map +1 -0
- package/dist/PlayRenderer.d.ts +26 -47
- package/dist/PlayRenderer.d.ts.map +1 -1
- package/dist/PlayRenderer.js +31 -76
- package/dist/PlayRenderer.js.map +1 -1
- package/dist/PlayUIProvider.d.ts +67 -0
- package/dist/PlayUIProvider.d.ts.map +1 -0
- package/dist/PlayUIProvider.js +73 -0
- package/dist/PlayUIProvider.js.map +1 -0
- package/dist/errors.d.ts +23 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +26 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +31 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -2
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +6 -18
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -1
- package/dist/useActor.d.ts +26 -0
- package/dist/useActor.d.ts.map +1 -0
- package/dist/useActor.js +27 -0
- package/dist/useActor.js.map +1 -0
- package/dist/useSignalEffect.d.ts +6 -0
- package/dist/useSignalEffect.d.ts.map +1 -1
- package/dist/useSignalEffect.js +36 -19
- package/dist/useSignalEffect.js.map +1 -1
- package/package.json +32 -16
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mikael Karon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,358 +1,193 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
**React renderer consuming signals and UI schema with provider pattern**
|
|
4
|
-
|
|
5
|
-
Signal-driven React rendering layer observing actor state with zero React state for business logic.
|
|
6
|
-
|
|
7
|
-
## Overview
|
|
8
|
-
|
|
9
|
-
`@xmachines/play-react` provides `PlayRenderer` and `useSignalEffect` for building React UIs that passively observe actor signals. This package enables framework-swappable architecture where React is just a rendering target subscribing to signal changes — business logic lives entirely in the actor.
|
|
1
|
+
<!-- generated-by: gsd-doc-writer -->
|
|
10
2
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- **Signal-Only Reactivity (INV-05):** No useState/useReducer for business logic, signals only
|
|
14
|
-
- **Passive Infrastructure (INV-04):** Components observe signals, send events to actor
|
|
3
|
+
# @xmachines/play-react
|
|
15
4
|
|
|
16
|
-
|
|
5
|
+
React renderer for XMachines Play architecture with signal-driven rendering.
|
|
17
6
|
|
|
18
|
-
|
|
7
|
+
Part of the [xmachines-js monorepo](../../README.md).
|
|
19
8
|
|
|
20
9
|
## Installation
|
|
21
10
|
|
|
22
11
|
```bash
|
|
23
|
-
npm install react@^18.0.0 react-dom@^18.0.0
|
|
24
12
|
npm install @xmachines/play-react
|
|
25
13
|
```
|
|
26
14
|
|
|
27
|
-
|
|
15
|
+
**Peer dependencies** (must be installed separately):
|
|
28
16
|
|
|
29
|
-
|
|
30
|
-
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
**Peer dependencies:**
|
|
17
|
+
```bash
|
|
18
|
+
npm install react react-dom xstate @xstate/store @json-render/react @json-render/core @json-render/xstate
|
|
19
|
+
```
|
|
34
20
|
|
|
35
|
-
|
|
36
|
-
- `react-dom` ^18.0.0 - React DOM renderer
|
|
21
|
+
Supported versions:
|
|
37
22
|
|
|
38
|
-
|
|
23
|
+
- `react` / `react-dom`: `^18.0.0 || ^19.0.0`
|
|
24
|
+
- `xstate`: `^5.30.0`
|
|
25
|
+
- `@xstate/store`: `^3.17.0`
|
|
26
|
+
- `@json-render/*`: `^0.18.0`
|
|
39
27
|
|
|
40
|
-
|
|
41
|
-
import { createRoot } from "react-dom/client";
|
|
42
|
-
import { definePlayer } from "@xmachines/play-xstate";
|
|
43
|
-
import { defineCatalog } from "@xmachines/play-catalog";
|
|
44
|
-
import { PlayRenderer } from "@xmachines/play-react";
|
|
45
|
-
import { z } from "zod";
|
|
46
|
-
|
|
47
|
-
// 1. Define catalog (business logic layer)
|
|
48
|
-
const catalog = defineCatalog({
|
|
49
|
-
LoginForm: z.object({ error: z.string().optional() }),
|
|
50
|
-
Dashboard: z.object({
|
|
51
|
-
userId: z.string(),
|
|
52
|
-
username: z.string(),
|
|
53
|
-
}),
|
|
54
|
-
});
|
|
28
|
+
## Usage
|
|
55
29
|
|
|
56
|
-
|
|
57
|
-
const components = {
|
|
58
|
-
LoginForm: ({ error, send }) => (
|
|
59
|
-
<form
|
|
60
|
-
onSubmit={(e) => {
|
|
61
|
-
e.preventDefault();
|
|
62
|
-
const data = new FormData(e.currentTarget);
|
|
63
|
-
send({
|
|
64
|
-
type: "auth.login",
|
|
65
|
-
username: data.get("username"),
|
|
66
|
-
password: data.get("password"),
|
|
67
|
-
});
|
|
68
|
-
}}
|
|
69
|
-
>
|
|
70
|
-
{error && <p style={{ color: "red" }}>{error}</p>}
|
|
71
|
-
<input name="username" required placeholder="Username" />
|
|
72
|
-
<input name="password" type="password" required placeholder="Password" />
|
|
73
|
-
<button type="submit">Log In</button>
|
|
74
|
-
</form>
|
|
75
|
-
),
|
|
76
|
-
Dashboard: ({ userId, username, send }) => (
|
|
77
|
-
<div>
|
|
78
|
-
<h1>Welcome, {username}!</h1>
|
|
79
|
-
<p>User ID: {userId}</p>
|
|
80
|
-
<button onClick={() => send({ type: "auth.logout" })}>Log Out</button>
|
|
81
|
-
</div>
|
|
82
|
-
),
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
// 3. Create player actor (business logic runtime)
|
|
86
|
-
const createPlayer = definePlayer({ machine: authMachine, catalog });
|
|
87
|
-
const actor = createPlayer();
|
|
88
|
-
actor.start();
|
|
30
|
+
### Standard usage — `PlayUIProvider` + `PlayRenderer`
|
|
89
31
|
|
|
90
|
-
|
|
91
|
-
const root = createRoot(document.getElementById("app")!);
|
|
92
|
-
root.render(<PlayRenderer actor={actor} components={components} />);
|
|
93
|
-
```
|
|
32
|
+
The recommended pattern for actor-driven React rendering:
|
|
94
33
|
|
|
95
|
-
|
|
34
|
+
```tsx
|
|
35
|
+
import { PlayUIProvider, PlayRenderer, defineRegistry } from "@xmachines/play-react";
|
|
36
|
+
import { definePlayer } from "@xmachines/play-xstate";
|
|
96
37
|
|
|
97
|
-
|
|
38
|
+
// 1. Create and start the actor
|
|
39
|
+
const actor = definePlayer({ machine: myMachine })();
|
|
40
|
+
actor.start();
|
|
98
41
|
|
|
99
|
-
|
|
42
|
+
// 2. Define the component registry with action handlers
|
|
43
|
+
const registryResult = defineRegistry(myCatalog, {
|
|
44
|
+
components: { Login, Dashboard },
|
|
45
|
+
actions: {
|
|
46
|
+
login: async ({ username }) => actor.send({ type: "auth.login", username }),
|
|
47
|
+
logout: async () => actor.send({ type: "auth.logout" }),
|
|
48
|
+
},
|
|
49
|
+
});
|
|
100
50
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
51
|
+
// 3. Render — signals drive view transitions automatically
|
|
52
|
+
function App() {
|
|
53
|
+
return (
|
|
54
|
+
<PlayUIProvider actor={actor} registryResult={registryResult}>
|
|
55
|
+
<PlayRenderer />
|
|
56
|
+
</PlayUIProvider>
|
|
57
|
+
);
|
|
106
58
|
}
|
|
107
59
|
```
|
|
108
60
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
- `actor` - Actor instance with `currentView` signal
|
|
112
|
-
- `components` - Map of component names to React components
|
|
113
|
-
- `fallback` - Component shown when `currentView` is null (default: `null`)
|
|
114
|
-
|
|
115
|
-
**Behavior:**
|
|
116
|
-
|
|
117
|
-
1. Subscribes to `actor.currentView` signal via `useSignalEffect`
|
|
118
|
-
2. Looks up component from `components` map using `view.component` string
|
|
119
|
-
3. Renders component with props from `view.props` + `send` function
|
|
61
|
+
### With optional `JSONUIProvider` props
|
|
120
62
|
|
|
121
|
-
|
|
63
|
+
Pass navigation and validation helpers through `PlayUIProvider`:
|
|
122
64
|
|
|
123
|
-
```
|
|
124
|
-
<
|
|
65
|
+
```tsx
|
|
66
|
+
<PlayUIProvider
|
|
125
67
|
actor={actor}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
68
|
+
registryResult={registryResult}
|
|
69
|
+
navigate={(path) => router.push(path)}
|
|
70
|
+
validationFunctions={{ isEmail: (v) => /^.+@.+$/.test(String(v)) }}
|
|
71
|
+
>
|
|
72
|
+
<PlayRenderer />
|
|
73
|
+
</PlayUIProvider>
|
|
132
74
|
```
|
|
133
75
|
|
|
134
|
-
###
|
|
135
|
-
|
|
136
|
-
Hook for subscribing to signal changes with automatic cleanup:
|
|
137
|
-
|
|
138
|
-
```typescript
|
|
139
|
-
useSignalEffect(() => {
|
|
140
|
-
const value = signal.get();
|
|
141
|
-
// React re-renders when signal changes
|
|
142
|
-
});
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
**Behavior:**
|
|
146
|
-
|
|
147
|
-
- Tracks signal dependencies automatically via `Signal.Computed` wrapper
|
|
148
|
-
- Uses `Signal.subtle.Watcher` with microtask batching
|
|
149
|
-
- Triggers React state update to force re-render
|
|
150
|
-
- Cleans up watcher on unmount with explicit `unwatch`
|
|
151
|
-
|
|
152
|
-
**Canonical watcher lifecycle:**
|
|
153
|
-
|
|
154
|
-
1. `notify`
|
|
155
|
-
2. `queueMicrotask`
|
|
156
|
-
3. drain pending work (`getPending` and/or `Computed.get`)
|
|
157
|
-
4. run effect + trigger render
|
|
158
|
-
5. re-arm watcher via `watch()`
|
|
159
|
-
|
|
160
|
-
Watcher notification is one-shot, so re-arm and explicit cleanup are both required.
|
|
161
|
-
|
|
162
|
-
**Example:**
|
|
163
|
-
|
|
164
|
-
```typescript
|
|
165
|
-
import { useSignalEffect } from "@xmachines/play-react";
|
|
166
|
-
import { useState } from "react";
|
|
76
|
+
### Escape hatch — custom provider composition
|
|
167
77
|
|
|
168
|
-
|
|
169
|
-
const [route, setRoute] = useState<string | null>(null);
|
|
78
|
+
Use `ActorProvider` directly when you need to compose providers manually:
|
|
170
79
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
setRoute(currentRoute);
|
|
174
|
-
});
|
|
80
|
+
```tsx
|
|
81
|
+
import { ActorProvider, JSONUIProvider, PlayRenderer } from "@xmachines/play-react";
|
|
175
82
|
|
|
176
|
-
|
|
177
|
-
}
|
|
83
|
+
<ActorProvider actor={actor} registryResult={registryResult}>
|
|
84
|
+
<JSONUIProvider registry={registryResult.registry}>
|
|
85
|
+
<PlayRenderer />
|
|
86
|
+
</JSONUIProvider>
|
|
87
|
+
</ActorProvider>;
|
|
178
88
|
```
|
|
179
89
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
## Examples
|
|
183
|
-
|
|
184
|
-
### Component Receiving Props from Catalog
|
|
90
|
+
### Accessing the actor from inside the tree
|
|
185
91
|
|
|
186
|
-
```
|
|
187
|
-
import {
|
|
188
|
-
import { defineCatalog } from "@xmachines/play-catalog";
|
|
189
|
-
import { z } from "zod";
|
|
92
|
+
```tsx
|
|
93
|
+
import { useActor } from "@xmachines/play-react";
|
|
190
94
|
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
name: z.string(),
|
|
196
|
-
avatar: z.string().url().optional(),
|
|
197
|
-
stats: z.object({
|
|
198
|
-
posts: z.number(),
|
|
199
|
-
followers: z.number(),
|
|
200
|
-
}),
|
|
201
|
-
}),
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
// Component receives type-safe props + send
|
|
205
|
-
const components = {
|
|
206
|
-
UserProfile: ({ userId, name, avatar, stats, send }) => (
|
|
207
|
-
<div>
|
|
208
|
-
{avatar && <img src={avatar} alt={name} />}
|
|
209
|
-
<h1>{name}</h1>
|
|
210
|
-
<p>ID: {userId}</p>
|
|
211
|
-
<div>
|
|
212
|
-
<span>{stats.posts} posts</span>
|
|
213
|
-
<span>{stats.followers} followers</span>
|
|
214
|
-
</div>
|
|
215
|
-
<button
|
|
216
|
-
onClick={() =>
|
|
217
|
-
send({
|
|
218
|
-
type: "profile.edit",
|
|
219
|
-
userId,
|
|
220
|
-
})
|
|
221
|
-
}
|
|
222
|
-
>
|
|
223
|
-
Edit Profile
|
|
224
|
-
</button>
|
|
225
|
-
</div>
|
|
226
|
-
),
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
<PlayRenderer actor={actor} components={components} />;
|
|
95
|
+
function SubmitButton() {
|
|
96
|
+
const actor = useActor();
|
|
97
|
+
return <button onClick={() => actor.send({ type: "SUBMIT" })}>Submit</button>;
|
|
98
|
+
}
|
|
230
99
|
```
|
|
231
100
|
|
|
232
|
-
###
|
|
101
|
+
### Subscribing to signals directly
|
|
233
102
|
|
|
234
|
-
```
|
|
103
|
+
```tsx
|
|
235
104
|
import { useSignalEffect } from "@xmachines/play-react";
|
|
236
|
-
import { AbstractActor } from "@xmachines/play-actor";
|
|
237
105
|
|
|
238
|
-
function
|
|
106
|
+
function MyComponent({ actor }) {
|
|
239
107
|
const [view, setView] = useState(null);
|
|
240
108
|
|
|
241
|
-
// Subscribe to currentView signal
|
|
242
109
|
useSignalEffect(() => {
|
|
243
|
-
|
|
244
|
-
setView(currentView);
|
|
110
|
+
setView(actor.currentView.get());
|
|
245
111
|
});
|
|
246
112
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
// Custom rendering logic
|
|
250
|
-
if (view.component === "SpecialCase") {
|
|
251
|
-
return <SpecialCaseComponent {...view.props} actor={actor} />;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// Fallback to standard rendering
|
|
255
|
-
return <DefaultComponent view={view} actor={actor} />;
|
|
113
|
+
return <div>{view?.component}</div>;
|
|
256
114
|
}
|
|
257
115
|
```
|
|
258
116
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
117
|
+
## API Summary
|
|
118
|
+
|
|
119
|
+
### Components
|
|
120
|
+
|
|
121
|
+
| Export | Description |
|
|
122
|
+
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
|
|
123
|
+
| `<PlayUIProvider>` | Batteries-included provider: wraps `ActorProvider` + `JSONUIProvider`. Standard entry point. |
|
|
124
|
+
| `<PlayRenderer>` | Zero-prop leaf component. Reads the current actor view from context and renders it. Must be inside `PlayUIProvider` or `ActorProvider`. |
|
|
125
|
+
| `<ActorProvider>` | Escape-hatch primitive. Owns actor bridging, signal subscription, and per-view `StateStore` lifecycle. |
|
|
126
|
+
| `<PlayErrorBoundary>` | React class error boundary for catching catalog component render errors. |
|
|
127
|
+
|
|
128
|
+
### Hooks
|
|
129
|
+
|
|
130
|
+
| Export | Description |
|
|
131
|
+
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
132
|
+
| `useSignalEffect(callback)` | Subscribes to TC39 signal changes; re-runs the callback and forces a React re-render when any accessed signal changes. Cleanup is automatic on unmount. |
|
|
133
|
+
| `useActor()` | Returns the raw actor instance. Must be called inside an `ActorProvider`/`PlayUIProvider` tree. |
|
|
134
|
+
| `usePlayView()` | Returns `{ spec, handlers, registry, store }` for the current view. Must be called inside an `ActorProvider`/`PlayUIProvider` tree. |
|
|
135
|
+
|
|
136
|
+
### Types
|
|
137
|
+
|
|
138
|
+
| Export | Description |
|
|
139
|
+
| ------------------------ | ---------------------------------------------------------------------------------------------- |
|
|
140
|
+
| `PlayUIProviderProps` | Props for `<PlayUIProvider>` |
|
|
141
|
+
| `ActorProviderProps` | Props for `<ActorProvider>` (also exported as `PlayRendererProps` for migration compatibility) |
|
|
142
|
+
| `PlayErrorBoundaryProps` | Props for `<PlayErrorBoundary>` |
|
|
143
|
+
| `PlayErrorBoundaryState` | State shape for `<PlayErrorBoundary>` |
|
|
144
|
+
| `PlayActor` | Type alias for `AbstractActor<AnyActorLogic>` |
|
|
145
|
+
| `ViewContextValue` | Value shape returned by `usePlayView()` |
|
|
146
|
+
| `RenderErrorHandler` | Error handler callback type for render errors |
|
|
147
|
+
|
|
148
|
+
### Re-exports from `@json-render/react`
|
|
149
|
+
|
|
150
|
+
`@xmachines/play-react` re-exports the full `@json-render/react` surface so consumers only need one import:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import {
|
|
154
|
+
defineRegistry,
|
|
155
|
+
useBoundProp,
|
|
156
|
+
JSONUIProvider,
|
|
157
|
+
StateProvider,
|
|
158
|
+
ActionProvider,
|
|
159
|
+
VisibilityProvider,
|
|
160
|
+
ValidationProvider,
|
|
161
|
+
Renderer,
|
|
162
|
+
} from "@xmachines/play-react";
|
|
299
163
|
```
|
|
300
164
|
|
|
301
|
-
##
|
|
302
|
-
|
|
303
|
-
This package implements **Signal-Only Reactivity (INV-05)** and **Passive Infrastructure (INV-04)**:
|
|
304
|
-
|
|
305
|
-
1. **No Business Logic in React:**
|
|
306
|
-
- No useState/useReducer for business state
|
|
307
|
-
- No useEffect for side effects
|
|
308
|
-
- React only triggers renders, doesn't control state
|
|
309
|
-
|
|
310
|
-
2. **Signals as Source of Truth:**
|
|
311
|
-
- `actor.currentView.get()` provides UI structure
|
|
312
|
-
- `actor.currentRoute.get()` provides navigation state
|
|
313
|
-
- Components observe signals via `useSignalEffect`
|
|
165
|
+
## Key Principle
|
|
314
166
|
|
|
315
|
-
|
|
316
|
-
- Components receive `send` function via props
|
|
317
|
-
- User actions send events to actor (e.g., `{ type: "auth.login" }`)
|
|
318
|
-
- Actor guards validate and process events
|
|
167
|
+
React state is **never** used for business logic — only for triggering React's render cycle. Signals (`@xmachines/play-signals`) are the source of truth. `PlayUIProvider` passively observes actor signals via `useSignalEffect` and re-renders when the current view changes. Rapid signal updates are batched via microtasks to prevent unnecessary React renders.
|
|
319
168
|
|
|
320
|
-
|
|
321
|
-
- `Signal.subtle.Watcher` coalesces rapid signal changes
|
|
322
|
-
- Prevents React thrashing from multiple signal updates
|
|
323
|
-
- Single React render per microtask batch
|
|
169
|
+
## Testing
|
|
324
170
|
|
|
325
|
-
|
|
326
|
-
- Component teardown must call watcher `unwatch` in cleanup
|
|
327
|
-
- Do not rely on GC-only cleanup
|
|
171
|
+
Run unit tests (jsdom environment):
|
|
328
172
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
- Enables composition with navigation, headers, footers
|
|
333
|
-
- Supports multiple renderers in same app
|
|
334
|
-
|
|
335
|
-
**Architectural Invariants:**
|
|
173
|
+
```bash
|
|
174
|
+
npm test -w @xmachines/play-react
|
|
175
|
+
```
|
|
336
176
|
|
|
337
|
-
|
|
338
|
-
- **Passive Infrastructure (INV-04):** Components reflect, never decide
|
|
177
|
+
Run tests with coverage:
|
|
339
178
|
|
|
340
|
-
|
|
179
|
+
```bash
|
|
180
|
+
npm run test:coverage -w @xmachines/play-react
|
|
181
|
+
```
|
|
341
182
|
|
|
342
|
-
|
|
343
|
-
- **Type Safety:** Props validated against catalog schemas
|
|
344
|
-
- **Simple Testing:** Test actors without React renderer
|
|
345
|
-
- **Performance:** Microtask batching reduces unnecessary renders
|
|
346
|
-
- **Composability:** Renderer prop enables complex layouts ()
|
|
183
|
+
Run browser integration tests (requires Chromium):
|
|
347
184
|
|
|
348
|
-
|
|
185
|
+
```bash
|
|
186
|
+
npm run test:browser -w @xmachines/play-react
|
|
187
|
+
```
|
|
349
188
|
|
|
350
|
-
|
|
351
|
-
- **[@xmachines/play-catalog](../play-catalog)** - UI schema validation
|
|
352
|
-
- **[@xmachines/play-tanstack-react-router](../play-tanstack-react-router)** - TanStack Router integration
|
|
353
|
-
- **[@xmachines/play-actor](../play-actor)** - Actor base
|
|
354
|
-
- **[@xmachines/play-signals](../play-signals)** - TC39 Signals primitives
|
|
189
|
+
Coverage thresholds: 80% lines, functions, branches, and statements.
|
|
355
190
|
|
|
356
191
|
## License
|
|
357
192
|
|
|
358
|
-
MIT
|
|
193
|
+
MIT — see [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ActorProvider — escape hatch primitive for actor lifecycle management.
|
|
3
|
+
*
|
|
4
|
+
* Owns: actor bridging, signal subscription (useSignalEffect), per-view StateStore
|
|
5
|
+
* lifecycle (controlled/uncontrolled), handler resolution via inner component pattern
|
|
6
|
+
* (uses useStateStore()), StateProvider wrap, PlayErrorBoundary wrap, onRenderError injection.
|
|
7
|
+
*
|
|
8
|
+
* Standard usage: prefer <PlayUIProvider> unless you need to compose providers manually.
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
import React from "react";
|
|
13
|
+
import type { DefineRegistryResult, ComponentRegistry } from "@json-render/react";
|
|
14
|
+
import type { BaseActorProviderProps, BaseViewContextValue } from "@xmachines/play-actor";
|
|
15
|
+
/**
|
|
16
|
+
* Props for the ActorProvider component.
|
|
17
|
+
*
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export interface ActorProviderProps extends BaseActorProviderProps<DefineRegistryResult> {
|
|
21
|
+
/** Optional component shown when currentView is null or a catalog component throws */
|
|
22
|
+
fallback?: React.ReactNode;
|
|
23
|
+
/** Optional error handler callback invoked when a catalog component throws during render */
|
|
24
|
+
onError?: (error: Error, info: React.ErrorInfo) => void;
|
|
25
|
+
/** Child components to render inside the provider tree */
|
|
26
|
+
children: React.ReactNode;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Value provided by ViewContext (accessible via usePlayView()).
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
export interface ViewContextValue extends BaseViewContextValue<ComponentRegistry> {
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Hook to access the current view spec, handlers, and registry.
|
|
37
|
+
*
|
|
38
|
+
* Must be called inside <ActorProvider> or <PlayUIProvider>.
|
|
39
|
+
*
|
|
40
|
+
* @throws {Error} If called outside an ActorProvider/PlayUIProvider tree
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { usePlayView } from "@xmachines/play-react";
|
|
45
|
+
*
|
|
46
|
+
* function MyRenderer() {
|
|
47
|
+
* const view = usePlayView();
|
|
48
|
+
* return <Renderer spec={view.spec} registry={view.registry} />;
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
export declare function usePlayView(): ViewContextValue;
|
|
55
|
+
/**
|
|
56
|
+
* ActorProvider — escape hatch primitive for composing actor lifecycle with custom providers.
|
|
57
|
+
*
|
|
58
|
+
* Subscribes to actor.currentView signal, manages the per-view StateStore lifecycle,
|
|
59
|
+
* wraps children in StateProvider and PlayErrorBoundary, and injects onRenderError
|
|
60
|
+
* into the component registry.
|
|
61
|
+
*
|
|
62
|
+
* Standard usage: prefer <PlayUIProvider> unless you need to compose providers manually.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```tsx
|
|
66
|
+
* // Custom composition (escape hatch):
|
|
67
|
+
* <ActorProvider actor={actor} registryResult={registryResult}>
|
|
68
|
+
* <JSONUIProvider registry={registryResult.registry}>
|
|
69
|
+
* <PlayRenderer />
|
|
70
|
+
* </JSONUIProvider>
|
|
71
|
+
* </ActorProvider>
|
|
72
|
+
*
|
|
73
|
+
* // Standard usage: prefer PlayUIProvider
|
|
74
|
+
* <PlayUIProvider actor={actor} registryResult={registryResult}>
|
|
75
|
+
* <PlayRenderer />
|
|
76
|
+
* </PlayUIProvider>
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @public
|
|
80
|
+
*/
|
|
81
|
+
export declare const ActorProvider: React.FC<ActorProviderProps>;
|
|
82
|
+
//# sourceMappingURL=ActorProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ActorProvider.d.ts","sourceRoot":"","sources":["../src/ActorProvider.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAA+D,MAAM,OAAO,CAAC;AAEpF,OAAO,KAAK,EAAE,oBAAoB,EAAY,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAO5F,OAAO,KAAK,EAAY,sBAAsB,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAIpG;;;;GAIG;AACH,MAAM,WAAW,kBAAmB,SAAQ,sBAAsB,CAAC,oBAAoB,CAAC;IACvF,sFAAsF;IACtF,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,4FAA4F;IAC5F,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,SAAS,KAAK,IAAI,CAAC;IACxD,0DAA0D;IAC1D,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC1B;AAED;;;;GAIG;AACH,MAAM,WAAW,gBAAiB,SAAQ,oBAAoB,CAAC,iBAAiB,CAAC;CAAG;AAQpF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,IAAI,gBAAgB,CAE9C;AAoED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,kBAAkB,CA0FtD,CAAC"}
|