@typeonce/effect-machine 0.47.1 → 0.48.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 +374 -22
- package/CLAUDE.md +374 -22
- package/README.md +56 -7
- package/ai-docs/src/10_modeling/01_create-a-machine.ts +3 -3
- package/ai-docs/src/10_modeling/index.md +64 -17
- package/ai-docs/src/20_root-api/index.md +2 -2
- package/ai-docs/src/35_cluster/index.md +213 -0
- package/ai-docs/src/37_new-host/index.md +90 -0
- package/dist/LocalMachine.d.ts +417 -0
- package/dist/LocalMachine.d.ts.map +1 -0
- package/dist/LocalMachine.js +193 -0
- package/dist/LocalMachine.js.map +1 -0
- package/dist/Machine.d.ts +1120 -812
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +195 -186
- package/dist/Machine.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/machine/atom.d.ts +8 -10
- package/dist/internal/machine/atom.d.ts.map +1 -1
- package/dist/internal/machine/atom.js +7 -6
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/capability.d.ts +101 -0
- package/dist/internal/machine/capability.d.ts.map +1 -0
- package/dist/internal/machine/capability.js +70 -0
- package/dist/internal/machine/capability.js.map +1 -0
- package/dist/internal/machine/{invocationDefinition.d.ts → capturedInvocation.d.ts} +11 -11
- package/dist/internal/machine/capturedInvocation.d.ts.map +1 -0
- package/dist/internal/machine/{invocationDefinition.js → capturedInvocation.js} +2 -2
- package/dist/internal/machine/capturedInvocation.js.map +1 -0
- package/dist/internal/machine/child.d.ts +14 -0
- package/dist/internal/machine/child.d.ts.map +1 -0
- package/dist/internal/machine/child.js +19 -0
- package/dist/internal/machine/child.js.map +1 -0
- package/dist/internal/machine/childRegistry.d.ts +0 -2
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/childRegistry.js +2 -2
- package/dist/internal/machine/childRegistry.js.map +1 -1
- package/dist/internal/machine/cluster.d.ts +19 -4
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +263 -110
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterChild.d.ts +66 -0
- package/dist/internal/machine/clusterChild.d.ts.map +1 -0
- package/dist/internal/machine/clusterChild.js +105 -0
- package/dist/internal/machine/clusterChild.js.map +1 -0
- package/dist/internal/machine/clusterFailure.d.ts +14 -0
- package/dist/internal/machine/clusterFailure.d.ts.map +1 -0
- package/dist/internal/machine/clusterFailure.js +44 -0
- package/dist/internal/machine/clusterFailure.js.map +1 -0
- package/dist/internal/machine/clusterInstance.d.ts +194 -0
- package/dist/internal/machine/clusterInstance.d.ts.map +1 -0
- package/dist/internal/machine/clusterInstance.js +1076 -0
- package/dist/internal/machine/clusterInstance.js.map +1 -0
- package/dist/internal/machine/clusterKeepAlive.d.ts +47 -0
- package/dist/internal/machine/clusterKeepAlive.d.ts.map +1 -0
- package/dist/internal/machine/clusterKeepAlive.js +72 -0
- package/dist/internal/machine/clusterKeepAlive.js.map +1 -0
- package/dist/internal/machine/clusterProgress.d.ts +73 -0
- package/dist/internal/machine/clusterProgress.d.ts.map +1 -0
- package/dist/internal/machine/clusterProgress.js +116 -0
- package/dist/internal/machine/clusterProgress.js.map +1 -0
- package/dist/internal/machine/clusterProtocol.d.ts +37 -4
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -1
- package/dist/internal/machine/clusterProtocol.js +18 -4
- package/dist/internal/machine/clusterProtocol.js.map +1 -1
- package/dist/internal/machine/clusterProxy.d.ts +46 -0
- package/dist/internal/machine/clusterProxy.d.ts.map +1 -0
- package/dist/internal/machine/clusterProxy.js +150 -0
- package/dist/internal/machine/clusterProxy.js.map +1 -0
- package/dist/internal/machine/clusterRead.d.ts +61 -0
- package/dist/internal/machine/clusterRead.d.ts.map +1 -0
- package/dist/internal/machine/clusterRead.js +108 -0
- package/dist/internal/machine/clusterRead.js.map +1 -0
- package/dist/internal/machine/clusterSqlStorage.d.ts +16 -0
- package/dist/internal/machine/clusterSqlStorage.d.ts.map +1 -0
- package/dist/internal/machine/clusterSqlStorage.js +138 -0
- package/dist/internal/machine/clusterSqlStorage.js.map +1 -0
- package/dist/internal/machine/clusterStorageCheck.d.ts +19 -0
- package/dist/internal/machine/clusterStorageCheck.d.ts.map +1 -0
- package/dist/internal/machine/clusterStorageCheck.js +186 -0
- package/dist/internal/machine/clusterStorageCheck.js.map +1 -0
- package/dist/internal/machine/clusterTarget.d.ts +74 -0
- package/dist/internal/machine/clusterTarget.d.ts.map +1 -0
- package/dist/internal/machine/clusterTarget.js +81 -0
- package/dist/internal/machine/clusterTarget.js.map +1 -0
- package/dist/internal/machine/clusterTimer.d.ts +21 -0
- package/dist/internal/machine/clusterTimer.d.ts.map +1 -0
- package/dist/internal/machine/clusterTimer.js +50 -0
- package/dist/internal/machine/clusterTimer.js.map +1 -0
- package/dist/internal/machine/clusterWake.d.ts +61 -0
- package/dist/internal/machine/clusterWake.d.ts.map +1 -0
- package/dist/internal/machine/clusterWake.js +81 -0
- package/dist/internal/machine/clusterWake.js.map +1 -0
- package/dist/internal/machine/clusterWork.d.ts +130 -0
- package/dist/internal/machine/clusterWork.d.ts.map +1 -0
- package/dist/internal/machine/clusterWork.js +191 -0
- package/dist/internal/machine/clusterWork.js.map +1 -0
- package/dist/internal/machine/command.d.ts +4 -4
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.d.ts +8 -6
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.js +1 -5
- package/dist/internal/machine/commandRuntime.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +0 -13
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +0 -151
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/errors.d.ts +11 -2
- package/dist/internal/machine/errors.d.ts.map +1 -1
- package/dist/internal/machine/errors.js +11 -2
- package/dist/internal/machine/errors.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts +45 -3
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +328 -191
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +6 -6
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/inspectionRuntime.d.ts +1 -1
- package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -1
- package/dist/internal/machine/invocationChanges.d.ts +60 -0
- package/dist/internal/machine/invocationChanges.d.ts.map +1 -0
- package/dist/internal/machine/invocationChanges.js +54 -0
- package/dist/internal/machine/invocationChanges.js.map +1 -0
- package/dist/internal/machine/{activities.d.ts → invocationDefinitions.d.ts} +8 -8
- package/dist/internal/machine/invocationDefinitions.d.ts.map +1 -0
- package/dist/internal/machine/{activities.js → invocationDefinitions.js} +5 -5
- package/dist/internal/machine/invocationDefinitions.js.map +1 -0
- package/dist/internal/machine/invocationEvent.d.ts +16 -7
- package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
- package/dist/internal/machine/invocationEvent.js +26 -12
- package/dist/internal/machine/invocationEvent.js.map +1 -1
- package/dist/internal/machine/invocationOutcome.d.ts +89 -0
- package/dist/internal/machine/invocationOutcome.d.ts.map +1 -0
- package/dist/internal/machine/invocationOutcome.js +76 -0
- package/dist/internal/machine/invocationOutcome.js.map +1 -0
- package/dist/internal/machine/invocationResolution.d.ts +137 -0
- package/dist/internal/machine/invocationResolution.d.ts.map +1 -0
- package/dist/internal/machine/invocationResolution.js +150 -0
- package/dist/internal/machine/invocationResolution.js.map +1 -0
- package/dist/internal/machine/logicScope.d.ts +24 -0
- package/dist/internal/machine/logicScope.d.ts.map +1 -0
- package/dist/internal/machine/logicScope.js +33 -0
- package/dist/internal/machine/logicScope.js.map +1 -0
- package/dist/internal/machine/machine.d.ts +17 -23
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +24 -40
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/observation.d.ts +3 -0
- package/dist/internal/machine/observation.d.ts.map +1 -1
- package/dist/internal/machine/observation.js +56 -2
- package/dist/internal/machine/observation.js.map +1 -1
- package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -1
- package/dist/internal/machine/plannedSnapshot.js +11 -20
- package/dist/internal/machine/plannedSnapshot.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +36 -23
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +15 -8
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +17 -9
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +43 -503
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js +48 -10
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +20 -16
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +93 -88
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -1
- package/dist/internal/machine/runtimeCompiled.js +359 -120
- package/dist/internal/machine/runtimeCompiled.js.map +1 -1
- package/dist/internal/machine/runtimeProtocol.d.ts +151 -82
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
- package/dist/internal/machine/runtimeProtocol.js +236 -219
- package/dist/internal/machine/runtimeProtocol.js.map +1 -1
- package/dist/internal/machine/stateDefinition.d.ts +0 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +0 -1
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/step.d.ts +486 -0
- package/dist/internal/machine/step.d.ts.map +1 -0
- package/dist/internal/machine/step.js +957 -0
- package/dist/internal/machine/step.js.map +1 -0
- package/dist/internal/machine/stepHost.d.ts +33 -0
- package/dist/internal/machine/stepHost.d.ts.map +1 -0
- package/dist/internal/machine/stepHost.js +501 -0
- package/dist/internal/machine/stepHost.js.map +1 -0
- package/dist/internal/machine/symbols.d.ts +2 -11
- package/dist/internal/machine/symbols.d.ts.map +1 -1
- package/dist/internal/machine/symbols.js +2 -2
- package/dist/internal/machine/symbols.js.map +1 -1
- package/dist/internal/machine/transition.d.ts +7 -7
- package/dist/internal/machine/transition.d.ts.map +1 -1
- package/dist/internal/testing/machine/check.d.ts.map +1 -1
- package/dist/internal/testing/machine/check.js +217 -25
- package/dist/internal/testing/machine/check.js.map +1 -1
- package/dist/internal/testing/machine/exploration.d.ts +19 -2
- package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
- package/dist/internal/testing/machine/exploration.js +14 -3
- package/dist/internal/testing/machine/exploration.js.map +1 -1
- package/dist/internal/testing/machine/format.js.map +1 -1
- package/dist/internal/testing/machine/probe.d.ts.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.js.map +1 -1
- package/dist/internal/testing/machine/runtime.d.ts +2 -2
- package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
- package/dist/internal/testing/machine/runtime.js +10 -3
- package/dist/internal/testing/machine/runtime.js.map +1 -1
- package/dist/internal/testing/machine/trace.d.ts +38 -3
- package/dist/internal/testing/machine/trace.d.ts.map +1 -1
- package/dist/internal/testing/machine/trace.js +180 -33
- package/dist/internal/testing/machine/trace.js.map +1 -1
- package/dist/internal/testing/machine/transitionCoverage.d.ts +1 -1
- package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +9 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/internal/testing/machine/work.d.ts +47 -0
- package/dist/internal/testing/machine/work.d.ts.map +1 -0
- package/dist/internal/testing/machine/work.js +74 -0
- package/dist/internal/testing/machine/work.js.map +1 -0
- package/dist/testing/MachineTest.d.ts +253 -37
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +81 -9
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +1071 -41
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +441 -12
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachineProxy.d.ts +257 -0
- package/dist/unstable/cluster/ClusterMachineProxy.d.ts.map +1 -0
- package/dist/unstable/cluster/ClusterMachineProxy.js +73 -0
- package/dist/unstable/cluster/ClusterMachineProxy.js.map +1 -0
- package/dist/unstable/cluster/SqlMachineStorage.d.ts +125 -0
- package/dist/unstable/cluster/SqlMachineStorage.d.ts.map +1 -0
- package/dist/unstable/cluster/SqlMachineStorage.js +62 -0
- package/dist/unstable/cluster/SqlMachineStorage.js.map +1 -0
- package/dist/unstable/cluster/index.d.ts +13 -0
- package/dist/unstable/cluster/index.d.ts.map +1 -1
- package/dist/unstable/cluster/index.js +13 -0
- package/dist/unstable/cluster/index.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +21 -26
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/package.json +5 -3
- package/src/LocalMachine.ts +611 -0
- package/src/Machine.ts +1379 -1334
- package/src/index.ts +1 -0
- package/src/internal/machine/atom.ts +20 -49
- package/src/internal/machine/capability.ts +123 -0
- package/src/internal/machine/{invocationDefinition.ts → capturedInvocation.ts} +13 -13
- package/src/internal/machine/child.ts +25 -0
- package/src/internal/machine/childRegistry.ts +1 -4
- package/src/internal/machine/cluster.ts +420 -205
- package/src/internal/machine/clusterChild.ts +117 -0
- package/src/internal/machine/clusterFailure.ts +53 -0
- package/src/internal/machine/clusterInstance.ts +1779 -0
- package/src/internal/machine/clusterKeepAlive.ts +93 -0
- package/src/internal/machine/clusterProgress.ts +159 -0
- package/src/internal/machine/clusterProtocol.ts +46 -4
- package/src/internal/machine/clusterProxy.ts +233 -0
- package/src/internal/machine/clusterRead.ts +207 -0
- package/src/internal/machine/clusterSqlStorage.ts +181 -0
- package/src/internal/machine/clusterStorageCheck.ts +274 -0
- package/src/internal/machine/clusterTarget.ts +154 -0
- package/src/internal/machine/clusterTimer.ts +58 -0
- package/src/internal/machine/clusterWake.ts +100 -0
- package/src/internal/machine/clusterWork.ts +269 -0
- package/src/internal/machine/command.ts +4 -4
- package/src/internal/machine/commandRuntime.ts +12 -24
- package/src/internal/machine/configuration.ts +0 -181
- package/src/internal/machine/errors.ts +11 -2
- package/src/internal/machine/executionPlan.ts +493 -263
- package/src/internal/machine/implementation.ts +6 -6
- package/src/internal/machine/inspectionRuntime.ts +1 -1
- package/src/internal/machine/invocationChanges.ts +89 -0
- package/src/internal/machine/{activities.ts → invocationDefinitions.ts} +10 -10
- package/src/internal/machine/invocationEvent.ts +35 -12
- package/src/internal/machine/invocationOutcome.ts +145 -0
- package/src/internal/machine/invocationResolution.ts +307 -0
- package/src/internal/machine/logicScope.ts +47 -0
- package/src/internal/machine/machine.ts +35 -100
- package/src/internal/machine/observation.ts +67 -2
- package/src/internal/machine/plannedSnapshot.ts +17 -20
- package/src/internal/machine/planner.ts +75 -99
- package/src/internal/machine/process.ts +78 -722
- package/src/internal/machine/protocol.ts +70 -26
- package/src/internal/machine/runtime.ts +198 -195
- package/src/internal/machine/runtimeCompiled.ts +496 -171
- package/src/internal/machine/runtimeProtocol.ts +416 -305
- package/src/internal/machine/stateDefinition.ts +0 -5
- package/src/internal/machine/step.ts +1518 -0
- package/src/internal/machine/stepHost.ts +634 -0
- package/src/internal/machine/symbols.ts +3 -14
- package/src/internal/machine/transition.ts +11 -11
- package/src/internal/testing/machine/check.ts +240 -36
- package/src/internal/testing/machine/exploration.ts +42 -3
- package/src/internal/testing/machine/format.ts +1 -1
- package/src/internal/testing/machine/probe.ts +2 -1
- package/src/internal/testing/machine/runtime.ts +15 -5
- package/src/internal/testing/machine/trace.ts +261 -41
- package/src/internal/testing/machine/transitionCoverage.ts +1 -1
- package/src/internal/testing/machine/verification.ts +11 -5
- package/src/internal/testing/machine/work.ts +101 -0
- package/src/testing/MachineTest.ts +268 -44
- package/src/unstable/cluster/ClusterMachine.ts +1183 -59
- package/src/unstable/cluster/ClusterMachineProxy.ts +372 -0
- package/src/unstable/cluster/SqlMachineStorage.ts +136 -0
- package/src/unstable/cluster/index.ts +15 -0
- package/src/unstable/reactivity/AtomMachine.ts +27 -88
- package/dist/internal/machine/activities.d.ts.map +0 -1
- package/dist/internal/machine/activities.js.map +0 -1
- package/dist/internal/machine/invocation.d.ts +0 -33
- package/dist/internal/machine/invocation.d.ts.map +0 -1
- package/dist/internal/machine/invocation.js +0 -220
- package/dist/internal/machine/invocation.js.map +0 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +0 -1
- package/dist/internal/machine/invocationDefinition.js.map +0 -1
- package/dist/internal/machine/requirements.d.ts +0 -4
- package/dist/internal/machine/requirements.d.ts.map +0 -1
- package/dist/internal/machine/requirements.js +0 -2
- package/dist/internal/machine/requirements.js.map +0 -1
- package/dist/internal/machine/runtimeGeneric.d.ts +0 -3
- package/dist/internal/machine/runtimeGeneric.d.ts.map +0 -1
- package/dist/internal/machine/runtimeGeneric.js +0 -465
- package/dist/internal/machine/runtimeGeneric.js.map +0 -1
- package/src/internal/machine/invocation.ts +0 -376
- package/src/internal/machine/requirements.ts +0 -11
- package/src/internal/machine/runtimeGeneric.ts +0 -797
|
@@ -83,18 +83,20 @@ Start the implemented machine at the application boundary and send events
|
|
|
83
83
|
through the exported descriptor:
|
|
84
84
|
|
|
85
85
|
```ts
|
|
86
|
+
import { LocalMachine } from "@typeonce/effect-machine"
|
|
86
87
|
import { Effect } from "effect"
|
|
87
88
|
|
|
88
89
|
const program = Effect.scoped(Effect.gen(function*() {
|
|
89
|
-
const counter = yield*
|
|
90
|
+
const counter = yield* LocalMachine.start(CounterMachine)
|
|
90
91
|
|
|
91
92
|
yield* counter.send(CounterEvents.Start())
|
|
92
93
|
yield* counter.send(CounterEvents.Increment())
|
|
93
94
|
}))
|
|
94
95
|
```
|
|
95
96
|
|
|
96
|
-
`
|
|
97
|
-
it owns; use `ref.stop` to stop
|
|
97
|
+
`LocalMachine` runs a machine in memory. `LocalMachine.start` requires a `Scope`.
|
|
98
|
+
Closing it stops the machine and everything it owns; use `ref.stop` to stop
|
|
99
|
+
earlier.
|
|
98
100
|
|
|
99
101
|
### Make impossible states unrepresentable
|
|
100
102
|
|
|
@@ -344,6 +346,20 @@ const LoadMachine = Machine.make({
|
|
|
344
346
|
implemented machine type, so the runtime must provide them when it starts the
|
|
345
347
|
machine.
|
|
346
348
|
|
|
349
|
+
The same requirements say what a machine needs of the host that runs it.
|
|
350
|
+
`Machine.InvocationSupport<"Stream">`, `<"Logic">`, and `<"Child">` mark a machine
|
|
351
|
+
whose states invoke that kind. `Machine.MachineRuntime` marks one whose work uses
|
|
352
|
+
`Machine.spawn`, or `Machine.sendTo` or `Machine.stopChild` with a child address,
|
|
353
|
+
and `Machine.ChildMachineRuntime` one whose work uses `Machine.sendTo` or
|
|
354
|
+
`Machine.stopChild` with the descriptor of a child machine. `LocalMachine` and
|
|
355
|
+
`AtomMachine` run all of them. `ClusterMachine.make` runs Effects, timers, and
|
|
356
|
+
child machines, and provides `Machine.ChildMachineRuntime`. It does not compile
|
|
357
|
+
for a machine that invokes a Stream or a `Logic`, or needs
|
|
358
|
+
`Machine.MachineRuntime`, and its error lists what to replace. Cluster stores
|
|
359
|
+
each step and runs work again after a crash, so it runs only work that ends with
|
|
360
|
+
one outcome: the progress of a Stream and the state of a `Logic` live in memory
|
|
361
|
+
and would be lost. Model such work as a child machine.
|
|
362
|
+
|
|
347
363
|
Model a per-instance capability, such as a router or a callback owned by a UI
|
|
348
364
|
component, as such a service. Do not put functions in state data or startup
|
|
349
365
|
input: snapshots must remain encodable. A React owner provides the capability
|
|
@@ -353,11 +369,11 @@ through its own runtime, as described in
|
|
|
353
369
|
Do not start a promise inside a transition callback. A transition has no
|
|
354
370
|
lifetime in which to own that work. A state does.
|
|
355
371
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
in a later turn, even when the
|
|
359
|
-
|
|
360
|
-
browser call that requires user activation, such as `window.open` or
|
|
372
|
+
A local machine, including one hosted by `AtomMachine`, starts invoked work while
|
|
373
|
+
the event that entered its state is processed. The work runs in the sender's turn
|
|
374
|
+
until it first suspends, and its outcome is handled in a later turn, even when the
|
|
375
|
+
work completes without suspending. A state that invokes work is therefore always
|
|
376
|
+
observed before its outcome leaves it. Put a browser call that requires user activation, such as `window.open` or
|
|
361
377
|
`navigator.clipboard.writeText`, in the first step of the invoked Effect:
|
|
362
378
|
|
|
363
379
|
```ts
|
|
@@ -454,6 +470,14 @@ Do not read the clock, generate randomness, call a service, or await work while
|
|
|
454
470
|
choosing a transition. Receive such values in an event or produce them through
|
|
455
471
|
state-owned work first.
|
|
456
472
|
|
|
473
|
+
A transition that throws follows one rule on every host: the error goes to the
|
|
474
|
+
sender when one waits for the event to be processed, and otherwise the machine
|
|
475
|
+
fails with it. `send` of `LocalMachine` returns before the event is processed,
|
|
476
|
+
so the machine fails; a `ClusterMachine` client waits, so it is rejected and the
|
|
477
|
+
machine goes on. An event that its schema refuses is different: every host
|
|
478
|
+
rejects it at `send`, `LocalMachine` with `MachineSchemaDecodeError` and
|
|
479
|
+
`ClusterMachine` as `InvalidEvent`, and the machine keeps running.
|
|
480
|
+
|
|
457
481
|
`resolve` is checked against `branches` in both directions, and each `select`
|
|
458
482
|
call takes the values of the path it is given. An agent can edit this one
|
|
459
483
|
transition without reading anything else: a missing entry, an entry that is never
|
|
@@ -526,9 +550,26 @@ const verifyCounter = Effect.gen(function*() {
|
|
|
526
550
|
From a shell, `effect-machine check src/counter.ts --json` from
|
|
527
551
|
`@typeonce/effect-machine-devtools` does the same for every machine a file exports.
|
|
528
552
|
|
|
529
|
-
To fix a finding, keep the failing evidence first.
|
|
530
|
-
it, make the smallest behavior change, then run
|
|
531
|
-
finding by weakening a type or a test.
|
|
553
|
+
To fix a finding, keep the failing evidence first. Pass its `replay` to
|
|
554
|
+
`MachineTest.run` to reproduce it, make the smallest behavior change, then run
|
|
555
|
+
the check again. Do not silence a finding by weakening a type or a test.
|
|
556
|
+
|
|
557
|
+
The check takes the results of invoked work as further branches while that work
|
|
558
|
+
runs. A timer elapses without any option. An Effect's results are only a type,
|
|
559
|
+
so supply the values to try for each registered source in `outcomes`; each value
|
|
560
|
+
is one branch, and the compiler rejects an unknown source or a value of the
|
|
561
|
+
wrong type:
|
|
562
|
+
|
|
563
|
+
```ts
|
|
564
|
+
const report = yield* MachineTest.check(LoadMachine, {
|
|
565
|
+
outcomes: {
|
|
566
|
+
loadDocument: { done: ["# Title"], failure: [new LoadError()] }
|
|
567
|
+
}
|
|
568
|
+
})
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
A replay lists the steps in order: events, and the results of work such as
|
|
572
|
+
`done Loading#loadDocument "# Title"` or `elapsed Waiting#timeout`.
|
|
532
573
|
|
|
533
574
|
| Law | Meaning | Fix |
|
|
534
575
|
| ---------------------------- | --------------------------------------------------------------- | ------------------------------------------------------------------- |
|
|
@@ -538,13 +579,18 @@ finding by weakening a type or a test.
|
|
|
538
579
|
| `planning.failed` | A planning callback threw or a value failed its schema. | Replay the events, then fix the callback or the value. |
|
|
539
580
|
| `planning.verification` | A planner law failed. This usually means an Effect Machine bug. | Report it with the replay. |
|
|
540
581
|
| `invariant.violated` | An invariant you supplied failed. | Replay the events, then fix the behavior or the invariant. |
|
|
541
|
-
| `
|
|
582
|
+
| `outcome.failsMachine` | A supplied result fails the machine, whose types were erased. | Add the missing `onFailure` or `onDone`. |
|
|
583
|
+
| `outcomes.invalid` | `outcomes` names no Effect source that a state invokes. | Fix the source name, or remove the entry. |
|
|
584
|
+
| `transition.notCovered` | Exploration never took a declared branch. This is a warning. | Cover it with a scenario or another outcome, or remove it. |
|
|
542
585
|
| `schema.underivable` | Input or events could not be sampled from their schemas. | Pass `input` or `events` explicitly. |
|
|
543
586
|
|
|
544
587
|
A report is `"exhaustive"` only when `limits` is empty. Otherwise a pass means
|
|
545
588
|
nothing was found in what was explored. Keep sampled checks, bounded exploration,
|
|
546
|
-
and proof distinct when reporting a result.
|
|
547
|
-
|
|
589
|
+
and proof distinct when reporting a result. Supplied outcomes add the
|
|
590
|
+
`customOutcomes` limit. An Effect without outcomes adds `effectsWithoutOutcomes`,
|
|
591
|
+
and Streams, logic, and child machines add `streams`, `logic`, and `children`:
|
|
592
|
+
their results are not explored, so test them on a started machine with
|
|
593
|
+
`MachineTest.probe`.
|
|
548
594
|
|
|
549
595
|
### Test paths and invariants
|
|
550
596
|
|
|
@@ -584,9 +630,10 @@ const testProgram = Effect.gen(function*() {
|
|
|
584
630
|
})
|
|
585
631
|
```
|
|
586
632
|
|
|
587
|
-
Use pure planner traces for state and transition rules
|
|
588
|
-
|
|
589
|
-
|
|
633
|
+
Use pure planner traces for state and transition rules, including the results
|
|
634
|
+
of invoked work, which a scenario lists beside its events. Start a live machine
|
|
635
|
+
and use `MachineTest.probe` when a test depends on how the work actually runs,
|
|
636
|
+
on raised events, or on runtime scheduling.
|
|
590
637
|
|
|
591
638
|
### Choose observation deliberately
|
|
592
639
|
|
|
@@ -43,7 +43,7 @@ const Document = Machine.make({
|
|
|
43
43
|
})
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
Start with `
|
|
46
|
+
Start with `LocalMachine.start(Document, { locale: "en", documentId: "intro" })`.
|
|
47
47
|
Root construction runs before its initial child construction. Both receive the
|
|
48
48
|
same validated input. `documentId` belongs only to Loading; it need not be stored
|
|
49
49
|
in root data. Input is not retained as a reset value on the machine or snapshot.
|
|
@@ -183,7 +183,7 @@ history: {
|
|
|
183
183
|
|
|
184
184
|
A fallback must explicitly construct a complete tree containing its owner,
|
|
185
185
|
including required ancestors and parallel siblings. Its root data is state data,
|
|
186
|
-
not startup input. `
|
|
186
|
+
not startup input. `LocalMachine.resume` restores a validated snapshot without rerunning
|
|
187
187
|
startup constructors.
|
|
188
188
|
|
|
189
189
|
`guard` declines before construction. A resolver may return `select.decline()` only
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
## Run a machine on Effect Cluster
|
|
2
|
+
|
|
3
|
+
`ClusterMachine` from `@typeonce/effect-machine/cluster` runs a machine as a
|
|
4
|
+
persisted Cluster entity. It takes the same machine that `LocalMachine` runs.
|
|
5
|
+
Nothing about how the machine is written changes.
|
|
6
|
+
|
|
7
|
+
### Choose a host
|
|
8
|
+
|
|
9
|
+
| Host | Where the machine lives | What survives a crash |
|
|
10
|
+
| ---------------- | --------------------------------------------- | ---------------------------------------- |
|
|
11
|
+
| `LocalMachine` | in memory, owned by a `Scope` | nothing, unless you encode and resume it |
|
|
12
|
+
| `AtomMachine` | in memory, owned by an `AtomRegistry` | nothing |
|
|
13
|
+
| `ClusterMachine` | one entity per id, on any runner of a Cluster | every committed step, from `Storage` |
|
|
14
|
+
|
|
15
|
+
Use `ClusterMachine` when a machine must outlive the process that started it,
|
|
16
|
+
be addressed by id from anywhere, or wait on timers longer than a process
|
|
17
|
+
lives. Effect Cluster owns identity, placement, routing, and delivery.
|
|
18
|
+
`ClusterMachine` owns only the step: it applies one input at a time and commits
|
|
19
|
+
the result to `Storage`.
|
|
20
|
+
|
|
21
|
+
`ClusterMachine` needs `effect` 4.1.5 or later. Earlier versions answer the
|
|
22
|
+
keep-alive request of an entity after about ten minutes, so a machine whose
|
|
23
|
+
work runs or whose timer waits longer would not come up again after a crash.
|
|
24
|
+
|
|
25
|
+
### Make an adapter
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { Machine } from "@typeonce/effect-machine"
|
|
29
|
+
import { ClusterMachine, SqlMachineStorage } from "@typeonce/effect-machine/cluster"
|
|
30
|
+
import { Layer } from "effect"
|
|
31
|
+
import { SqlMessageStorage } from "effect/cluster"
|
|
32
|
+
|
|
33
|
+
const Invoice = ClusterMachine.make("Invoice", invoiceMachine, { version: "1" })
|
|
34
|
+
|
|
35
|
+
// Provide with a Cluster runner layer and an `SqlClient`.
|
|
36
|
+
const InvoiceLive = Invoice.toLayer().pipe(
|
|
37
|
+
Layer.provide(Layer.mergeAll(SqlMessageStorage.layer, SqlMachineStorage.layer))
|
|
38
|
+
)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`version` is stored with every checkpoint. A checkpoint written by another
|
|
42
|
+
definition id or version is refused, so change it together with a migration of
|
|
43
|
+
the stored checkpoints. Pass `shardGroup` to `ClusterMachine.make` to route
|
|
44
|
+
entities (and their invoked child entities) to named Cluster shard groups.
|
|
45
|
+
|
|
46
|
+
`adapter.toLayer(options)` forwards standard Effect Cluster `Entity.toLayer`
|
|
47
|
+
options (`maxIdleTime`, `concurrency`, `mailboxCapacity`, `mailboxMetrics`,
|
|
48
|
+
`disableFatalDefects`, `defectRetryPolicy`, `spanAttributes`) to the entity and
|
|
49
|
+
its children. It also accepts transactional hooks that run inside the same
|
|
50
|
+
`MessageStorage` transaction as the checkpoint write:
|
|
51
|
+
|
|
52
|
+
- `enqueue(event)` durably writes emitted events to an outbox table.
|
|
53
|
+
- `onCommit({ entityId, checkpoint, snapshot })` runs after every committed
|
|
54
|
+
checkpoint (active, done, failed), so SQL read-model projections or audit
|
|
55
|
+
tables commit or roll back atomically with the machine state.
|
|
56
|
+
|
|
57
|
+
State values, completion outputs, startup input, and public input events must
|
|
58
|
+
have JSON encoded forms. A schema that does not is a compile error that names
|
|
59
|
+
the offending states, events, or input.
|
|
60
|
+
|
|
61
|
+
### What runs, and how often
|
|
62
|
+
|
|
63
|
+
Each live entity keeps one machine in memory and applies requests, the ends
|
|
64
|
+
of invoked effects, and timers one at a time. `Storage` is the truth and
|
|
65
|
+
memory is a cache: an entity that comes up anywhere continues from its last
|
|
66
|
+
checkpoint.
|
|
67
|
+
|
|
68
|
+
Invoked effects run at least once. Work starts once the step that entered its
|
|
69
|
+
state is stored, and runs again after a crash, a move to another runner, or a
|
|
70
|
+
restart, with `Machine.InitialEvent` as the event of its input mapper. Two
|
|
71
|
+
copies can briefly run at the same time while a shard moves. Exactly one result
|
|
72
|
+
is applied, and results are not stored. Make every invoked effect safe to
|
|
73
|
+
repeat, for example with an idempotency key from the state.
|
|
74
|
+
|
|
75
|
+
Work that ends in a way the model does not handle fails the machine, as it
|
|
76
|
+
does locally. The failure is stored as text with the last state, the
|
|
77
|
+
snapshot reads `failed`, and every later request is rejected as
|
|
78
|
+
`MachineFailed`. There is no hidden retry: a model that recovers declares
|
|
79
|
+
`onFailure`. A failure of storage or of Cluster itself never fails the
|
|
80
|
+
machine; it is retried.
|
|
81
|
+
|
|
82
|
+
### Timers
|
|
83
|
+
|
|
84
|
+
A timer is stored as the time at which it is due, in the checkpoint of the step
|
|
85
|
+
that enters its state. An entity that comes up again waits only for the time
|
|
86
|
+
that remains, and completes a timer that is already due at once. A timer up to
|
|
87
|
+
`DurableClock.InMemoryThreshold` (one minute by default) keeps its entity in
|
|
88
|
+
memory. A longer one is saved as a delayed Cluster message, in the same
|
|
89
|
+
transaction, so the entity may leave memory and is brought back when the timer
|
|
90
|
+
is due, up to `entityMessagePollInterval` late.
|
|
91
|
+
|
|
92
|
+
This differs from `LocalMachine.resume`, which starts a timer's full duration
|
|
93
|
+
again.
|
|
94
|
+
|
|
95
|
+
### Children and messages between machines
|
|
96
|
+
|
|
97
|
+
A child machine invoked by a state runs as an entity of its own, of type
|
|
98
|
+
`Type/childId` and id `entityId/childId/revision`, and starts and stops with
|
|
99
|
+
the stored state. `onDone`, `onFailure`, and `onSnapshot` work as they do
|
|
100
|
+
locally. A failure of a child fails its parent, even with `onFailure`, because
|
|
101
|
+
the failure is stored as text.
|
|
102
|
+
|
|
103
|
+
`adapter.target(id)` names another Cluster machine. `enqueue.sendTo` with a
|
|
104
|
+
target or with the descriptor of an invoked child saves the event with the
|
|
105
|
+
checkpoint of the step, so it is sent if and only if the step is stored, and
|
|
106
|
+
applied once. `enqueue.stop` with the descriptor of an invoked child stops the
|
|
107
|
+
running child in the same transaction and settles its progress so it does not
|
|
108
|
+
restart while its state stays active. Commands run before the work of the states
|
|
109
|
+
a step enters starts, as they do locally, so both drop a child that the same step
|
|
110
|
+
enters. Work sends with `Machine.sendTo` and stops a child with
|
|
111
|
+
`Machine.stopChild`, given the child's descriptor, and an input mapper does the
|
|
112
|
+
same with `children.sendTo` and `children.stop`. `children.spawn` fails the
|
|
113
|
+
machine.
|
|
114
|
+
|
|
115
|
+
### What is refused, and why
|
|
116
|
+
|
|
117
|
+
These do not compile with `ClusterMachine.make`. The error names the property
|
|
118
|
+
`~effect/cluster/ClusterMachine/UnsupportedProcessLocal` and lists what to replace:
|
|
119
|
+
|
|
120
|
+
- a state that invokes a Stream or a `Logic`;
|
|
121
|
+
- work that requires `Machine.MachineRuntime`, such as `Machine.spawn`, or
|
|
122
|
+
`Machine.sendTo` and `Machine.stopChild` with a child address.
|
|
123
|
+
|
|
124
|
+
A checkpoint holds the state of a machine, and after a crash its work runs
|
|
125
|
+
again from the start. An Effect or a timer ends with one outcome, so running it
|
|
126
|
+
again changes nothing that was applied. A Stream ends after many elements, and
|
|
127
|
+
how far it got lives only in memory; a `Logic` has a state and a mailbox of its
|
|
128
|
+
own. Both would be lost. Model a source of many events as a child machine,
|
|
129
|
+
whose every step is stored, or as an Effect that ends with one result and a
|
|
130
|
+
state that is entered again for the next one.
|
|
131
|
+
|
|
132
|
+
### Talk to a machine from outside
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
const program = Effect.gen(function*() {
|
|
136
|
+
const client = (yield* Invoice.client)("invoice-1")
|
|
137
|
+
yield* client.send(Events.Send({ invoiceId: "inv-1" }))
|
|
138
|
+
const latest = yield* client.snapshot // Option of the last committed checkpoint
|
|
139
|
+
const receipt = yield* client.join // waits for the output, or fails with MachineFailedError
|
|
140
|
+
})
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
`start` initializes an entity with its per-entity startup input (or commits the
|
|
144
|
+
initial state of a machine without input without needing an event). It is an
|
|
145
|
+
idempotent no-op when the entity is already active, and rejects as
|
|
146
|
+
`MachineFailed` if the entity failed or was stopped. A machine that declares
|
|
147
|
+
`input` can omit a static default input in `ClusterMachine.make` so each entity
|
|
148
|
+
is started with `client.start(input)` (sending an event before start rejects as
|
|
149
|
+
`InvalidEvent`), or pass a default input so `send` can auto-start while
|
|
150
|
+
`client.start(customInput)` still starts with per-entity input.
|
|
151
|
+
|
|
152
|
+
`send` decodes the event as a local reference does and waits until the step is
|
|
153
|
+
committed. An event the machine refuses fails with `Rejected` and its reason.
|
|
154
|
+
Failures of Cluster itself are `ClientError`s; a request that ended in one may
|
|
155
|
+
still have been delivered, and is applied at most once when sent again.
|
|
156
|
+
|
|
157
|
+
`snapshot`, `changes`, and `join` read `Storage`, never the entity, so they
|
|
158
|
+
work from any process that reaches the store and never wake the entity. A
|
|
159
|
+
client is a remote view, narrower than a `MachineRef`: it sees committed
|
|
160
|
+
checkpoints only, `changes` polls every `readInterval` (one second by default)
|
|
161
|
+
and may skip checkpoints in between, a failure is text rather than a `Cause`,
|
|
162
|
+
and nothing is read before the first event is committed.
|
|
163
|
+
|
|
164
|
+
### Storage
|
|
165
|
+
|
|
166
|
+
`ClusterMachine.Storage` holds one checkpoint per entity and the ids of the
|
|
167
|
+
requests it committed. `SqlMachineStorage` implements it for PostgreSQL and
|
|
168
|
+
SQLite. Use it over the same `SqlClient` as `SqlMessageStorage`, so a
|
|
169
|
+
checkpoint, the events it sends, and the reply commit in one transaction. Its
|
|
170
|
+
`layer` runs migrations first; use `layerMigrations` in a deploy step and
|
|
171
|
+
`layerStorage` at runtime to run them with another connection.
|
|
172
|
+
`ClusterMachine.layerMemory` is for tests and is not durable.
|
|
173
|
+
|
|
174
|
+
To write a store of your own, run `ClusterMachine.checkStorage` against it in
|
|
175
|
+
your tests, with the `withTransaction` of its `MessageStorage`. It fails with
|
|
176
|
+
the first part of the contract the store breaks.
|
|
177
|
+
|
|
178
|
+
### Run in a Web Worker or over RPC
|
|
179
|
+
|
|
180
|
+
`ClusterMachine` and `SqlMachineStorage` have no Node.js dependencies, so a
|
|
181
|
+
single-node Cluster can run inside a Web Worker or `SharedWorker` with
|
|
182
|
+
`SingleRunner.layer` and a browser SQL client such as `@effect/sql-sqlite-wasm`
|
|
183
|
+
or `@effect/sql-pglite`.
|
|
184
|
+
|
|
185
|
+
When the runner and its database live in another thread or process, callers do
|
|
186
|
+
not have direct access to `Sharding` or `ClusterMachine.Storage`.
|
|
187
|
+
`ClusterMachineProxy` from `@typeonce/effect-machine/cluster` exposes `start`,
|
|
188
|
+
`send`, `snapshot`, `changes`, and `join` over any `effect/rpc` transport (Web
|
|
189
|
+
Workers, `SharedWorker`, Node worker threads, WebSockets, or HTTP):
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
// Inside the Web Worker: serve one machine over RpcServer.Protocol
|
|
193
|
+
const WorkerLive = ClusterMachineProxy.toLayer(Invoice).pipe(
|
|
194
|
+
Layer.provide(Invoice.toLayer()),
|
|
195
|
+
Layer.provide(ClusterLive),
|
|
196
|
+
Layer.provide(RpcServer.layerProtocolWorkerRunner)
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
// On the main thread: zero Sharding, zero Storage, zero SQL
|
|
200
|
+
const program = Effect.gen(function*() {
|
|
201
|
+
const client = (yield* ClusterMachineProxy.client(Invoice))("invoice-1")
|
|
202
|
+
yield* client.send(Events.Send({ invoiceId: "inv-1" }))
|
|
203
|
+
const latest = yield* client.snapshot
|
|
204
|
+
const receipt = yield* client.join
|
|
205
|
+
})
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
To host several machines on one worker or server, merge their groups with
|
|
209
|
+
`ClusterMachineProxy.toRpcGroup(A).merge(ClusterMachineProxy.toRpcGroup(B))` and
|
|
210
|
+
provide each with `ClusterMachineProxy.layerHandlers`. When using
|
|
211
|
+
`RpcClient.layerProtocolWorker` with `size: 1`, set `concurrency` above `1` (for
|
|
212
|
+
example `{ size: 1, concurrency: 100 }`) so streaming `changes` or `join` calls
|
|
213
|
+
do not block concurrent `send` calls.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
## Write a host for a new runtime
|
|
2
|
+
|
|
3
|
+
Applications run machines with `LocalMachine`, `AtomMachine`, or
|
|
4
|
+
`ClusterMachine`. Use `Machine.toStep` only to run a machine somewhere these
|
|
5
|
+
hosts do not reach, such as a UI store that keeps the state itself.
|
|
6
|
+
|
|
7
|
+
`Machine.toStep(machine, options)` returns pure functions. They run nothing and
|
|
8
|
+
call no user code: each one returns the next state as a value, with the
|
|
9
|
+
commands to carry out and the work to stop and to start.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
const step = Machine.toStep(machine, { self, parent, children })
|
|
13
|
+
|
|
14
|
+
step.start(input) // Exit<Step, StartError>: the machine starts
|
|
15
|
+
step.send(state, event) // Exit<Step, Rejection>: a public event arrives
|
|
16
|
+
step.settle(state, work, exit) // Step: work ended, or could not start
|
|
17
|
+
step.element(state, work, value) // Step: a running Stream produced a value
|
|
18
|
+
step.report(state, work, snapshot) // Step: a running Logic or child machine reached a state
|
|
19
|
+
step.stop(state) // Step: the owner stops the machine
|
|
20
|
+
step.resume(snapshot, progress) // Exit<Step, MachineSchemaDecodeError>: from a decoded snapshot
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The options are what the machine needs of its host:
|
|
24
|
+
|
|
25
|
+
- `self` is the host's own target. Transitions and input mappers receive it,
|
|
26
|
+
so its `send` must plan the event in a later step.
|
|
27
|
+
- `parent` is the target of the owner, for a machine that declares a parent.
|
|
28
|
+
It is required when the parent is.
|
|
29
|
+
- `children` are the operations on the children of the machine, which the host
|
|
30
|
+
owns: `spawn`, `spawnLogic`, `sendTo`, and `stop`. They are required when work
|
|
31
|
+
requires `Machine.MachineRuntime` or `Machine.ChildMachineRuntime`. Without
|
|
32
|
+
them, every operation on children fails the machine with `ProcessLocalError`.
|
|
33
|
+
|
|
34
|
+
A host carries out each `Step` in this order: carry out `commands`, interrupt
|
|
35
|
+
`stop`, publish `snapshot`, deliver `emitted`, start `start`, then interrupt
|
|
36
|
+
`stopAfter`. Pass `step.state` to the next call.
|
|
37
|
+
|
|
38
|
+
- A failed `Exit` from `send`, `start`, or `resume` means nothing happened. An
|
|
39
|
+
event is rejected because its schema refused it or because the machine has
|
|
40
|
+
ended, and the state you passed still holds.
|
|
41
|
+
- A machine that fails or ends still returns a `Step`. Its `exit` is set: when
|
|
42
|
+
it reaches a final state, `stop` lists the work of exited states (interrupted
|
|
43
|
+
before `snapshot` and `emitted`) and `stopAfter` lists the remaining work of
|
|
44
|
+
states that stayed active (interrupted after `emitted`, latest first); when it
|
|
45
|
+
fails or is stopped by its owner, `stop` lists every piece of work that still
|
|
46
|
+
runs and `stopAfter` is empty.
|
|
47
|
+
- `commands` are `Send` (call `target.send(event)`), `SendToChild`, and
|
|
48
|
+
`StopChild`. A command to a child names the running `Logic` or child machine
|
|
49
|
+
it selects as `work`. A stopped one no longer runs, and reports nothing. With
|
|
50
|
+
no `work`, the command is for a child that work spawned.
|
|
51
|
+
- `snapshot` and `microsteps` are built when first read. `event` is the event
|
|
52
|
+
the step planned, for inspection.
|
|
53
|
+
|
|
54
|
+
Work is data. The host evaluates each piece as it starts it, in order, with the
|
|
55
|
+
accessor of its kind. Each accessor calls the source and the input mapper at
|
|
56
|
+
most once and returns an `Exit`:
|
|
57
|
+
|
|
58
|
+
- `Effect`: `work.effect()`, then run it and settle its `Exit`.
|
|
59
|
+
- `Timer`: `work.duration()` holds how long to wait, then settle with
|
|
60
|
+
`Exit.void`. It fails, like every accessor, when the timer cannot start,
|
|
61
|
+
also when its input names no duration. `work.effect()` is the same timer as
|
|
62
|
+
an Effect, for a host whose timers wait on fibers.
|
|
63
|
+
- `Stream`: `work.stream()`. Report each element with `element`, pull the next
|
|
64
|
+
element after that step, and settle with the end of the stream.
|
|
65
|
+
- `Logic`: `work.logic()`. Run `initial` and `run` with contexts of the host,
|
|
66
|
+
`report` each state it sets, and settle with the end of `run`.
|
|
67
|
+
- `Child`: `work.input()` holds the arguments of its `start`. Host
|
|
68
|
+
`work.child.machine` with `Machine.toStep` again, with this machine as its
|
|
69
|
+
`parent`, `report` its snapshots, and settle with its `exit`.
|
|
70
|
+
|
|
71
|
+
A failed accessor means the work could not start: settle the work with that
|
|
72
|
+
`Exit` at once. The machine fails and runs no more work, so do not start the rest
|
|
73
|
+
of the step.
|
|
74
|
+
|
|
75
|
+
The state value records which entry of a state each piece of work belongs to.
|
|
76
|
+
`settle`, `element`, and `report` ignore work whose state was exited, so the host
|
|
77
|
+
never compares anything. Pass back the work object that a step listed: work is
|
|
78
|
+
matched by identity, so a result from before a `resume` is ignored too.
|
|
79
|
+
|
|
80
|
+
A durable host passes `progress` to `resume`: `{ path, id, status }` for each
|
|
81
|
+
invocation of an active state that `"Ended"` or is still `"Running"`. Ended work
|
|
82
|
+
is not listed again. Running work is not in `start`; find it in `step.running`
|
|
83
|
+
and settle it when it ends. To keep that progress, read `step.exited` and
|
|
84
|
+
`step.entered`, the paths of the states a step left and entered, without
|
|
85
|
+
building a snapshot: forget what you stored for the work of an exited state.
|
|
86
|
+
A result that is ignored returns a step whose `state` is the state you passed.
|
|
87
|
+
`ClusterMachine` is such a host.
|
|
88
|
+
|
|
89
|
+
A host that runs only some kinds of work refuses the others at compile time:
|
|
90
|
+
`machine: M & Machine.MachineStep.Supported<M, "Stream">`.
|