@typeonce/effect-machine 0.33.0 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +216 -636
- package/dist/Machine.d.ts +815 -583
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +20 -8
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/childRegistry.d.ts +1 -1
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/cluster.d.ts +2 -50
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +4 -27
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterProtocol.d.ts +35 -0
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -0
- package/dist/internal/machine/clusterProtocol.js +28 -0
- package/dist/internal/machine/clusterProtocol.js.map +1 -0
- package/dist/internal/machine/commandRuntime.d.ts +1 -1
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/configuration.d.ts +2 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -45
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +17 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -0
- package/dist/internal/machine/declaration.js +120 -0
- package/dist/internal/machine/declaration.js.map +1 -0
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +5 -4
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +34 -0
- package/dist/internal/machine/implementation.d.ts.map +1 -0
- package/dist/internal/machine/implementation.js +3 -0
- package/dist/internal/machine/implementation.js.map +1 -0
- package/dist/internal/machine/invocation.d.ts +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +1 -1
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
- package/dist/internal/machine/machine.d.ts +13 -2
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +140 -278
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +12 -11
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +8 -7
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +16 -12
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +2 -230
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +8 -1857
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
- package/dist/internal/machine/runtimeCompiled.js +867 -0
- package/dist/internal/machine/runtimeCompiled.js.map +1 -0
- package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
- package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
- package/dist/internal/machine/runtimeGeneric.js +471 -0
- package/dist/internal/machine/runtimeGeneric.js.map +1 -0
- package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
- package/dist/internal/machine/runtimeProtocol.js +561 -0
- package/dist/internal/machine/runtimeProtocol.js.map +1 -0
- package/dist/internal/machine/targetReference.d.ts +15 -0
- package/dist/internal/machine/targetReference.d.ts.map +1 -0
- package/dist/internal/machine/targetReference.js +31 -0
- package/dist/internal/machine/targetReference.js.map +1 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +2 -1
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +38 -40
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +5 -5
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +5 -17
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/docs/agent-guide.md +66 -91
- package/docs/machine-review.md +19 -49
- package/docs/root-api.md +62 -62
- package/package.json +1 -1
- package/src/Machine.ts +1514 -2312
- package/src/internal/machine/childRegistry.ts +1 -1
- package/src/internal/machine/cluster.ts +7 -73
- package/src/internal/machine/clusterProtocol.ts +56 -0
- package/src/internal/machine/commandRuntime.ts +1 -1
- package/src/internal/machine/configuration.ts +53 -45
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +6 -5
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +199 -409
- package/src/internal/machine/planner.ts +54 -35
- package/src/internal/machine/process.ts +40 -33
- package/src/internal/machine/runtime.ts +20 -3037
- package/src/internal/machine/runtimeCompiled.ts +1166 -0
- package/src/internal/machine/runtimeGeneric.ts +799 -0
- package/src/internal/machine/runtimeProtocol.ts +1213 -0
- package/src/internal/machine/targetReference.ts +40 -0
- package/src/internal/machine/topology.ts +2 -1
- package/src/internal/testing/machine/finiteModel.ts +42 -43
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/unstable/cluster/ClusterMachine.ts +9 -17
|
@@ -39,6 +39,7 @@ import {
|
|
|
39
39
|
validateInitialConfiguration
|
|
40
40
|
} from "./configuration.js"
|
|
41
41
|
import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError, StoppedError } from "./errors.js"
|
|
42
|
+
import { type CapturedStateConfig, toImpl } from "./implementation.js"
|
|
42
43
|
import { getStateInitializeValues, makeStateInitializeBuilder } from "./initialization.js"
|
|
43
44
|
import * as InvocationDefinition from "./invocationDefinition.js"
|
|
44
45
|
import * as InvocationEvent from "./invocationEvent.js"
|
|
@@ -330,7 +331,7 @@ const completeHistoryConfiguration = (
|
|
|
330
331
|
}
|
|
331
332
|
active.add(child.path)
|
|
332
333
|
if (child.schema !== undefined) {
|
|
333
|
-
const initializer = machine.handlers[path]?.initialize
|
|
334
|
+
const initializer = toImpl(machine).handlers[path]?.initialize
|
|
334
335
|
if (initializer === undefined) {
|
|
335
336
|
values.set(child.path, decodeStateValueSync(machine, child, makeStateInput({})))
|
|
336
337
|
changed = true
|
|
@@ -363,7 +364,7 @@ const completeHistoryConfiguration = (
|
|
|
363
364
|
history: configuration.history,
|
|
364
365
|
machineReferences: configuration.machineReferences
|
|
365
366
|
} as ActiveConfiguration
|
|
366
|
-
const initializer = valuedMissing.length === 0 ? undefined : machine.handlers[path]?.initialize
|
|
367
|
+
const initializer = valuedMissing.length === 0 ? undefined : toImpl(machine).handlers[path]?.initialize
|
|
367
368
|
const initialized = initializer === undefined ? undefined : collectStateInitializer(machine, initializer, {
|
|
368
369
|
...resolveMachineReferences(machine, current),
|
|
369
370
|
state: current.values.get(path),
|
|
@@ -472,7 +473,7 @@ function resolveHistoryTarget(
|
|
|
472
473
|
}
|
|
473
474
|
|
|
474
475
|
const key = node.key
|
|
475
|
-
const fallback = machine.handlers[target.parent]?.history?.[key]?.default
|
|
476
|
+
const fallback = toImpl(machine).handlers[target.parent]?.history?.[key]?.default
|
|
476
477
|
if (fallback === undefined) {
|
|
477
478
|
throw new Error(`Machine history state "${target.path}" requires a default implementation`)
|
|
478
479
|
}
|
|
@@ -709,7 +710,7 @@ const makeTransitionContext = <
|
|
|
709
710
|
path: string,
|
|
710
711
|
event: Machine.EventByTag<Events, EventTag>,
|
|
711
712
|
snapshot: Machine.Snapshot<States>
|
|
712
|
-
): Machine.HandlerContext<States, Events, Emits, StateId, EventTag, any, any
|
|
713
|
+
): ConstructionContext<Machine.HandlerContext<States, Events, Emits, StateId, EventTag, any, any>> => ({
|
|
713
714
|
...resolveMachineReferences(machine, configuration),
|
|
714
715
|
state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
|
|
715
716
|
containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
|
|
@@ -731,7 +732,7 @@ const makeDoneContext = <
|
|
|
731
732
|
event: Machine.LifecycleEvent<Events>,
|
|
732
733
|
output: unknown,
|
|
733
734
|
snapshot: Machine.Snapshot<States>
|
|
734
|
-
): Machine.DoneContext<States, Events, Emits, StateId
|
|
735
|
+
): ConstructionContext<Machine.DoneContext<States, Events, Emits, StateId>> => ({
|
|
735
736
|
...resolveMachineReferences(machine, configuration),
|
|
736
737
|
state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
|
|
737
738
|
containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
|
|
@@ -766,7 +767,7 @@ const collectStateActions = <
|
|
|
766
767
|
R
|
|
767
768
|
>(
|
|
768
769
|
machine,
|
|
769
|
-
machine.handlers[path]?.[key],
|
|
770
|
+
toImpl(machine).handlers[path]?.[key],
|
|
770
771
|
makeStateActionContext<States, Events, Emits, Machine.StateIdentifier<States>>(
|
|
771
772
|
machine,
|
|
772
773
|
configuration,
|
|
@@ -796,7 +797,7 @@ const selectAlwaysTransitions = <
|
|
|
796
797
|
States,
|
|
797
798
|
E,
|
|
798
799
|
R,
|
|
799
|
-
Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
800
|
+
ConstructionContext<Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
800
801
|
>
|
|
801
802
|
> => {
|
|
802
803
|
const selected: Array<
|
|
@@ -804,7 +805,7 @@ const selectAlwaysTransitions = <
|
|
|
804
805
|
States,
|
|
805
806
|
E,
|
|
806
807
|
R,
|
|
807
|
-
Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
808
|
+
ConstructionContext<Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
808
809
|
>
|
|
809
810
|
> = []
|
|
810
811
|
const selectedSources = new Set<string>()
|
|
@@ -813,7 +814,7 @@ const selectAlwaysTransitions = <
|
|
|
813
814
|
const capturedSnapshot = () => snapshot ??= snapshotFromConfiguration<States>(machine, configuration)
|
|
814
815
|
for (const leaf of getActiveLeafPaths(machine, configuration)) {
|
|
815
816
|
for (const path of getLeafCandidatePaths(machine, leaf)) {
|
|
816
|
-
const always = normalizeTransition(machine.handlers[path]?.always)
|
|
817
|
+
const always = normalizeTransition(toImpl(machine).handlers[path]?.always)
|
|
817
818
|
if (always !== undefined) {
|
|
818
819
|
let candidate = evaluatedSources.get(path)
|
|
819
820
|
if (!evaluatedSources.has(path)) {
|
|
@@ -825,7 +826,7 @@ const selectAlwaysTransitions = <
|
|
|
825
826
|
States,
|
|
826
827
|
E,
|
|
827
828
|
R,
|
|
828
|
-
Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
829
|
+
ConstructionContext<Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
829
830
|
>,
|
|
830
831
|
context: {
|
|
831
832
|
...resolveMachineReferences(machine, configuration),
|
|
@@ -876,7 +877,7 @@ const selectDoneTransitions = <
|
|
|
876
877
|
States,
|
|
877
878
|
E,
|
|
878
879
|
R,
|
|
879
|
-
Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
880
|
+
ConstructionContext<Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
880
881
|
>
|
|
881
882
|
> => {
|
|
882
883
|
const selected: Array<
|
|
@@ -884,14 +885,14 @@ const selectDoneTransitions = <
|
|
|
884
885
|
States,
|
|
885
886
|
E,
|
|
886
887
|
R,
|
|
887
|
-
Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
888
|
+
ConstructionContext<Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
888
889
|
>
|
|
889
890
|
> = []
|
|
890
891
|
const selectedSources = new Set<string>()
|
|
891
892
|
let snapshot: Machine.Snapshot<States> | undefined
|
|
892
893
|
const capturedSnapshot = () => snapshot ??= snapshotFromConfiguration<States>(machine, configuration)
|
|
893
894
|
for (const completion of completions) {
|
|
894
|
-
const onDone = normalizeTransition(machine.handlers[completion.path]?.onDone)
|
|
895
|
+
const onDone = normalizeTransition(toImpl(machine).handlers[completion.path]?.onDone)
|
|
895
896
|
if (onDone !== undefined && !selectedSources.has(completion.path)) {
|
|
896
897
|
selectedSources.add(completion.path)
|
|
897
898
|
const candidate = resolveDeclinableCandidate(machine, {
|
|
@@ -902,7 +903,7 @@ const selectDoneTransitions = <
|
|
|
902
903
|
States,
|
|
903
904
|
E,
|
|
904
905
|
R,
|
|
905
|
-
Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
906
|
+
ConstructionContext<Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
906
907
|
>,
|
|
907
908
|
context: makeDoneContext<States, Events, Emits, Machine.StateIdentifier<States>>(
|
|
908
909
|
machine,
|
|
@@ -934,14 +935,7 @@ const selectEventTransitions = <
|
|
|
934
935
|
States,
|
|
935
936
|
E,
|
|
936
937
|
R,
|
|
937
|
-
|
|
938
|
-
>
|
|
939
|
-
> => {
|
|
940
|
-
const selected: Array<
|
|
941
|
-
SelectedTransition<
|
|
942
|
-
States,
|
|
943
|
-
E,
|
|
944
|
-
R,
|
|
938
|
+
ConstructionContext<
|
|
945
939
|
Machine.HandlerContext<
|
|
946
940
|
States,
|
|
947
941
|
Events,
|
|
@@ -952,6 +946,25 @@ const selectEventTransitions = <
|
|
|
952
946
|
R
|
|
953
947
|
>
|
|
954
948
|
>
|
|
949
|
+
>
|
|
950
|
+
> => {
|
|
951
|
+
const selected: Array<
|
|
952
|
+
SelectedTransition<
|
|
953
|
+
States,
|
|
954
|
+
E,
|
|
955
|
+
R,
|
|
956
|
+
ConstructionContext<
|
|
957
|
+
Machine.HandlerContext<
|
|
958
|
+
States,
|
|
959
|
+
Events,
|
|
960
|
+
Emits,
|
|
961
|
+
Machine.StateIdentifier<States>,
|
|
962
|
+
Machine.TagOf<Events[number]>,
|
|
963
|
+
E,
|
|
964
|
+
R
|
|
965
|
+
>
|
|
966
|
+
>
|
|
967
|
+
>
|
|
955
968
|
> = []
|
|
956
969
|
const selectedSources = new Set<string>()
|
|
957
970
|
const evaluatedSources = new Map<string, SelectedTransition<States, E, R, any> | undefined>()
|
|
@@ -959,7 +972,7 @@ const selectEventTransitions = <
|
|
|
959
972
|
const capturedSnapshot = () => snapshot ??= snapshotFromConfiguration<States>(machine, configuration)
|
|
960
973
|
for (const leaf of getActiveLeafPaths(machine, configuration)) {
|
|
961
974
|
for (const path of getLeafCandidatePaths(machine, leaf)) {
|
|
962
|
-
const transition = normalizeTransition(machine.handlers[path]?.on?.[event._tag])
|
|
975
|
+
const transition = normalizeTransition(toImpl(machine).handlers[path]?.on?.[event._tag])
|
|
963
976
|
if (transition !== undefined) {
|
|
964
977
|
let candidate = evaluatedSources.get(path)
|
|
965
978
|
if (!evaluatedSources.has(path)) {
|
|
@@ -971,14 +984,16 @@ const selectEventTransitions = <
|
|
|
971
984
|
States,
|
|
972
985
|
E,
|
|
973
986
|
R,
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
987
|
+
ConstructionContext<
|
|
988
|
+
Machine.HandlerContext<
|
|
989
|
+
States,
|
|
990
|
+
Events,
|
|
991
|
+
Emits,
|
|
992
|
+
Machine.StateIdentifier<States>,
|
|
993
|
+
Machine.TagOf<Events[number]>,
|
|
994
|
+
E,
|
|
995
|
+
R
|
|
996
|
+
>
|
|
982
997
|
>
|
|
983
998
|
>,
|
|
984
999
|
context: makeTransitionContext<
|
|
@@ -1015,7 +1030,7 @@ const selectInvocationTransition = <
|
|
|
1015
1030
|
event: InvocationEvent.InvocationEvent
|
|
1016
1031
|
): ReadonlyArray<SelectedTransition<States, E, R, any>> => {
|
|
1017
1032
|
if (!configuration.active.has(event.path)) return []
|
|
1018
|
-
const config = machine.handlers[event.path] as
|
|
1033
|
+
const config = toImpl(machine).handlers[event.path] as CapturedStateConfig | undefined
|
|
1019
1034
|
const invoke = InvocationDefinition.definitions(config?.invoke).find((definition) => {
|
|
1020
1035
|
const id = "child" in definition ? definition.child?.id : definition.id
|
|
1021
1036
|
return String(id) === event.id
|
|
@@ -1265,7 +1280,7 @@ function resolveChoiceTarget(
|
|
|
1265
1280
|
if (node.type !== "choice" || node.parent !== extracted.target.parent) {
|
|
1266
1281
|
throw new Error(`Machine expected choice target "${extracted.target.path}" to resolve to its declared parent`)
|
|
1267
1282
|
}
|
|
1268
|
-
const choice = machine.handlers[node.path]?.choice
|
|
1283
|
+
const choice = toImpl(machine).handlers[node.path]?.choice
|
|
1269
1284
|
if (choice === undefined || typeof choice.transition !== "function") {
|
|
1270
1285
|
throw new Error(`Machine choice state "${node.path}" requires an implementation`)
|
|
1271
1286
|
}
|
|
@@ -1876,7 +1891,7 @@ export const enabled = <
|
|
|
1876
1891
|
const tags: Array<Machine.TagOf<Events[number]>> = []
|
|
1877
1892
|
const seen = new Set<PropertyKey>()
|
|
1878
1893
|
for (const path of getCandidatePaths(machine, configuration)) {
|
|
1879
|
-
for (const tag of Reflect.ownKeys(machine.handlers[path]?.on ?? {})) {
|
|
1894
|
+
for (const tag of Reflect.ownKeys(toImpl(machine).handlers[path]?.on ?? {})) {
|
|
1880
1895
|
if (!seen.has(tag)) {
|
|
1881
1896
|
seen.add(tag)
|
|
1882
1897
|
tags.push(tag as Machine.TagOf<Events[number]>)
|
|
@@ -2090,7 +2105,7 @@ const settle = <
|
|
|
2090
2105
|
const completed = completeConfigurationSync(machine, currentState, currentEvent)
|
|
2091
2106
|
currentState = completed.configuration
|
|
2092
2107
|
pendingCompletions.push(
|
|
2093
|
-
...completed.completions.filter((completion) => machine.handlers[completion.path]?.onDone !== undefined)
|
|
2108
|
+
...completed.completions.filter((completion) => toImpl(machine).handlers[completion.path]?.onDone !== undefined)
|
|
2094
2109
|
)
|
|
2095
2110
|
while (pendingCompletions.length > 0 && !currentState.active.has(pendingCompletions[0]!.path)) {
|
|
2096
2111
|
pendingCompletions.shift()
|
|
@@ -2356,3 +2371,7 @@ export const planInitial = (
|
|
|
2356
2371
|
: new StartupError({ cause: Cause.die(error) })
|
|
2357
2372
|
}
|
|
2358
2373
|
})
|
|
2374
|
+
|
|
2375
|
+
// Captured constructors are private planner inputs. Public callbacks receive root data
|
|
2376
|
+
// and the constructors bound to their declared branches during normalization.
|
|
2377
|
+
type ConstructionContext<C> = Omit<C, "root"> & { readonly target: unknown }
|
|
@@ -14,10 +14,12 @@ import * as Configuration from "./configuration.js"
|
|
|
14
14
|
import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError } from "./errors.js"
|
|
15
15
|
import type { StoppedError } from "./errors.js"
|
|
16
16
|
import * as ExecutionPlan from "./executionPlan.js"
|
|
17
|
+
import { type CapturedStateConfig, toImpl } from "./implementation.js"
|
|
17
18
|
import * as Invocation from "./invocation.js"
|
|
18
19
|
import * as internalPlanner from "./planner.js"
|
|
19
20
|
import type { ExcludeCompatibleRuntime } from "./requirements.js"
|
|
20
21
|
import * as internalRuntime from "./runtime.js"
|
|
22
|
+
import * as internalRuntimeProtocol from "./runtimeProtocol.js"
|
|
21
23
|
import * as Serialization from "./serialization.js"
|
|
22
24
|
|
|
23
25
|
type ProcessEntry<States extends Machine.StateSchemas, Input extends Schema.Top> =
|
|
@@ -80,7 +82,7 @@ const hasInvokeCapability = (machine: Machine.Any): boolean => {
|
|
|
80
82
|
return cached
|
|
81
83
|
}
|
|
82
84
|
const hasInvokes = Object.values(
|
|
83
|
-
machine.handlers as Record<string,
|
|
85
|
+
toImpl(machine).handlers as Record<string, CapturedStateConfig>
|
|
84
86
|
).some((config) => config.invoke !== undefined)
|
|
85
87
|
invokeCapabilityCache.set(machine, hasInvokes)
|
|
86
88
|
return hasInvokes
|
|
@@ -90,7 +92,7 @@ const makeChildlessCompiledDrain = (
|
|
|
90
92
|
machine: Machine.Any,
|
|
91
93
|
checkInitialFinal: boolean
|
|
92
94
|
): (
|
|
93
|
-
context:
|
|
95
|
+
context: internalRuntimeProtocol.CompiledProcessContext<any, any>
|
|
94
96
|
) => Effect.Effect<Option.Option<any>, any, any> => {
|
|
95
97
|
const executionPlan = ExecutionPlan.compileExecutionPlan(machine)
|
|
96
98
|
return (context) => {
|
|
@@ -114,7 +116,7 @@ const makeChildlessCompiledDrain = (
|
|
|
114
116
|
}
|
|
115
117
|
|
|
116
118
|
const message = pending.value
|
|
117
|
-
const acknowledged =
|
|
119
|
+
const acknowledged = internalRuntimeProtocol.isAcknowledgedMessage(message)
|
|
118
120
|
const event = acknowledged ? message.event : message
|
|
119
121
|
const before = current
|
|
120
122
|
|
|
@@ -180,7 +182,7 @@ const makeChildlessCompiledDrain = (
|
|
|
180
182
|
beforeCommit.pipe(Effect.andThen(Effect.suspend(commitAndContinue)))
|
|
181
183
|
)
|
|
182
184
|
})
|
|
183
|
-
return
|
|
185
|
+
return internalRuntimeProtocol.provideMachineRuntime(loop, context.scope)
|
|
184
186
|
}
|
|
185
187
|
}
|
|
186
188
|
|
|
@@ -207,7 +209,7 @@ const makeInvokingCompiledDrain = (
|
|
|
207
209
|
machine: Machine.Any,
|
|
208
210
|
checkInitialFinal: boolean
|
|
209
211
|
): (
|
|
210
|
-
context:
|
|
212
|
+
context: internalRuntimeProtocol.CompiledProcessContext<any, any>
|
|
211
213
|
) => Effect.Effect<Option.Option<any>, any, any> => {
|
|
212
214
|
const executionPlan = ExecutionPlan.compileExecutionPlan(machine)
|
|
213
215
|
return (context) => {
|
|
@@ -238,7 +240,7 @@ const makeInvokingCompiledDrain = (
|
|
|
238
240
|
}
|
|
239
241
|
|
|
240
242
|
const message = pending.value
|
|
241
|
-
const acknowledged =
|
|
243
|
+
const acknowledged = internalRuntimeProtocol.isAcknowledgedMessage(message)
|
|
242
244
|
const event = acknowledged ? message.event : message
|
|
243
245
|
const before = current
|
|
244
246
|
|
|
@@ -364,7 +366,7 @@ const makeInvokingCompiledDrain = (
|
|
|
364
366
|
execution.initialized = true
|
|
365
367
|
return starting === undefined ? loop : starting.pipe(Effect.andThen(loop))
|
|
366
368
|
}
|
|
367
|
-
return
|
|
369
|
+
return internalRuntimeProtocol.provideMachineRuntime(Effect.suspend(initialize), scope)
|
|
368
370
|
}
|
|
369
371
|
}
|
|
370
372
|
|
|
@@ -383,12 +385,12 @@ const makeProcessLogic: <
|
|
|
383
385
|
>(
|
|
384
386
|
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
385
387
|
entry: ProcessEntry<States, Input>
|
|
386
|
-
) =>
|
|
388
|
+
) => internalRuntimeProtocol.ProcessLogic<
|
|
387
389
|
Machine.Snapshot<States>,
|
|
388
390
|
Machine.EventOf<Events>,
|
|
389
391
|
E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
|
|
390
392
|
ExcludeCompatibleRuntime<
|
|
391
|
-
Exclude<ExecutionServices<InitialR | R>,
|
|
393
|
+
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
392
394
|
Machine.EventOf<Events>,
|
|
393
395
|
Machine.EmittedEventOf<Emits>
|
|
394
396
|
>,
|
|
@@ -421,7 +423,7 @@ const makeProcessLogic: <
|
|
|
421
423
|
const initialArgs = entry._tag === "Initial" ? entry.args : []
|
|
422
424
|
const compiledInitial = entry._tag === "Initial" ? executionPlan.initial : undefined
|
|
423
425
|
const makeCompiledInitial = compiledInitial === undefined ? undefined : (
|
|
424
|
-
scope:
|
|
426
|
+
scope: internalRuntimeProtocol.ProcessScope<Machine.EventOf<Events>>
|
|
425
427
|
) => {
|
|
426
428
|
try {
|
|
427
429
|
const planned = compiledInitial(initialArgs, scope)
|
|
@@ -448,10 +450,10 @@ const makeProcessLogic: <
|
|
|
448
450
|
}
|
|
449
451
|
}
|
|
450
452
|
const makeInitial = (
|
|
451
|
-
scope:
|
|
453
|
+
scope: internalRuntimeProtocol.ProcessScope<Machine.EventOf<Events>>
|
|
452
454
|
) =>
|
|
453
455
|
compiledInitial === undefined
|
|
454
|
-
?
|
|
456
|
+
? internalRuntimeProtocol.provideMachineRuntime(
|
|
455
457
|
internalPlanner.planInitial(internalPlanner.withMachineReferences(machine, scope), ...initialArgs).pipe(
|
|
456
458
|
Effect.flatMap((planned) => {
|
|
457
459
|
scope.inspectInitial(planned.initialEntryPaths, planned.microsteps)
|
|
@@ -495,10 +497,13 @@ const makeProcessLogic: <
|
|
|
495
497
|
},
|
|
496
498
|
initial: (scope) =>
|
|
497
499
|
entry._tag === "Resume"
|
|
498
|
-
?
|
|
500
|
+
? internalRuntimeProtocol.provideMachineRuntime(
|
|
501
|
+
Serialization.normalizeSnapshotEffect(machine, entry.snapshot),
|
|
502
|
+
scope
|
|
503
|
+
)
|
|
499
504
|
: makeInitial(scope).pipe(Effect.map((initialized) => initialized.state)),
|
|
500
505
|
run: (context) =>
|
|
501
|
-
|
|
506
|
+
internalRuntimeProtocol.provideMachineRuntime(
|
|
502
507
|
Effect.gen(function*() {
|
|
503
508
|
const { completeMessage, pollMessage, receiveMessage, state, setState } = context
|
|
504
509
|
if (completeMessage === undefined || pollMessage === undefined || receiveMessage === undefined) {
|
|
@@ -523,12 +528,13 @@ const makeProcessLogic: <
|
|
|
523
528
|
// per iteration; every iteration still crosses Effect boundaries,
|
|
524
529
|
// so the Effect scheduler remains responsible for cooperative yield.
|
|
525
530
|
let configuration: Configuration.ActiveConfiguration | undefined
|
|
526
|
-
let pendingMessage: Option.Option<
|
|
531
|
+
let pendingMessage: Option.Option<internalRuntimeProtocol.ProcessMessage<Machine.EventOf<Events>>> = Option
|
|
532
|
+
.none()
|
|
527
533
|
let liveRuntime: Runtime<Machine.EventOf<Events>, Machine.EmittedEventOf<Emits>> | undefined
|
|
528
534
|
while (terminal === undefined) {
|
|
529
535
|
const message = Option.isSome(pendingMessage) ? pendingMessage.value : yield* receiveMessage
|
|
530
536
|
pendingMessage = Option.none()
|
|
531
|
-
const acknowledged =
|
|
537
|
+
const acknowledged = internalRuntimeProtocol.isAcknowledgedMessage(message)
|
|
532
538
|
const event = acknowledged ? message.event : message
|
|
533
539
|
const before = current
|
|
534
540
|
let planned
|
|
@@ -631,7 +637,8 @@ const makeProcessLogic: <
|
|
|
631
637
|
// As above, keep the normalized configuration only while this
|
|
632
638
|
// worker can continue draining an already queued batch.
|
|
633
639
|
configuration = undefined
|
|
634
|
-
let pendingMessage: Option.Option<
|
|
640
|
+
let pendingMessage: Option.Option<internalRuntimeProtocol.ProcessMessage<Machine.EventOf<Events>>> = Option
|
|
641
|
+
.none()
|
|
635
642
|
let liveRuntime: Runtime<Machine.EventOf<Events>, Machine.EmittedEventOf<Emits>> | undefined
|
|
636
643
|
|
|
637
644
|
// Match the compact non-invoke loop while retaining state-scoped
|
|
@@ -639,7 +646,7 @@ const makeProcessLogic: <
|
|
|
639
646
|
while (terminal === undefined) {
|
|
640
647
|
const message = Option.isSome(pendingMessage) ? pendingMessage.value : yield* receiveMessage
|
|
641
648
|
pendingMessage = Option.none()
|
|
642
|
-
const acknowledged =
|
|
649
|
+
const acknowledged = internalRuntimeProtocol.isAcknowledgedMessage(message)
|
|
643
650
|
const event = acknowledged ? message.event : message
|
|
644
651
|
const before = current
|
|
645
652
|
let planned
|
|
@@ -727,12 +734,12 @@ const makeProcessLogic: <
|
|
|
727
734
|
}),
|
|
728
735
|
context
|
|
729
736
|
)
|
|
730
|
-
}) as
|
|
737
|
+
}) as internalRuntimeProtocol.ProcessLogic<
|
|
731
738
|
Machine.Snapshot<States>,
|
|
732
739
|
Machine.EventOf<Events>,
|
|
733
740
|
E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
|
|
734
741
|
ExcludeCompatibleRuntime<
|
|
735
|
-
Exclude<ExecutionServices<InitialR | R>,
|
|
742
|
+
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
736
743
|
Machine.EventOf<Events>,
|
|
737
744
|
Machine.EmittedEventOf<Emits>
|
|
738
745
|
>,
|
|
@@ -749,7 +756,7 @@ const makeProcessLogic: <
|
|
|
749
756
|
|
|
750
757
|
const initialProcessLogicCache = new WeakMap<
|
|
751
758
|
Machine.Any,
|
|
752
|
-
|
|
759
|
+
internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any>
|
|
753
760
|
>()
|
|
754
761
|
|
|
755
762
|
export const toProcessLogic: <
|
|
@@ -767,12 +774,12 @@ export const toProcessLogic: <
|
|
|
767
774
|
>(
|
|
768
775
|
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
769
776
|
...args: [...Machine.InputArgs<Input>]
|
|
770
|
-
) =>
|
|
777
|
+
) => internalRuntimeProtocol.ProcessLogic<
|
|
771
778
|
Machine.Snapshot<States>,
|
|
772
779
|
Machine.EventOf<Events>,
|
|
773
780
|
E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
|
|
774
781
|
ExcludeCompatibleRuntime<
|
|
775
|
-
Exclude<ExecutionServices<InitialR | R>,
|
|
782
|
+
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
776
783
|
Machine.EventOf<Events>,
|
|
777
784
|
Machine.EmittedEventOf<Emits>
|
|
778
785
|
>,
|
|
@@ -805,7 +812,7 @@ export const toProcessLogic: <
|
|
|
805
812
|
const toResumedProcessLogic = (
|
|
806
813
|
machine: Machine.Any,
|
|
807
814
|
snapshot: Machine.Snapshot<any>
|
|
808
|
-
):
|
|
815
|
+
): internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any> =>
|
|
809
816
|
(makeProcessLogic as any)(machine, { _tag: "Resume", snapshot })
|
|
810
817
|
|
|
811
818
|
/** @internal Test-only runtime strategy selection for a fresh machine. */
|
|
@@ -813,7 +820,7 @@ export const startWithRuntimeStrategyForTesting = (
|
|
|
813
820
|
machine: Machine.Any,
|
|
814
821
|
strategy: internalRuntime.ProcessRuntimeStrategy,
|
|
815
822
|
...args: ReadonlyArray<unknown>
|
|
816
|
-
): Effect.Effect<
|
|
823
|
+
): Effect.Effect<internalRuntimeProtocol.MachineRef<any, any, any, any>, any, any> =>
|
|
817
824
|
internalRuntime.startProcessWithStrategyForTesting(
|
|
818
825
|
(toProcessLogic as any)(machine, ...args),
|
|
819
826
|
strategy,
|
|
@@ -825,7 +832,7 @@ export const prepareWithRuntimeStrategyForTesting = (
|
|
|
825
832
|
machine: Machine.Any,
|
|
826
833
|
strategy: internalRuntime.ProcessRuntimeStrategy,
|
|
827
834
|
...args: ReadonlyArray<unknown>
|
|
828
|
-
): Effect.Effect<
|
|
835
|
+
): Effect.Effect<internalRuntimeProtocol.PreparedProcess<any, any, any, any, any, any, any>, any, any> =>
|
|
829
836
|
internalRuntime.prepareProcessWithStrategyForTesting(
|
|
830
837
|
(toProcessLogic as any)(machine, ...args),
|
|
831
838
|
strategy,
|
|
@@ -837,7 +844,7 @@ export const resumeWithRuntimeStrategyForTesting = (
|
|
|
837
844
|
machine: Machine.Any,
|
|
838
845
|
snapshot: Machine.Snapshot<any>,
|
|
839
846
|
strategy: internalRuntime.ProcessRuntimeStrategy
|
|
840
|
-
): Effect.Effect<
|
|
847
|
+
): Effect.Effect<internalRuntimeProtocol.MachineRef<any, any, any, any>, any, any> =>
|
|
841
848
|
internalRuntime.startProcessWithStrategyForTesting(
|
|
842
849
|
toResumedProcessLogic(machine, snapshot),
|
|
843
850
|
strategy,
|
|
@@ -860,7 +867,7 @@ export const start: <
|
|
|
860
867
|
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
861
868
|
...args: [...Machine.InputArgs<Input>]
|
|
862
869
|
) => Effect.Effect<
|
|
863
|
-
|
|
870
|
+
internalRuntimeProtocol.MachineRef<
|
|
864
871
|
Machine.Snapshot<States>,
|
|
865
872
|
Machine.EventOf<Events>,
|
|
866
873
|
| E
|
|
@@ -878,7 +885,7 @@ export const start: <
|
|
|
878
885
|
| StartupError
|
|
879
886
|
| StoppedError,
|
|
880
887
|
ExcludeCompatibleRuntime<
|
|
881
|
-
Exclude<ExecutionServices<InitialR | R>,
|
|
888
|
+
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
882
889
|
Machine.EventOf<Events>,
|
|
883
890
|
Machine.EmittedEventOf<Emits>
|
|
884
891
|
>
|
|
@@ -904,7 +911,7 @@ export const prepare: <
|
|
|
904
911
|
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
905
912
|
...args: [...Machine.InputArgs<Input>]
|
|
906
913
|
) => Effect.Effect<
|
|
907
|
-
|
|
914
|
+
internalRuntimeProtocol.PreparedProcess<
|
|
908
915
|
Machine.Snapshot<States>,
|
|
909
916
|
Machine.EventOf<Events>,
|
|
910
917
|
| E
|
|
@@ -922,7 +929,7 @@ export const prepare: <
|
|
|
922
929
|
| StartupError
|
|
923
930
|
| StoppedError,
|
|
924
931
|
ExcludeCompatibleRuntime<
|
|
925
|
-
Exclude<ExecutionServices<InitialR | R>,
|
|
932
|
+
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
926
933
|
Machine.EventOf<Events>,
|
|
927
934
|
Machine.EmittedEventOf<Emits>
|
|
928
935
|
>
|
|
@@ -949,7 +956,7 @@ export const resume: <
|
|
|
949
956
|
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
950
957
|
snapshot: Machine.Snapshot<States>
|
|
951
958
|
) => Effect.Effect<
|
|
952
|
-
|
|
959
|
+
internalRuntimeProtocol.MachineRef<
|
|
953
960
|
Machine.Snapshot<States>,
|
|
954
961
|
Machine.EventOf<Events>,
|
|
955
962
|
E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
|
|
@@ -957,7 +964,7 @@ export const resume: <
|
|
|
957
964
|
>,
|
|
958
965
|
MachineSchemaDecodeError,
|
|
959
966
|
ExcludeCompatibleRuntime<
|
|
960
|
-
Exclude<ExecutionServices<R>,
|
|
967
|
+
Exclude<ExecutionServices<R>, internalRuntimeProtocol.MachineRuntime>,
|
|
961
968
|
Machine.EventOf<Events>,
|
|
962
969
|
Machine.EmittedEventOf<Emits>
|
|
963
970
|
>
|