@typeonce/effect-machine 0.33.0 → 0.35.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 +272 -628
- package/dist/Machine.d.ts +1076 -896
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +141 -90
- 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 -2
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -64
- 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 +15 -22
- 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/initialDeclaration.d.ts +14 -0
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
- package/dist/internal/machine/initialDeclaration.js +137 -0
- package/dist/internal/machine/initialDeclaration.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 -4
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +169 -388
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +8 -0
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +37 -78
- 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/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.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/stateDefinition.d.ts +1 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +1 -13
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- 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 +41 -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 +5 -3
- 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 +53 -46
- 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/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +7 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +105 -73
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +105 -73
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +18 -13
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +18 -25
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +41 -24
- package/dist/unstable/reactivity/AtomMachine.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 +1836 -2907
- 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 -65
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +16 -22
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/initialDeclaration.ts +157 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +225 -576
- package/src/internal/machine/planner.ts +84 -118
- package/src/internal/machine/process.ts +40 -33
- package/src/internal/machine/protocol.ts +3 -3
- 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/stateDefinition.ts +1 -15
- package/src/internal/machine/targetReference.ts +55 -0
- package/src/internal/machine/topology.ts +5 -3
- package/src/internal/testing/machine/finiteModel.ts +58 -53
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/internal/testing/machine/verification.ts +7 -1
- package/src/testing/MachineTest.ts +105 -73
- package/src/unstable/cluster/ClusterMachine.ts +22 -25
- package/src/unstable/reactivity/AtomMachine.ts +41 -24
|
@@ -35,10 +35,11 @@ import {
|
|
|
35
35
|
normalizeTargetConfigurationSync,
|
|
36
36
|
pathDepth,
|
|
37
37
|
snapshotFromConfiguration,
|
|
38
|
-
snapshotFromConfigurationAtPath
|
|
39
|
-
validateInitialConfiguration
|
|
38
|
+
snapshotFromConfigurationAtPath
|
|
40
39
|
} from "./configuration.js"
|
|
41
40
|
import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError, StoppedError } from "./errors.js"
|
|
41
|
+
import { type CapturedStateConfig, toImpl } from "./implementation.js"
|
|
42
|
+
import { isDataInitializer } from "./initialDeclaration.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"
|
|
@@ -299,11 +300,13 @@ const completeHistoryConfiguration = (
|
|
|
299
300
|
)
|
|
300
301
|
let next = current
|
|
301
302
|
for (const routed of [choice.target, ...choice.additionalTargets]) {
|
|
302
|
-
if (routed === undefined
|
|
303
|
+
if (routed === undefined) {
|
|
303
304
|
throw new Error(`Machine initial choice "${child.path}" must resolve to an active state target`)
|
|
304
305
|
}
|
|
305
306
|
const resolved = isInitialTarget(routed)
|
|
306
307
|
? resolveInitialTarget(machine, next, routed, event)
|
|
308
|
+
: isHistoryTarget(routed)
|
|
309
|
+
? resolveHistoryTarget(machine, next, routed, event)
|
|
307
310
|
: undefined
|
|
308
311
|
next = normalizeTargetConfigurationSync(
|
|
309
312
|
machine,
|
|
@@ -330,7 +333,7 @@ const completeHistoryConfiguration = (
|
|
|
330
333
|
}
|
|
331
334
|
active.add(child.path)
|
|
332
335
|
if (child.schema !== undefined) {
|
|
333
|
-
const initializer = machine.handlers[path]?.initialize
|
|
336
|
+
const initializer = toImpl(machine).handlers[path]?.initialize
|
|
334
337
|
if (initializer === undefined) {
|
|
335
338
|
values.set(child.path, decodeStateValueSync(machine, child, makeStateInput({})))
|
|
336
339
|
changed = true
|
|
@@ -342,7 +345,7 @@ const completeHistoryConfiguration = (
|
|
|
342
345
|
containingState: getParentValue(machine, current, path),
|
|
343
346
|
ancestors: getParentValues(machine, current, path),
|
|
344
347
|
event,
|
|
345
|
-
builder: makeStateInitializeBuilder(machine, path)
|
|
348
|
+
...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
|
|
346
349
|
})
|
|
347
350
|
const initializedValues = getStateInitializeValues(path, initialized.value)
|
|
348
351
|
values.set(child.path, decodeStateValueSync(machine, child, initializedValues[child.key]))
|
|
@@ -363,14 +366,14 @@ const completeHistoryConfiguration = (
|
|
|
363
366
|
history: configuration.history,
|
|
364
367
|
machineReferences: configuration.machineReferences
|
|
365
368
|
} as ActiveConfiguration
|
|
366
|
-
const initializer = valuedMissing.length === 0 ? undefined : machine.handlers[path]?.initialize
|
|
369
|
+
const initializer = valuedMissing.length === 0 ? undefined : toImpl(machine).handlers[path]?.initialize
|
|
367
370
|
const initialized = initializer === undefined ? undefined : collectStateInitializer(machine, initializer, {
|
|
368
371
|
...resolveMachineReferences(machine, current),
|
|
369
372
|
state: current.values.get(path),
|
|
370
373
|
containingState: getParentValue(machine, current, path),
|
|
371
374
|
ancestors: getParentValues(machine, current, path),
|
|
372
375
|
event,
|
|
373
|
-
builder: makeStateInitializeBuilder(machine, path)
|
|
376
|
+
...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
|
|
374
377
|
})
|
|
375
378
|
const initializedValues = initialized === undefined
|
|
376
379
|
? undefined
|
|
@@ -386,6 +389,10 @@ const completeHistoryConfiguration = (
|
|
|
386
389
|
if (
|
|
387
390
|
initializedValues === undefined || !Object.prototype.hasOwnProperty.call(initializedValues, child.key)
|
|
388
391
|
) {
|
|
392
|
+
if (initializer !== undefined && isDataInitializer(initializer)) {
|
|
393
|
+
values.set(child.path, decodeStateValueSync(machine, child, makeStateInput({})))
|
|
394
|
+
continue
|
|
395
|
+
}
|
|
389
396
|
throw new Error(`Machine parallel state initializer for "${path}" must return region "${child.key}"`)
|
|
390
397
|
}
|
|
391
398
|
values.set(
|
|
@@ -418,14 +425,24 @@ const completeHistoryConfiguration = (
|
|
|
418
425
|
}
|
|
419
426
|
}
|
|
420
427
|
|
|
421
|
-
|
|
428
|
+
/** Resolves initial data into owned configuration without a snapshot round trip. */
|
|
429
|
+
export function resolveInitialConfiguration(
|
|
422
430
|
machine: Machine.Any,
|
|
423
431
|
configuration: ActiveConfiguration,
|
|
424
432
|
target: InitialTargetInstruction,
|
|
425
433
|
event: unknown
|
|
426
434
|
) {
|
|
427
435
|
const partial = configurationFromInitialTargetSync(machine, configuration, target)
|
|
428
|
-
|
|
436
|
+
return completeHistoryConfiguration(machine, partial, event)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export function resolveInitialTarget(
|
|
440
|
+
machine: Machine.Any,
|
|
441
|
+
configuration: ActiveConfiguration,
|
|
442
|
+
target: InitialTargetInstruction,
|
|
443
|
+
event: unknown
|
|
444
|
+
) {
|
|
445
|
+
const completed = resolveInitialConfiguration(machine, configuration, target, event)
|
|
429
446
|
const snapshot = snapshotFromConfigurationAtPath(machine, completed.configuration, target.path)
|
|
430
447
|
return {
|
|
431
448
|
target: makeTarget(target.path as any, snapshot.value as any, {
|
|
@@ -472,7 +489,7 @@ function resolveHistoryTarget(
|
|
|
472
489
|
}
|
|
473
490
|
|
|
474
491
|
const key = node.key
|
|
475
|
-
const fallback = machine.handlers[target.parent]?.history?.[key]?.default
|
|
492
|
+
const fallback = toImpl(machine).handlers[target.parent]?.history?.[key]?.default
|
|
476
493
|
if (fallback === undefined) {
|
|
477
494
|
throw new Error(`Machine history state "${target.path}" requires a default implementation`)
|
|
478
495
|
}
|
|
@@ -709,7 +726,7 @@ const makeTransitionContext = <
|
|
|
709
726
|
path: string,
|
|
710
727
|
event: Machine.EventByTag<Events, EventTag>,
|
|
711
728
|
snapshot: Machine.Snapshot<States>
|
|
712
|
-
): Machine.HandlerContext<States, Events, Emits, StateId, EventTag, any, any
|
|
729
|
+
): ConstructionContext<Machine.HandlerContext<States, Events, Emits, StateId, EventTag, any, any>> => ({
|
|
713
730
|
...resolveMachineReferences(machine, configuration),
|
|
714
731
|
state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
|
|
715
732
|
containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
|
|
@@ -731,7 +748,7 @@ const makeDoneContext = <
|
|
|
731
748
|
event: Machine.LifecycleEvent<Events>,
|
|
732
749
|
output: unknown,
|
|
733
750
|
snapshot: Machine.Snapshot<States>
|
|
734
|
-
): Machine.DoneContext<States, Events, Emits, StateId
|
|
751
|
+
): ConstructionContext<Machine.DoneContext<States, Events, Emits, StateId>> => ({
|
|
735
752
|
...resolveMachineReferences(machine, configuration),
|
|
736
753
|
state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
|
|
737
754
|
containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
|
|
@@ -766,7 +783,7 @@ const collectStateActions = <
|
|
|
766
783
|
R
|
|
767
784
|
>(
|
|
768
785
|
machine,
|
|
769
|
-
machine.handlers[path]?.[key],
|
|
786
|
+
toImpl(machine).handlers[path]?.[key],
|
|
770
787
|
makeStateActionContext<States, Events, Emits, Machine.StateIdentifier<States>>(
|
|
771
788
|
machine,
|
|
772
789
|
configuration,
|
|
@@ -796,7 +813,7 @@ const selectAlwaysTransitions = <
|
|
|
796
813
|
States,
|
|
797
814
|
E,
|
|
798
815
|
R,
|
|
799
|
-
Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
816
|
+
ConstructionContext<Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
800
817
|
>
|
|
801
818
|
> => {
|
|
802
819
|
const selected: Array<
|
|
@@ -804,7 +821,7 @@ const selectAlwaysTransitions = <
|
|
|
804
821
|
States,
|
|
805
822
|
E,
|
|
806
823
|
R,
|
|
807
|
-
Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
824
|
+
ConstructionContext<Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
808
825
|
>
|
|
809
826
|
> = []
|
|
810
827
|
const selectedSources = new Set<string>()
|
|
@@ -813,7 +830,7 @@ const selectAlwaysTransitions = <
|
|
|
813
830
|
const capturedSnapshot = () => snapshot ??= snapshotFromConfiguration<States>(machine, configuration)
|
|
814
831
|
for (const leaf of getActiveLeafPaths(machine, configuration)) {
|
|
815
832
|
for (const path of getLeafCandidatePaths(machine, leaf)) {
|
|
816
|
-
const always = normalizeTransition(machine.handlers[path]?.always)
|
|
833
|
+
const always = normalizeTransition(toImpl(machine).handlers[path]?.always)
|
|
817
834
|
if (always !== undefined) {
|
|
818
835
|
let candidate = evaluatedSources.get(path)
|
|
819
836
|
if (!evaluatedSources.has(path)) {
|
|
@@ -825,7 +842,7 @@ const selectAlwaysTransitions = <
|
|
|
825
842
|
States,
|
|
826
843
|
E,
|
|
827
844
|
R,
|
|
828
|
-
Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
845
|
+
ConstructionContext<Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
829
846
|
>,
|
|
830
847
|
context: {
|
|
831
848
|
...resolveMachineReferences(machine, configuration),
|
|
@@ -876,7 +893,7 @@ const selectDoneTransitions = <
|
|
|
876
893
|
States,
|
|
877
894
|
E,
|
|
878
895
|
R,
|
|
879
|
-
Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
896
|
+
ConstructionContext<Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
880
897
|
>
|
|
881
898
|
> => {
|
|
882
899
|
const selected: Array<
|
|
@@ -884,14 +901,14 @@ const selectDoneTransitions = <
|
|
|
884
901
|
States,
|
|
885
902
|
E,
|
|
886
903
|
R,
|
|
887
|
-
Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
904
|
+
ConstructionContext<Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
888
905
|
>
|
|
889
906
|
> = []
|
|
890
907
|
const selectedSources = new Set<string>()
|
|
891
908
|
let snapshot: Machine.Snapshot<States> | undefined
|
|
892
909
|
const capturedSnapshot = () => snapshot ??= snapshotFromConfiguration<States>(machine, configuration)
|
|
893
910
|
for (const completion of completions) {
|
|
894
|
-
const onDone = normalizeTransition(machine.handlers[completion.path]?.onDone)
|
|
911
|
+
const onDone = normalizeTransition(toImpl(machine).handlers[completion.path]?.onDone)
|
|
895
912
|
if (onDone !== undefined && !selectedSources.has(completion.path)) {
|
|
896
913
|
selectedSources.add(completion.path)
|
|
897
914
|
const candidate = resolveDeclinableCandidate(machine, {
|
|
@@ -902,7 +919,7 @@ const selectDoneTransitions = <
|
|
|
902
919
|
States,
|
|
903
920
|
E,
|
|
904
921
|
R,
|
|
905
|
-
Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States
|
|
922
|
+
ConstructionContext<Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>>
|
|
906
923
|
>,
|
|
907
924
|
context: makeDoneContext<States, Events, Emits, Machine.StateIdentifier<States>>(
|
|
908
925
|
machine,
|
|
@@ -934,14 +951,7 @@ const selectEventTransitions = <
|
|
|
934
951
|
States,
|
|
935
952
|
E,
|
|
936
953
|
R,
|
|
937
|
-
|
|
938
|
-
>
|
|
939
|
-
> => {
|
|
940
|
-
const selected: Array<
|
|
941
|
-
SelectedTransition<
|
|
942
|
-
States,
|
|
943
|
-
E,
|
|
944
|
-
R,
|
|
954
|
+
ConstructionContext<
|
|
945
955
|
Machine.HandlerContext<
|
|
946
956
|
States,
|
|
947
957
|
Events,
|
|
@@ -952,6 +962,25 @@ const selectEventTransitions = <
|
|
|
952
962
|
R
|
|
953
963
|
>
|
|
954
964
|
>
|
|
965
|
+
>
|
|
966
|
+
> => {
|
|
967
|
+
const selected: Array<
|
|
968
|
+
SelectedTransition<
|
|
969
|
+
States,
|
|
970
|
+
E,
|
|
971
|
+
R,
|
|
972
|
+
ConstructionContext<
|
|
973
|
+
Machine.HandlerContext<
|
|
974
|
+
States,
|
|
975
|
+
Events,
|
|
976
|
+
Emits,
|
|
977
|
+
Machine.StateIdentifier<States>,
|
|
978
|
+
Machine.TagOf<Events[number]>,
|
|
979
|
+
E,
|
|
980
|
+
R
|
|
981
|
+
>
|
|
982
|
+
>
|
|
983
|
+
>
|
|
955
984
|
> = []
|
|
956
985
|
const selectedSources = new Set<string>()
|
|
957
986
|
const evaluatedSources = new Map<string, SelectedTransition<States, E, R, any> | undefined>()
|
|
@@ -959,7 +988,7 @@ const selectEventTransitions = <
|
|
|
959
988
|
const capturedSnapshot = () => snapshot ??= snapshotFromConfiguration<States>(machine, configuration)
|
|
960
989
|
for (const leaf of getActiveLeafPaths(machine, configuration)) {
|
|
961
990
|
for (const path of getLeafCandidatePaths(machine, leaf)) {
|
|
962
|
-
const transition = normalizeTransition(machine.handlers[path]?.on?.[event._tag])
|
|
991
|
+
const transition = normalizeTransition(toImpl(machine).handlers[path]?.on?.[event._tag])
|
|
963
992
|
if (transition !== undefined) {
|
|
964
993
|
let candidate = evaluatedSources.get(path)
|
|
965
994
|
if (!evaluatedSources.has(path)) {
|
|
@@ -971,14 +1000,16 @@ const selectEventTransitions = <
|
|
|
971
1000
|
States,
|
|
972
1001
|
E,
|
|
973
1002
|
R,
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
1003
|
+
ConstructionContext<
|
|
1004
|
+
Machine.HandlerContext<
|
|
1005
|
+
States,
|
|
1006
|
+
Events,
|
|
1007
|
+
Emits,
|
|
1008
|
+
Machine.StateIdentifier<States>,
|
|
1009
|
+
Machine.TagOf<Events[number]>,
|
|
1010
|
+
E,
|
|
1011
|
+
R
|
|
1012
|
+
>
|
|
982
1013
|
>
|
|
983
1014
|
>,
|
|
984
1015
|
context: makeTransitionContext<
|
|
@@ -1015,7 +1046,7 @@ const selectInvocationTransition = <
|
|
|
1015
1046
|
event: InvocationEvent.InvocationEvent
|
|
1016
1047
|
): ReadonlyArray<SelectedTransition<States, E, R, any>> => {
|
|
1017
1048
|
if (!configuration.active.has(event.path)) return []
|
|
1018
|
-
const config = machine.handlers[event.path] as
|
|
1049
|
+
const config = toImpl(machine).handlers[event.path] as CapturedStateConfig | undefined
|
|
1019
1050
|
const invoke = InvocationDefinition.definitions(config?.invoke).find((definition) => {
|
|
1020
1051
|
const id = "child" in definition ? definition.child?.id : definition.id
|
|
1021
1052
|
return String(id) === event.id
|
|
@@ -1265,7 +1296,7 @@ function resolveChoiceTarget(
|
|
|
1265
1296
|
if (node.type !== "choice" || node.parent !== extracted.target.parent) {
|
|
1266
1297
|
throw new Error(`Machine expected choice target "${extracted.target.path}" to resolve to its declared parent`)
|
|
1267
1298
|
}
|
|
1268
|
-
const choice = machine.handlers[node.path]?.choice
|
|
1299
|
+
const choice = toImpl(machine).handlers[node.path]?.choice
|
|
1269
1300
|
if (choice === undefined || typeof choice.transition !== "function") {
|
|
1270
1301
|
throw new Error(`Machine choice state "${node.path}" requires an implementation`)
|
|
1271
1302
|
}
|
|
@@ -1705,77 +1736,13 @@ export const planInitialSync = <
|
|
|
1705
1736
|
outputs: new Map(),
|
|
1706
1737
|
history: new Map()
|
|
1707
1738
|
}
|
|
1708
|
-
const rootResolution =
|
|
1709
|
-
|
|
1710
|
-
: undefined
|
|
1711
|
-
const state = rootResolution?.target ?? initial
|
|
1712
|
-
const initialChoice = choiceFromTarget(state)
|
|
1713
|
-
const choiceResolution = initialChoice === undefined
|
|
1714
|
-
? undefined
|
|
1715
|
-
: resolveChoiceTarget(
|
|
1716
|
-
machine,
|
|
1717
|
-
emptyConfiguration,
|
|
1718
|
-
state,
|
|
1719
|
-
InitialEvent
|
|
1720
|
-
)
|
|
1721
|
-
const initialHistoryActions: Array<RuntimeCommand> = []
|
|
1722
|
-
const initialHistoryRaisedEvents: Array<unknown> = []
|
|
1723
|
-
const initialHistoryEmittedEvents: Array<unknown> = []
|
|
1724
|
-
const initialHistoryChoiceTransitions: Array<ResolvedChoiceTransition> = []
|
|
1725
|
-
const resolvedInitialTargets: Array<unknown> = []
|
|
1726
|
-
for (
|
|
1727
|
-
const target of choiceResolution !== undefined
|
|
1728
|
-
? [choiceResolution.target, ...choiceResolution.additionalTargets]
|
|
1729
|
-
: rootResolution === undefined
|
|
1730
|
-
? []
|
|
1731
|
-
: [rootResolution.target]
|
|
1732
|
-
) {
|
|
1733
|
-
if (!isHistoryTarget(target)) {
|
|
1734
|
-
resolvedInitialTargets.push(target)
|
|
1735
|
-
continue
|
|
1736
|
-
}
|
|
1737
|
-
const history = resolveHistoryTarget(machine, emptyConfiguration, target, InitialEvent)
|
|
1738
|
-
resolvedInitialTargets.push(history.target)
|
|
1739
|
-
initialHistoryActions.push(...history.commands)
|
|
1740
|
-
initialHistoryRaisedEvents.push(...history.raisedEvents)
|
|
1741
|
-
initialHistoryEmittedEvents.push(...history.emittedEvents)
|
|
1742
|
-
initialHistoryChoiceTransitions.push(...history.transitions)
|
|
1743
|
-
}
|
|
1744
|
-
let resolvedConfiguration = resolvedInitialTargets.length === 0
|
|
1745
|
-
? normalizeConfigurationSync<States>(machine, state as Machine.Snapshot<States>)
|
|
1746
|
-
: normalizeTargetConfigurationSync<States>(
|
|
1747
|
-
machine,
|
|
1748
|
-
emptyConfiguration,
|
|
1749
|
-
resolvedInitialTargets[0] as
|
|
1750
|
-
| Machine.Snapshot<States>
|
|
1751
|
-
| Machine.Target<States, Machine.StateIdentifier<States>>
|
|
1752
|
-
)
|
|
1753
|
-
for (const additionalTarget of resolvedInitialTargets.slice(1)) {
|
|
1754
|
-
resolvedConfiguration = normalizeTargetConfigurationSync<States>(
|
|
1755
|
-
machine,
|
|
1756
|
-
resolvedConfiguration,
|
|
1757
|
-
additionalTarget as Machine.Snapshot<States> | Machine.Target<States, Machine.StateIdentifier<States>>
|
|
1758
|
-
)
|
|
1759
|
-
}
|
|
1760
|
-
const configuration: ActiveConfiguration = resolvedConfiguration
|
|
1761
|
-
if (machine.initialDefinition.selection.kind === "initial") validateInitialConfiguration(machine, configuration)
|
|
1739
|
+
const rootResolution = resolveInitialTarget(machine, emptyConfiguration, initial, InitialEvent)
|
|
1740
|
+
const configuration = normalizeTargetConfigurationSync(machine, emptyConfiguration, rootResolution.target)
|
|
1762
1741
|
const startingState = snapshotFromConfiguration<States>(machine, configuration)
|
|
1763
1742
|
const initialEntryPaths = getInitialEntryPaths(machine, configuration)
|
|
1764
|
-
const commands =
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
...initialHistoryActions
|
|
1768
|
-
]
|
|
1769
|
-
const raisedEvents = [
|
|
1770
|
-
...(rootResolution?.raisedEvents ?? []),
|
|
1771
|
-
...(choiceResolution?.raisedEvents ?? []),
|
|
1772
|
-
...initialHistoryRaisedEvents
|
|
1773
|
-
]
|
|
1774
|
-
const emittedEvents = [
|
|
1775
|
-
...(rootResolution?.emittedEvents ?? []),
|
|
1776
|
-
...(choiceResolution?.emittedEvents ?? []),
|
|
1777
|
-
...initialHistoryEmittedEvents
|
|
1778
|
-
]
|
|
1743
|
+
const commands = rootResolution.commands
|
|
1744
|
+
const raisedEvents = rootResolution.raisedEvents
|
|
1745
|
+
const emittedEvents = rootResolution.emittedEvents
|
|
1779
1746
|
const entry = collectStateActions<States, Events, Emits, E, R>(
|
|
1780
1747
|
machine,
|
|
1781
1748
|
configuration,
|
|
@@ -1790,17 +1757,12 @@ export const planInitialSync = <
|
|
|
1790
1757
|
[...entry.commands],
|
|
1791
1758
|
[...raisedEvents, ...entry.raisedEvents] as Array<Machine.EventOf<Events>>,
|
|
1792
1759
|
[...emittedEvents, ...entry.emittedEvents],
|
|
1793
|
-
|
|
1794
|
-
initialHistoryChoiceTransitions.length === 0 ?
|
|
1760
|
+
rootResolution.transitions.length === 0 ?
|
|
1795
1761
|
[] :
|
|
1796
1762
|
[{
|
|
1797
1763
|
next: configuration,
|
|
1798
1764
|
event: InitialEvent,
|
|
1799
|
-
transitions:
|
|
1800
|
-
...(rootResolution?.transitions ?? []),
|
|
1801
|
-
...(choiceResolution?.transitions ?? []),
|
|
1802
|
-
...initialHistoryChoiceTransitions
|
|
1803
|
-
],
|
|
1765
|
+
transitions: rootResolution.transitions,
|
|
1804
1766
|
commands,
|
|
1805
1767
|
raisedEvents: [
|
|
1806
1768
|
...raisedEvents
|
|
@@ -1876,7 +1838,7 @@ export const enabled = <
|
|
|
1876
1838
|
const tags: Array<Machine.TagOf<Events[number]>> = []
|
|
1877
1839
|
const seen = new Set<PropertyKey>()
|
|
1878
1840
|
for (const path of getCandidatePaths(machine, configuration)) {
|
|
1879
|
-
for (const tag of Reflect.ownKeys(machine.handlers[path]?.on ?? {})) {
|
|
1841
|
+
for (const tag of Reflect.ownKeys(toImpl(machine).handlers[path]?.on ?? {})) {
|
|
1880
1842
|
if (!seen.has(tag)) {
|
|
1881
1843
|
seen.add(tag)
|
|
1882
1844
|
tags.push(tag as Machine.TagOf<Events[number]>)
|
|
@@ -2090,7 +2052,7 @@ const settle = <
|
|
|
2090
2052
|
const completed = completeConfigurationSync(machine, currentState, currentEvent)
|
|
2091
2053
|
currentState = completed.configuration
|
|
2092
2054
|
pendingCompletions.push(
|
|
2093
|
-
...completed.completions.filter((completion) => machine.handlers[completion.path]?.onDone !== undefined)
|
|
2055
|
+
...completed.completions.filter((completion) => toImpl(machine).handlers[completion.path]?.onDone !== undefined)
|
|
2094
2056
|
)
|
|
2095
2057
|
while (pendingCompletions.length > 0 && !currentState.active.has(pendingCompletions[0]!.path)) {
|
|
2096
2058
|
pendingCompletions.shift()
|
|
@@ -2356,3 +2318,7 @@ export const planInitial = (
|
|
|
2356
2318
|
: new StartupError({ cause: Cause.die(error) })
|
|
2357
2319
|
}
|
|
2358
2320
|
})
|
|
2321
|
+
|
|
2322
|
+
// Captured constructors are private planner inputs. Public callbacks receive root data
|
|
2323
|
+
// and the constructors bound to their declared branches during normalization.
|
|
2324
|
+
type ConstructionContext<C> = Omit<C, "root"> & { readonly target: unknown }
|