@typeonce/effect-machine 0.33.0 → 0.34.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/README.md +216 -636
- package/dist/Machine.d.ts +815 -583
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +20 -8
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/childRegistry.d.ts +1 -1
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/cluster.d.ts +2 -50
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +4 -27
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterProtocol.d.ts +35 -0
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -0
- package/dist/internal/machine/clusterProtocol.js +28 -0
- package/dist/internal/machine/clusterProtocol.js.map +1 -0
- package/dist/internal/machine/commandRuntime.d.ts +1 -1
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/configuration.d.ts +2 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -45
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +17 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -0
- package/dist/internal/machine/declaration.js +120 -0
- package/dist/internal/machine/declaration.js.map +1 -0
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +5 -4
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +34 -0
- package/dist/internal/machine/implementation.d.ts.map +1 -0
- package/dist/internal/machine/implementation.js +3 -0
- package/dist/internal/machine/implementation.js.map +1 -0
- package/dist/internal/machine/invocation.d.ts +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +1 -1
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
- package/dist/internal/machine/machine.d.ts +13 -2
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +140 -278
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +12 -11
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +8 -7
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +16 -12
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +2 -230
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +8 -1857
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
- package/dist/internal/machine/runtimeCompiled.js +867 -0
- package/dist/internal/machine/runtimeCompiled.js.map +1 -0
- package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
- package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
- package/dist/internal/machine/runtimeGeneric.js +471 -0
- package/dist/internal/machine/runtimeGeneric.js.map +1 -0
- package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
- package/dist/internal/machine/runtimeProtocol.js +561 -0
- package/dist/internal/machine/runtimeProtocol.js.map +1 -0
- package/dist/internal/machine/targetReference.d.ts +15 -0
- package/dist/internal/machine/targetReference.d.ts.map +1 -0
- package/dist/internal/machine/targetReference.js +31 -0
- package/dist/internal/machine/targetReference.js.map +1 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +2 -1
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +38 -40
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +5 -5
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +5 -17
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/docs/agent-guide.md +66 -91
- package/docs/machine-review.md +19 -49
- package/docs/root-api.md +62 -62
- package/package.json +1 -1
- package/src/Machine.ts +1514 -2312
- package/src/internal/machine/childRegistry.ts +1 -1
- package/src/internal/machine/cluster.ts +7 -73
- package/src/internal/machine/clusterProtocol.ts +56 -0
- package/src/internal/machine/commandRuntime.ts +1 -1
- package/src/internal/machine/configuration.ts +53 -45
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +6 -5
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +199 -409
- package/src/internal/machine/planner.ts +54 -35
- package/src/internal/machine/process.ts +40 -33
- package/src/internal/machine/runtime.ts +20 -3037
- package/src/internal/machine/runtimeCompiled.ts +1166 -0
- package/src/internal/machine/runtimeGeneric.ts +799 -0
- package/src/internal/machine/runtimeProtocol.ts +1213 -0
- package/src/internal/machine/targetReference.ts +40 -0
- package/src/internal/machine/topology.ts +2 -1
- package/src/internal/testing/machine/finiteModel.ts +42 -43
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/unstable/cluster/ClusterMachine.ts +9 -17
package/README.md
CHANGED
|
@@ -42,25 +42,25 @@ in lockstep with this package.
|
|
|
42
42
|
|
|
43
43
|
## Quick start
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
Declare the root once, derive its references, then add behavior with event maps:
|
|
46
46
|
|
|
47
47
|
```ts
|
|
48
48
|
import { Machine } from "@typeonce/effect-machine"
|
|
49
49
|
import { Effect, Schema } from "effect"
|
|
50
50
|
|
|
51
51
|
const Root = Machine.state({ fields: { count: Schema.Number } })
|
|
52
|
+
const targets = Machine.targets(Root)
|
|
52
53
|
const Events = Machine.events({ Increment: { by: Schema.Number } })
|
|
53
|
-
const
|
|
54
|
+
const Counter = Machine.make({
|
|
54
55
|
root: Root,
|
|
55
56
|
events: Events,
|
|
56
57
|
initial: (root) => root.from(() => ({ count: 0 }))
|
|
57
|
-
})
|
|
58
|
-
const Counter = CounterDefinition.handle({
|
|
58
|
+
}).handle({
|
|
59
59
|
on: {
|
|
60
|
-
Increment:
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
Increment: {
|
|
61
|
+
update: targets.root,
|
|
62
|
+
from: ({ root, event }) => ({ count: root.count + event.by })
|
|
63
|
+
}
|
|
64
64
|
}
|
|
65
65
|
})
|
|
66
66
|
|
|
@@ -70,688 +70,267 @@ const program = Effect.scoped(Effect.gen(function*() {
|
|
|
70
70
|
}))
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
-
`
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
survives transitions between those children.
|
|
77
|
-
|
|
78
|
-
See [the root API guide](./docs/root-api.md) for topology-only machines,
|
|
79
|
-
initialization, reusable schemas, guards, migration, and observation contracts.
|
|
80
|
-
|
|
81
|
-
`handle` creates a complete implementation boundary. Its result does not
|
|
82
|
-
expose `handle`, so all behavior for one machine belongs in the same handler
|
|
83
|
-
tree. Reuse the definition when multiple independent implementations are
|
|
84
|
-
useful, such as production and testing variants:
|
|
85
|
-
|
|
86
|
-
```ts
|
|
87
|
-
const ProductionCounter = CounterDefinition.handle(productionHandlers)
|
|
88
|
-
const TestingCounter = CounterDefinition.handle(testingHandlers)
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
`Machine.start` returns a `MachineRef` with `send`, `state`, `snapshot`,
|
|
92
|
-
`changes`, `emissions`, `join`, and `stop`. Sending enqueues an event; observe
|
|
93
|
-
`changes` or use the testing probe when work must be causally acknowledged.
|
|
73
|
+
Root data is shared state. `update: targets.root` replaces that value while
|
|
74
|
+
retaining the active child configuration and its running work. Ordinary
|
|
75
|
+
transitions target a descendant; the root itself is not a destination.
|
|
94
76
|
|
|
95
77
|
## Modeling workflow
|
|
96
78
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
3. Declare field records with `Machine.events`, `Machine.internalEvents`, and
|
|
102
|
-
`Machine.emittedEvents`. Import existing tagged unions or tagged classes with
|
|
103
|
-
the corresponding `eventsFromSchemas`, `internalEventsFromSchemas`, or
|
|
104
|
-
`emittedEventsFromSchemas` constructor.
|
|
105
|
-
4. Pass the root descriptor to `Machine.make({ root, events })`. Supply root
|
|
106
|
-
values with `initial`; declared child defaults determine startup topology.
|
|
107
|
-
5. Implement root behavior directly in `handle`, with child behavior in its
|
|
108
|
-
`states` property. Add runtime, Atom, testing, or Cluster adapters at the
|
|
109
|
-
application boundary.
|
|
110
|
-
|
|
111
|
-
The root has path `""`; children retain paths such as `Editing` and
|
|
112
|
-
`Editing.Form`. Use `Root.path(...)` to validate path literals. Type full
|
|
113
|
-
snapshots as `Machine.Snapshot<typeof Root>` or `Machine.Snapshot<typeof machine>`,
|
|
114
|
-
state values as `Machine.Value<typeof Root, Path>`, and selected snapshots as
|
|
115
|
-
`Machine.SnapshotAt<typeof Root, Path>`.
|
|
116
|
-
|
|
117
|
-
### Make invalid states unrepresentable
|
|
118
|
-
|
|
119
|
-
Treat topology as a domain contract, not as file organization. A parallel state
|
|
120
|
-
declares the full Cartesian product of its regions, so use it only when every
|
|
121
|
-
combination has a coherent meaning. If one region must inspect another before
|
|
122
|
-
entering a state safely, prefer a compound hierarchy that makes the forbidden
|
|
123
|
-
combination impossible. `matches` remains useful for views, tests, and genuine
|
|
124
|
-
coordination between independent regions; it should not repair an invalid
|
|
125
|
-
state product.
|
|
126
|
-
|
|
127
|
-
Keep state-scoped Effects beneath the state that guarantees their resources,
|
|
128
|
-
and enforce command availability in the machine rather than only by disabling
|
|
129
|
-
UI controls. When entering an inactive compound or parallel state's declared
|
|
130
|
-
default, select `.initial`; explicitly construct descendants only for a
|
|
131
|
-
non-default configuration or a complete replacement of an already-active
|
|
132
|
-
parallel root.
|
|
133
|
-
|
|
134
|
-
### Construct state through builders
|
|
135
|
-
|
|
136
|
-
Use `.from(...)` when constructing a new state from schema make input:
|
|
137
|
-
|
|
138
|
-
```ts
|
|
139
|
-
target.from({ draft: event.draft })
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
The machine runs these inputs through the state schema while planning. Schema
|
|
143
|
-
defaults, transformations, refinements, and tagged-class identity are
|
|
144
|
-
therefore preserved, and decode failures remain typed machine failures. This
|
|
145
|
-
is the default construction path.
|
|
146
|
-
|
|
147
|
-
Use `.decoded(...)` when the value is already a `Schema.Type`:
|
|
148
|
-
|
|
149
|
-
```ts
|
|
150
|
-
target.decoded(new Ready({ notice: null }))
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
The machine still validates the value against the schema's type side. It does
|
|
154
|
-
not run encoded-input transformations again. State builders are not callable;
|
|
155
|
-
the method name always makes the construction mode visible.
|
|
79
|
+
Put data on the lowest state that owns it. Use tagged schemas or `fields` for
|
|
80
|
+
state-owned values and `{}` for structural states. Prefer distinct states over
|
|
81
|
+
boolean flags that allow impossible combinations. Put shared behavior on the
|
|
82
|
+
lowest common ancestor, and reserve parallel states for independent modes.
|
|
156
83
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const handlers = {
|
|
162
|
-
Submit: (to) =>
|
|
163
|
-
to.local.Saving().resolve(({ state, target }) => {
|
|
164
|
-
const { _tag: _, ...fields } = state
|
|
165
|
-
return target.from({ ...fields, attempt: 1 })
|
|
166
|
-
})
|
|
167
|
-
}
|
|
168
|
-
```
|
|
84
|
+
`Machine.state` declares topology. `Machine.make` captures protocols, startup
|
|
85
|
+
input, sources, and named branch groups. `.handle` implements behavior in one
|
|
86
|
+
nested object and returns an executable machine. One definition can create
|
|
87
|
+
independent implementations by calling `.handle` more than once.
|
|
169
88
|
|
|
170
|
-
|
|
171
|
-
instead of defining an empty tagged schema:
|
|
89
|
+
### Construct state values
|
|
172
90
|
|
|
173
91
|
```ts
|
|
174
|
-
const
|
|
175
|
-
initial: "
|
|
92
|
+
const Root = Machine.state({
|
|
93
|
+
initial: "Idle",
|
|
176
94
|
states: {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
Saving
|
|
182
|
-
}
|
|
183
|
-
}
|
|
95
|
+
Idle: {},
|
|
96
|
+
Loading: { fields: { query: Schema.String } },
|
|
97
|
+
Ready: { fields: { items: Schema.Array(Schema.String) } },
|
|
98
|
+
Failed: { fields: { message: Schema.String } }
|
|
184
99
|
}
|
|
185
100
|
})
|
|
101
|
+
const targets = Machine.targets(Root)
|
|
102
|
+
const Events = Machine.events({ Search: { query: Schema.String }, Reset: {} })
|
|
186
103
|
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
104
|
+
const Search = Machine.make({ root: Root, events: Events }).handle({
|
|
105
|
+
states: {
|
|
106
|
+
Idle: {
|
|
107
|
+
on: {
|
|
108
|
+
Search: {
|
|
109
|
+
target: targets.root.Loading,
|
|
110
|
+
from: ({ event }) => ({ query: event.query })
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
Loading: { on: { Reset: { target: targets.root.Idle } } }
|
|
115
|
+
}
|
|
192
116
|
})
|
|
193
117
|
```
|
|
194
118
|
|
|
195
|
-
|
|
196
|
-
`
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
Keep data-bearing state schemas together in a named `Schema.TaggedUnion` and
|
|
201
|
-
reference its cases from the topology. For a standalone state schema whose
|
|
202
|
-
class identity is useful, declare a named `Schema.TaggedClass`. Do not bury
|
|
203
|
-
inline `fields` in a `Machine.state` descriptor.
|
|
119
|
+
`from` accepts the schema's make input, including defaults and transformations.
|
|
120
|
+
`decoded` accepts its decoded value, preserving class instances. The planner
|
|
121
|
+
validates both boundaries and reports `MachineSchemaDecodeError`. Omit construction
|
|
122
|
+
only when the selected builder supports empty default construction.
|
|
204
123
|
|
|
205
|
-
|
|
206
|
-
|
|
124
|
+
A callback receives the specific event and source-state types, plus `root`,
|
|
125
|
+
`containingState`, `ancestors`, and the appropriate machine references. A
|
|
126
|
+
transition callback also receives the current logical `snapshot`. There is no
|
|
127
|
+
unrestricted destination builder in the callback.
|
|
207
128
|
|
|
208
|
-
###
|
|
129
|
+
### Declare advanced and conditional transitions
|
|
209
130
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
131
|
+
A resolver that chooses a branch, constructs nested children, or enqueues
|
|
132
|
+
commands uses a named group in `make`. The group captures every possible edge
|
|
133
|
+
before any resolver executes:
|
|
213
134
|
|
|
214
135
|
```ts
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
)
|
|
225
|
-
const Emissions = Machine.emittedEventsFromSchemas(
|
|
226
|
-
Schema.TaggedUnion({
|
|
227
|
-
SaveObserved: { id: Schema.String }
|
|
228
|
-
})
|
|
229
|
-
)
|
|
230
|
-
|
|
231
|
-
const definition = Machine.make({
|
|
232
|
-
root: States,
|
|
233
|
-
events: CommandEvent,
|
|
234
|
-
internalEvents: InternalEvent,
|
|
235
|
-
emittedEvents: Emissions,
|
|
236
|
-
initial: (to) => to.Idle()
|
|
237
|
-
})
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
Handlers see both protocols. Typed `send`, `Machine.can`, and `Machine.plan`
|
|
241
|
-
accept only public events. Event tags must be unique and public/internal tags
|
|
242
|
-
must be disjoint.
|
|
243
|
-
|
|
244
|
-
Export the descriptor returned by `Machine.events` instead of exporting its
|
|
245
|
-
schemas. This keeps the deferred constructors as the standard way to create
|
|
246
|
-
events without exposing schema `.make` methods:
|
|
247
|
-
|
|
248
|
-
```ts
|
|
249
|
-
ref.send(CommandEvent.Save())
|
|
250
|
-
enqueue.raise(InternalEvent.Saved({ id: "entry-1" }))
|
|
251
|
-
enqueue.emit(Emissions.SaveObserved({ id: "entry-1" }))
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
The returned constructors preserve each schema's make input, including required
|
|
255
|
-
fields and constructor defaults. They defer schema construction until delivery,
|
|
256
|
-
so invalid values fail planning or the running machine with
|
|
257
|
-
`MachineSchemaDecodeError` instead of throwing at the call site.
|
|
258
|
-
Schemas with an open discriminator such as `_tag: Schema.String` remain valid
|
|
259
|
-
protocols but cannot expose a finite constructor set; pass a complete event
|
|
260
|
-
object to `send` or `Machine.plan` for those events.
|
|
261
|
-
|
|
262
|
-
`ref.emissions` is a hot `Stream`: it publishes only notifications produced
|
|
263
|
-
after subscription, replays nothing, and completes when the machine terminates.
|
|
264
|
-
Snapshots remain separate and stateful: `ref.changes` begins with the current
|
|
265
|
-
lifecycle snapshot and then follows later changes. Use `Machine.prepare` when
|
|
266
|
-
an observer must be installed before initial-entry actions run:
|
|
267
|
-
|
|
268
|
-
```ts
|
|
269
|
-
const prepared = yield * Machine.prepare(machine)
|
|
270
|
-
|
|
271
|
-
yield * prepared.emissions.pipe(
|
|
272
|
-
Stream.runForEach(handleEmission),
|
|
273
|
-
Effect.forkScoped({ startImmediately: true })
|
|
274
|
-
)
|
|
275
|
-
|
|
276
|
-
const ref = yield * prepared.start
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
`Machine.start(machine)` remains the one-step convenience for callers that do
|
|
280
|
-
not observe startup emissions. Preparation does not retain or replay an
|
|
281
|
-
emission: the observer is simply subscribed before initialization begins.
|
|
282
|
-
|
|
283
|
-
### Inspect a live machine tree
|
|
284
|
-
|
|
285
|
-
`Machine.prepare(machine).inspection` is the operational counterpart to the
|
|
286
|
-
domain-facing `changes` and `emissions` streams. It observes the prepared root
|
|
287
|
-
and every locally owned child, `Logic` process, Effect, and timer in one total
|
|
288
|
-
publication order:
|
|
289
|
-
|
|
290
|
-
```ts
|
|
291
|
-
const prepared = yield * Machine.prepare(checkout)
|
|
292
|
-
|
|
293
|
-
yield * prepared.inspection.pipe(
|
|
294
|
-
Stream.runForEach((record) => Console.log(record.sequence, record.subject.id, record._tag)),
|
|
295
|
-
Effect.forkScoped({ startImmediately: true })
|
|
296
|
-
)
|
|
297
|
-
|
|
298
|
-
const checkoutRef = yield * prepared.start
|
|
299
|
-
```
|
|
300
|
-
|
|
301
|
-
For a handled input, the stream may expose values such as:
|
|
302
|
-
|
|
303
|
-
```ts
|
|
304
|
-
{ _tag: "EventSent", sequence: 2, deliveryId: 0,
|
|
305
|
-
subject: { id: "checkout", sessionId: "machine:0", kind: "Machine" },
|
|
306
|
-
source: undefined, target: { id: "checkout", sessionId: "machine:0" },
|
|
307
|
-
event: CheckoutEvents.Submit(), causedBy: undefined }
|
|
308
|
-
|
|
309
|
-
{ _tag: "EventProcessed", sequence: 4, macrostepId: 0,
|
|
310
|
-
deliveryId: 0, handled: true, configurationChanged: true,
|
|
311
|
-
before: { status: "active", state: /* ... */ },
|
|
312
|
-
after: { status: "active", state: /* ... */ }, microsteps: [/* ... */] }
|
|
313
|
-
```
|
|
314
|
-
|
|
315
|
-
The closed `Machine.Inspection.Event` union also reports creation,
|
|
316
|
-
initialization and startup failure, direct `Logic` state updates, outward
|
|
317
|
-
emissions, Effect/timer activity lifecycles, and termination. Records erase
|
|
318
|
-
unrelated child protocols to `unknown`; application-level observation remains
|
|
319
|
-
typed through each reference's `changes` and `emissions`.
|
|
320
|
-
|
|
321
|
-
The stream is hot, non-replayed, never fails, and completes after the root
|
|
322
|
-
terminates. Subscribe before `prepared.start` to capture initialization. Local
|
|
323
|
-
session ids are unique only inside that prepared ownership tree: `machine:0`
|
|
324
|
-
is the root and later ids identify its descendants. They are intentionally not
|
|
325
|
-
distributed identities. Cluster placement, routing, and correlation continue
|
|
326
|
-
to use Cluster entity, runner, and request identities at the integration
|
|
327
|
-
boundary.
|
|
328
|
-
|
|
329
|
-
`AtomMachine.inspection(machineAtom)` provides the same root-scoped stream and
|
|
330
|
-
starts a fresh atom-backed machine only after its inspection subscription is
|
|
331
|
-
installed.
|
|
332
|
-
|
|
333
|
-
Invalid event and emission constructions fail the machine with a typed
|
|
334
|
-
`MachineSchemaDecodeError`; they do not throw from the constructor call.
|
|
335
|
-
|
|
336
|
-
### Send explicitly between machines
|
|
337
|
-
|
|
338
|
-
`raise` targets the current machine in the same macrostep. `sendTo` targets a
|
|
339
|
-
machine mailbox and is processed later. A machine that requires an owner
|
|
340
|
-
declares the subset of parent inputs it may send with `Machine.parent`:
|
|
341
|
-
|
|
342
|
-
```ts
|
|
343
|
-
const ParentEvents = Machine.eventsFromSchemas(ChildFinished)
|
|
344
|
-
|
|
345
|
-
const child = Machine.make({
|
|
346
|
-
root: ChildStates,
|
|
347
|
-
events: ChildEvents,
|
|
348
|
-
parent: Machine.parent(ParentEvents),
|
|
349
|
-
initial: (to) => to.Working()
|
|
136
|
+
const machine = Machine.make({
|
|
137
|
+
root: Root,
|
|
138
|
+
events: Events,
|
|
139
|
+
branches: {
|
|
140
|
+
search: {
|
|
141
|
+
loading: { target: targets.root.Loading, title: "Query supplied" },
|
|
142
|
+
idle: { target: targets.root.Idle, title: "Empty query" }
|
|
143
|
+
}
|
|
144
|
+
}
|
|
350
145
|
}).handle({
|
|
351
146
|
states: {
|
|
352
|
-
|
|
147
|
+
Idle: {
|
|
353
148
|
on: {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
149
|
+
Search: {
|
|
150
|
+
branches: "search",
|
|
151
|
+
resolve: ({ event, select }) =>
|
|
152
|
+
event.query.length > 0
|
|
153
|
+
? select.loading.from({ query: event.query })
|
|
154
|
+
: select.idle.from()
|
|
155
|
+
}
|
|
359
156
|
}
|
|
360
|
-
}
|
|
361
|
-
Done: {}
|
|
157
|
+
}
|
|
362
158
|
}
|
|
363
159
|
})
|
|
364
|
-
|
|
365
|
-
const Child = Machine.child("worker", child)
|
|
366
|
-
const ParentInputs = Machine.eventsFromSchemas(Start, ParentEvents)
|
|
367
160
|
```
|
|
368
161
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
intentionally valid both as a root and as a child. In that case `parent` is
|
|
377
|
-
`MachineTarget<...> | undefined` and must be narrowed before sending. When no
|
|
378
|
-
parent declaration is present, callbacks do not expose a `parent` property.
|
|
379
|
-
`emit` never sends to the parent: it only publishes on the emitting machine's
|
|
380
|
-
`emissions` stream.
|
|
381
|
-
|
|
382
|
-
Every handler also receives `self`, which can be targeted with `sendTo` when a
|
|
383
|
-
later mailbox turn is required. Use `raise` instead for same-macrostep work.
|
|
384
|
-
Both `self` and `parent` are minimal `Machine.MachineTarget<Event>` values. The
|
|
385
|
-
shared `Machine.MachineReferences<InputEvents, ParentEvents>` context keeps
|
|
386
|
-
their input protocols separate without exposing snapshot or lifecycle APIs.
|
|
387
|
-
Structural state values use distinct names: `containingState` is the immediate
|
|
388
|
-
valued state in the same statechart, while `ancestors` maps valued ancestor
|
|
389
|
-
paths. `parent` always means the owning machine target.
|
|
390
|
-
|
|
391
|
-
### Choose the target by scope
|
|
392
|
-
|
|
393
|
-
| Builder | Use when | Preserves |
|
|
394
|
-
| ---------------- | ---------------------------------------- | ------------------------------------------------- |
|
|
395
|
-
| `target.none()` | Handling without selecting a destination | The complete current configuration |
|
|
396
|
-
| `target.local` | Moving inside the nearest compound scope | Ancestors and unrelated parallel regions |
|
|
397
|
-
| `target.branch` | Moving elsewhere under the active root | Omitted active ancestors and parallel regions |
|
|
398
|
-
| `target.full` | Replacing or selecting a complete root | Nothing implicit for a newly selected root |
|
|
399
|
-
| `target.history` | Restoring a declared history node | The remembered configuration or its typed default |
|
|
400
|
-
|
|
401
|
-
Every required transition handler selects a target from its inline `to`
|
|
402
|
-
builder. Return a bare selection such as `to.local.Idle()` when the selected
|
|
403
|
-
builder supports zero-argument construction; the machine applies the same
|
|
404
|
-
default construction as `target.from()`. This includes empty schemas and
|
|
405
|
-
schemas whose constructor fields are all optional or defaulted. TypeScript
|
|
406
|
-
rejects the bare form when state data or nested configuration is required.
|
|
407
|
-
Call `.resolve(...)` when construction depends on handler context or the
|
|
408
|
-
transition needs to enqueue commands. An absent handler ignores the trigger; `to.none` handles
|
|
409
|
-
it and retains queued commands, raised events, and emitted events without
|
|
410
|
-
selecting a destination. Concrete destinations stay narrowed inside their
|
|
411
|
-
resolver, and `to.branches({...})` gives the resolver only the declared named
|
|
412
|
-
`select` builders. Builders describe the
|
|
413
|
-
next logical configuration. Shared states exit and enter only when paths
|
|
414
|
-
change; call `.reenter()` before `.from(...)`, `.decoded(...)`, or `.resolve(...)`
|
|
415
|
-
when the source must restart. Default-constructible targets can finish at
|
|
416
|
-
`.reenter()`. With `to.none`, reentry
|
|
417
|
-
restarts the source while retaining its configuration.
|
|
418
|
-
|
|
419
|
-
Topology-only definition instructions are values: `to.none`, declared
|
|
420
|
-
`.initial` and history selections, and `to.local.with`. Concrete state and
|
|
421
|
-
choice destinations remain calls such as `to.local.Running()`. Runtime named
|
|
422
|
-
branch builders remain callable, including `select.unchanged()`, because their
|
|
423
|
-
result carries the selected branch evidence.
|
|
424
|
-
|
|
425
|
-
### Update an active scope value
|
|
426
|
-
|
|
427
|
-
Use `to.local.update(...)` to replace the value owned by the nearest active
|
|
428
|
-
compound scope without rebuilding its active child. Use
|
|
429
|
-
`to.branch.<path>.update(...)` for a valued compound or parallel ancestor of the
|
|
430
|
-
handler source:
|
|
162
|
+
Each selector is bound to its declared destination. Its constructor rejects a
|
|
163
|
+
payload belonging to another branch. A single-target group works the same way;
|
|
164
|
+
there is no second path declaration in the resolver. For a compound target,
|
|
165
|
+
`select.checkout.from(parentValues, child => child.Review.from(childValues))`
|
|
166
|
+
constructs the explicitly selected subtree. Parallel constructors require every
|
|
167
|
+
entered region. Required initializers and final output implementations remain
|
|
168
|
+
part of machine readiness checking.
|
|
431
169
|
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
170
|
+
Use `guard: context => boolean` to decline before construction or commands.
|
|
171
|
+
For a resolver that can decline, explicitly add `declinable: true`; only then may
|
|
172
|
+
it return `context.decline()`. `reenter: true` restarts the handler's source
|
|
173
|
+
where that operation supports reentry. A retained source-value update does not
|
|
174
|
+
restart its lifecycle.
|
|
437
175
|
|
|
438
|
-
|
|
439
|
-
completion outputs, and unrelated parallel regions. It runs no exit or entry
|
|
440
|
-
actions and does not restart state-owned work. Eventless stabilization still
|
|
441
|
-
runs, so an `always` transition can react to the new value.
|
|
176
|
+
### Select destinations and retain owners
|
|
442
177
|
|
|
443
|
-
|
|
444
|
-
also a static selection for a named branch:
|
|
178
|
+
All references come from the same root descriptor supplied to `make`:
|
|
445
179
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
)
|
|
455
|
-
```
|
|
456
|
-
|
|
457
|
-
### Change topology and a retained owner together
|
|
180
|
+
| Declaration | Meaning |
|
|
181
|
+
| ----------------------------------------------------- | ------------------------------------------------------------------------- |
|
|
182
|
+
| `{ target: targets.root.Checkout.Review, from: ... }` | Enter a declared destination with its value. |
|
|
183
|
+
| `{ initial: targets.root.Checkout, from: ... }` | Enter the declared initial configuration of a compound or parallel state. |
|
|
184
|
+
| `{ history: targets.root.Checkout.recent }` | Restore a declared history state. |
|
|
185
|
+
| `{ update: targets.root.Checkout, from: ... }` | Replace a retained active owner's value. |
|
|
186
|
+
| `{ update: targets.root, from: ... }` | Replace root data and retain active descendants. |
|
|
187
|
+
| `{ none: true }` | Accept an event without changing the configuration. |
|
|
458
188
|
|
|
459
|
-
|
|
460
|
-
that stays active, declare both operations on the same target:
|
|
189
|
+
An atomic transition can enter a destination and update one retained owner:
|
|
461
190
|
|
|
462
191
|
```ts
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
}))
|
|
192
|
+
Save: {
|
|
193
|
+
target: targets.root.Checkout.Saving,
|
|
194
|
+
update: targets.root.Checkout,
|
|
195
|
+
from: ({ event, ancestors }) => ({
|
|
196
|
+
target: { request: event.request },
|
|
197
|
+
update: { ...ancestors.Checkout, revision: event.revision }
|
|
198
|
+
})
|
|
471
199
|
}
|
|
472
200
|
```
|
|
473
201
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
when constructing explicit children, mixing construction methods, or queuing
|
|
480
|
-
commands; its `target` and `owner` builders construct the two values.
|
|
481
|
-
|
|
482
|
-
The topology change and owner replacement apply atomically in one microstep.
|
|
483
|
-
The owner does not exit or reenter, its work is not restarted, and destination
|
|
484
|
-
entry actions observe the new owner value. Eventless stabilization follows.
|
|
485
|
-
Only one retained owner may be replaced by a combined target. A `full` target,
|
|
486
|
-
or any target that exits the selected owner, does not expose `.updating`.
|
|
487
|
-
Named branches support value-only updates; a combined update declares its
|
|
488
|
-
destination directly.
|
|
489
|
-
|
|
490
|
-
For a schema-less destination, construction remains explicit:
|
|
491
|
-
|
|
492
|
-
```ts
|
|
493
|
-
to.local.Idle()
|
|
494
|
-
.updating(to.branch.Ready)
|
|
495
|
-
.resolve(({ current, output, owner, target }) =>
|
|
496
|
-
target.from().update(
|
|
497
|
-
owner.decoded(
|
|
498
|
-
new Ready({
|
|
499
|
-
...current,
|
|
500
|
-
day: output,
|
|
501
|
-
notice: "Plan changed."
|
|
502
|
-
})
|
|
503
|
-
)
|
|
504
|
-
)
|
|
505
|
-
)
|
|
506
|
-
```
|
|
507
|
-
|
|
508
|
-
Both values derive from the same pre-transition snapshot and are validated
|
|
509
|
-
before lifecycle actions run. Competing transitions that write the same owner
|
|
510
|
-
conflict; document order and hierarchy select one writer rather than applying
|
|
511
|
-
last-write-wins behavior.
|
|
512
|
-
|
|
513
|
-
Use `.guard(predicate)` before construction to decline an update without
|
|
514
|
-
constructing values or queuing commands. It is available on standalone and
|
|
515
|
-
combined updates. A false guard allows ancestor fallback. A resolver may also
|
|
516
|
-
return `decline()` with `{ declinable: true }` for decisions during resolution.
|
|
517
|
-
Call `.reenter()` before construction on event or invocation transitions when
|
|
518
|
-
the handler source should exit and enter again. Reentry applies to that source,
|
|
519
|
-
not to the retained ancestor whose value changed.
|
|
202
|
+
The complete replacement values are validated before either change is applied.
|
|
203
|
+
Advanced construction declares both references in a branch and uses
|
|
204
|
+
`select.saved.from(destinationValues).update.from(ownerValues)`.
|
|
205
|
+
A retained owner must be active for that source and remain active through the
|
|
206
|
+
transition. A sibling region's value cannot be updated through this operation.
|
|
520
207
|
|
|
521
|
-
The
|
|
522
|
-
|
|
523
|
-
|
|
208
|
+
The runtime transition API does not replace arbitrary complete root
|
|
209
|
+
configurations. `initialConfiguration` and history defaults retain complete
|
|
210
|
+
configuration construction for startup and restoration.
|
|
524
211
|
|
|
525
|
-
|
|
526
|
-
enabled. Only that resolver receives `decline()`, and its return type expands to
|
|
527
|
-
accept the opaque declined result:
|
|
212
|
+
### Protocols and ownership
|
|
528
213
|
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
{ declinable: true }
|
|
535
|
-
)
|
|
536
|
-
}
|
|
537
|
-
```
|
|
538
|
-
|
|
539
|
-
Declining discards work enqueued by that resolver. Event and eventless dispatch
|
|
540
|
-
continues with the next eligible ancestor; if no candidate accepts, no
|
|
541
|
-
transition is selected. This differs from `target.none()`, which consumes the
|
|
542
|
-
trigger and prevents an ancestor from handling it. `transitionDefinitions`
|
|
543
|
-
reports each handler's `acceptance` as `"required"` or `"declinable"` while
|
|
544
|
-
preserving the exact declared target branches. Choices and initial routing must
|
|
545
|
-
remain total and cannot use declinable transitions. Completion and invocation
|
|
546
|
-
outcomes have no ancestor candidate: declining one ignores that lifecycle
|
|
547
|
-
occurrence and leaves the current configuration active.
|
|
548
|
-
|
|
549
|
-
Use `Machine.can` when a caller needs to test a concrete event against a
|
|
550
|
-
snapshot. The direct and machine-specialized forms have the same semantics:
|
|
214
|
+
`Machine.events` defines public messages. `Machine.internalEvents` adds
|
|
215
|
+
machine-local raised events; `Machine.emittedEvents` defines notifications.
|
|
216
|
+
Their `FromSchemas` counterparts accept existing tagged schemas. Public and
|
|
217
|
+
internal tags must be disjoint. Deferred event constructors are validated when
|
|
218
|
+
the runtime receives the event.
|
|
551
219
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
```
|
|
220
|
+
Declare `parent: Machine.parent(events)` for a required owner protocol, or
|
|
221
|
+
`Machine.optionalParent(events)` for a machine that may run independently.
|
|
222
|
+
Handlers receive typed `self` and, when declared, `parent` references. Resolvers
|
|
223
|
+
use their second `enqueue` parameter to raise, emit, send, or stop work.
|
|
224
|
+
Planning stays synchronous: perform asynchronous work in an invocation.
|
|
558
225
|
|
|
559
|
-
`
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
emissions, and raised events are discarded. Required resolvers and transition
|
|
564
|
-
lifecycle do not run.
|
|
226
|
+
`Machine.prepare(machine)` exposes an `inspection` stream for the live local
|
|
227
|
+
machine tree; subscribe before running `prepared.start` to observe startup. Distributed
|
|
228
|
+
placement, transport, routing, and identity belong to the explicit Cluster
|
|
229
|
+
adapter.
|
|
565
230
|
|
|
566
231
|
## Statechart capabilities
|
|
567
232
|
|
|
568
|
-
`
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
- final states and typed outputs;
|
|
574
|
-
- transient choice states;
|
|
575
|
-
- shallow and deep history states.
|
|
576
|
-
|
|
577
|
-
Declare topology—including finality, output schemas, choices, and history—only
|
|
578
|
-
in `states`. Handlers implement behavior and output computation without
|
|
579
|
-
repeating structural metadata. Final children complete their parent, so
|
|
580
|
-
`onDone` belongs on that compound or parallel parent.
|
|
233
|
+
The same root supports compound and parallel states, eventless `always`
|
|
234
|
+
transitions, ordered named choices, final states, `onDone` completion,
|
|
235
|
+
initializers, shallow and deep history, and retained snapshots. The topology
|
|
236
|
+
is captured directly from declarations; visualization and branch coverage do
|
|
237
|
+
not execute user resolvers or depend on TypeScript source extraction.
|
|
581
238
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
arbitrary asynchronous Effects do not run inside planning.
|
|
239
|
+
A compound root completes with its direct completed workflow's output unless
|
|
240
|
+
that child handles completion first. Parallel completion waits for all required
|
|
241
|
+
regions. An arbitrary final descendant does not finish every ancestor.
|
|
586
242
|
|
|
587
243
|
## Effects, Streams, timers, and child machines
|
|
588
244
|
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
```ts
|
|
592
|
-
machine.handle({
|
|
593
|
-
states: {
|
|
594
|
-
Loading: {
|
|
595
|
-
invoke: (from) =>
|
|
596
|
-
from.effect("save-document", () => saveDocument)
|
|
597
|
-
.onDone((to) => to.branch.Saved().resolve(({ output, target }) => target.from({ id: output.id })))
|
|
598
|
-
.onFailure((to) => to.branch.Failed().resolve(({ error, target }) => target.from({ message: String(error) })))
|
|
599
|
-
},
|
|
600
|
-
Waiting: {
|
|
601
|
-
invoke: (from) =>
|
|
602
|
-
from.timer("save-timeout", "3 seconds")
|
|
603
|
-
.onDone((to) => to.branch.Failed().resolve(({ target }) => target.from({ message: "Timed out" })))
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
})
|
|
607
|
-
```
|
|
608
|
-
|
|
609
|
-
The state-local `from` selector starts an `effect`, `stream`, `timer`, reusable
|
|
610
|
-
`logic`, or complete `child` statechart. The selected source determines which
|
|
611
|
-
lifecycle methods the chain requires and which methods are available. For
|
|
612
|
-
example, an Effect with non-`never` output and error channels must handle both;
|
|
613
|
-
the completed chain is the value returned by the callback:
|
|
614
|
-
|
|
615
|
-
```ts
|
|
616
|
-
machine.handle({
|
|
617
|
-
states: {
|
|
618
|
-
Loading: {
|
|
619
|
-
invoke: (from) =>
|
|
620
|
-
from.effect("load-document", ({ state }) => loadDocument(state.documentId))
|
|
621
|
-
.onDone((to) => to.branch.Ready().resolve(({ output, target }) => target.from({ document: output })))
|
|
622
|
-
.onFailure((to) => to.branch.Failed().resolve(({ error, target }) => target.from({ message: error.message })))
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
})
|
|
626
|
-
```
|
|
627
|
-
|
|
628
|
-
A Stream source remains independent of the parent event protocol. Each element
|
|
629
|
-
is mapped by `onElement`, and the next element is not pulled until that parent
|
|
630
|
-
macrostep commits:
|
|
245
|
+
Register programs inside `make`, then invoke them by `src`:
|
|
631
246
|
|
|
632
247
|
```ts
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
.onFailure((to) => to.branch.Failed().resolve(({ error, target }) => target.from({ error })))
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
})
|
|
648
|
-
```
|
|
649
|
-
|
|
650
|
-
`to.none` is the targetless transition value. Return it directly to keep the
|
|
651
|
-
current configuration, or call `.resolve(...)` when the transition only needs
|
|
652
|
-
to enqueue commands. A block resolver may omit its return because it is
|
|
653
|
-
contextually typed to return `undefined`.
|
|
654
|
-
|
|
655
|
-
Inside `.handle(...)`, `from` receives the owning machine's public input and
|
|
656
|
-
declared parent protocol contextually. Source and lifecycle callbacks can send
|
|
657
|
-
through `self` and `parent` while retaining the invoked Effect's output and
|
|
658
|
-
error inference:
|
|
659
|
-
|
|
660
|
-
```ts
|
|
661
|
-
const machine = Machine.make({
|
|
662
|
-
events: Commands,
|
|
663
|
-
internalEvents: InternalEvents,
|
|
664
|
-
parent: Machine.parent(ParentEvents)
|
|
665
|
-
// ...
|
|
666
|
-
}).handle({
|
|
667
|
-
states: {
|
|
668
|
-
Saving: {
|
|
669
|
-
invoke: (from) =>
|
|
670
|
-
from.effect("notify-parent", () => saveDocument)
|
|
671
|
-
.onDone((to) =>
|
|
672
|
-
to.none.resolve(({ parent, self }, enqueue) => {
|
|
673
|
-
enqueue.sendTo(self, Commands.Save())
|
|
674
|
-
enqueue.sendTo(parent, ParentEvents.ChildFinished({ id: "job-1" }))
|
|
675
|
-
})
|
|
676
|
-
)
|
|
677
|
-
.onFailure((to) => to.none)
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
})
|
|
681
|
-
```
|
|
682
|
-
|
|
683
|
-
Return an array of completed chains to compose multiple state-owned activities.
|
|
684
|
-
The source computation itself, process logic, or `Machine.child(id, machine)`
|
|
685
|
-
descriptor can be named and reused; the invocation chain stays local so its
|
|
686
|
-
transitions retain the exact owning state and machine protocols.
|
|
687
|
-
|
|
688
|
-
```ts
|
|
689
|
-
const refreshCache = Cache.refresh
|
|
690
|
-
|
|
691
|
-
machine.handle({
|
|
692
|
-
states: {
|
|
693
|
-
Active: {
|
|
694
|
-
invoke: (from) => [
|
|
695
|
-
from.effect("refresh-cache", () => refreshCache).onDone((to) => to.none).onFailure((to) => to.none),
|
|
696
|
-
from.timer("expire-session", "5 minutes").onDone((to) => to.branch.Expired())
|
|
697
|
-
]
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
})
|
|
701
|
-
```
|
|
702
|
-
|
|
703
|
-
`onDone` is required for a non-`never` output, and `onFailure` is required for a
|
|
704
|
-
non-`never` typed error. Streams additionally require `onElement` when their
|
|
705
|
-
element channel is non-`never` and always require `onDone`; logic and child
|
|
706
|
-
chains optionally expose `onSnapshot`. A handled method disappears from the
|
|
707
|
-
next builder step, so every reachable lifecycle channel is handled exactly
|
|
708
|
-
once. Defects, interruption, and source-construction failures terminate the
|
|
709
|
-
owning runtime. Effect sources are factories evaluated when their state is
|
|
710
|
-
entered. Use an Effect containing `Effect.sleep(...)` for generic work, while
|
|
711
|
-
`from.timer(...)` keeps timer intent explicit and makes static durations visible
|
|
712
|
-
through activity inspection.
|
|
713
|
-
|
|
714
|
-
### Spawn dynamic child machines
|
|
715
|
-
|
|
716
|
-
Use `from.child(...)` when a state owns a fixed child lifecycle. Use the
|
|
717
|
-
`children` context inside an invoked Effect when the machine process owns an
|
|
718
|
-
open set of children that must survive state changes:
|
|
719
|
-
|
|
720
|
-
```ts
|
|
721
|
-
const Plant = Machine.childFamily(plantMachine)
|
|
722
|
-
|
|
723
|
-
const central = Machine.make({
|
|
724
|
-
events: Machine.eventsFromSchemas(ResourcesOffered, PlantBroken)
|
|
725
|
-
// ...
|
|
248
|
+
const Search = Machine.make({
|
|
249
|
+
root: Root,
|
|
250
|
+
events: Events,
|
|
251
|
+
effects: {
|
|
252
|
+
load: (query: string) => searchItems(query),
|
|
253
|
+
refreshCache: Cache.refresh
|
|
254
|
+
},
|
|
255
|
+
streams: { updates: changes },
|
|
256
|
+
timers: { timeout: "5 seconds" },
|
|
257
|
+
logic: { worker: workerLogic },
|
|
258
|
+
children: { validation: ValidationChild }
|
|
726
259
|
}).handle({
|
|
727
260
|
states: {
|
|
728
|
-
|
|
729
|
-
invoke:
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
))
|
|
736
|
-
.onDone((to) => to.branch.Operating())
|
|
737
|
-
.onFailure((to) => to.branch.CommissioningFailed())
|
|
261
|
+
Loading: {
|
|
262
|
+
invoke: {
|
|
263
|
+
src: "load",
|
|
264
|
+
input: ({ state }) => state.query,
|
|
265
|
+
onDone: { target: targets.root.Ready, from: ({ output }) => ({ items: output }) },
|
|
266
|
+
onFailure: { target: targets.root.Failed, from: ({ error }) => ({ message: error.message }) }
|
|
267
|
+
}
|
|
738
268
|
}
|
|
739
269
|
}
|
|
740
270
|
})
|
|
741
271
|
```
|
|
742
272
|
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
273
|
+
Effects and Streams are lazy values: pass them directly when they need no
|
|
274
|
+
input. Use `Effect.suspend` or `Stream.suspend` when construction itself must be
|
|
275
|
+
deferred. An input-taking source has exactly one required parameter and the
|
|
276
|
+
invocation must supply a typed `input` mapper. A direct value forbids `input`.
|
|
277
|
+
Zero-argument and optional-argument factories are rejected to keep this choice
|
|
278
|
+
explicit. Source names are unique across all five registries.
|
|
279
|
+
|
|
280
|
+
| Source | Required handlers | Optional handlers |
|
|
281
|
+
| -------------- | --------------------------------------------------------------------------------------------------- | ----------------- |
|
|
282
|
+
| Effect | `onDone` when success is not `never`; `onFailure` when error is not `never` | None |
|
|
283
|
+
| Stream | `onElement` when its element is not `never`; `onFailure` when error is not `never`; always `onDone` | None |
|
|
284
|
+
| Timer | `onDone` | None |
|
|
285
|
+
| Logic or child | Reachable `onDone` and `onFailure` | `onSnapshot` |
|
|
286
|
+
|
|
287
|
+
A `void` success still requires `onDone`. Unreachable channels are rejected.
|
|
288
|
+
Startup errors, defects, and interruption follow the existing runtime failure
|
|
289
|
+
and cancellation boundaries; a typed `onFailure` handles the source's declared
|
|
290
|
+
runtime error channel.
|
|
291
|
+
|
|
292
|
+
```ts
|
|
293
|
+
invoke: ;
|
|
294
|
+
;[
|
|
295
|
+
{
|
|
296
|
+
src: "updates",
|
|
297
|
+
onElement: {
|
|
298
|
+
none: true,
|
|
299
|
+
resolve: ({ element }, enqueue) => {
|
|
300
|
+
enqueue.raise(Events.Changed({ value: element }))
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
onDone: { none: true },
|
|
304
|
+
onFailure: { none: true }
|
|
305
|
+
},
|
|
306
|
+
{ src: "timeout", onDone: { target: targets.root.Expired } },
|
|
307
|
+
{ src: "worker", id: "worker", address: WorkerAddress, onSnapshot: { none: true } },
|
|
308
|
+
{ src: "validation", input: ({ state }) => ({ query: state.query }), onDone: { none: true } }
|
|
309
|
+
]
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
Include each handler demanded by the concrete program's type. Use an explicit
|
|
313
|
+
`id` for simultaneous instances of one source; effect, stream, and timer IDs
|
|
314
|
+
default to `src`. Logic requires a lifecycle `id` and a typed `Machine.childAddress`.
|
|
315
|
+
A child uses its `Machine.child` descriptor's identity and protocol. All
|
|
316
|
+
invocations are owned by their state and cancelled when it exits.
|
|
317
|
+
|
|
318
|
+
Effect services flow through the used sources and lifecycle effects into the
|
|
319
|
+
machine's requirements. Unused registrations add no requirements. Supply them
|
|
320
|
+
with ordinary `Effect.provide` / `Effect.provideService`, or an Atom runtime
|
|
321
|
+
backed by a Layer. Context services provide implementation substitution and
|
|
322
|
+
scoped resources; there is no separate machine-specific dependency container.
|
|
323
|
+
|
|
324
|
+
### Dynamic children
|
|
325
|
+
|
|
326
|
+
A registered `Machine.child` describes a fixed state-owned child. For an open
|
|
327
|
+
set of process-owned children, use `Machine.childFamily` with the `children`
|
|
328
|
+
capability supplied to a registered Effect through its input mapper.
|
|
329
|
+
`children.spawn` completes after child initialization; the child survives the
|
|
330
|
+
commissioning invocation and state. `children.sendTo` and `children.stop`
|
|
331
|
+
address it from Effects; resolvers use `enqueue.sendTo` and `enqueue.stop`.
|
|
332
|
+
Duplicate active IDs fail with `ChildAlreadyExistsError` and preserve the
|
|
333
|
+
existing child. Parent-protocol compatibility is checked at each spawn.
|
|
755
334
|
|
|
756
335
|
## Reactivity
|
|
757
336
|
|
|
@@ -902,8 +481,8 @@ import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
|
902
481
|
|
|
903
482
|
const trace = yield* MachineTest.run(Counter, {
|
|
904
483
|
events: [
|
|
905
|
-
{ _tag: "
|
|
906
|
-
{ _tag: "Increment" }
|
|
484
|
+
{ _tag: "Increment", by: 1 },
|
|
485
|
+
{ _tag: "Increment", by: 2 }
|
|
907
486
|
]
|
|
908
487
|
})
|
|
909
488
|
|
|
@@ -939,8 +518,9 @@ pnpm install --frozen-lockfile
|
|
|
939
518
|
pnpm check
|
|
940
519
|
```
|
|
941
520
|
|
|
942
|
-
|
|
943
|
-
|
|
521
|
+
Use `guard: predicate` to decline before construction, or `declinable: true`
|
|
522
|
+
when a resolver can return `decline()`.
|
|
523
|
+
Pull requests that change `src/` or `package.json` need
|
|
944
524
|
a changeset and the performance checks described in `AGENTS.md`.
|
|
945
525
|
|
|
946
526
|
When equivalent Machine modules ship in Effect, this package is intended to
|