@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
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
type MicrostepTransition,
|
|
39
39
|
normalizeTransition,
|
|
40
40
|
planConfiguration,
|
|
41
|
+
planDecodedConfiguration,
|
|
41
42
|
removeConflictingTransitions,
|
|
42
43
|
resolveInitialConfiguration,
|
|
43
44
|
resolveInitialTarget,
|
|
@@ -76,7 +77,7 @@ interface IndexedExecutionDescriptor {
|
|
|
76
77
|
number,
|
|
77
78
|
ReadonlyMap<PropertyKey, {
|
|
78
79
|
readonly sourceIndex: number
|
|
79
|
-
readonly transition: MicrostepTransition<any, any
|
|
80
|
+
readonly transition: MicrostepTransition<any, any>
|
|
80
81
|
}>
|
|
81
82
|
>
|
|
82
83
|
}
|
|
@@ -137,7 +138,7 @@ const compileIndexedExecutionDescriptor = (
|
|
|
137
138
|
const finalPaths: Array<string> = []
|
|
138
139
|
const transitionsByPath = new Map<
|
|
139
140
|
PropertyKey,
|
|
140
|
-
ReadonlyMap<PropertyKey, MicrostepTransition<any, any
|
|
141
|
+
ReadonlyMap<PropertyKey, MicrostepTransition<any, any>>
|
|
141
142
|
>()
|
|
142
143
|
|
|
143
144
|
for (const node of machine.stateNodes.byPath.values() as Iterable<Machine.StateNode>) {
|
|
@@ -159,7 +160,7 @@ const compileIndexedExecutionDescriptor = (
|
|
|
159
160
|
if (config?.on === undefined) {
|
|
160
161
|
continue
|
|
161
162
|
}
|
|
162
|
-
const byEvent = new Map<PropertyKey, MicrostepTransition<any, any
|
|
163
|
+
const byEvent = new Map<PropertyKey, MicrostepTransition<any, any>>()
|
|
163
164
|
for (const tag of Reflect.ownKeys(config.on)) {
|
|
164
165
|
const transition = normalizeTransition(config.on[tag] as Parameters<typeof normalizeTransition>[0])
|
|
165
166
|
if (transition !== undefined) {
|
|
@@ -192,13 +193,13 @@ const compileIndexedExecutionDescriptor = (
|
|
|
192
193
|
number,
|
|
193
194
|
ReadonlyMap<PropertyKey, {
|
|
194
195
|
readonly sourceIndex: number
|
|
195
|
-
readonly transition: MicrostepTransition<any, any
|
|
196
|
+
readonly transition: MicrostepTransition<any, any>
|
|
196
197
|
}>
|
|
197
198
|
>()
|
|
198
199
|
for (const leafIndex of leafIndices) {
|
|
199
200
|
const dispatch = new Map<PropertyKey, {
|
|
200
201
|
readonly sourceIndex: number
|
|
201
|
-
readonly transition: MicrostepTransition<any, any
|
|
202
|
+
readonly transition: MicrostepTransition<any, any>
|
|
202
203
|
}>()
|
|
203
204
|
const candidates = [leafIndex, ...ancestorIndices[leafIndex]!.slice().reverse()]
|
|
204
205
|
for (const sourceIndex of candidates) {
|
|
@@ -237,15 +238,17 @@ const compileIndexedExecutionDescriptor = (
|
|
|
237
238
|
}
|
|
238
239
|
|
|
239
240
|
/**
|
|
240
|
-
* The compact execution state
|
|
241
|
+
* The compact execution state of the indexed kernels.
|
|
241
242
|
*
|
|
242
|
-
* This representation never crosses the public snapshot boundary.
|
|
243
|
-
*
|
|
244
|
-
* context, whose snapshot is detached from this storage.
|
|
245
|
-
*
|
|
246
|
-
* state
|
|
243
|
+
* This representation never crosses the public snapshot boundary. Only
|
|
244
|
+
* `values` is ever written in place, and only by the flat kernel, after it has
|
|
245
|
+
* built the handler context, whose snapshot is detached from this storage. Who
|
|
246
|
+
* may let it write a state is the {@link ExecutionOwnership} of the plan. The
|
|
247
|
+
* hierarchical kernel never writes a state in place: it copies the table
|
|
248
|
+
* whenever it changes it, so simultaneous transitions read one stable source
|
|
249
|
+
* state, and its microsteps can share states with what it returns.
|
|
247
250
|
*/
|
|
248
|
-
interface
|
|
251
|
+
interface IndexedExecutionState {
|
|
249
252
|
readonly active: Uint8Array
|
|
250
253
|
readonly activeLeaves: ReadonlyArray<number>
|
|
251
254
|
readonly values: Array<unknown>
|
|
@@ -254,7 +257,28 @@ interface OwnedIndexedState {
|
|
|
254
257
|
readonly completedOrder: ReadonlyArray<number>
|
|
255
258
|
}
|
|
256
259
|
|
|
257
|
-
|
|
260
|
+
/**
|
|
261
|
+
* Who may write the execution state that a plan receives.
|
|
262
|
+
*
|
|
263
|
+
* - `"Persistent"`: the state is a persistent value. The caller may keep it and
|
|
264
|
+
* plan from it again, and may keep what the plan returns. The plan writes no
|
|
265
|
+
* state that it did not create in that same call, and returns no state that a
|
|
266
|
+
* later `"Persistent"` plan writes.
|
|
267
|
+
* - `"Owned"`: the caller hands the state over to the plan, and reads it again
|
|
268
|
+
* only through what the plan returns. The plan may update it in place. What a
|
|
269
|
+
* plan returns, its next state and its microsteps, stays valid until that
|
|
270
|
+
* next state is planned again. A plan that throws leaves the state it received
|
|
271
|
+
* as it was.
|
|
272
|
+
*
|
|
273
|
+
* Every plan of one chain of states takes the same ownership, since an
|
|
274
|
+
* `"Owned"` plan may write a state that a `"Persistent"` plan returned to a
|
|
275
|
+
* caller that keeps it.
|
|
276
|
+
*
|
|
277
|
+
* @internal
|
|
278
|
+
*/
|
|
279
|
+
export type ExecutionOwnership = "Owned" | "Persistent"
|
|
280
|
+
|
|
281
|
+
const copyIndexedState = (state: IndexedExecutionState): IndexedExecutionState => ({
|
|
258
282
|
active: state.active.slice(),
|
|
259
283
|
activeLeaves: state.activeLeaves.slice(),
|
|
260
284
|
values: state.values.slice(),
|
|
@@ -263,23 +287,16 @@ const copyOwnedIndexedState = (state: OwnedIndexedState): OwnedIndexedState => (
|
|
|
263
287
|
completedOrder: state.completedOrder.slice()
|
|
264
288
|
})
|
|
265
289
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const updateOwnedIndexedValue = (
|
|
272
|
-
state: OwnedIndexedState,
|
|
273
|
-
index: number,
|
|
274
|
-
value: unknown
|
|
275
|
-
): void => {
|
|
276
|
-
state.values[index] = value
|
|
277
|
-
}
|
|
290
|
+
/** A state equal to `state` whose values the kernel may write, as only `values` is ever written. */
|
|
291
|
+
const withWritableIndexedValues = (state: IndexedExecutionState): IndexedExecutionState => ({
|
|
292
|
+
...state,
|
|
293
|
+
values: state.values.slice()
|
|
294
|
+
})
|
|
278
295
|
|
|
279
|
-
const
|
|
296
|
+
const indexedStateFromActive = (
|
|
280
297
|
descriptor: IndexedExecutionDescriptor,
|
|
281
298
|
configuration: ActiveConfiguration
|
|
282
|
-
):
|
|
299
|
+
): IndexedExecutionState => {
|
|
283
300
|
const active = new Uint8Array(descriptor.nodes.length)
|
|
284
301
|
const values: Array<unknown> = new Array(descriptor.nodes.length)
|
|
285
302
|
const completed = new Uint8Array(descriptor.nodes.length)
|
|
@@ -310,7 +327,7 @@ const ownedIndexedStateFromActive = (
|
|
|
310
327
|
|
|
311
328
|
const activeConfigurationFromIndexedState = (
|
|
312
329
|
descriptor: IndexedExecutionDescriptor,
|
|
313
|
-
configuration:
|
|
330
|
+
configuration: IndexedExecutionState
|
|
314
331
|
): ActiveConfiguration => {
|
|
315
332
|
const active = new Set<string>()
|
|
316
333
|
const values = new Map<string, unknown>()
|
|
@@ -340,7 +357,7 @@ interface IndexedSnapshotNode {
|
|
|
340
357
|
|
|
341
358
|
const snapshotFromIndexedStateStatePath = (
|
|
342
359
|
descriptor: IndexedExecutionDescriptor,
|
|
343
|
-
configuration:
|
|
360
|
+
configuration: IndexedExecutionState,
|
|
344
361
|
index: number
|
|
345
362
|
): IndexedSnapshotNode => {
|
|
346
363
|
const node = descriptor.nodes[index]!
|
|
@@ -377,7 +394,7 @@ const snapshotFromIndexedStateStatePath = (
|
|
|
377
394
|
|
|
378
395
|
const snapshotFromIndexedState = (
|
|
379
396
|
descriptor: IndexedExecutionDescriptor,
|
|
380
|
-
configuration:
|
|
397
|
+
configuration: IndexedExecutionState
|
|
381
398
|
): Machine.Snapshot<any> => {
|
|
382
399
|
const rootIndex = descriptor.rootIndices.find((index) => configuration.active[index] === 1)
|
|
383
400
|
if (rootIndex === undefined) throw new Error("Machine expected an active indexed root state")
|
|
@@ -410,7 +427,7 @@ interface IndexedTransitionContext {
|
|
|
410
427
|
const makeIndexedTransitionContext = (
|
|
411
428
|
machine: Machine.Any,
|
|
412
429
|
descriptor: IndexedExecutionDescriptor,
|
|
413
|
-
configuration:
|
|
430
|
+
configuration: IndexedExecutionState,
|
|
414
431
|
sourceIndex: number,
|
|
415
432
|
event: IndexedEvent,
|
|
416
433
|
machineReferences?: PlanningMachineReferences
|
|
@@ -432,23 +449,26 @@ const makeIndexedTransitionContext = (
|
|
|
432
449
|
}
|
|
433
450
|
}
|
|
434
451
|
|
|
435
|
-
type IndexedSelectedTransition = SelectedTransition<any,
|
|
452
|
+
type IndexedSelectedTransition = SelectedTransition<any, IndexedTransitionContext> & {
|
|
436
453
|
readonly sourceIndex: number
|
|
437
454
|
readonly leafIndex: number
|
|
438
455
|
}
|
|
439
456
|
|
|
440
457
|
type IndexedEvaluatedTransition =
|
|
441
458
|
& Omit<
|
|
442
|
-
EvaluatedTransition<any, IndexedEvent,
|
|
459
|
+
EvaluatedTransition<any, IndexedEvent, IndexedTransitionContext>,
|
|
443
460
|
"selection"
|
|
444
461
|
>
|
|
445
462
|
& {
|
|
446
463
|
readonly selection: IndexedSelectedTransition
|
|
447
|
-
readonly next:
|
|
464
|
+
readonly next: IndexedExecutionState
|
|
448
465
|
}
|
|
449
466
|
|
|
450
467
|
const emptyExecutionValues: ReadonlyArray<never> = Object.freeze([])
|
|
451
468
|
|
|
469
|
+
/** Marks that a flat microstep writes no value. A decoded value may be `undefined`. */
|
|
470
|
+
const noIndexedValue: unique symbol = Symbol("effect/Machine/ExecutionPlan/NoValue")
|
|
471
|
+
|
|
452
472
|
export interface ExecutionMicrostep<State = unknown> {
|
|
453
473
|
readonly next: State
|
|
454
474
|
readonly event: unknown
|
|
@@ -473,9 +493,9 @@ export interface ExecutionMacrostep<State = unknown> {
|
|
|
473
493
|
|
|
474
494
|
const collectIndexedTransition = <Context>(
|
|
475
495
|
machine: Machine.Any,
|
|
476
|
-
transition: TransitionHandler<any,
|
|
496
|
+
transition: TransitionHandler<any, Context>,
|
|
477
497
|
context: Context,
|
|
478
|
-
evaluate?: NonNullable<MicrostepTransition<any,
|
|
498
|
+
evaluate?: NonNullable<MicrostepTransition<any, Context>["evaluate"]>
|
|
479
499
|
) => {
|
|
480
500
|
let commands: Array<RuntimeCommand> | undefined
|
|
481
501
|
let raisedEvents: Array<IndexedEvent> | undefined
|
|
@@ -510,7 +530,7 @@ const collectIndexedTransition = <Context>(
|
|
|
510
530
|
const selectIndexedEventTransitions = (
|
|
511
531
|
machine: Machine.Any,
|
|
512
532
|
descriptor: IndexedExecutionDescriptor,
|
|
513
|
-
configuration:
|
|
533
|
+
configuration: IndexedExecutionState,
|
|
514
534
|
event: IndexedEvent,
|
|
515
535
|
machineReferences?: PlanningMachineReferences
|
|
516
536
|
): ReadonlyArray<IndexedSelectedTransition> => {
|
|
@@ -543,7 +563,7 @@ const selectIndexedEventTransitions = (
|
|
|
543
563
|
return selected
|
|
544
564
|
}
|
|
545
565
|
|
|
546
|
-
const hasSameIndexedActive = (left:
|
|
566
|
+
const hasSameIndexedActive = (left: IndexedExecutionState, right: IndexedExecutionState): boolean => {
|
|
547
567
|
if (left.active === right.active) return true
|
|
548
568
|
for (let index = 0; index < left.active.length; index++) {
|
|
549
569
|
if (left.active[index] !== right.active[index]) return false
|
|
@@ -554,10 +574,10 @@ const hasSameIndexedActive = (left: OwnedIndexedState, right: OwnedIndexedState)
|
|
|
554
574
|
const normalizeIndexedTargetStateSync = (
|
|
555
575
|
machine: Machine.Any,
|
|
556
576
|
descriptor: IndexedExecutionDescriptor,
|
|
557
|
-
current:
|
|
577
|
+
current: IndexedExecutionState,
|
|
558
578
|
target: Machine.Target<any, any> | Machine.Snapshot<any>,
|
|
559
579
|
activeLeafIndex: number
|
|
560
|
-
):
|
|
580
|
+
): IndexedExecutionState => {
|
|
561
581
|
const targetIndex = isTarget(target) ? descriptor.indexByPath.get(String(target.path)) : undefined
|
|
562
582
|
if (
|
|
563
583
|
targetIndex === activeLeafIndex && current.active[activeLeafIndex] === 1 && isTarget(target) &&
|
|
@@ -573,7 +593,7 @@ const normalizeIndexedTargetStateSync = (
|
|
|
573
593
|
)
|
|
574
594
|
return { ...current, values }
|
|
575
595
|
}
|
|
576
|
-
return
|
|
596
|
+
return indexedStateFromActive(
|
|
577
597
|
descriptor,
|
|
578
598
|
normalizeTargetConfigurationSync(
|
|
579
599
|
machine,
|
|
@@ -586,7 +606,7 @@ const normalizeIndexedTargetStateSync = (
|
|
|
586
606
|
const collectIndexedEvaluatedTransition = (
|
|
587
607
|
machine: Machine.Any,
|
|
588
608
|
descriptor: IndexedExecutionDescriptor,
|
|
589
|
-
state:
|
|
609
|
+
state: IndexedExecutionState,
|
|
590
610
|
selection: IndexedSelectedTransition
|
|
591
611
|
): IndexedEvaluatedTransition => {
|
|
592
612
|
const transitionResult = collectIndexedTransition(
|
|
@@ -648,7 +668,7 @@ const collectIndexedEvaluatedTransition = (
|
|
|
648
668
|
const initialRaisedEvents = (initialResolution?.raisedEvents ?? emptyExecutionValues) as ReadonlyArray<IndexedEvent>
|
|
649
669
|
let next = target === undefined
|
|
650
670
|
? update === undefined ? state : (() => {
|
|
651
|
-
const next =
|
|
671
|
+
const next = copyIndexedState(state)
|
|
652
672
|
next.values[descriptor.indexByPath.get(update.path)!] = update.value
|
|
653
673
|
return next
|
|
654
674
|
})()
|
|
@@ -712,41 +732,140 @@ const collectIndexedEvaluatedTransition = (
|
|
|
712
732
|
}
|
|
713
733
|
|
|
714
734
|
/** Indexed microsteps keep decoded raised events typed for the next dispatch. */
|
|
715
|
-
type IndexedMicrostep = ExecutionMicrostep<
|
|
735
|
+
type IndexedMicrostep = ExecutionMicrostep<IndexedExecutionState> & {
|
|
716
736
|
readonly raisedEvents: ReadonlyArray<IndexedEvent>
|
|
717
737
|
}
|
|
718
738
|
|
|
739
|
+
const retainIndexedTransition = (transition: IndexedEvaluatedTransition): Machine.RetainedTransition => ({
|
|
740
|
+
source: transition.selection.sourcePath,
|
|
741
|
+
trigger: transition.selection.trigger,
|
|
742
|
+
reenter: transition.selection.transition.reenter,
|
|
743
|
+
branchIndex: transition.branchIndex,
|
|
744
|
+
branchKey: transition.branchKey,
|
|
745
|
+
target: transition.unresolvedTarget === undefined ? undefined : getTargetNodePath(transition.unresolvedTarget),
|
|
746
|
+
resolvedTarget: transition.target === undefined ? undefined : getTargetNodePath(transition.target),
|
|
747
|
+
updates: transition.update === undefined ? [] : [transition.update.path]
|
|
748
|
+
})
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* A retained microstep of the hierarchical kernel. It builds its retained
|
|
752
|
+
* transitions on first read, from evaluated transitions that hold only values
|
|
753
|
+
* no later plan writes.
|
|
754
|
+
*/
|
|
755
|
+
class RetainedHierarchicalMicrostep implements IndexedMicrostep {
|
|
756
|
+
#evaluated: ReadonlyArray<IndexedEvaluatedTransition> | undefined
|
|
757
|
+
#transitions: ReadonlyArray<Machine.RetainedTransition> | undefined
|
|
758
|
+
|
|
759
|
+
constructor(
|
|
760
|
+
readonly next: IndexedExecutionState,
|
|
761
|
+
readonly event: IndexedEvent,
|
|
762
|
+
evaluated: ReadonlyArray<IndexedEvaluatedTransition>,
|
|
763
|
+
readonly commands: ReadonlyArray<RuntimeCommand>,
|
|
764
|
+
readonly raisedEvents: ReadonlyArray<IndexedEvent>,
|
|
765
|
+
readonly emittedEvents: ReadonlyArray<unknown>,
|
|
766
|
+
readonly exitPaths: ReadonlyArray<string>,
|
|
767
|
+
readonly entryPaths: ReadonlyArray<string>,
|
|
768
|
+
readonly changed: boolean
|
|
769
|
+
) {
|
|
770
|
+
this.#evaluated = evaluated
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
get transitions(): ReadonlyArray<Machine.RetainedTransition> {
|
|
774
|
+
if (this.#transitions === undefined) {
|
|
775
|
+
this.#transitions = this.#evaluated!.map(retainIndexedTransition)
|
|
776
|
+
this.#evaluated = undefined
|
|
777
|
+
}
|
|
778
|
+
return this.#transitions
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* A retained microstep of the flat kernel, which builds its one retained
|
|
784
|
+
* transition on first read.
|
|
785
|
+
*/
|
|
786
|
+
class RetainedFlatMicrostep implements IndexedMicrostep {
|
|
787
|
+
readonly #source: string
|
|
788
|
+
readonly #reenter: boolean
|
|
789
|
+
readonly #branchIndex: number
|
|
790
|
+
readonly #branchKey: string | undefined
|
|
791
|
+
readonly #target: Machine.Target<any, any> | Machine.Snapshot<any> | undefined
|
|
792
|
+
readonly #update: { readonly path: string } | undefined
|
|
793
|
+
#transitions: ReadonlyArray<Machine.RetainedTransition> | undefined
|
|
794
|
+
|
|
795
|
+
constructor(
|
|
796
|
+
readonly next: IndexedExecutionState,
|
|
797
|
+
readonly event: IndexedEvent,
|
|
798
|
+
readonly commands: ReadonlyArray<RuntimeCommand>,
|
|
799
|
+
readonly raisedEvents: ReadonlyArray<IndexedEvent>,
|
|
800
|
+
readonly emittedEvents: ReadonlyArray<unknown>,
|
|
801
|
+
readonly exitPaths: ReadonlyArray<string>,
|
|
802
|
+
readonly entryPaths: ReadonlyArray<string>,
|
|
803
|
+
readonly changed: boolean,
|
|
804
|
+
source: string,
|
|
805
|
+
reenter: boolean,
|
|
806
|
+
branchIndex: number,
|
|
807
|
+
branchKey: string | undefined,
|
|
808
|
+
target: Machine.Target<any, any> | Machine.Snapshot<any> | undefined,
|
|
809
|
+
update: { readonly path: string } | undefined
|
|
810
|
+
) {
|
|
811
|
+
this.#source = source
|
|
812
|
+
this.#reenter = reenter
|
|
813
|
+
this.#branchIndex = branchIndex
|
|
814
|
+
this.#branchKey = branchKey
|
|
815
|
+
this.#target = target
|
|
816
|
+
this.#update = update
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
get transitions(): ReadonlyArray<Machine.RetainedTransition> {
|
|
820
|
+
if (this.#transitions === undefined) {
|
|
821
|
+
const target = this.#target === undefined ? undefined : getTargetNodePath(this.#target)
|
|
822
|
+
this.#transitions = [{
|
|
823
|
+
source: this.#source,
|
|
824
|
+
trigger: { type: "event", event: this.event._tag },
|
|
825
|
+
reenter: this.#reenter,
|
|
826
|
+
branchIndex: this.#branchIndex,
|
|
827
|
+
branchKey: this.#branchKey,
|
|
828
|
+
target,
|
|
829
|
+
resolvedTarget: target,
|
|
830
|
+
updates: this.#update === undefined ? [] : [this.#update.path]
|
|
831
|
+
}]
|
|
832
|
+
}
|
|
833
|
+
return this.#transitions
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
719
837
|
const indexedMicrostep = (
|
|
720
838
|
machine: Machine.Any,
|
|
721
839
|
descriptor: IndexedExecutionDescriptor,
|
|
722
|
-
state:
|
|
840
|
+
state: IndexedExecutionState,
|
|
723
841
|
event: IndexedEvent,
|
|
724
842
|
selections: ReadonlyArray<IndexedSelectedTransition>,
|
|
725
843
|
retainTransitions: boolean
|
|
726
844
|
): IndexedMicrostep => {
|
|
727
|
-
const retain = (transition: IndexedEvaluatedTransition): Machine.RetainedTransition => ({
|
|
728
|
-
source: transition.selection.sourcePath,
|
|
729
|
-
trigger: transition.selection.trigger,
|
|
730
|
-
reenter: transition.selection.transition.reenter,
|
|
731
|
-
branchIndex: transition.branchIndex,
|
|
732
|
-
branchKey: transition.branchKey,
|
|
733
|
-
target: transition.unresolvedTarget === undefined ? undefined : getTargetNodePath(transition.unresolvedTarget),
|
|
734
|
-
resolvedTarget: transition.target === undefined ? undefined : getTargetNodePath(transition.target),
|
|
735
|
-
updates: transition.update === undefined ? [] : [transition.update.path]
|
|
736
|
-
})
|
|
737
845
|
if (selections.length === 1) {
|
|
738
846
|
const transition = collectIndexedEvaluatedTransition(machine, descriptor, state, selections[0]!)
|
|
739
|
-
return
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
847
|
+
return retainTransitions
|
|
848
|
+
? new RetainedHierarchicalMicrostep(
|
|
849
|
+
transition.next,
|
|
850
|
+
event,
|
|
851
|
+
[transition],
|
|
852
|
+
transition.commands,
|
|
853
|
+
transition.raisedEvents,
|
|
854
|
+
transition.emittedEvents,
|
|
855
|
+
transition.exitPaths,
|
|
856
|
+
transition.entryPaths,
|
|
857
|
+
transition.changed
|
|
858
|
+
)
|
|
859
|
+
: {
|
|
860
|
+
next: transition.next,
|
|
861
|
+
event,
|
|
862
|
+
commands: transition.commands,
|
|
863
|
+
raisedEvents: transition.raisedEvents,
|
|
864
|
+
emittedEvents: transition.emittedEvents,
|
|
865
|
+
exitPaths: transition.exitPaths,
|
|
866
|
+
entryPaths: transition.entryPaths,
|
|
867
|
+
changed: transition.changed
|
|
868
|
+
}
|
|
750
869
|
}
|
|
751
870
|
const activeSelections = selections.filter((selection) =>
|
|
752
871
|
!selections.some((other) =>
|
|
@@ -791,199 +910,235 @@ const indexedMicrostep = (
|
|
|
791
910
|
const raisedEvents = transitions.flatMap((transition) => transition.raisedEvents)
|
|
792
911
|
const emittedEvents = transitions.flatMap((transition) => transition.emittedEvents)
|
|
793
912
|
const changed = transitions.some((transition) => transition.changed)
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
913
|
+
const exitPaths = changed ? sortExitPaths(machine, transitions.flatMap((transition) => transition.exitPaths)) : []
|
|
914
|
+
const entryPaths = changed ? sortEntryPaths(machine, transitions.flatMap((transition) => transition.entryPaths)) : []
|
|
915
|
+
return retainTransitions
|
|
916
|
+
? new RetainedHierarchicalMicrostep(
|
|
917
|
+
next,
|
|
918
|
+
event,
|
|
919
|
+
transitions,
|
|
920
|
+
commands,
|
|
921
|
+
raisedEvents,
|
|
922
|
+
emittedEvents,
|
|
923
|
+
exitPaths,
|
|
924
|
+
entryPaths,
|
|
925
|
+
changed
|
|
926
|
+
)
|
|
927
|
+
: { next, event, commands, raisedEvents, emittedEvents, exitPaths, entryPaths, changed }
|
|
805
928
|
}
|
|
806
929
|
|
|
807
930
|
const planIndexedFlatState = (
|
|
808
931
|
machine: Machine.Any,
|
|
809
932
|
descriptor: IndexedExecutionDescriptor,
|
|
810
|
-
configuration:
|
|
933
|
+
configuration: IndexedExecutionState,
|
|
811
934
|
decoded: IndexedEvent,
|
|
812
935
|
retainMicrosteps: boolean,
|
|
936
|
+
ownership: ExecutionOwnership,
|
|
813
937
|
machineReferences?: PlanningMachineReferences
|
|
814
|
-
): ExecutionMacrostep<
|
|
938
|
+
): ExecutionMacrostep<IndexedExecutionState> => {
|
|
815
939
|
let current = configuration
|
|
816
940
|
let event: IndexedEvent = decoded
|
|
817
941
|
let commands: Array<RuntimeCommand> | undefined
|
|
818
942
|
let raisedEvents: Array<IndexedEvent> | undefined
|
|
819
943
|
let emittedEvents: Array<unknown> | undefined
|
|
820
|
-
let microsteps: Array<ExecutionMicrostep<
|
|
944
|
+
let microsteps: Array<ExecutionMicrostep<IndexedExecutionState>> | undefined
|
|
821
945
|
let raisedIndex = 0
|
|
822
946
|
let iterations = 0
|
|
947
|
+
// Whether `current` may be written in place, because nothing but this plan
|
|
948
|
+
// references it. A persistent caller's state, and the state of a retained
|
|
949
|
+
// microstep, are copied before they would be written.
|
|
950
|
+
let writable = ownership === "Owned"
|
|
951
|
+
// The value that this plan overwrote in an owned caller's state, so that a
|
|
952
|
+
// plan that throws leaves that state as it was. Only the active leaf is
|
|
953
|
+
// written, and the leaf of the caller's state stays active while `current`
|
|
954
|
+
// is that state, so one value is all a plan overwrites in it.
|
|
955
|
+
let restoreIndex = -1
|
|
956
|
+
let restoreValue: unknown
|
|
823
957
|
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
958
|
+
try {
|
|
959
|
+
while (true) {
|
|
960
|
+
iterations += 1
|
|
961
|
+
if (iterations > MaxMacrostepIterations) {
|
|
962
|
+
throw new InfiniteTransitionError({
|
|
963
|
+
machineId: machine.id,
|
|
964
|
+
state: descriptor.nodes[current.activeLeaves[0]!]!.path,
|
|
965
|
+
maxIterations: MaxMacrostepIterations
|
|
966
|
+
})
|
|
967
|
+
}
|
|
833
968
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
}
|
|
838
|
-
if (descriptor.nodes[sourceIndex]!.type === "final") {
|
|
839
|
-
const completed = completeConfigurationSync(
|
|
840
|
-
machine,
|
|
841
|
-
activeConfigurationFromIndexedState(descriptor, current),
|
|
842
|
-
event
|
|
843
|
-
).configuration
|
|
844
|
-
const root = getRootPath(machine, completed)
|
|
845
|
-
if (!completed.outputs.has(root)) {
|
|
846
|
-
throw new Error("Machine reached a terminal indexed configuration without a completed root output")
|
|
969
|
+
const sourceIndex = current.activeLeaves[0]
|
|
970
|
+
if (sourceIndex === undefined) {
|
|
971
|
+
throw new Error("Machine expected an active indexed root state")
|
|
847
972
|
}
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
973
|
+
if (descriptor.nodes[sourceIndex]!.type === "final") {
|
|
974
|
+
const completed = completeConfigurationSync(
|
|
975
|
+
machine,
|
|
976
|
+
activeConfigurationFromIndexedState(descriptor, current),
|
|
977
|
+
event
|
|
978
|
+
).configuration
|
|
979
|
+
const root = getRootPath(machine, completed)
|
|
980
|
+
if (!completed.outputs.has(root)) {
|
|
981
|
+
throw new Error("Machine reached a terminal indexed configuration without a completed root output")
|
|
982
|
+
}
|
|
983
|
+
return {
|
|
984
|
+
event: decoded,
|
|
985
|
+
next: indexedStateFromActive(descriptor, completed),
|
|
986
|
+
commands: commands ?? emptyExecutionValues,
|
|
987
|
+
emittedEvents: emittedEvents ?? emptyExecutionValues,
|
|
988
|
+
microsteps: microsteps ?? emptyExecutionValues,
|
|
989
|
+
done: true,
|
|
990
|
+
output: completed.outputs.get(root)
|
|
991
|
+
}
|
|
856
992
|
}
|
|
857
|
-
}
|
|
858
993
|
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
if (update
|
|
880
|
-
|
|
994
|
+
const sourcePath = descriptor.nodes[sourceIndex]!.path
|
|
995
|
+
const transition = normalizeTransition(toImpl(machine).handlers[sourcePath]?.on?.[event._tag])
|
|
996
|
+
if (transition !== undefined) {
|
|
997
|
+
const transitionResult = collectIndexedTransition(
|
|
998
|
+
machine,
|
|
999
|
+
transition.transition,
|
|
1000
|
+
{
|
|
1001
|
+
...resolveMachineReferences(machine, machineReferences),
|
|
1002
|
+
state: current.values[sourceIndex],
|
|
1003
|
+
containingState: undefined,
|
|
1004
|
+
ancestors: {},
|
|
1005
|
+
event,
|
|
1006
|
+
snapshot: snapshotFromIndexedState(descriptor, current)
|
|
1007
|
+
},
|
|
1008
|
+
transition.evaluate
|
|
1009
|
+
)
|
|
1010
|
+
const update = isStateUpdate(transitionResult.state) ? transitionResult.state : undefined
|
|
1011
|
+
const target = update === undefined ? transitionResult.state : undefined
|
|
1012
|
+
// The new value of the source state, when this microstep keeps it active.
|
|
1013
|
+
let value: unknown = noIndexedValue
|
|
1014
|
+
if (update !== undefined) {
|
|
1015
|
+
const node = descriptor.nodes[sourceIndex]!
|
|
1016
|
+
if (update.path !== sourcePath || node.schema === undefined) {
|
|
1017
|
+
throw new Error(`Machine state update owner "${update.path}" must be the active valued state`)
|
|
1018
|
+
}
|
|
1019
|
+
value = decodeStateValueSync(machine, node, update.value)
|
|
1020
|
+
}
|
|
1021
|
+
validateDeclaredTransitionTarget(
|
|
1022
|
+
sourcePath,
|
|
1023
|
+
{ type: "event", event: event._tag },
|
|
1024
|
+
transition.targets,
|
|
1025
|
+
target
|
|
1026
|
+
)
|
|
1027
|
+
if (target !== undefined && !isTarget(target) && !isSnapshot(target)) {
|
|
1028
|
+
throw new Error("Machine expected indexed transition target to be a snapshot or target builder result")
|
|
881
1029
|
}
|
|
882
|
-
updateOwnedIndexedValue(current, sourceIndex, decodeStateValueSync(machine, node, update.value))
|
|
883
|
-
}
|
|
884
|
-
validateDeclaredTransitionTarget(
|
|
885
|
-
sourcePath,
|
|
886
|
-
{ type: "event", event: event._tag },
|
|
887
|
-
transition.targets,
|
|
888
|
-
target
|
|
889
|
-
)
|
|
890
|
-
if (target !== undefined && !isTarget(target) && !isSnapshot(target)) {
|
|
891
|
-
throw new Error("Machine expected indexed transition target to be a snapshot or target builder result")
|
|
892
|
-
}
|
|
893
1030
|
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
sourceIndex
|
|
907
|
-
|
|
908
|
-
machine,
|
|
909
|
-
descriptor.nodes[sourceIndex]!,
|
|
910
|
-
target.value
|
|
911
|
-
)
|
|
912
|
-
)
|
|
913
|
-
} else {
|
|
914
|
-
next = normalizeIndexedTargetStateSync(machine, descriptor, current, target, sourceIndex)
|
|
1031
|
+
let next = current
|
|
1032
|
+
if (target !== undefined) {
|
|
1033
|
+
const targetIndex = descriptor.indexByPath.get(String(target.path))
|
|
1034
|
+
const isSimpleTarget = isTarget(target)
|
|
1035
|
+
? (target as TargetSnapshotSlot)[TargetSnapshotTypeId] === undefined && target.values === undefined
|
|
1036
|
+
: !("state" in target) && !("states" in target) && !("completed" in target) && !("history" in target)
|
|
1037
|
+
if (
|
|
1038
|
+
targetIndex === sourceIndex && isSimpleTarget && current.completedOrder.length === 0 &&
|
|
1039
|
+
descriptor.valued[sourceIndex]
|
|
1040
|
+
) {
|
|
1041
|
+
value = decodeStateValueSync(machine, descriptor.nodes[sourceIndex]!, target.value)
|
|
1042
|
+
} else {
|
|
1043
|
+
next = normalizeIndexedTargetStateSync(machine, descriptor, current, target, sourceIndex)
|
|
1044
|
+
}
|
|
915
1045
|
}
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
next,
|
|
924
|
-
event,
|
|
925
|
-
...(retainMicrosteps
|
|
926
|
-
? {
|
|
927
|
-
transitions: [{
|
|
928
|
-
source: sourcePath,
|
|
929
|
-
trigger: { type: "event", event: event._tag },
|
|
930
|
-
reenter: transition.reenter,
|
|
931
|
-
branchIndex: transitionResult.branchIndex,
|
|
932
|
-
branchKey: transitionResult.branchKey,
|
|
933
|
-
target: target === undefined ? undefined : getTargetNodePath(target),
|
|
934
|
-
resolvedTarget: target === undefined ? undefined : getTargetNodePath(target),
|
|
935
|
-
updates: update === undefined ? [] : [update.path]
|
|
936
|
-
}]
|
|
1046
|
+
if (value !== noIndexedValue) {
|
|
1047
|
+
if (!writable) {
|
|
1048
|
+
current = withWritableIndexedValues(current)
|
|
1049
|
+
writable = true
|
|
1050
|
+
} else if (current === configuration && restoreIndex === -1) {
|
|
1051
|
+
restoreIndex = sourceIndex
|
|
1052
|
+
restoreValue = configuration.values[sourceIndex]
|
|
937
1053
|
}
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
1054
|
+
current.values[sourceIndex] = value
|
|
1055
|
+
next = current
|
|
1056
|
+
}
|
|
1057
|
+
const changed = transition.reenter || !hasSameIndexedActive(current, next)
|
|
1058
|
+
const nextIndex = next.activeLeaves[0]
|
|
1059
|
+
if (nextIndex === undefined) {
|
|
1060
|
+
throw new Error("Machine expected an active indexed transition target")
|
|
1061
|
+
}
|
|
1062
|
+
const exitPaths = changed ? [sourcePath] : emptyExecutionValues
|
|
1063
|
+
const entryPaths = changed ? [descriptor.nodes[nextIndex]!.path] : emptyExecutionValues
|
|
1064
|
+
const step: ExecutionMicrostep<IndexedExecutionState> = retainMicrosteps
|
|
1065
|
+
? new RetainedFlatMicrostep(
|
|
1066
|
+
next,
|
|
1067
|
+
event,
|
|
1068
|
+
transitionResult.commands,
|
|
1069
|
+
transitionResult.raisedEvents,
|
|
1070
|
+
transitionResult.emittedEvents,
|
|
1071
|
+
exitPaths,
|
|
1072
|
+
entryPaths,
|
|
1073
|
+
changed,
|
|
1074
|
+
sourcePath,
|
|
1075
|
+
transition.reenter,
|
|
1076
|
+
transitionResult.branchIndex,
|
|
1077
|
+
transitionResult.branchKey,
|
|
1078
|
+
target,
|
|
1079
|
+
update
|
|
1080
|
+
)
|
|
1081
|
+
: {
|
|
1082
|
+
next,
|
|
1083
|
+
event,
|
|
1084
|
+
commands: transitionResult.commands,
|
|
1085
|
+
raisedEvents: transitionResult.raisedEvents,
|
|
1086
|
+
emittedEvents: transitionResult.emittedEvents,
|
|
1087
|
+
exitPaths,
|
|
1088
|
+
entryPaths,
|
|
1089
|
+
changed
|
|
1090
|
+
}
|
|
1091
|
+
// A state that this microstep created is referenced by nothing else,
|
|
1092
|
+
// unless the microstep is retained.
|
|
1093
|
+
if (next !== current) writable = true
|
|
1094
|
+
current = next
|
|
1095
|
+
if (retainMicrosteps) writable = false
|
|
1096
|
+
;(microsteps ??= []).push(step)
|
|
1097
|
+
if (transitionResult.commands.length > 0) {
|
|
1098
|
+
;(commands ??= []).push(...transitionResult.commands)
|
|
1099
|
+
}
|
|
1100
|
+
if (transitionResult.raisedEvents.length > 0) {
|
|
1101
|
+
;(raisedEvents ??= []).push(...transitionResult.raisedEvents)
|
|
1102
|
+
}
|
|
1103
|
+
if (transitionResult.emittedEvents.length > 0) {
|
|
1104
|
+
;(emittedEvents ??= []).push(...transitionResult.emittedEvents)
|
|
1105
|
+
}
|
|
956
1106
|
}
|
|
957
|
-
}
|
|
958
1107
|
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
1108
|
+
if (descriptor.nodes[current.activeLeaves[0]!]!.type === "final") {
|
|
1109
|
+
continue
|
|
1110
|
+
}
|
|
1111
|
+
const raised = raisedEvents?.[raisedIndex]
|
|
1112
|
+
if (raised === undefined) {
|
|
1113
|
+
return {
|
|
1114
|
+
event: decoded,
|
|
1115
|
+
next: current,
|
|
1116
|
+
commands: commands ?? emptyExecutionValues,
|
|
1117
|
+
emittedEvents: emittedEvents ?? emptyExecutionValues,
|
|
1118
|
+
microsteps: microsteps ?? emptyExecutionValues,
|
|
1119
|
+
done: false,
|
|
1120
|
+
output: undefined
|
|
1121
|
+
}
|
|
972
1122
|
}
|
|
1123
|
+
raisedIndex += 1
|
|
1124
|
+
event = raised
|
|
973
1125
|
}
|
|
974
|
-
|
|
975
|
-
|
|
1126
|
+
} catch (error) {
|
|
1127
|
+
if (restoreIndex !== -1) configuration.values[restoreIndex] = restoreValue
|
|
1128
|
+
throw error
|
|
976
1129
|
}
|
|
977
1130
|
}
|
|
978
1131
|
|
|
979
1132
|
const planIndexedState = (
|
|
980
1133
|
machine: Machine.Any,
|
|
981
1134
|
descriptor: IndexedExecutionDescriptor,
|
|
982
|
-
configuration:
|
|
1135
|
+
configuration: IndexedExecutionState,
|
|
983
1136
|
input: unknown,
|
|
1137
|
+
decode: boolean,
|
|
984
1138
|
retainMicrosteps: boolean,
|
|
1139
|
+
ownership: ExecutionOwnership,
|
|
985
1140
|
machineReferences?: PlanningMachineReferences
|
|
986
|
-
): ExecutionMacrostep<
|
|
1141
|
+
): ExecutionMacrostep<IndexedExecutionState> => {
|
|
987
1142
|
if (InvocationEvent.isInvocationEvent(input)) {
|
|
988
1143
|
// Invocation lifecycle transitions retain the generic planner as their
|
|
989
1144
|
// semantic reference. Ordinary machine events and initialization stay on
|
|
@@ -998,11 +1153,11 @@ const planIndexedState = (
|
|
|
998
1153
|
)
|
|
999
1154
|
return {
|
|
1000
1155
|
event: planned.event,
|
|
1001
|
-
next:
|
|
1156
|
+
next: indexedStateFromActive(descriptor, planned.next),
|
|
1002
1157
|
commands: planned.commands,
|
|
1003
1158
|
emittedEvents: planned.emittedEvents,
|
|
1004
1159
|
microsteps: planned.microsteps.map((step) => ({
|
|
1005
|
-
next:
|
|
1160
|
+
next: indexedStateFromActive(descriptor, step.next),
|
|
1006
1161
|
event: step.event,
|
|
1007
1162
|
transitions: step.transitions,
|
|
1008
1163
|
commands: step.commands,
|
|
@@ -1016,10 +1171,20 @@ const planIndexedState = (
|
|
|
1016
1171
|
output: planned.output
|
|
1017
1172
|
}
|
|
1018
1173
|
}
|
|
1019
|
-
const decoded = decodeEventSync(machine, input)
|
|
1174
|
+
const decoded: IndexedEvent = decode ? decodeEventSync(machine, input) : input as IndexedEvent
|
|
1020
1175
|
if (descriptor.flat) {
|
|
1021
|
-
return planIndexedFlatState(
|
|
1176
|
+
return planIndexedFlatState(
|
|
1177
|
+
machine,
|
|
1178
|
+
descriptor,
|
|
1179
|
+
configuration,
|
|
1180
|
+
decoded,
|
|
1181
|
+
retainMicrosteps,
|
|
1182
|
+
ownership,
|
|
1183
|
+
machineReferences
|
|
1184
|
+
)
|
|
1022
1185
|
}
|
|
1186
|
+
// The hierarchical kernel never writes a state in place, so it plans every
|
|
1187
|
+
// ownership alike.
|
|
1023
1188
|
if (descriptor.finalIndices.some((index) => configuration.active[index] === 1)) {
|
|
1024
1189
|
const active = activeConfigurationFromIndexedState(descriptor, configuration)
|
|
1025
1190
|
if (isActiveFinalConfiguration(machine, active)) {
|
|
@@ -1030,7 +1195,7 @@ const planIndexedState = (
|
|
|
1030
1195
|
}
|
|
1031
1196
|
return {
|
|
1032
1197
|
event: decoded,
|
|
1033
|
-
next:
|
|
1198
|
+
next: indexedStateFromActive(descriptor, completed),
|
|
1034
1199
|
commands: [],
|
|
1035
1200
|
emittedEvents: [],
|
|
1036
1201
|
microsteps: [],
|
|
@@ -1059,7 +1224,7 @@ const planIndexedState = (
|
|
|
1059
1224
|
const commands = [...first.commands]
|
|
1060
1225
|
const raisedEvents = [...first.raisedEvents]
|
|
1061
1226
|
const emittedEvents = [...first.emittedEvents]
|
|
1062
|
-
const microsteps = [
|
|
1227
|
+
const microsteps = [first]
|
|
1063
1228
|
let raisedIndex = 0
|
|
1064
1229
|
let iterations = 0
|
|
1065
1230
|
|
|
@@ -1079,7 +1244,7 @@ const planIndexedState = (
|
|
|
1079
1244
|
activeConfigurationFromIndexedState(descriptor, current),
|
|
1080
1245
|
currentEvent
|
|
1081
1246
|
).configuration
|
|
1082
|
-
current =
|
|
1247
|
+
current = indexedStateFromActive(descriptor, completed)
|
|
1083
1248
|
if (isActiveFinalConfiguration(machine, completed)) {
|
|
1084
1249
|
const root = getRootPath(machine, completed)
|
|
1085
1250
|
if (!completed.outputs.has(root)) {
|
|
@@ -1118,10 +1283,19 @@ const planIndexedState = (
|
|
|
1118
1283
|
commands.push(...step.commands)
|
|
1119
1284
|
raisedEvents.push(...step.raisedEvents)
|
|
1120
1285
|
emittedEvents.push(...step.emittedEvents)
|
|
1121
|
-
microsteps.push(
|
|
1286
|
+
microsteps.push(step)
|
|
1122
1287
|
}
|
|
1123
1288
|
}
|
|
1124
1289
|
|
|
1290
|
+
/**
|
|
1291
|
+
* A machine compiled for planning, with an execution state of its own.
|
|
1292
|
+
*
|
|
1293
|
+
* `plan` takes the {@link ExecutionOwnership} of the state it receives. With
|
|
1294
|
+
* `retainMicrosteps`, each microstep keeps its retained transitions, and its
|
|
1295
|
+
* `next` stays readable for as long as that ownership keeps what a plan returns
|
|
1296
|
+
* valid. Without it, a microstep's `next` may have been written by a later
|
|
1297
|
+
* microstep of the same plan, and only its paths and `changed` are meaningful.
|
|
1298
|
+
*/
|
|
1125
1299
|
export interface CompiledExecutionPlan {
|
|
1126
1300
|
readonly fromConfiguration: (configuration: ActiveConfiguration) => unknown
|
|
1127
1301
|
readonly toConfiguration: (state: unknown) => ActiveConfiguration
|
|
@@ -1129,7 +1303,19 @@ export interface CompiledExecutionPlan {
|
|
|
1129
1303
|
readonly plan: (
|
|
1130
1304
|
state: unknown,
|
|
1131
1305
|
event: unknown,
|
|
1132
|
-
|
|
1306
|
+
ownership: ExecutionOwnership,
|
|
1307
|
+
retainMicrosteps: boolean,
|
|
1308
|
+
machineReferences?: PlanningMachineReferences
|
|
1309
|
+
) => ExecutionMacrostep
|
|
1310
|
+
/**
|
|
1311
|
+
* Plans an event that needs no decoding: a public event that
|
|
1312
|
+
* `decodeEventSync` returned, or an invocation event. Otherwise as `plan`.
|
|
1313
|
+
*/
|
|
1314
|
+
readonly planDecoded: (
|
|
1315
|
+
state: unknown,
|
|
1316
|
+
event: Machine.EventOf<any> | InvocationEvent.InvocationEvent,
|
|
1317
|
+
ownership: ExecutionOwnership,
|
|
1318
|
+
retainMicrosteps: boolean,
|
|
1133
1319
|
machineReferences?: PlanningMachineReferences
|
|
1134
1320
|
) => ExecutionMacrostep
|
|
1135
1321
|
readonly initial?: (
|
|
@@ -1146,13 +1332,23 @@ export interface CompiledExecutionPlan {
|
|
|
1146
1332
|
}
|
|
1147
1333
|
|
|
1148
1334
|
const executionPlanCache = new WeakMap<Machine.Any, CompiledExecutionPlan>()
|
|
1335
|
+
const referenceExecutionPlanCache = new WeakMap<Machine.Any, CompiledExecutionPlan>()
|
|
1149
1336
|
|
|
1150
1337
|
const makeActiveExecutionPlan = (machine: Machine.Any): CompiledExecutionPlan => ({
|
|
1151
1338
|
fromConfiguration: (configuration) => configuration,
|
|
1152
1339
|
toConfiguration: (state) => state as ActiveConfiguration,
|
|
1153
1340
|
snapshot: (state) => snapshotFromConfiguration(machine, state as ActiveConfiguration),
|
|
1154
|
-
|
|
1341
|
+
// The reference planner never writes a configuration in place.
|
|
1342
|
+
plan: (state, event, _ownership, _retainMicrosteps, machineReferences) =>
|
|
1155
1343
|
planConfiguration(
|
|
1344
|
+
machine,
|
|
1345
|
+
machineReferences === undefined
|
|
1346
|
+
? state as ActiveConfiguration
|
|
1347
|
+
: withMachineReferences(state as ActiveConfiguration, machineReferences),
|
|
1348
|
+
event as Machine.EventOf<any>
|
|
1349
|
+
),
|
|
1350
|
+
planDecoded: (state, event, _ownership, _retainMicrosteps, machineReferences) =>
|
|
1351
|
+
planDecodedConfiguration(
|
|
1156
1352
|
machine,
|
|
1157
1353
|
machineReferences === undefined
|
|
1158
1354
|
? state as ActiveConfiguration
|
|
@@ -1165,11 +1361,31 @@ const makeIndexedExecutionPlan = (
|
|
|
1165
1361
|
machine: Machine.Any,
|
|
1166
1362
|
indexed: IndexedExecutionDescriptor
|
|
1167
1363
|
): CompiledExecutionPlan => ({
|
|
1168
|
-
fromConfiguration: (configuration) =>
|
|
1169
|
-
toConfiguration: (state) => activeConfigurationFromIndexedState(indexed, state as
|
|
1170
|
-
snapshot: (state) => snapshotFromIndexedState(indexed, state as
|
|
1171
|
-
plan: (state, event, retainMicrosteps
|
|
1172
|
-
planIndexedState(
|
|
1364
|
+
fromConfiguration: (configuration) => indexedStateFromActive(indexed, configuration),
|
|
1365
|
+
toConfiguration: (state) => activeConfigurationFromIndexedState(indexed, state as IndexedExecutionState),
|
|
1366
|
+
snapshot: (state) => snapshotFromIndexedState(indexed, state as IndexedExecutionState),
|
|
1367
|
+
plan: (state, event, ownership, retainMicrosteps, machineReferences) =>
|
|
1368
|
+
planIndexedState(
|
|
1369
|
+
machine,
|
|
1370
|
+
indexed,
|
|
1371
|
+
state as IndexedExecutionState,
|
|
1372
|
+
event,
|
|
1373
|
+
true,
|
|
1374
|
+
retainMicrosteps,
|
|
1375
|
+
ownership,
|
|
1376
|
+
machineReferences
|
|
1377
|
+
),
|
|
1378
|
+
planDecoded: (state, event, ownership, retainMicrosteps, machineReferences) =>
|
|
1379
|
+
planIndexedState(
|
|
1380
|
+
machine,
|
|
1381
|
+
indexed,
|
|
1382
|
+
state as IndexedExecutionState,
|
|
1383
|
+
event,
|
|
1384
|
+
false,
|
|
1385
|
+
retainMicrosteps,
|
|
1386
|
+
ownership,
|
|
1387
|
+
machineReferences
|
|
1388
|
+
),
|
|
1173
1389
|
// Only captured data constructors can use indexed startup. Unknown initializer
|
|
1174
1390
|
// protocols retain generic startup so commands and emissions cannot be dropped.
|
|
1175
1391
|
...(Object.values(toImpl(machine).handlers as Record<string, CapturedStateConfig>).some((config) =>
|
|
@@ -1202,7 +1418,7 @@ const makeIndexedExecutionPlan = (
|
|
|
1202
1418
|
? normalized
|
|
1203
1419
|
: withMachineReferences(normalized, machineReferences)
|
|
1204
1420
|
const completed = completeConfigurationSync(machine, active, InitialEvent).configuration
|
|
1205
|
-
const configuration =
|
|
1421
|
+
const configuration = indexedStateFromActive(indexed, completed)
|
|
1206
1422
|
const state = snapshotFromIndexedState(indexed, configuration)
|
|
1207
1423
|
const done = isActiveFinalConfiguration(machine, completed)
|
|
1208
1424
|
if (!done) {
|
|
@@ -1238,33 +1454,25 @@ export interface SelectedExecutionPlan {
|
|
|
1238
1454
|
readonly plan: CompiledExecutionPlan
|
|
1239
1455
|
}
|
|
1240
1456
|
|
|
1241
|
-
const
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
throw new Error(`Machine cannot compile the requested ${strategy} execution plan`)
|
|
1254
|
-
}
|
|
1255
|
-
const selected = indexed.flat ? "indexed-flat" : "indexed-hierarchical"
|
|
1256
|
-
if (strategy !== "auto" && strategy !== selected) {
|
|
1257
|
-
throw new Error(`Machine compiled ${selected}, not the requested ${strategy} execution plan`)
|
|
1457
|
+
const executionPlanStrategyCache = new WeakMap<CompiledExecutionPlan, Exclude<ExecutionPlanStrategy, "auto">>()
|
|
1458
|
+
|
|
1459
|
+
/**
|
|
1460
|
+
* The reference execution plan of `machine`, which always plans on the generic
|
|
1461
|
+
* planner without indexed compilation. Cached per machine.
|
|
1462
|
+
*
|
|
1463
|
+
* @internal
|
|
1464
|
+
*/
|
|
1465
|
+
export const referenceExecutionPlan = (machine: Machine.Any): CompiledExecutionPlan => {
|
|
1466
|
+
const cached = referenceExecutionPlanCache.get(machine)
|
|
1467
|
+
if (cached !== undefined) {
|
|
1468
|
+
return cached
|
|
1258
1469
|
}
|
|
1259
|
-
|
|
1470
|
+
const reference = makeActiveExecutionPlan(machine)
|
|
1471
|
+
referenceExecutionPlanCache.set(machine, reference)
|
|
1472
|
+
executionPlanStrategyCache.set(reference, "generic")
|
|
1473
|
+
return reference
|
|
1260
1474
|
}
|
|
1261
1475
|
|
|
1262
|
-
/** @internal Test-only uncached strategy selection. */
|
|
1263
|
-
export const selectExecutionPlanForTesting = (
|
|
1264
|
-
machine: Machine.Any,
|
|
1265
|
-
strategy: ExecutionPlanStrategy
|
|
1266
|
-
): SelectedExecutionPlan => selectExecutionPlan(machine, strategy)
|
|
1267
|
-
|
|
1268
1476
|
export const compileExecutionPlan = (machine: Machine.Any): CompiledExecutionPlan => {
|
|
1269
1477
|
const cached = executionPlanCache.get(machine)
|
|
1270
1478
|
if (cached !== undefined) {
|
|
@@ -1272,8 +1480,30 @@ export const compileExecutionPlan = (machine: Machine.Any): CompiledExecutionPla
|
|
|
1272
1480
|
}
|
|
1273
1481
|
const indexed = compileIndexedExecutionDescriptor(machine)
|
|
1274
1482
|
const compiled = indexed === undefined
|
|
1275
|
-
?
|
|
1483
|
+
? referenceExecutionPlan(machine)
|
|
1276
1484
|
: makeIndexedExecutionPlan(machine, indexed)
|
|
1485
|
+
if (indexed !== undefined) {
|
|
1486
|
+
executionPlanStrategyCache.set(compiled, indexed.flat ? "indexed-flat" : "indexed-hierarchical")
|
|
1487
|
+
}
|
|
1277
1488
|
executionPlanCache.set(machine, compiled)
|
|
1278
1489
|
return compiled
|
|
1279
1490
|
}
|
|
1491
|
+
|
|
1492
|
+
/** @internal Test-only planner selection, delegating to the cached reference and compiled plans. */
|
|
1493
|
+
export const selectExecutionPlanForTesting = (
|
|
1494
|
+
machine: Machine.Any,
|
|
1495
|
+
strategy: ExecutionPlanStrategy
|
|
1496
|
+
): SelectedExecutionPlan => {
|
|
1497
|
+
if (strategy === "generic") {
|
|
1498
|
+
return { strategy, plan: referenceExecutionPlan(machine) }
|
|
1499
|
+
}
|
|
1500
|
+
const plan = compileExecutionPlan(machine)
|
|
1501
|
+
const selected = executionPlanStrategyCache.get(plan) ?? "generic"
|
|
1502
|
+
if (strategy !== "auto" && strategy !== selected) {
|
|
1503
|
+
if (selected === "generic") {
|
|
1504
|
+
throw new Error(`Machine cannot compile the requested ${strategy} execution plan`)
|
|
1505
|
+
}
|
|
1506
|
+
throw new Error(`Machine compiled ${selected}, not the requested ${strategy} execution plan`)
|
|
1507
|
+
}
|
|
1508
|
+
return { strategy: selected, plan }
|
|
1509
|
+
}
|