@xmachines/play-react-demo
Examples / @xmachines/play-react-demo
React renderer demo for @xmachines/play-react — actor + PlayRenderer without a router.
What This Demonstrates
- Shared auth machine reused without framework-specific business logic
PlayRendererrendering actor-projected views with a typeddefineRegistrycatalog- Auth machine states drive view switching via
auth.login/auth.logoutevents only — noplay.routerouting - Canonical atoms lifecycle surfaced through React’s rendering model
- Non-browser invariant tests plus browser renderer coverage
- Stately inspector attached at creation via
options.inspect— the debug panel’s Show Inspector button opens the live machine in its own window - json-render 0.20 showcase pages driven purely by spec:
- Overview — two data planes side by side:
/context/tasks(machine context via the read-only/contextprojection;repeat+key,$item-filtered lists, per-row actions sending inspectable machine events) next to ephemeral store state (pushState/removeState/setStatebuilt-ins driving filter chips and toggles that survive context updates) - Stats — nested repeats (
repeat.statePath: { $item: "members" }) over/context/teams - Settings — declarative field validation, the
validateFormbuilt-in, a confirm-gatedresetFormhost action, andonSuccess/onErrorchains - Contact — a chained named action: submit validates, then routes home on success or writes
"$error.message"to state on failure
- Overview — two data planes side by side:
Running the Demo
From the repository root:
pnpm installpnpm --filter @xmachines/play-react-demo run devThen open http://localhost:3001.
Step-by-Step Code Flow
Use this order to understand the implementation:
src/main.tsxmounts<App />.src/App.tsxcreates the page’s inspector withcreateDemoInspector()and the actor at module scope viadefinePlayer({ machine: authMachine, options: { inspect: inspector.inspect }, actor: compose(PlayerActor, withRouting, withView) })()— both created once when the module is first evaluated.defineRegistry(authCatalog, { components, actions })builds the typedregistryResult— real async action handlers dispatching toactor.send().<PlayUIProvider actor={actor} registryResult={registryResult}><PlayRenderer /></PlayUIProvider>observesactor.currentViewand renders the active spec.- A
NavBarcomponent observesactoratoms directly for nav visibility. <DebugPanel actor={actor} inspector={inspector} />shows live state, auth status, and current route, plus the Show Inspector button.- Browser tests in
test/browser/validate startup and interaction behavior.
// src/main.tsx (shape)createRoot(document.getElementById("root")!).render(<App />);// src/App.tsx (shape)// Both actor and registryResult are module-scope singletons — created once,// never recreated, no useMemo needed inside the component.const inspector = createDemoInspector();const actor: AuthActor = definePlayer({ machine: authMachine, options: { inspect: inspector.inspect }, actor: compose(PlayerActor, withRouting, withView),})();actor.start();
const registryResult = defineRegistry(authCatalog, { components: demoComponents, // one shared map — Home…NavBar plus the 0.20 Panel/Field/Button leaves actions: createAuthActionHandlers(actor), // login/logout/route + task events + form actions});
export function App() { return ( <div className="demo-app" data-demo-shell> <header className="demo-header"> <h1 className="demo-title">XMachines Play React Demo</h1> <NavBar actor={actor} /> </header> <main className="demo-content" data-demo-content> <PlayUIProvider actor={actor} registryResult={registryResult}> <PlayRenderer /> </PlayUIProvider> </main> <DebugPanel actor={actor} inspector={inspector} /> </div> );}Key Files
src/main.tsx- React entry point that mounts<App />src/App.tsx- actor lifecycle, registry construction, andPlayUIProvider+PlayRenderercompositionsrc/catalog.ts- catalog definition usingdefineCatalogfrom@xmachines/json-render-corewith@xmachines/json-render-react/schemasrc/components/- demo view components bound to catalog component keys (Home, Login, Dashboard, Profile, etc.)test/library-pattern.test.ts- architecture boundary and invariant assertionstest/browser/renderer-demo.browser.test.tsx- browser-mode renderer coverage
State Machine & Architecture Details
The demo utilizes XMachines architectural invariants:
- Actor Authority: View components dispatch
auth.loginandauth.logoutevents to the actor. The actor evaluates guards and transitions — React never decides which view to render. - Passive Infrastructure:
PlayRendererobservesactor.currentViewatoms only. It holds no business state and makes no routing decisions. - Atom-Only Reactivity: The bridge leverages React’s rendering model to react precisely when actor atoms update, without polluting the component tree with
useStateoruseEffectfor business logic.
Watcher Lifecycle and Cleanup Contract
This demo follows the canonical watcher lifecycle used across all @xmachines framework adapters:
- The atom notifies the subscription, synchronously, from the
setthat changed it watchAtomqueues one microtask, and it drops each duplicate notification of that batch- The microtask reads the actor atoms and projects React render state
One subscription lives for the whole watch, and no caller arms a watcher again. Cleanup is explicit: actor.stop() is called on component unmount via useEffect, and PlayRenderer handles internal watcher teardown natively.
Adapter Boundaries
PlayRenderer and defineRegistry stay passive infrastructure. Business validity remains actor-owned. The registry maps catalog component keys to React component implementations — the actor owns which key is active. The demo intentionally omits URL routing to isolate the renderer contract.
Available Scripts
These commands are defined in package.json:
| Command | Description |
|---|---|
pnpm --filter @xmachines/play-react-demo run dev | Start Vite dev server (http://localhost:3001/) |
pnpm --filter @xmachines/play-react-demo run build | Build production bundle |
pnpm --filter @xmachines/play-react-demo run preview | Preview built bundle |
pnpm --filter @xmachines/play-react-demo run test | Run Vitest test suite |
pnpm --filter @xmachines/play-react-demo run test:browser | Run browser-focused Vitest suite |
Learn More
Type Aliases
Variables
- About
- authCatalog
- Button
- Contact
- Dashboard
- DebugPanel
- demoComponents
- Field
- Home
- Login
- NavBar
- NavBarView
- Navigation
- Overview
- Panel
- Profile
- Settings
- Shell
- Stats