@typeonce/effect-machine 0.46.0 → 0.47.0
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/AGENTS.md +1823 -0
- package/CLAUDE.md +1823 -0
- package/README.md +100 -77
- package/ai-docs/README.md +18 -0
- package/ai-docs/src/10_modeling/01_create-a-machine.ts +51 -0
- package/ai-docs/src/10_modeling/02_branch-on-an-event.ts +55 -0
- package/ai-docs/src/10_modeling/03_update-retained-state.ts +58 -0
- package/ai-docs/src/10_modeling/04_verify-after-changing.ts +29 -0
- package/{docs/agent-guide.md → ai-docs/src/10_modeling/index.md} +234 -155
- package/{docs/root-api.md → ai-docs/src/20_root-api/index.md} +29 -57
- package/{docs/effect-atom-react.md → ai-docs/src/30_react/index.md} +42 -49
- package/{docs/machine-review.md → ai-docs/src/40_review/index.md} +42 -40
- package/ai-docs/src/index.md +7 -0
- package/ai-docs/tsconfig.json +22 -0
- package/dist/Machine.d.ts +1195 -1474
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +366 -214
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/command.d.ts +8 -0
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/command.js +2 -2
- package/dist/internal/machine/command.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +21 -5
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +1 -0
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/construction.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +2 -1
- package/dist/internal/machine/declaration.d.ts.map +1 -1
- package/dist/internal/machine/declaration.js +28 -46
- package/dist/internal/machine/declaration.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +23 -16
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +2 -0
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/implementation.js.map +1 -1
- package/dist/internal/machine/initialDeclaration.d.ts +2 -0
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -1
- package/dist/internal/machine/initialDeclaration.js +15 -0
- package/dist/internal/machine/initialDeclaration.js.map +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +25 -19
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/invocationEvent.d.ts +34 -17
- package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
- package/dist/internal/machine/invocationEvent.js +16 -4
- package/dist/internal/machine/invocationEvent.js.map +1 -1
- package/dist/internal/machine/machine.d.ts +5 -22
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +157 -119
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +15 -4
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +18 -21
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +2 -0
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +2 -0
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.js.map +1 -1
- package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -1
- package/dist/internal/machine/runtimeGeneric.js.map +1 -1
- package/dist/internal/machine/runtimeProtocol.d.ts +26 -0
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
- package/dist/internal/machine/runtimeProtocol.js +71 -0
- package/dist/internal/machine/runtimeProtocol.js.map +1 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +0 -14
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/symbols.d.ts +9 -0
- package/dist/internal/machine/symbols.d.ts.map +1 -1
- package/dist/internal/machine/symbols.js.map +1 -1
- package/dist/internal/machine/topology.d.ts +18 -9
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +16 -1
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/arbitrary.d.ts.map +1 -1
- package/dist/internal/testing/machine/arbitrary.js +2 -0
- package/dist/internal/testing/machine/arbitrary.js.map +1 -1
- package/dist/internal/testing/machine/check.d.ts +17 -0
- package/dist/internal/testing/machine/check.d.ts.map +1 -0
- package/dist/internal/testing/machine/check.js +371 -0
- package/dist/internal/testing/machine/check.js.map +1 -0
- package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
- package/dist/internal/testing/machine/exploration.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts +14 -13
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +21 -23
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/runtime.d.ts +19 -88
- package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
- package/dist/internal/testing/machine/runtime.js +0 -19
- package/dist/internal/testing/machine/runtime.js.map +1 -1
- package/dist/internal/testing/machine/runtimeInvariant.js.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +349 -139
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +201 -136
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +9 -14
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +9 -14
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +169 -80
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +165 -76
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/package.json +11 -9
- package/src/Machine.ts +1139 -2130
- package/src/internal/machine/atom.ts +3 -3
- package/src/internal/machine/cluster.ts +2 -2
- package/src/internal/machine/command.ts +10 -4
- package/src/internal/machine/configuration.ts +16 -17
- package/src/internal/machine/construction.ts +1 -1
- package/src/internal/machine/declaration.ts +25 -50
- package/src/internal/machine/executionPlan.ts +90 -56
- package/src/internal/machine/implementation.ts +3 -1
- package/src/internal/machine/initialDeclaration.ts +16 -0
- package/src/internal/machine/invocation.ts +38 -44
- package/src/internal/machine/invocationEvent.ts +46 -20
- package/src/internal/machine/machine.ts +217 -243
- package/src/internal/machine/planner.ts +72 -106
- package/src/internal/machine/process.ts +12 -0
- package/src/internal/machine/protocol.ts +6 -6
- package/src/internal/machine/runtime.ts +1 -5
- package/src/internal/machine/runtimeCompiled.ts +2 -2
- package/src/internal/machine/runtimeGeneric.ts +2 -4
- package/src/internal/machine/runtimeProtocol.ts +80 -4
- package/src/internal/machine/stateDefinition.ts +0 -39
- package/src/internal/machine/symbols.ts +11 -0
- package/src/internal/machine/topology.ts +40 -18
- package/src/internal/testing/machine/arbitrary.ts +2 -0
- package/src/internal/testing/machine/check.ts +460 -0
- package/src/internal/testing/machine/exploration.ts +4 -5
- package/src/internal/testing/machine/finiteModel.ts +40 -42
- package/src/internal/testing/machine/runtime.ts +31 -170
- package/src/internal/testing/machine/runtimeInvariant.ts +3 -3
- package/src/internal/testing/machine/verification.ts +14 -14
- package/src/testing/MachineTest.ts +366 -145
- package/src/unstable/cluster/ClusterMachine.ts +9 -14
- package/src/unstable/reactivity/AtomMachine.ts +167 -78
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
## Root machine API
|
|
2
2
|
|
|
3
3
|
A machine has one root state. It can own data, child topology, both, or neither.
|
|
4
4
|
`Machine.state` declares that structure; `Machine.make` declares protocols and
|
|
5
5
|
reusable sources; `.handle` supplies initialization and behavior.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
### Startup input and state data
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
10
|
import { Machine } from "@typeonce/effect-machine"
|
|
@@ -66,7 +66,7 @@ optional region data. Compound handlers declare one direct initial child with
|
|
|
66
66
|
`initial: { target, data }`; each compound has its own initial declaration.
|
|
67
67
|
There is no `initialConfiguration` or initializer in `make`.
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
### Root initialization and updates
|
|
70
70
|
|
|
71
71
|
The root is spelled `"root"` in declarations, so a top-level state cannot use
|
|
72
72
|
that name. It is not a transition destination.
|
|
@@ -74,9 +74,9 @@ that name. It is not a transition destination.
|
|
|
74
74
|
`{ initialize: ... }` reconstructs root data and follows its initial
|
|
75
75
|
declarations using fresh machine input: a value or a callback of the transition
|
|
76
76
|
context. A machine without input declares `{ initialize: true }`. `initialize`
|
|
77
|
-
rejects `target`, `update`, `history`, `
|
|
78
|
-
|
|
79
|
-
`select.
|
|
77
|
+
rejects `target`, `update`, `history`, `stay`, `data`, and `decoded`. A `branches`
|
|
78
|
+
entry declares `{ initialize: true }`, and its resolver passes the input with
|
|
79
|
+
`select.initialize({ input })`.
|
|
80
80
|
|
|
81
81
|
Initializing root retains its lifecycle unless it is reentered. Put a machine-wide
|
|
82
82
|
reset handler at root and use `reenter: true` to exit and enter root as well as
|
|
@@ -85,11 +85,11 @@ reentry, retained root work continues; it does not automatically acquire new
|
|
|
85
85
|
construction values. Ordinary transition conflict and lifecycle rules still apply.
|
|
86
86
|
This does not create a new machine reference or discard history records.
|
|
87
87
|
|
|
88
|
-
`{ update:
|
|
88
|
+
`{ update: { root: ... } }` instead replaces root's complete value,
|
|
89
89
|
retaining its active children and running work. It takes no input and does not
|
|
90
90
|
run initial constructors. Other source/ancestor updates follow the same rules.
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
### Inline and branching construction
|
|
93
93
|
|
|
94
94
|
Ordinary transitions stay inline:
|
|
95
95
|
|
|
@@ -102,32 +102,21 @@ Load: {
|
|
|
102
102
|
|
|
103
103
|
Targeting a compound or parallel state follows its initial declarations. Event
|
|
104
104
|
transitions use `target`, never `initial`. `initial` names only handler startup
|
|
105
|
-
edges. `history` restores a declared history state; `
|
|
105
|
+
edges. `history` restores a declared history state; `stay: true` accepts an
|
|
106
106
|
event without changing its target configuration.
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
explicit subtree
|
|
110
|
-
|
|
111
|
-
```ts
|
|
112
|
-
branches: {
|
|
113
|
-
open: {
|
|
114
|
-
checkout: { target: "Checkout" },
|
|
115
|
-
unchanged: { none: true }
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
The resolver calls a constructor bound to that declaration:
|
|
108
|
+
List `branches` next to `resolve` when a transition chooses an outcome or builds
|
|
109
|
+
an explicit subtree. The compiler checks one against the other in both directions:
|
|
121
110
|
|
|
122
111
|
```ts
|
|
123
112
|
Open: {
|
|
124
|
-
branches: "
|
|
113
|
+
branches: [{ target: "Checkout" }, { stay: true }],
|
|
125
114
|
resolve: ({ event, select }) => event.open
|
|
126
|
-
? select.
|
|
115
|
+
? select.target("Checkout", {
|
|
127
116
|
data: { cartId: event.cartId },
|
|
128
117
|
states: { Review: { data: { total: event.total } } }
|
|
129
118
|
})
|
|
130
|
-
: select.
|
|
119
|
+
: select.stay()
|
|
131
120
|
}
|
|
132
121
|
```
|
|
133
122
|
|
|
@@ -142,39 +131,38 @@ requires every region. Source-local parallel construction can select a single
|
|
|
142
131
|
region while retaining its active siblings, preserving the existing scope rules.
|
|
143
132
|
Names under `states` cannot collide with construction metadata.
|
|
144
133
|
|
|
145
|
-
|
|
134
|
+
Each branch's destination remains inspectable without executing `resolve`.
|
|
146
135
|
Dynamically selected descendants are visible after resolution; this API does not
|
|
147
136
|
add static analysis of resolver bodies.
|
|
148
137
|
|
|
149
|
-
|
|
138
|
+
### Retained-owner updates
|
|
150
139
|
|
|
151
|
-
An inline combined transition keeps
|
|
140
|
+
An inline combined transition keeps the target's value and the owner's value
|
|
141
|
+
separate, so `data` only ever means the target's value:
|
|
152
142
|
|
|
153
143
|
```ts
|
|
154
144
|
Save: {
|
|
155
145
|
target: "Checkout.Saving",
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
target: { requestId: event.requestId },
|
|
159
|
-
update: { cartId: event.cartId }
|
|
160
|
-
})
|
|
146
|
+
data: ({ event }) => ({ requestId: event.requestId }),
|
|
147
|
+
update: { Checkout: ({ event }) => ({ cartId: event.cartId }) }
|
|
161
148
|
}
|
|
162
149
|
```
|
|
163
150
|
|
|
164
|
-
|
|
151
|
+
A `branches` entry names the same pair, `{ target: "Checkout.Saving", update: "Checkout" }`,
|
|
152
|
+
and its call constructs both:
|
|
165
153
|
|
|
166
154
|
```ts
|
|
167
|
-
select.
|
|
155
|
+
select.target("Checkout.Saving", {
|
|
168
156
|
data: { requestId: event.requestId },
|
|
169
|
-
update: {
|
|
157
|
+
update: { Checkout: { cartId: event.cartId } }
|
|
170
158
|
})
|
|
171
159
|
```
|
|
172
160
|
|
|
173
|
-
`update`
|
|
161
|
+
`update` takes exactly one owner. It is not an
|
|
174
162
|
arbitrary nested-state operation. The owner must be active and retained across
|
|
175
163
|
the transition. Both complete values validate before either is committed.
|
|
176
164
|
|
|
177
|
-
|
|
165
|
+
### History and lifecycle
|
|
178
166
|
|
|
179
167
|
History fallback uses a bound function with the same node object format:
|
|
180
168
|
|
|
@@ -198,23 +186,7 @@ including required ancestors and parallel siblings. Its root data is state data,
|
|
|
198
186
|
not startup input. `Machine.resume` restores a validated snapshot without rerunning
|
|
199
187
|
startup constructors.
|
|
200
188
|
|
|
201
|
-
`guard` declines before construction. A resolver may return `decline()` only
|
|
202
|
-
`
|
|
189
|
+
`guard` declines before construction. A resolver may return `select.decline()` only
|
|
190
|
+
when `branches` lists `{ decline: true }`. Both exist only on `on` handlers and
|
|
191
|
+
`always`. Effect, Stream, timer, logic, child invocation, completion,
|
|
203
192
|
entry/exit, and required outcome channels retain their existing contracts.
|
|
204
|
-
|
|
205
|
-
## Migration
|
|
206
|
-
|
|
207
|
-
- Root initial constructors can read `input`; remove root fields used only to
|
|
208
|
-
forward that input to the initial child.
|
|
209
|
-
- Remove `Machine.targets(Root)`. Replace `targets.root.A.B` with `"A.B"`,
|
|
210
|
-
`update: targets.root` with `update: "root"`, and
|
|
211
|
-
`{ target: targets.root, input }` with `{ initialize: input }` (or
|
|
212
|
-
`{ initialize: true }` without machine input). Rename a top-level state named
|
|
213
|
-
`root`.
|
|
214
|
-
- Replace event `{ initial: path, data }` with `{ target: path, data }`.
|
|
215
|
-
- Replace `select.branch.from(value)` with `select.branch({ data: value })`.
|
|
216
|
-
- Replace `.decoded(value)` with `({ decoded: true, data: value })`.
|
|
217
|
-
- Replace child callbacks with `states: { Child: { data, states } }`.
|
|
218
|
-
- Replace `.update.from(value)` chaining with `update: { data: value }` inside
|
|
219
|
-
the same selector call.
|
|
220
|
-
- Replace history fallback chains with `target({ states: ... })`.
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
## Effect Atom and React
|
|
2
2
|
|
|
3
3
|
React code should own one stable machine atom, pass it through props or
|
|
4
4
|
Context, and subscribe in the descendants that render machine state. Keep the
|
|
5
5
|
machine definition free of React dependencies.
|
|
6
6
|
|
|
7
|
-
Read
|
|
7
|
+
Read [Effect Machine agent guide](#effect-machine-agent-guide) for statechart
|
|
8
8
|
modeling, transitions, services, and testing.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
### Recommended folder structure
|
|
11
11
|
|
|
12
12
|
```text
|
|
13
13
|
src/
|
|
@@ -20,7 +20,7 @@ src/
|
|
|
20
20
|
└── auth-machine.ts # States, events, and behavior
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
`machine.ts` owns the workflow. A Context module only creates and distributes
|
|
23
|
+
`auth-machine.ts` owns the workflow. A Context module only creates and distributes
|
|
24
24
|
the machine atom. State-slot components decide which state paths they render.
|
|
25
25
|
|
|
26
26
|
Bind service-backed machines once at the application runtime:
|
|
@@ -37,19 +37,17 @@ export const MachineAtoms = AtomMachine.bind(atomRuntime)
|
|
|
37
37
|
|
|
38
38
|
Service-free machines can use `AtomMachine.make` directly.
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
### Own a machine in one React subtree
|
|
41
41
|
|
|
42
42
|
Create a context from a factory. Each Provider owns a fresh bridge in its current
|
|
43
43
|
Atom registry; it does not subscribe to state changes.
|
|
44
44
|
|
|
45
45
|
```tsx
|
|
46
46
|
import { createMachineContext } from "@typeonce/effect-machine-react"
|
|
47
|
-
import { AuthMachine } from "../machines/auth-machine"
|
|
48
47
|
import { MachineAtoms } from "../lib/atom-runtime"
|
|
48
|
+
import { AuthMachine } from "../machines/auth-machine"
|
|
49
49
|
|
|
50
|
-
export const Auth = createMachineContext(({ input }: { input: AuthInput }) =>
|
|
51
|
-
MachineAtoms.make(AuthMachine, input)
|
|
52
|
-
)
|
|
50
|
+
export const Auth = createMachineContext(({ input }: { input: AuthInput }) => MachineAtoms.make(AuthMachine, input))
|
|
53
51
|
export const useAuthMachine = Auth.useMachine
|
|
54
52
|
|
|
55
53
|
function AuthRoute({ input, attemptId }: Props) {
|
|
@@ -75,7 +73,7 @@ machines, even when their inputs are equal.
|
|
|
75
73
|
`useMachineAtom(() => makeMachine(input))` remains available for custom owners.
|
|
76
74
|
It mounts the reference after commit without reading `result` or `snapshot`.
|
|
77
75
|
|
|
78
|
-
|
|
76
|
+
### Per-owner capabilities
|
|
79
77
|
|
|
80
78
|
A router, a callback owned by a component, or another per-instance capability
|
|
81
79
|
is a service, never a schema field or startup input. Declare the service, let a
|
|
@@ -84,8 +82,8 @@ React owner:
|
|
|
84
82
|
|
|
85
83
|
```tsx
|
|
86
84
|
import { Machine } from "@typeonce/effect-machine"
|
|
87
|
-
import { AtomMachine } from "@typeonce/effect-machine/reactivity"
|
|
88
85
|
import { useMachineAtom } from "@typeonce/effect-machine-react"
|
|
86
|
+
import { AtomMachine } from "@typeonce/effect-machine/reactivity"
|
|
89
87
|
import { Context, Effect, Layer } from "effect"
|
|
90
88
|
import { Atom } from "effect/reactivity"
|
|
91
89
|
import { AppLayer } from "../lib/app-layer"
|
|
@@ -100,8 +98,7 @@ export const OnboardingMachine = Machine.make({
|
|
|
100
98
|
}),
|
|
101
99
|
events: Machine.events({ Submit: {} }),
|
|
102
100
|
effects: {
|
|
103
|
-
navigateNext: Effect.flatMap(Navigation, (navigation) =>
|
|
104
|
-
navigation.push("/onboarding/next"))
|
|
101
|
+
navigateNext: Effect.flatMap(Navigation, (navigation) => navigation.push("/onboarding/next"))
|
|
105
102
|
}
|
|
106
103
|
}).handle({
|
|
107
104
|
initial: { target: "Editing" },
|
|
@@ -179,7 +176,7 @@ Provider's React `key` to create a new owner with a new layer. Binding still
|
|
|
179
176
|
checks the machine's requirements, so a layer type that omits `Navigation` is a
|
|
180
177
|
compile error.
|
|
181
178
|
|
|
182
|
-
|
|
179
|
+
### Render state-owned data
|
|
183
180
|
|
|
184
181
|
Render a typed path in the smallest component that needs it:
|
|
185
182
|
|
|
@@ -265,7 +262,7 @@ function AuthCard() {
|
|
|
265
262
|
Atom granularity cannot isolate hooks that all live in `AuthCard`. Any selected
|
|
266
263
|
change rerenders the component that called the hook.
|
|
267
264
|
|
|
268
|
-
|
|
265
|
+
### Send without subscribing
|
|
269
266
|
|
|
270
267
|
Use the writable atom directly:
|
|
271
268
|
|
|
@@ -302,15 +299,15 @@ Writes after the machine stopped or completed fail with `StoppedError`. The
|
|
|
302
299
|
same applies to `stop` and to child `send` and `stop`; a child command fails
|
|
303
300
|
with `ChildNotActiveError` when the state that owns the child is not active.
|
|
304
301
|
|
|
305
|
-
|
|
302
|
+
### Query concrete event acceptance
|
|
306
303
|
|
|
307
304
|
`AtomMachine.can` turns one concrete event input into a reusable machine
|
|
308
305
|
projection. Declare the projection once, then apply it to the React-owned
|
|
309
306
|
machine. Repeated applications to the same machine return the same atom:
|
|
310
307
|
|
|
311
308
|
```tsx
|
|
312
|
-
import { AtomMachine } from "@typeonce/effect-machine/reactivity"
|
|
313
309
|
import { useAtomSet, useAtomSuspense } from "@effect/atom-react"
|
|
310
|
+
import { AtomMachine } from "@typeonce/effect-machine/reactivity"
|
|
314
311
|
import { AuthEvents } from "../machines/auth-machine"
|
|
315
312
|
|
|
316
313
|
const submitAllowed = AtomMachine.can(AuthEvents.Submitted())
|
|
@@ -340,8 +337,7 @@ When acceptance depends on a changing payload, project an event atom instead:
|
|
|
340
337
|
```ts
|
|
341
338
|
import { Atom } from "effect/reactivity"
|
|
342
339
|
|
|
343
|
-
const submitEvent = Atom.map(draftAtom, (draft) =>
|
|
344
|
-
AuthEvents.Submitted({ draft }))
|
|
340
|
+
const submitEvent = Atom.map(draftAtom, (draft) => AuthEvents.Submitted({ draft }))
|
|
345
341
|
|
|
346
342
|
const submitAllowed = AtomMachine.can(submitEvent)
|
|
347
343
|
```
|
|
@@ -350,39 +346,36 @@ Changes to `draftAtom` recompute acceptance. The event atom contains the event
|
|
|
350
346
|
input itself rather than an `AsyncResult`.
|
|
351
347
|
|
|
352
348
|
`can` is `true` whenever the active configuration selects a transition. A
|
|
353
|
-
`{
|
|
354
|
-
changing state. A resolver that falls back to
|
|
355
|
-
|
|
349
|
+
`{ stay: true }` branch is a selected transition: it accepts the event without
|
|
350
|
+
changing state. A resolver that falls back to `stay` therefore keeps the button
|
|
351
|
+
enabled:
|
|
356
352
|
|
|
357
353
|
```ts
|
|
358
354
|
// Before: an incomplete form still accepts Submitted, so `can` is true.
|
|
359
|
-
// In make: branches: { submit: { review: { target: "Reviewing" }, incomplete: { none: true } } }
|
|
360
355
|
Submitted: {
|
|
361
|
-
branches: "
|
|
356
|
+
branches: [{ target: "Reviewing" }, { stay: true }],
|
|
362
357
|
resolve: ({ state, select }) =>
|
|
363
|
-
state.email.length > 0 ? select.
|
|
358
|
+
state.email.length > 0 ? select.target("Reviewing") : select.stay()
|
|
364
359
|
}
|
|
365
360
|
```
|
|
366
361
|
|
|
367
362
|
When the event must be refused, decline it. Use `guard` for an ordinary
|
|
368
|
-
transition, or `
|
|
363
|
+
transition, or `{ decline: true }` with `select.decline()` in a resolver:
|
|
369
364
|
|
|
370
365
|
```ts
|
|
371
366
|
// After: an incomplete form declines Submitted, so `can` is false.
|
|
372
367
|
Submitted: { target: "Reviewing", guard: ({ state }) => state.email.length > 0 }
|
|
373
368
|
|
|
374
369
|
// A resolver that needs commands or construction declines explicitly.
|
|
375
|
-
// In make: branches: { submit: { review: { target: "Reviewing" } } }
|
|
376
370
|
Submitted: {
|
|
377
|
-
branches: "
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
state.email.length > 0 ? select.review() : decline()
|
|
371
|
+
branches: [{ target: "Reviewing" }, { decline: true }],
|
|
372
|
+
resolve: ({ state, select }) =>
|
|
373
|
+
state.email.length > 0 ? select.target("Reviewing") : select.decline()
|
|
381
374
|
}
|
|
382
375
|
```
|
|
383
376
|
|
|
384
|
-
Keep `
|
|
385
|
-
`effect-machine/no-conditional-
|
|
377
|
+
Keep `stay` for events that are deliberately accepted and ignored. The
|
|
378
|
+
`effect-machine/no-conditional-stay-branch` lint rule reports the conditional
|
|
386
379
|
fallback.
|
|
387
380
|
|
|
388
381
|
Query acceptance during render through `AtomMachine.can(event)(machine)`. Do
|
|
@@ -390,7 +383,7 @@ not call `Effect.runSync(Machine.can(...))` in a component: the atom shares the
|
|
|
390
383
|
running machine's snapshot, suspends during startup, and recomputes when the
|
|
391
384
|
snapshot changes.
|
|
392
385
|
|
|
393
|
-
|
|
386
|
+
### Whole-result and custom selections
|
|
394
387
|
|
|
395
388
|
Reading the full result is correct when a component renders the complete
|
|
396
389
|
machine state:
|
|
@@ -440,7 +433,7 @@ function EmailField() {
|
|
|
440
433
|
The component rerenders only when the projected email changes, not when other
|
|
441
434
|
`Editing` fields change. Do not create a fresh derived atom on every render.
|
|
442
435
|
|
|
443
|
-
|
|
436
|
+
### Share a keyed machine outside one React owner
|
|
444
437
|
|
|
445
438
|
`AtomMachine.family` is for registry-owned machines that unrelated consumers
|
|
446
439
|
find by startup input. It is not the default for one React-owned workflow.
|
|
@@ -470,7 +463,7 @@ Family keys use Effect `Equal` and `Hash` semantics. Keep them immutable. If a
|
|
|
470
463
|
changing value should update one running workflow, model it as an event instead
|
|
471
464
|
of changing the family key.
|
|
472
465
|
|
|
473
|
-
|
|
466
|
+
### Module-owned machines
|
|
474
467
|
|
|
475
468
|
A no-input machine may intentionally have one module-owned identity:
|
|
476
469
|
|
|
@@ -482,7 +475,7 @@ export const CounterStateAtom = AtomMachine.select(CounterMachineAtom, "Count")
|
|
|
482
475
|
Every consumer using the same `AtomRegistry` reaches the same running machine.
|
|
483
476
|
Different registries still run independent instances.
|
|
484
477
|
|
|
485
|
-
|
|
478
|
+
### Child machines
|
|
486
479
|
|
|
487
480
|
Direct child selectors follow the active child and preserve inactivity:
|
|
488
481
|
|
|
@@ -513,7 +506,7 @@ export const plantAtoms = AtomMachine.familyChild(CentralMachineAtom, {
|
|
|
513
506
|
})
|
|
514
507
|
```
|
|
515
508
|
|
|
516
|
-
|
|
509
|
+
### Generic helpers
|
|
517
510
|
|
|
518
511
|
Use `AtomMachine.MachineAtom.Any` and `AtomMachine.ChildMachineAtom.Any` to
|
|
519
512
|
accept any bridge, including bridges for machines without events. Recover the
|
|
@@ -534,7 +527,7 @@ type EditorState = AtomMachine.ChildMachineAtom.State<
|
|
|
534
527
|
>
|
|
535
528
|
```
|
|
536
529
|
|
|
537
|
-
|
|
530
|
+
### Keep workflow effects out of emissions
|
|
538
531
|
|
|
539
532
|
Emissions are ephemeral notifications for observers. They are not replayed, so
|
|
540
533
|
a subscriber that mounts late never sees an earlier emission. Do not drive work
|
|
@@ -552,7 +545,7 @@ An emission that only feeds UI feedback has two stream-atom details:
|
|
|
552
545
|
Pass `{ immediate: true }` so a lazy emissions atom starts; without it the
|
|
553
546
|
callback never runs. The callback then also receives the initial result.
|
|
554
547
|
|
|
555
|
-
|
|
548
|
+
### Registry and rendering semantics
|
|
556
549
|
|
|
557
550
|
A `MachineAtom` identifies one machine per `AtomRegistry`. Passing the same
|
|
558
551
|
machine atom through two registry providers creates two independent runtimes.
|
|
@@ -566,14 +559,14 @@ either. Every bridge atom carries an Effect Atom server value, which
|
|
|
566
559
|
`@effect/atom-react` hooks read during server rendering and hydration, and
|
|
567
560
|
`Atom.getServerValue` reads directly:
|
|
568
561
|
|
|
569
|
-
| Atom
|
|
570
|
-
|
|
|
571
|
-
| `ref`, `send`, `stop`
|
|
572
|
-
| `snapshot`
|
|
573
|
-
| `result`
|
|
574
|
-
| `select`, `selectSnapshot`, `matches`, `can`
|
|
575
|
-
| `child(Child).result` and child projections
|
|
576
|
-
| `family` and `familyChild` projections
|
|
562
|
+
| Atom | Server value |
|
|
563
|
+
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
|
|
564
|
+
| `ref`, `send`, `stop` | `Initial` |
|
|
565
|
+
| `snapshot` | `Success({ status: "active", state })`, waiting |
|
|
566
|
+
| `result` | `Success(state)`, waiting |
|
|
567
|
+
| `select`, `selectSnapshot`, `matches`, `can` | the projection of `state`, waiting |
|
|
568
|
+
| `child(Child).result` and child projections | `Success(Option.some(childState))` when the planned state invokes `Child`, otherwise `Success(Option.none())` |
|
|
569
|
+
| `family` and `familyChild` projections | the server value of the projected bridge atom |
|
|
577
570
|
|
|
578
571
|
`state` is the machine's planned startup state: the state `Machine.planInitial`
|
|
579
572
|
settles into with the bridge's input, without running startup commands,
|
|
@@ -583,7 +576,7 @@ startup state cannot be planned synchronously, for example because the input
|
|
|
583
576
|
is invalid, every server value is `Initial`. Each bridge plans at most once,
|
|
584
577
|
on its first server read.
|
|
585
578
|
|
|
586
|
-
|
|
579
|
+
### Server rendering and hydration
|
|
587
580
|
|
|
588
581
|
Render state slots with `MachineState` and no Suspense boundary:
|
|
589
582
|
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
## Effect Machine implementation review
|
|
2
2
|
|
|
3
3
|
Use this review after a machine and its UI adapter have been implemented. Look
|
|
4
4
|
for modeling mistakes, redundant API usage, and logic that escaped the machine.
|
|
5
5
|
Do not force a change when the current ownership is intentional.
|
|
6
6
|
|
|
7
|
-
Read
|
|
8
|
-
and [Effect Atom and React
|
|
7
|
+
Read [Effect Machine agent guide](#effect-machine-agent-guide) for statechart
|
|
8
|
+
modeling and [Effect Atom and React](#effect-atom-and-react) for integration
|
|
9
9
|
patterns.
|
|
10
10
|
|
|
11
11
|
Run the recommended rules from
|
|
12
|
-
|
|
12
|
+
`@typeonce/oxlint-plugin-effect-machine` before
|
|
13
13
|
the manual review. They catch provable duplicate invocation identities,
|
|
14
|
-
asynchronous work, browser access, nondeterminism,
|
|
15
|
-
|
|
14
|
+
asynchronous work, browser access, nondeterminism, conditional `stay`
|
|
15
|
+
fallbacks, and one-use intermediate definitions in direct same-module machine
|
|
16
|
+
definitions.
|
|
16
17
|
Continue with this review for cross-module ownership and architectural
|
|
17
18
|
questions that syntax alone cannot answer.
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
### Review the responsibility boundaries
|
|
20
21
|
|
|
21
22
|
Use this split when deciding where code belongs:
|
|
22
23
|
|
|
23
|
-
| Layer
|
|
24
|
-
|
|
|
25
|
-
| Machine
|
|
26
|
-
| Atoms
|
|
27
|
-
| Component
|
|
28
|
-
| `RegistryProvider` | An optional Atom registry ownership boundary
|
|
24
|
+
| Layer | Owns |
|
|
25
|
+
| ------------------ | ------------------------------------------------------------------------------- |
|
|
26
|
+
| Machine | State, transitions, workflow decisions, effect lifetimes, and service execution |
|
|
27
|
+
| Atoms | Reactive selection, projections, and keyed machine lookup |
|
|
28
|
+
| Component | Rendering and translating user interaction into domain events |
|
|
29
|
+
| `RegistryProvider` | An optional Atom registry ownership boundary |
|
|
29
30
|
|
|
30
31
|
If a component coordinates a workflow, an atom performs business work, or a
|
|
31
32
|
provider has no ownership purpose, inspect that code more closely.
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
### Keep ordinary transitions inline
|
|
34
35
|
|
|
35
36
|
Use `{ target: "Running" }` for a state with default construction.
|
|
36
37
|
Use `{ target: "Running", data: ({ event }) => ({ count: event.count }) }`
|
|
37
38
|
when the destination needs data. The checker rejects missing required fields.
|
|
38
39
|
Use `decoded` for values already decoded by their schema.
|
|
39
40
|
|
|
40
|
-
Reserve
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
A `{ none: true }` branch accepts its event, so `Machine.can` and
|
|
41
|
+
Reserve `branches` and `resolve` for conditional outcomes or nested
|
|
42
|
+
construction. List them on the transition itself; the resolver can select only
|
|
43
|
+
the entries listed next to it. Use `commands` to enqueue work on an ordinary
|
|
44
|
+
transition instead of adding a resolver. Use `reenter: true` when the source
|
|
45
|
+
must restart.
|
|
46
|
+
A `{ stay: true }` branch accepts its event, so `Machine.can` and
|
|
47
47
|
`AtomMachine.can` return `true` when a resolver selects it. Review check: for
|
|
48
48
|
every event that enables or disables a control, refuse it with `guard` or with
|
|
49
|
-
`
|
|
49
|
+
`{ decline: true }` and `select.decline()`. Keep `stay` only for events that are
|
|
50
50
|
deliberately accepted and ignored.
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
### Choose React ownership or keyed family lookup
|
|
53
53
|
|
|
54
54
|
Use `useMachineAtom` when one React subtree owns the workflow, including a
|
|
55
55
|
machine with startup input:
|
|
@@ -108,7 +108,7 @@ subtree, and component-local calls to `machineAtoms.make` without a stable
|
|
|
108
108
|
owner. Choose `AtomMachine.family` only when consumers need shared keyed
|
|
109
109
|
lookup.
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
### Justify each `RegistryProvider`
|
|
112
112
|
|
|
113
113
|
Effect Atom hooks use a shared default registry when no provider is present.
|
|
114
114
|
Do not add `RegistryProvider` automatically.
|
|
@@ -139,7 +139,7 @@ can split a machine that consumers expected to share.
|
|
|
139
139
|
Review check: for every `RegistryProvider`, state which registry boundary it
|
|
140
140
|
creates and why. Remove it when there is no deliberate boundary.
|
|
141
141
|
|
|
142
|
-
|
|
142
|
+
### Keep workflow logic in the machine
|
|
143
143
|
|
|
144
144
|
The machine owns business flow. Components render selected state and send
|
|
145
145
|
domain events. Atoms adapt machine state for reactive consumers. Neither layer
|
|
@@ -161,21 +161,23 @@ and its lifetime:
|
|
|
161
161
|
|
|
162
162
|
```ts
|
|
163
163
|
// In make: effects: { submitOrder }
|
|
164
|
-
machine.handle({
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
164
|
+
machine.handle({
|
|
165
|
+
states: {
|
|
166
|
+
Editing: {
|
|
167
|
+
on: {
|
|
168
|
+
Submit: { target: "Submitting", data: ({ event }) => ({ order: event.order }) }
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
Submitting: {
|
|
172
|
+
invoke: {
|
|
173
|
+
src: "submitOrder",
|
|
174
|
+
input: ({ state }) => state.order,
|
|
175
|
+
onDone: { target: "Complete", data: ({ output }) => ({ order: output }) },
|
|
176
|
+
onFailure: { target: "Failed", data: ({ error }) => ({ message: String(error) }) }
|
|
177
|
+
}
|
|
176
178
|
}
|
|
177
179
|
}
|
|
178
|
-
}
|
|
180
|
+
})
|
|
179
181
|
```
|
|
180
182
|
|
|
181
183
|
`submitOrder` can use Effect services for the API request and analytics. The
|
|
@@ -198,10 +200,10 @@ services provided by the React owner, not schema fields or startup input. A
|
|
|
198
200
|
function in state data or input cannot be encoded by `Machine.encodeSnapshot`.
|
|
199
201
|
Review check: search schemas for `Schema.declare` guards that accept functions
|
|
200
202
|
and move each one to a service, as in
|
|
201
|
-
[Per-owner capabilities](
|
|
203
|
+
[Per-owner capabilities](#per-owner-capabilities). Do not
|
|
202
204
|
drive navigation or other workflow effects from emissions.
|
|
203
205
|
|
|
204
|
-
|
|
206
|
+
### Report findings
|
|
205
207
|
|
|
206
208
|
For each issue, cite the file and line, name the violated boundary, and show the
|
|
207
209
|
smallest correction. Distinguish a confirmed problem from a provider or
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Effect Machine documentation
|
|
2
|
+
|
|
3
|
+
This documentation covers `@typeonce/effect-machine` and its related packages.
|
|
4
|
+
It is generated for the installed version, so prefer it over any other copy.
|
|
5
|
+
|
|
6
|
+
**Note**: The examples contain comments for illustration. In practice, you would
|
|
7
|
+
not include them in your code.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
+
"extends": "../tsconfig.base.json",
|
|
4
|
+
"include": ["src/**/*.ts"],
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"composite": false,
|
|
7
|
+
"incremental": false,
|
|
8
|
+
"rootDir": "..",
|
|
9
|
+
"noEmit": true,
|
|
10
|
+
"declaration": false,
|
|
11
|
+
"declarationMap": false,
|
|
12
|
+
"sourceMap": false,
|
|
13
|
+
"paths": {
|
|
14
|
+
"@typeonce/effect-machine": ["../packages/effect-machine/src/index.ts"],
|
|
15
|
+
"@typeonce/effect-machine/testing": ["../packages/effect-machine/src/testing/index.ts"],
|
|
16
|
+
"@typeonce/effect-machine/reactivity": ["../packages/effect-machine/src/unstable/reactivity/index.ts"],
|
|
17
|
+
"@typeonce/effect-machine/cluster": ["../packages/effect-machine/src/unstable/cluster/index.ts"],
|
|
18
|
+
"effect": ["../packages/effect-machine/node_modules/effect/dist/index.d.ts"],
|
|
19
|
+
"effect/*": ["../packages/effect-machine/node_modules/effect/dist/*.d.ts"]
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|