@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
|
@@ -0,0 +1,957 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pure form of a machine: what its next state is, and which work a host
|
|
3
|
+
* starts and stops, shared by every host that runs a machine.
|
|
4
|
+
*
|
|
5
|
+
* Every function is synchronous and returns a value. The host state holds the
|
|
6
|
+
* execution state of the plan, the work that runs, and how many entries of
|
|
7
|
+
* invoking states have begun. Whether work is current is read from
|
|
8
|
+
* the state that a caller passes back, and never from a handle shared with the
|
|
9
|
+
* caller: work is current when that state lists the very object the caller
|
|
10
|
+
* reports, so work of an earlier entry, of a step that was not kept, or of a
|
|
11
|
+
* machine before it resumed, is never taken for the work that runs.
|
|
12
|
+
*
|
|
13
|
+
* A step plans with the execution plan passed to its kernel — normally
|
|
14
|
+
* `compileExecutionPlan` (an indexed kernel for the machines and events it
|
|
15
|
+
* covers, and the reference planner for anything else), or
|
|
16
|
+
* `referenceExecutionPlan` when a caller forces the reference planner.
|
|
17
|
+
*
|
|
18
|
+
* A step function has an {@link Ownership}. A `"Persistent"` step, which
|
|
19
|
+
* `Machine.toStep` returns, takes and returns immutable state values. An
|
|
20
|
+
* `"Owned"` step, for a host that keeps only its latest state, takes each state
|
|
21
|
+
* over and may update its storage in place. See `build` for the full list of
|
|
22
|
+
* differences between the two ownerships.
|
|
23
|
+
*
|
|
24
|
+
* A host applies a step in one order, as the local host in `stepHost.ts`
|
|
25
|
+
* does: commands, `stop` (the work of exited states, or all work when the
|
|
26
|
+
* machine fails or is stopped), the new state, emitted events, `start` (the
|
|
27
|
+
* work of entered states), and `stopAfter` (remaining work of states that
|
|
28
|
+
* stayed active when the machine reached a final state). A step calls no user
|
|
29
|
+
* code for the work it lists: each piece of work evaluates its source and its
|
|
30
|
+
* input mapper when its host starts it, at most once, inside its own startup
|
|
31
|
+
* failure boundary.
|
|
32
|
+
*
|
|
33
|
+
* @since 0.48.0
|
|
34
|
+
*/
|
|
35
|
+
import * as Context from "effect/Context";
|
|
36
|
+
import * as Effect from "effect/Effect";
|
|
37
|
+
import * as Equal from "effect/Equal";
|
|
38
|
+
import * as Exit from "effect/Exit";
|
|
39
|
+
import * as Hash from "effect/Hash";
|
|
40
|
+
import * as Stream from "effect/Stream";
|
|
41
|
+
import { ChildMachineRuntime, MachineRuntime } from "./capability.js";
|
|
42
|
+
import * as CapturedInvocation from "./capturedInvocation.js";
|
|
43
|
+
import { isChildMachine } from "./child.js";
|
|
44
|
+
import * as Configuration from "./configuration.js";
|
|
45
|
+
import { isPlanningError, MachineSchemaDecodeError, ProcessLocalError, StoppedError, toStartupFailure } from "./errors.js";
|
|
46
|
+
import * as ExecutionPlan from "./executionPlan.js";
|
|
47
|
+
import * as InvocationChanges from "./invocationChanges.js";
|
|
48
|
+
import * as InvocationEvent from "./invocationEvent.js";
|
|
49
|
+
import * as InvocationOutcome from "./invocationOutcome.js";
|
|
50
|
+
import * as InvocationResolution from "./invocationResolution.js";
|
|
51
|
+
import * as LogicScope from "./logicScope.js";
|
|
52
|
+
import * as Planner from "./planner.js";
|
|
53
|
+
import * as Protocol from "./protocol.js";
|
|
54
|
+
import * as Serialization from "./serialization.js";
|
|
55
|
+
/** @internal */
|
|
56
|
+
export const StateTypeId = Symbol.for("effect/Machine/MachineStep/State");
|
|
57
|
+
/** @internal */
|
|
58
|
+
export const WorkTypeId = Symbol.for("effect/Machine/MachineStep/Work");
|
|
59
|
+
/** @internal */
|
|
60
|
+
export const OwnershipTypeId = Symbol.for("effect/Machine/MachineStep/Ownership");
|
|
61
|
+
/**
|
|
62
|
+
* Shared planning host base: holds only the machine and execution plan, so
|
|
63
|
+
* subclasses such as the local host can expose their scope's targets and
|
|
64
|
+
* children from the prototype without duplicating own slots.
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
export class PlanningBase {
|
|
69
|
+
machine;
|
|
70
|
+
plan;
|
|
71
|
+
constructor(machine, plan) {
|
|
72
|
+
this.machine = machine;
|
|
73
|
+
this.plan = plan;
|
|
74
|
+
}
|
|
75
|
+
// A host is compared by identity: hashing a state, as a failure that holds one
|
|
76
|
+
// may be hashed, never reads the targets of the host.
|
|
77
|
+
[Hash.symbol]() {
|
|
78
|
+
return Hash.random(this);
|
|
79
|
+
}
|
|
80
|
+
[Equal.symbol](that) {
|
|
81
|
+
return this === that;
|
|
82
|
+
}
|
|
83
|
+
/** What the source and input mapper of work receive, built for each entry that resolves work. */
|
|
84
|
+
get capabilities() {
|
|
85
|
+
return { self: this.self, parent: this.parent, children: childOwnerOf(this.children) };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The machine of a step function, the plan it steps with, and the host it runs
|
|
90
|
+
* on: what its transitions address, and the children its work acts on. A host
|
|
91
|
+
* builds one for each machine it runs, and every state of that machine carries
|
|
92
|
+
* it. What work needs of it is built when work first needs it, so a machine
|
|
93
|
+
* that invokes nothing holds only these fields.
|
|
94
|
+
*
|
|
95
|
+
* @internal
|
|
96
|
+
*/
|
|
97
|
+
export class Planning extends PlanningBase {
|
|
98
|
+
self;
|
|
99
|
+
parent;
|
|
100
|
+
ambient;
|
|
101
|
+
#services;
|
|
102
|
+
children;
|
|
103
|
+
constructor(machine, plan, self, parent,
|
|
104
|
+
/** The children of the machine, or `undefined` when the host supplies none. */
|
|
105
|
+
children = refusedChildren,
|
|
106
|
+
/**
|
|
107
|
+
* Whether the host runs work inside the services of this machine already,
|
|
108
|
+
* as the local runtime runs work in processes of the machine, so work is
|
|
109
|
+
* not provided them again.
|
|
110
|
+
*/
|
|
111
|
+
ambient = false) {
|
|
112
|
+
super(machine, plan);
|
|
113
|
+
this.self = self;
|
|
114
|
+
this.parent = parent;
|
|
115
|
+
this.ambient = ambient;
|
|
116
|
+
this.children = children;
|
|
117
|
+
}
|
|
118
|
+
/** The services through which work acts on the children of this machine. */
|
|
119
|
+
get services() {
|
|
120
|
+
return this.#services ??= servicesOf(this.children);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const empty = [];
|
|
124
|
+
/**
|
|
125
|
+
* The host state. `execution` is the execution state of the plan, which
|
|
126
|
+
* carries no machine references: they are attached for each plan, as the local
|
|
127
|
+
* runtime attaches them. The configuration and the public snapshot are built
|
|
128
|
+
* from it on first read.
|
|
129
|
+
*
|
|
130
|
+
* A state that rests holds only its snapshot, its work and its runs: its
|
|
131
|
+
* execution state is built again from the snapshot when a step needs it, as a
|
|
132
|
+
* host that keeps a machine idle does not hold it.
|
|
133
|
+
*
|
|
134
|
+
* A `"Persistent"` state never changes after it is built. The `execution` of
|
|
135
|
+
* an `"Owned"` state is written in place by the step that it is handed over to,
|
|
136
|
+
* and so is everything read from it that was not read before.
|
|
137
|
+
*
|
|
138
|
+
* @internal
|
|
139
|
+
*/
|
|
140
|
+
export class State {
|
|
141
|
+
planning;
|
|
142
|
+
running;
|
|
143
|
+
runs;
|
|
144
|
+
exit;
|
|
145
|
+
#execution;
|
|
146
|
+
#configuration;
|
|
147
|
+
#snapshot;
|
|
148
|
+
/** Whether an owned state was handed over to a step, which may have written it. */
|
|
149
|
+
#handedOver = false;
|
|
150
|
+
constructor(planning,
|
|
151
|
+
/** The execution state of the plan, or `undefined` for a state that rests. */
|
|
152
|
+
execution, configuration, snapshot,
|
|
153
|
+
/** The work that runs, in start order. */
|
|
154
|
+
running,
|
|
155
|
+
/** How many entries of invoking states have begun: the run of the next one. */
|
|
156
|
+
runs, exit) {
|
|
157
|
+
this.planning = planning;
|
|
158
|
+
this.running = running;
|
|
159
|
+
this.runs = runs;
|
|
160
|
+
this.exit = exit;
|
|
161
|
+
this.#execution = execution;
|
|
162
|
+
this.#configuration = configuration;
|
|
163
|
+
this.#snapshot = snapshot;
|
|
164
|
+
}
|
|
165
|
+
get machine() {
|
|
166
|
+
return this.planning.machine;
|
|
167
|
+
}
|
|
168
|
+
/** The execution state of the plan. Building it again from the snapshot may throw. */
|
|
169
|
+
get execution() {
|
|
170
|
+
const execution = this.#execution;
|
|
171
|
+
return execution !== undefined
|
|
172
|
+
? execution
|
|
173
|
+
: (this.#execution = this.planning.plan.fromConfiguration(this.configuration));
|
|
174
|
+
}
|
|
175
|
+
get configuration() {
|
|
176
|
+
const configuration = this.#configuration;
|
|
177
|
+
if (configuration !== undefined)
|
|
178
|
+
return configuration;
|
|
179
|
+
return this.#configuration = this.#execution === undefined
|
|
180
|
+
? Configuration.normalizeConfigurationSync(this.planning.machine, this.#snapshot)
|
|
181
|
+
: this.planning.plan.toConfiguration(this.#execution);
|
|
182
|
+
}
|
|
183
|
+
get snapshot() {
|
|
184
|
+
const snapshot = this.#snapshot;
|
|
185
|
+
return snapshot !== undefined ? snapshot : (this.#snapshot = this.planning.plan.snapshot(this.#execution));
|
|
186
|
+
}
|
|
187
|
+
/** This state with other running work, keeping what was read of it. */
|
|
188
|
+
withRunning(running) {
|
|
189
|
+
return new State(this.planning, this.#execution, this.#configuration, this.#snapshot, running, this.runs, this.exit);
|
|
190
|
+
}
|
|
191
|
+
/** This state as it rests: its snapshot, its work and its runs. */
|
|
192
|
+
resting() {
|
|
193
|
+
return new State(this.planning, undefined, undefined, this.snapshot, this.running, this.runs, this.exit);
|
|
194
|
+
}
|
|
195
|
+
/** Updates an owned state in place to rest without allocating a new wrapper. */
|
|
196
|
+
restInPlace() {
|
|
197
|
+
if (this.#handedOver) {
|
|
198
|
+
throw new Error("An owned MachineStep state was reused after it was handed over to a step: never reuse an owned state after passing it");
|
|
199
|
+
}
|
|
200
|
+
this.#snapshot ??= this.planning.plan.snapshot(this.#execution);
|
|
201
|
+
this.#execution = undefined;
|
|
202
|
+
this.#configuration = undefined;
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
/** Updates an owned state's execution and snapshot in place when a move needs no new state wrapper. */
|
|
206
|
+
replaceExecution(execution, snapshot) {
|
|
207
|
+
this.#execution = execution;
|
|
208
|
+
this.#configuration = undefined;
|
|
209
|
+
this.#snapshot = snapshot;
|
|
210
|
+
}
|
|
211
|
+
/** What was built of this state, without building anything. */
|
|
212
|
+
get built() {
|
|
213
|
+
return { execution: this.#execution, configuration: this.#configuration };
|
|
214
|
+
}
|
|
215
|
+
/** Checks that an owned state has not been handed over, without marking it handed over. */
|
|
216
|
+
assertNotHandedOver() {
|
|
217
|
+
if (this.#handedOver) {
|
|
218
|
+
throw new Error("An owned MachineStep state was reused after it was handed over to a step: never reuse an owned state after passing it");
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Hands an owned state over to one step. From this call on, the caller
|
|
223
|
+
* promises never to reuse this state, nor any step that returned it: the step
|
|
224
|
+
* may write it in place. Handing over a state twice throws.
|
|
225
|
+
*/
|
|
226
|
+
handOverNeverReuse() {
|
|
227
|
+
this.assertNotHandedOver();
|
|
228
|
+
this.#handedOver = true;
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Gives an owned state back to its caller, only when the step returned this
|
|
233
|
+
* very state unchanged, or refused the event: the caller may pass it again.
|
|
234
|
+
*/
|
|
235
|
+
giveBackUnchanged() {
|
|
236
|
+
this.#handedOver = false;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
const noPaths = { exited: empty, entered: empty };
|
|
240
|
+
/** The paths of an initial step, sorted when first read. */
|
|
241
|
+
class InitialPaths {
|
|
242
|
+
machine;
|
|
243
|
+
paths;
|
|
244
|
+
#entered;
|
|
245
|
+
exited = empty;
|
|
246
|
+
constructor(machine, paths) {
|
|
247
|
+
this.machine = machine;
|
|
248
|
+
this.paths = paths;
|
|
249
|
+
}
|
|
250
|
+
get entered() {
|
|
251
|
+
return this.#entered ??= Planner.sortEntryPaths(this.machine, typeof this.paths === "function" ? this.paths() : this.paths);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const pathsOf = (microsteps) => {
|
|
255
|
+
const changes = InvocationChanges.fromMicrosteps(microsteps);
|
|
256
|
+
return changes === undefined
|
|
257
|
+
? noPaths
|
|
258
|
+
: { exited: Array.from(new Set(changes.exited)), entered: Array.from(changes.entered.keys()) };
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* What one step did. Its microsteps are made public on first read.
|
|
262
|
+
*
|
|
263
|
+
* @internal
|
|
264
|
+
*/
|
|
265
|
+
export class Step {
|
|
266
|
+
state;
|
|
267
|
+
event;
|
|
268
|
+
emitted;
|
|
269
|
+
commands;
|
|
270
|
+
stop;
|
|
271
|
+
start;
|
|
272
|
+
stopAfter;
|
|
273
|
+
#microsteps;
|
|
274
|
+
#published;
|
|
275
|
+
#paths;
|
|
276
|
+
constructor(state, event, microsteps, emitted, commands, stop, start, stopAfter = empty,
|
|
277
|
+
/** The paths of an initial step, which its microsteps do not list. Read from the microsteps otherwise. */
|
|
278
|
+
paths) {
|
|
279
|
+
this.state = state;
|
|
280
|
+
this.event = event;
|
|
281
|
+
this.emitted = emitted;
|
|
282
|
+
this.commands = commands;
|
|
283
|
+
this.stop = stop;
|
|
284
|
+
this.start = start;
|
|
285
|
+
this.stopAfter = stopAfter;
|
|
286
|
+
this.#microsteps = microsteps;
|
|
287
|
+
this.#paths = paths;
|
|
288
|
+
}
|
|
289
|
+
/** Paths of the states the step exited, in exit order, each once. */
|
|
290
|
+
get exited() {
|
|
291
|
+
return (this.#paths ??= pathsOf(this.#microsteps ?? this.#published)).exited;
|
|
292
|
+
}
|
|
293
|
+
/** Paths of the states the step entered, in entry order, each once. */
|
|
294
|
+
get entered() {
|
|
295
|
+
return (this.#paths ??= pathsOf(this.#microsteps ?? this.#published)).entered;
|
|
296
|
+
}
|
|
297
|
+
get snapshot() {
|
|
298
|
+
return this.state.snapshot;
|
|
299
|
+
}
|
|
300
|
+
/** Whether the step planned a microstep, so its state is a new one to publish. */
|
|
301
|
+
get planned() {
|
|
302
|
+
return (this.#microsteps ?? this.#published).length > 0;
|
|
303
|
+
}
|
|
304
|
+
get running() {
|
|
305
|
+
return this.state.running;
|
|
306
|
+
}
|
|
307
|
+
get exit() {
|
|
308
|
+
return this.state.exit;
|
|
309
|
+
}
|
|
310
|
+
get microsteps() {
|
|
311
|
+
if (this.#published === undefined) {
|
|
312
|
+
const plan = this.state.planning.plan;
|
|
313
|
+
const planned = this.#microsteps;
|
|
314
|
+
this.#microsteps = undefined;
|
|
315
|
+
// Initial planning has published its microsteps already.
|
|
316
|
+
this.#published = planned.length === 0 || this.event === Planner.InitialEvent
|
|
317
|
+
? planned
|
|
318
|
+
: planned.map((microstep) => ({
|
|
319
|
+
next: plan.snapshot(microstep.next),
|
|
320
|
+
event: microstep.event,
|
|
321
|
+
transitions: microstep.transitions,
|
|
322
|
+
commands: microstep.commands,
|
|
323
|
+
raisedEvents: microstep.raisedEvents,
|
|
324
|
+
emittedEvents: microstep.emittedEvents,
|
|
325
|
+
exitPaths: microstep.exitPaths,
|
|
326
|
+
entryPaths: microstep.entryPaths,
|
|
327
|
+
changed: microstep.changed
|
|
328
|
+
}));
|
|
329
|
+
}
|
|
330
|
+
return this.#published;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/** @internal */
|
|
334
|
+
export const isState = (value) => value instanceof State;
|
|
335
|
+
/**
|
|
336
|
+
* One piece of work, as a step lists it. What it describes is read from its
|
|
337
|
+
* invocation, and each accessor evaluates user code once and keeps its result,
|
|
338
|
+
* so a piece of work holds no closure of its own. Work is compared by identity.
|
|
339
|
+
*/
|
|
340
|
+
class WorkOf {
|
|
341
|
+
run;
|
|
342
|
+
planning;
|
|
343
|
+
#evaluated;
|
|
344
|
+
[WorkTypeId];
|
|
345
|
+
constructor(invocation, run, planning) {
|
|
346
|
+
this.run = run;
|
|
347
|
+
this.planning = planning;
|
|
348
|
+
this[WorkTypeId] = invocation;
|
|
349
|
+
}
|
|
350
|
+
[Hash.symbol]() {
|
|
351
|
+
return Hash.random(this);
|
|
352
|
+
}
|
|
353
|
+
[Equal.symbol](that) {
|
|
354
|
+
return this === that;
|
|
355
|
+
}
|
|
356
|
+
get _tag() {
|
|
357
|
+
return this[WorkTypeId]._tag;
|
|
358
|
+
}
|
|
359
|
+
get path() {
|
|
360
|
+
return this[WorkTypeId].path;
|
|
361
|
+
}
|
|
362
|
+
get id() {
|
|
363
|
+
return this[WorkTypeId].id;
|
|
364
|
+
}
|
|
365
|
+
get source() {
|
|
366
|
+
const invocation = this[WorkTypeId];
|
|
367
|
+
return invocation._tag === "Child" ? undefined : invocation.sourceName;
|
|
368
|
+
}
|
|
369
|
+
get address() {
|
|
370
|
+
const invocation = this[WorkTypeId];
|
|
371
|
+
return invocation._tag === "Logic" ? invocation.address : undefined;
|
|
372
|
+
}
|
|
373
|
+
get child() {
|
|
374
|
+
const invocation = this[WorkTypeId];
|
|
375
|
+
return invocation._tag === "Child" ? invocation.child : undefined;
|
|
376
|
+
}
|
|
377
|
+
/** Evaluates user code once, and keeps its result or its throw. */
|
|
378
|
+
evaluate(evaluate) {
|
|
379
|
+
if (this.#evaluated === undefined) {
|
|
380
|
+
try {
|
|
381
|
+
this.#evaluated = Exit.succeed(evaluate(this[WorkTypeId]));
|
|
382
|
+
}
|
|
383
|
+
catch (error) {
|
|
384
|
+
this.#evaluated = Exit.die(error);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return this.#evaluated;
|
|
388
|
+
}
|
|
389
|
+
/** Work acts on the children of its machine through the services of its host. */
|
|
390
|
+
provide(effect) {
|
|
391
|
+
const planning = this.planning;
|
|
392
|
+
return planning === undefined || planning.ambient
|
|
393
|
+
? effect
|
|
394
|
+
: Effect.updateContext(effect, (context) => Context.merge(context, planning.services));
|
|
395
|
+
}
|
|
396
|
+
effect() {
|
|
397
|
+
if (this[WorkTypeId]._tag === "Timer")
|
|
398
|
+
return Exit.map(this.duration(), Effect.sleep);
|
|
399
|
+
// Each run owns a scope, closed with its exit, as on the local runtime.
|
|
400
|
+
return this.evaluate((invocation) => this.provide(Effect.scoped(invocation.effect())));
|
|
401
|
+
}
|
|
402
|
+
duration() {
|
|
403
|
+
return this.evaluate((invocation) => invocation.duration());
|
|
404
|
+
}
|
|
405
|
+
stream() {
|
|
406
|
+
// A run of a Stream owns a scope too, closed with its end or its stop.
|
|
407
|
+
return this.evaluate((invocation) => {
|
|
408
|
+
const planning = this.planning;
|
|
409
|
+
const stream = Stream.scoped(invocation.stream());
|
|
410
|
+
return planning === undefined || planning.ambient
|
|
411
|
+
? stream
|
|
412
|
+
: Stream.updateContext(stream, (context) => Context.merge(context, planning.services));
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
logic() {
|
|
416
|
+
return this.evaluate((invocation) => {
|
|
417
|
+
const scoped = LogicScope.scoped(invocation.logic());
|
|
418
|
+
if (this.planning === undefined || this.planning.ambient)
|
|
419
|
+
return scoped;
|
|
420
|
+
return {
|
|
421
|
+
initial: (context) => this.provide(scoped.initial(context)),
|
|
422
|
+
run: (context) => this.provide(scoped.run(context))
|
|
423
|
+
};
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
input() {
|
|
427
|
+
return this.evaluate((invocation) => invocation.input === undefined ? empty : [invocation.input()]);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
const unchanged = (state) => new Step(state, undefined, empty, empty, empty, empty, empty);
|
|
431
|
+
/** Builds the step of a childless machine for a planned macrostep. */
|
|
432
|
+
const childlessStep = (state, planned, commands) => new Step(new State(state.planning, planned.next, undefined, undefined, empty, state.runs, planned.done ? Exit.succeed(planned.output) : undefined), planned.event, planned.microsteps, planned.emittedEvents, commands, empty, empty);
|
|
433
|
+
/** A planning throw: typed for the planner's own errors, a defect otherwise. */
|
|
434
|
+
const exitOfThrow = (error) => isPlanningError(error) ? Exit.fail(error) : Exit.die(error);
|
|
435
|
+
/** The work that runs in a state, latest first, as a host stops it when its machine ends. */
|
|
436
|
+
const latestFirst = (running) => running.length < 2 ? running : running.slice().reverse();
|
|
437
|
+
/** Whether a running piece of work is the child that a command selects, as the local registry matches it. */
|
|
438
|
+
const selects = (work, child) => typeof child === "string"
|
|
439
|
+
? (work._tag === "Logic" && work.address === child) || (work._tag === "Child" && work.child.id === child)
|
|
440
|
+
: work._tag === "Child" && work.child.id === child.id && work.child.machine === child.machine;
|
|
441
|
+
const refuse = (operation) => Effect.die(new ProcessLocalError({ operation }));
|
|
442
|
+
// A host that supplies no children owns none, as on Cluster.
|
|
443
|
+
const refusedChildren = {
|
|
444
|
+
spawn: () => refuse("children.spawn"),
|
|
445
|
+
spawnLogic: () => refuse("spawn"),
|
|
446
|
+
sendTo: () => refuse("sendTo"),
|
|
447
|
+
stop: () => refuse("stopChild")
|
|
448
|
+
};
|
|
449
|
+
const childOwnerOf = (children) => ({
|
|
450
|
+
spawn: ((child, ...options) => children.spawn(child, options[0])),
|
|
451
|
+
sendTo: (child, event) => children.sendTo(child, event),
|
|
452
|
+
stop: (child) => children.stop(child)
|
|
453
|
+
});
|
|
454
|
+
const servicesOf = (children) => Context.make(MachineRuntime, MachineRuntime.of({
|
|
455
|
+
spawn: ((logic, options) => children.spawnLogic(logic, options)),
|
|
456
|
+
sendTo: (child, event) => children.sendTo(child, event),
|
|
457
|
+
stopChild: (child) => children.stop(child)
|
|
458
|
+
})).pipe(Context.add(ChildMachineRuntime, ChildMachineRuntime.of({
|
|
459
|
+
sendTo: (child, event) => children.sendTo(child, event),
|
|
460
|
+
stopChild: (child) => children.stop(child)
|
|
461
|
+
})));
|
|
462
|
+
/** The paths whose states invoke work. */
|
|
463
|
+
const invokingPathsCache = new WeakMap();
|
|
464
|
+
const invokingPaths = (machine) => {
|
|
465
|
+
let paths = invokingPathsCache.get(machine);
|
|
466
|
+
if (paths === undefined) {
|
|
467
|
+
const invoking = new Set();
|
|
468
|
+
for (const path of machine.stateNodes.byPath.keys()) {
|
|
469
|
+
if (CapturedInvocation.list(Configuration.getStateConfigByPath(machine, path)?.invoke).length > 0) {
|
|
470
|
+
invoking.add(path);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
paths = invoking;
|
|
474
|
+
invokingPathsCache.set(machine, paths);
|
|
475
|
+
}
|
|
476
|
+
return paths;
|
|
477
|
+
};
|
|
478
|
+
const observesSnapshots = (work) => work[WorkTypeId].channels.snapshot;
|
|
479
|
+
const settleEvent = (work, exit) => {
|
|
480
|
+
const decision = InvocationOutcome.decide(work[WorkTypeId], { current: true }, InvocationOutcome.fromExit(exit));
|
|
481
|
+
return decision._tag === "Deliver" ? decision.event : undefined;
|
|
482
|
+
};
|
|
483
|
+
const elementEvent = (work, value) => {
|
|
484
|
+
const invocation = work[WorkTypeId];
|
|
485
|
+
if (invocation._tag !== "Stream")
|
|
486
|
+
return undefined;
|
|
487
|
+
const decision = InvocationOutcome.decideElement(invocation, { current: true }, value);
|
|
488
|
+
return decision._tag === "Deliver" ? decision.event : undefined;
|
|
489
|
+
};
|
|
490
|
+
const snapshotEvent = (work, snapshot) => {
|
|
491
|
+
const invocation = work[WorkTypeId];
|
|
492
|
+
if (invocation._tag !== "Logic" && invocation._tag !== "Child")
|
|
493
|
+
return undefined;
|
|
494
|
+
const decision = InvocationOutcome.decideSnapshot(invocation, { current: true }, {
|
|
495
|
+
status: "active",
|
|
496
|
+
state: snapshot
|
|
497
|
+
});
|
|
498
|
+
return decision._tag === "Deliver" ? decision.event : undefined;
|
|
499
|
+
};
|
|
500
|
+
/**
|
|
501
|
+
* The step functions of `machine`, which plan with `plan` and take states of
|
|
502
|
+
* `ownership`. Every difference between `"Owned"` and `"Persistent"` lives here:
|
|
503
|
+
*
|
|
504
|
+
* 1. **Execution-state writes**: `ownership` is passed to `plan.planDecoded`
|
|
505
|
+
* (a `"Persistent"` plan copies what it writes; an `"Owned"` flat indexed
|
|
506
|
+
* plan writes active values in place, restoring them on throw).
|
|
507
|
+
* 2. **Handover enforcement**: `"Owned"` marks a passed state handed over so
|
|
508
|
+
* reusing it throws, giving it back only when a step returns that exact
|
|
509
|
+
* state unchanged or refuses the event.
|
|
510
|
+
* 3. **Retained configuration in `enter`**: `"Owned"` drops the resolved
|
|
511
|
+
* `ActiveConfiguration` after resolving invocations so idle or subsequent
|
|
512
|
+
* steps do not retain it; `"Persistent"` keeps it on the state.
|
|
513
|
+
* 4. **Resting (`rest`)**: `"Owned"` clears execution and configuration storage
|
|
514
|
+
* in place on the state (`restInPlace`); `"Persistent"` allocates a new
|
|
515
|
+
* resting `State`.
|
|
516
|
+
* 5. **Unretained childless moves (`move`)**: when a childless machine only
|
|
517
|
+
* moves to a new state (no commands, emissions, or end), `"Owned"` updates
|
|
518
|
+
* `state` in place and returns its new `Snapshot` directly; `"Persistent"`
|
|
519
|
+
* returns a `Step` with a fresh `State`.
|
|
520
|
+
*/
|
|
521
|
+
const build = (machine, ownership, plan) => {
|
|
522
|
+
// A machine that invokes no work runs none, so its steps track none.
|
|
523
|
+
const invoking = invokingPaths(machine);
|
|
524
|
+
const decodeEvent = Protocol.makeEventDecoderSync(machine);
|
|
525
|
+
/** Describes one invocation, without calling any of its user code. */
|
|
526
|
+
const describe = (planning, invocation, run) => new WorkOf(invocation, run, planning.ambient ? undefined : planning);
|
|
527
|
+
/**
|
|
528
|
+
* Commands of a transition, as data, in order. A command that selects a
|
|
529
|
+
* child names the running work it selects, which a stop ends.
|
|
530
|
+
*/
|
|
531
|
+
const commandsOf = (commands, running) => {
|
|
532
|
+
if (commands.length === 0)
|
|
533
|
+
return { commands: empty, running };
|
|
534
|
+
const out = [];
|
|
535
|
+
let current = running;
|
|
536
|
+
for (const command of commands) {
|
|
537
|
+
if (command._tag === "Stop") {
|
|
538
|
+
const child = command.child;
|
|
539
|
+
const work = current.find((work) => selects(work, child));
|
|
540
|
+
if (work !== undefined)
|
|
541
|
+
current = current.filter((other) => other !== work);
|
|
542
|
+
out.push({ _tag: "StopChild", child, work });
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
const target = command.target;
|
|
546
|
+
if (typeof target === "string" || isChildMachine(target)) {
|
|
547
|
+
const child = target;
|
|
548
|
+
out.push({
|
|
549
|
+
_tag: "SendToChild",
|
|
550
|
+
child,
|
|
551
|
+
work: current.find((work) => selects(work, child)),
|
|
552
|
+
event: command.event
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
out.push({ _tag: "Send", target, event: command.event });
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return { commands: out, running: current };
|
|
560
|
+
};
|
|
561
|
+
const nothing = { event: undefined, microsteps: empty, emitted: empty, commands: empty };
|
|
562
|
+
const reached = (planning, execution, configuration, snapshot, runs) => ({ planning, execution, configuration, snapshot, runs });
|
|
563
|
+
/**
|
|
564
|
+
* A machine that ends in a step stops all of its work: `stop` before
|
|
565
|
+
* publishing the final state, and `stopAfter` after publishing and delivering
|
|
566
|
+
* emitted events. `next` is the state the step reached, or the state before
|
|
567
|
+
* it when the step could not be applied.
|
|
568
|
+
*/
|
|
569
|
+
const ended = (next, stop, exit, published = nothing, stopAfter = empty) => new Step(new State(next.planning, next.execution, next.configuration, next.snapshot, empty, next.runs, exit), published.event, published.microsteps, published.emitted, published.commands, stop, empty, stopAfter, published.paths);
|
|
570
|
+
// A plan that throws leaves the state it received as it was, so the state
|
|
571
|
+
// before a failed step is still the state that was passed in.
|
|
572
|
+
const endedFrom = (state) => {
|
|
573
|
+
const built = state.built;
|
|
574
|
+
return reached(state.planning, built.execution, built.configuration, state.snapshot, state.runs);
|
|
575
|
+
};
|
|
576
|
+
/**
|
|
577
|
+
* Lists the work of the entered paths, one run per entry. Work that `progress`
|
|
578
|
+
* names is not started: work that ended is left out, and work that still runs
|
|
579
|
+
* is kept without being listed in `start`. Only a configuration that enters
|
|
580
|
+
* an invoking state is built to resolve its work.
|
|
581
|
+
*/
|
|
582
|
+
const enter = (next, entered, kept, stop, published, progress) => {
|
|
583
|
+
let configuration = next.configuration;
|
|
584
|
+
let runs = next.runs;
|
|
585
|
+
let start;
|
|
586
|
+
// Every piece of work that runs after the entry, in start order: what
|
|
587
|
+
// `progress` says still runs takes its place among the work that starts.
|
|
588
|
+
let added;
|
|
589
|
+
if (entered.some(([paths]) => paths.some((path) => invoking.has(path)))) {
|
|
590
|
+
configuration ??= plan.toConfiguration(next.execution);
|
|
591
|
+
for (const group of InvocationResolution.resolveEntries(machine, configuration, entered, () => next.planning.capabilities)) {
|
|
592
|
+
const run = runs++;
|
|
593
|
+
for (const invocation of group.invocations) {
|
|
594
|
+
const status = progress?.find((entry) => entry.path === invocation.path && entry.id === invocation.id)
|
|
595
|
+
?.status;
|
|
596
|
+
if (status === "Ended")
|
|
597
|
+
continue;
|
|
598
|
+
const work = describe(next.planning, invocation, run);
|
|
599
|
+
// A literal holds one entry where a push onto an empty array
|
|
600
|
+
// reserves seventeen, and the running work outlives this step.
|
|
601
|
+
if (added === undefined)
|
|
602
|
+
added = [work];
|
|
603
|
+
else
|
|
604
|
+
added.push(work);
|
|
605
|
+
if (status !== "Running")
|
|
606
|
+
(start ??= []).push(work);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
const running = added === undefined ? kept : kept.length === 0 ? added : [...kept, ...added];
|
|
611
|
+
return new Step(
|
|
612
|
+
// An owned state keeps only what its host reads again: the configuration
|
|
613
|
+
// is built again when a later step needs it.
|
|
614
|
+
new State(next.planning, next.execution, ownership === "Owned" ? undefined : configuration, next.snapshot, running, runs, undefined), published.event, published.microsteps, published.emitted, published.commands, stop, start ?? empty, empty, published.paths);
|
|
615
|
+
};
|
|
616
|
+
/**
|
|
617
|
+
* Plans one event on a state that runs `running`: a public event that `send`
|
|
618
|
+
* decoded, or the invocation event of a result.
|
|
619
|
+
*/
|
|
620
|
+
const advance = (state, running, event, retain) => {
|
|
621
|
+
let planned;
|
|
622
|
+
try {
|
|
623
|
+
// A step publishes its microsteps, so a plan retains them unless its host reads none.
|
|
624
|
+
planned = plan.planDecoded(state.execution, event, ownership, retain, state.planning);
|
|
625
|
+
}
|
|
626
|
+
catch (error) {
|
|
627
|
+
// The step planned the event, as transitions receive it, and its planning failed.
|
|
628
|
+
const planning = InvocationEvent.isInvocationEvent(event) ? InvocationEvent.toLifecycleEvent(event) : event;
|
|
629
|
+
return ended(endedFrom(state), latestFirst(running), exitOfThrow(error), { ...nothing, event: planning });
|
|
630
|
+
}
|
|
631
|
+
if (planned.microsteps.length === 0) {
|
|
632
|
+
return new Step(running === state.running ? state : state.withRunning(running), planned.event, empty, empty, empty, empty, empty);
|
|
633
|
+
}
|
|
634
|
+
const commanded = planned.commands.length === 0 ? undefined : commandsOf(planned.commands, running);
|
|
635
|
+
const commands = commanded === undefined ? empty : commanded.commands;
|
|
636
|
+
if (invoking.size === 0)
|
|
637
|
+
return childlessStep(state, planned, commands);
|
|
638
|
+
const changes = InvocationChanges.fromMicrosteps(planned.microsteps);
|
|
639
|
+
const published = {
|
|
640
|
+
event: planned.event,
|
|
641
|
+
microsteps: planned.microsteps,
|
|
642
|
+
emitted: planned.emittedEvents,
|
|
643
|
+
commands
|
|
644
|
+
};
|
|
645
|
+
const next = reached(state.planning, planned.next, undefined, undefined, state.runs);
|
|
646
|
+
const { kept, stop } = InvocationChanges.partition(commanded === undefined ? running : commanded.running, changes);
|
|
647
|
+
if (planned.done) {
|
|
648
|
+
return ended(next, stop, Exit.succeed(planned.output), published, latestFirst(kept));
|
|
649
|
+
}
|
|
650
|
+
return enter(next, InvocationChanges.entries(changes), kept, stop, published);
|
|
651
|
+
};
|
|
652
|
+
/**
|
|
653
|
+
* `work`, when `state` runs it and its result still applies. Work is compared
|
|
654
|
+
* by identity: `path`, `id` and `run` repeat after a resume and across steps
|
|
655
|
+
* taken from one kept state, and the object a step listed does not.
|
|
656
|
+
*/
|
|
657
|
+
const current = (state, work) => state.exit === undefined && state.running.includes(work) ? work : undefined;
|
|
658
|
+
// The planner reads currency from a generation handle. The state value has
|
|
659
|
+
// decided it already, so each delivered event gets a fresh one.
|
|
660
|
+
const generation = () => ({ current: true });
|
|
661
|
+
/** The state a step starts in, from a configuration that the reference planner built. */
|
|
662
|
+
const initial = (planning, configuration, snapshot) => reached(planning, plan.fromConfiguration(configuration), configuration, snapshot, 0);
|
|
663
|
+
// Starting and resuming plan with the reference planner: they happen once per
|
|
664
|
+
// machine, and their result enters the plan through `fromConfiguration`.
|
|
665
|
+
const start = (planning, input, retain = true) => {
|
|
666
|
+
// The execution plan starts a machine that it covers without publishing
|
|
667
|
+
// microsteps: a host that reads none takes that start.
|
|
668
|
+
if (!retain && plan.initial !== undefined)
|
|
669
|
+
return startUnretained(planning, plan.initial, input);
|
|
670
|
+
let planned;
|
|
671
|
+
let configuration;
|
|
672
|
+
try {
|
|
673
|
+
planned = Planner.planInitialSync(Planner.withMachineReferences(machine, planning), ...input);
|
|
674
|
+
configuration = Configuration.normalizeConfigurationSync(machine, planned.state);
|
|
675
|
+
}
|
|
676
|
+
catch (error) {
|
|
677
|
+
return Exit.fail(toStartupFailure(error));
|
|
678
|
+
}
|
|
679
|
+
const published = {
|
|
680
|
+
event: Planner.InitialEvent,
|
|
681
|
+
microsteps: planned.microsteps,
|
|
682
|
+
emitted: planned.emittedEvents,
|
|
683
|
+
commands: commandsOf(planned.commands, empty).commands,
|
|
684
|
+
paths: new InitialPaths(machine, () => Configuration.getInitialEntryPaths(machine, configuration))
|
|
685
|
+
};
|
|
686
|
+
const next = initial(planning, configuration, planned.state);
|
|
687
|
+
if (planned.done) {
|
|
688
|
+
return Exit.succeed(ended(next, empty, Exit.succeed(planned.output), published));
|
|
689
|
+
}
|
|
690
|
+
return Exit.succeed(enter(next, invoking.size === 0 ? empty : InvocationResolution.initialEntries(machine, configuration), empty, empty, published));
|
|
691
|
+
};
|
|
692
|
+
const startUnretained = (planning, initialOf, input) => {
|
|
693
|
+
let planned;
|
|
694
|
+
try {
|
|
695
|
+
planned = initialOf(input, planning);
|
|
696
|
+
}
|
|
697
|
+
catch (error) {
|
|
698
|
+
return Exit.fail(toStartupFailure(error));
|
|
699
|
+
}
|
|
700
|
+
const configuration = planned.activeConfiguration;
|
|
701
|
+
const published = {
|
|
702
|
+
event: Planner.InitialEvent,
|
|
703
|
+
microsteps: empty,
|
|
704
|
+
emitted: empty,
|
|
705
|
+
commands: empty,
|
|
706
|
+
paths: new InitialPaths(machine, planned.initialEntryPaths)
|
|
707
|
+
};
|
|
708
|
+
const next = reached(planning, planned.configuration, configuration, planned.state, 0);
|
|
709
|
+
if (planned.done) {
|
|
710
|
+
return Exit.succeed(ended(next, empty, Exit.succeed(planned.output), published));
|
|
711
|
+
}
|
|
712
|
+
return Exit.succeed(enter(next, invoking.size === 0 ? empty : [[planned.initialEntryPaths, Planner.InitialEvent]], empty, empty, published));
|
|
713
|
+
};
|
|
714
|
+
const startSnapshot = invoking.size === 0 && plan.initial !== undefined
|
|
715
|
+
? (references, input) => {
|
|
716
|
+
let planned;
|
|
717
|
+
try {
|
|
718
|
+
planned = plan.initial(input, references);
|
|
719
|
+
}
|
|
720
|
+
catch (error) {
|
|
721
|
+
throw toStartupFailure(error);
|
|
722
|
+
}
|
|
723
|
+
return planned.done
|
|
724
|
+
? { state: planned.state, done: true, output: planned.output }
|
|
725
|
+
: { state: planned.state, done: false, output: undefined };
|
|
726
|
+
}
|
|
727
|
+
: undefined;
|
|
728
|
+
const fromSnapshot = (planning, snapshot) => {
|
|
729
|
+
if (invoking.size > 0)
|
|
730
|
+
throw new Error("A machine that invokes work runs its work in its state");
|
|
731
|
+
return new State(planning, undefined, undefined, snapshot, empty, 0, undefined);
|
|
732
|
+
};
|
|
733
|
+
const send = (state, event) => {
|
|
734
|
+
if (state.exit !== undefined)
|
|
735
|
+
return Exit.fail(new StoppedError());
|
|
736
|
+
let decoded;
|
|
737
|
+
try {
|
|
738
|
+
decoded = decodeEvent(event);
|
|
739
|
+
}
|
|
740
|
+
catch (error) {
|
|
741
|
+
// Only an event that its schema refuses is rejected. Any other throw is
|
|
742
|
+
// a defect of the machine, as it is when the planner decodes the event.
|
|
743
|
+
return error instanceof MachineSchemaDecodeError
|
|
744
|
+
? Exit.fail(error)
|
|
745
|
+
: Exit.succeed(ended(endedFrom(state), latestFirst(state.running), Exit.die(error)));
|
|
746
|
+
}
|
|
747
|
+
return Exit.succeed(advance(state, state.running, decoded, true));
|
|
748
|
+
};
|
|
749
|
+
const sendDecoded = (state, event, retain = true) => state.exit === undefined ? advance(state, state.running, event, retain) : unchanged(state);
|
|
750
|
+
const moveChildlessOwned = (state, event) => {
|
|
751
|
+
state.assertNotHandedOver();
|
|
752
|
+
if (state.exit !== undefined)
|
|
753
|
+
return undefined;
|
|
754
|
+
let planned;
|
|
755
|
+
try {
|
|
756
|
+
planned = plan.planDecoded(state.execution, event, "Owned", false, state.planning);
|
|
757
|
+
}
|
|
758
|
+
catch (error) {
|
|
759
|
+
state.handOverNeverReuse();
|
|
760
|
+
return ended(endedFrom(state), empty, exitOfThrow(error), { ...nothing, event });
|
|
761
|
+
}
|
|
762
|
+
if (planned.microsteps.length === 0)
|
|
763
|
+
return undefined;
|
|
764
|
+
if (!planned.done && planned.commands.length === 0 && planned.emittedEvents.length === 0) {
|
|
765
|
+
const snapshot = plan.snapshot(planned.next);
|
|
766
|
+
state.replaceExecution(planned.next, snapshot);
|
|
767
|
+
return snapshot;
|
|
768
|
+
}
|
|
769
|
+
state.handOverNeverReuse();
|
|
770
|
+
const commands = planned.commands.length === 0 ? empty : commandsOf(planned.commands, empty).commands;
|
|
771
|
+
return childlessStep(state, planned, commands);
|
|
772
|
+
};
|
|
773
|
+
const move = (state, event) => {
|
|
774
|
+
if (state.exit !== undefined)
|
|
775
|
+
return undefined;
|
|
776
|
+
const step = advance(state, state.running, event, false);
|
|
777
|
+
return step.state === state && !step.planned && step.exit === undefined ? undefined : step;
|
|
778
|
+
};
|
|
779
|
+
const settle = (state, work, exit, retain = true) => {
|
|
780
|
+
const settled = current(state, work);
|
|
781
|
+
if (settled === undefined)
|
|
782
|
+
return unchanged(state);
|
|
783
|
+
const running = state.running.filter((other) => other !== settled);
|
|
784
|
+
const decision = InvocationOutcome.decide(settled[WorkTypeId], generation(), InvocationOutcome.fromExit(exit));
|
|
785
|
+
switch (decision._tag) {
|
|
786
|
+
case "Deliver":
|
|
787
|
+
return advance(state, running, decision.event, retain);
|
|
788
|
+
case "Fail":
|
|
789
|
+
return ended(endedFrom(state), latestFirst(running), Exit.failCause(decision.cause));
|
|
790
|
+
case "Ignore":
|
|
791
|
+
return unchanged(state.withRunning(running));
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
const element = (state, work, value, retain = true) => {
|
|
795
|
+
const streaming = current(state, work);
|
|
796
|
+
if (streaming === undefined)
|
|
797
|
+
return unchanged(state);
|
|
798
|
+
const invocation = streaming[WorkTypeId];
|
|
799
|
+
if (invocation._tag !== "Stream")
|
|
800
|
+
return unchanged(state);
|
|
801
|
+
const decision = InvocationOutcome.decideElement(invocation, generation(), value);
|
|
802
|
+
return decision._tag === "Ignore" ? unchanged(state) : advance(state, state.running, decision.event, retain);
|
|
803
|
+
};
|
|
804
|
+
const report = (state, work, snapshot, retain = true) => {
|
|
805
|
+
const reporting = current(state, work);
|
|
806
|
+
if (reporting === undefined)
|
|
807
|
+
return unchanged(state);
|
|
808
|
+
const invocation = reporting[WorkTypeId];
|
|
809
|
+
if (invocation._tag !== "Logic" && invocation._tag !== "Child")
|
|
810
|
+
return unchanged(state);
|
|
811
|
+
// Owners read the state of an active process, as the local runtime reports it.
|
|
812
|
+
const decision = InvocationOutcome.decideSnapshot(invocation, generation(), { status: "active", state: snapshot });
|
|
813
|
+
return decision._tag === "Ignore" ? unchanged(state) : advance(state, state.running, decision.event, retain);
|
|
814
|
+
};
|
|
815
|
+
const stop = (state) => state.exit === undefined
|
|
816
|
+
? ended(endedFrom(state), latestFirst(state.running), Exit.fail(new StoppedError()))
|
|
817
|
+
: unchanged(state);
|
|
818
|
+
const resume = (planning, snapshot, progress) => {
|
|
819
|
+
const normalized = Effect.runSyncExit(Serialization.normalizeSnapshotEffect(machine, snapshot));
|
|
820
|
+
if (Exit.isFailure(normalized))
|
|
821
|
+
return Exit.failCause(normalized.cause);
|
|
822
|
+
const state = normalized.value;
|
|
823
|
+
let configuration;
|
|
824
|
+
try {
|
|
825
|
+
configuration = Configuration.normalizeConfigurationSync(machine, state);
|
|
826
|
+
}
|
|
827
|
+
catch (error) {
|
|
828
|
+
return exitOfThrow(error);
|
|
829
|
+
}
|
|
830
|
+
const published = { ...nothing, event: Planner.InitialEvent };
|
|
831
|
+
const next = initial(planning, configuration, state);
|
|
832
|
+
if (!Planner.isFinalState(machine, state)) {
|
|
833
|
+
return Exit.succeed(enter(next, InvocationResolution.initialEntries(machine, configuration), empty, empty, published, progress));
|
|
834
|
+
}
|
|
835
|
+
const output = Effect.runSyncExit(Planner.getFinalOutputEffect(machine, state, Planner.InitialEvent));
|
|
836
|
+
return Exit.isFailure(output)
|
|
837
|
+
? Exit.failCause(output.cause)
|
|
838
|
+
: Exit.succeed(ended(next, empty, Exit.succeed(output.value), published));
|
|
839
|
+
};
|
|
840
|
+
if (ownership === "Persistent") {
|
|
841
|
+
return {
|
|
842
|
+
plan,
|
|
843
|
+
start,
|
|
844
|
+
startSnapshot,
|
|
845
|
+
fromSnapshot,
|
|
846
|
+
rest: (state) => state.resting(),
|
|
847
|
+
send,
|
|
848
|
+
sendDecoded,
|
|
849
|
+
move,
|
|
850
|
+
settle,
|
|
851
|
+
element,
|
|
852
|
+
report,
|
|
853
|
+
observesSnapshots,
|
|
854
|
+
settleEvent,
|
|
855
|
+
elementEvent,
|
|
856
|
+
snapshotEvent,
|
|
857
|
+
stop,
|
|
858
|
+
resume
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
// An owned state is taken over by the step it is passed to, and handed back
|
|
862
|
+
// when the step returns that very state, or refuses the event.
|
|
863
|
+
return {
|
|
864
|
+
plan,
|
|
865
|
+
start,
|
|
866
|
+
startSnapshot,
|
|
867
|
+
fromSnapshot,
|
|
868
|
+
rest: (state) => state.restInPlace(),
|
|
869
|
+
send: (state, event) => {
|
|
870
|
+
const exit = send(state.handOverNeverReuse(), event);
|
|
871
|
+
if (Exit.isFailure(exit) || exit.value.state === state)
|
|
872
|
+
state.giveBackUnchanged();
|
|
873
|
+
return exit;
|
|
874
|
+
},
|
|
875
|
+
sendDecoded: (state, event, retain) => givenBackIfUnchanged(state, sendDecoded(state.handOverNeverReuse(), event, retain)),
|
|
876
|
+
move: invoking.size === 0
|
|
877
|
+
? moveChildlessOwned
|
|
878
|
+
: (state, event) => {
|
|
879
|
+
const moved = move(state.handOverNeverReuse(), event);
|
|
880
|
+
if (!(moved instanceof Step) || moved.state === state)
|
|
881
|
+
state.giveBackUnchanged();
|
|
882
|
+
return moved;
|
|
883
|
+
},
|
|
884
|
+
settle: (state, work, exit, retain) => givenBackIfUnchanged(state, settle(state.handOverNeverReuse(), work, exit, retain)),
|
|
885
|
+
element: (state, work, value, retain) => givenBackIfUnchanged(state, element(state.handOverNeverReuse(), work, value, retain)),
|
|
886
|
+
report: (state, work, snapshot, retain) => givenBackIfUnchanged(state, report(state.handOverNeverReuse(), work, snapshot, retain)),
|
|
887
|
+
observesSnapshots,
|
|
888
|
+
settleEvent,
|
|
889
|
+
elementEvent,
|
|
890
|
+
snapshotEvent,
|
|
891
|
+
stop: (state) => givenBackIfUnchanged(state, stop(state.handOverNeverReuse())),
|
|
892
|
+
resume
|
|
893
|
+
};
|
|
894
|
+
};
|
|
895
|
+
Object.defineProperty(State.prototype, StateTypeId, { value: StateTypeId });
|
|
896
|
+
/** Gives an owned state back to the caller of a step that returned it unchanged. */
|
|
897
|
+
const givenBackIfUnchanged = (state, step) => {
|
|
898
|
+
if (step.state === state)
|
|
899
|
+
state.giveBackUnchanged();
|
|
900
|
+
return step;
|
|
901
|
+
};
|
|
902
|
+
const kernels = {
|
|
903
|
+
Persistent: new WeakMap(),
|
|
904
|
+
Owned: new WeakMap()
|
|
905
|
+
};
|
|
906
|
+
/**
|
|
907
|
+
* The step functions of `machine` that take states of `ownership` and plan
|
|
908
|
+
* with `plan`. Cached per `(ownership, plan)`, since execution plans are
|
|
909
|
+
* cached per machine.
|
|
910
|
+
*
|
|
911
|
+
* @internal
|
|
912
|
+
*/
|
|
913
|
+
export const kernel = (machine, ownership, plan) => {
|
|
914
|
+
const cache = kernels[ownership];
|
|
915
|
+
let built = cache.get(plan);
|
|
916
|
+
if (built === undefined) {
|
|
917
|
+
built = build(machine, ownership, plan);
|
|
918
|
+
cache.set(plan, built);
|
|
919
|
+
}
|
|
920
|
+
return built;
|
|
921
|
+
};
|
|
922
|
+
/** The step functions of `kernel`, bound to one host. */
|
|
923
|
+
const bind = (machine, kernel, options) => {
|
|
924
|
+
const planning = new Planning(machine, kernel.plan, options.self, options.parent, options.children ?? refusedChildren);
|
|
925
|
+
return {
|
|
926
|
+
start: (...input) => kernel.start(planning, input),
|
|
927
|
+
send: kernel.send,
|
|
928
|
+
sendDecoded: kernel.sendDecoded,
|
|
929
|
+
settle: kernel.settle,
|
|
930
|
+
element: kernel.element,
|
|
931
|
+
report: kernel.report,
|
|
932
|
+
stop: kernel.stop,
|
|
933
|
+
resume: (snapshot, progress) => kernel.resume(planning, snapshot, progress)
|
|
934
|
+
};
|
|
935
|
+
};
|
|
936
|
+
/**
|
|
937
|
+
* The step functions of `Machine.toStep`, which take immutable state values.
|
|
938
|
+
*
|
|
939
|
+
* @internal
|
|
940
|
+
*/
|
|
941
|
+
export const make = (machine, options) => bind(machine, kernel(machine, "Persistent", ExecutionPlan.compileExecutionPlan(machine)), options);
|
|
942
|
+
/**
|
|
943
|
+
* Step functions that take each state over and may update it in place, for a
|
|
944
|
+
* host that keeps only its latest state. The caller never uses a state again
|
|
945
|
+
* after it passes it to one of them, nor any step that returned it. See
|
|
946
|
+
* {@link Ownership}.
|
|
947
|
+
*
|
|
948
|
+
* @internal
|
|
949
|
+
*/
|
|
950
|
+
export const makeOwned = (machine, options) => bind(machine, kernel(machine, "Owned", ExecutionPlan.compileExecutionPlan(machine)), options);
|
|
951
|
+
/**
|
|
952
|
+
* Step functions of either ownership that plan with `plan`.
|
|
953
|
+
*
|
|
954
|
+
* @internal
|
|
955
|
+
*/
|
|
956
|
+
export const makeForTesting = (machine, options, ownership, plan) => bind(machine, kernel(machine, ownership, plan), options);
|
|
957
|
+
//# sourceMappingURL=step.js.map
|