@typeonce/effect-machine 0.9.0 → 0.10.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 +128 -28
- package/dist/Machine.d.ts +334 -238
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +45 -76
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/atom.d.ts +6 -2
- package/dist/internal/machine/atom.d.ts.map +1 -1
- package/dist/internal/machine/atom.js +5 -0
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +2 -1
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/command.js +2 -2
- package/dist/internal/machine/command.js.map +1 -1
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.js +6 -3
- package/dist/internal/machine/commandRuntime.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +8 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +9 -4
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/errors.d.ts +1 -1
- package/dist/internal/machine/errors.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts +3 -3
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +50 -25
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +3 -2
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/machine.d.ts +14 -19
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +12 -10
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +2 -1
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +61 -24
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +8 -8
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +7 -13
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js +128 -50
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +8 -2
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +86 -8
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/topology.d.ts +7 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +6 -0
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +1 -1
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
- package/dist/internal/testing/machine/runtime.js +2 -1
- package/dist/internal/testing/machine/runtime.js.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +5 -3
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +7 -7
- package/dist/testing/MachineTest.js +7 -7
- package/dist/unstable/cluster/ClusterMachine.d.ts +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +25 -8
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +18 -3
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/docs/agent-guide.md +143 -48
- package/package.json +1 -1
- package/src/Machine.ts +559 -307
- package/src/internal/machine/atom.ts +35 -10
- package/src/internal/machine/cluster.ts +2 -1
- package/src/internal/machine/command.ts +2 -2
- package/src/internal/machine/commandRuntime.ts +9 -4
- package/src/internal/machine/configuration.ts +23 -5
- package/src/internal/machine/errors.ts +1 -1
- package/src/internal/machine/executionPlan.ts +74 -29
- package/src/internal/machine/invocation.ts +3 -2
- package/src/internal/machine/machine.ts +68 -49
- package/src/internal/machine/planner.ts +68 -24
- package/src/internal/machine/process.ts +18 -8
- package/src/internal/machine/protocol.ts +207 -68
- package/src/internal/machine/runtime.ts +135 -9
- package/src/internal/machine/topology.ts +15 -0
- package/src/internal/testing/machine/finiteModel.ts +1 -1
- package/src/internal/testing/machine/runtime.ts +2 -1
- package/src/internal/testing/machine/verification.ts +5 -3
- package/src/testing/MachineTest.ts +7 -7
- package/src/unstable/cluster/ClusterMachine.ts +1 -1
- package/src/unstable/reactivity/AtomMachine.ts +42 -10
|
@@ -86,7 +86,8 @@ interface DefineStates {
|
|
|
86
86
|
): Machine.DefinedStates<States>
|
|
87
87
|
<const States extends Machine.StateSchemas>(states: InvalidDefinedStateTreeInput<States>): never
|
|
88
88
|
}
|
|
89
|
-
type ValidateInputEventProtocol<InputEvents extends ReadonlyArray<Machine.TaggedSchema>> = InputEvents
|
|
89
|
+
type ValidateInputEventProtocol<InputEvents extends ReadonlyArray<Machine.TaggedSchema>> = InputEvents extends
|
|
90
|
+
ReadonlyArray<Machine.TaggedSchema> ? unknown : never
|
|
90
91
|
type ValidateInternalEventProtocol<
|
|
91
92
|
InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
|
|
92
93
|
InternalEvents extends ReadonlyArray<Machine.TaggedSchema>
|
|
@@ -111,7 +112,8 @@ const cloneWithHandlers = (
|
|
|
111
112
|
machine.states = self.states
|
|
112
113
|
machine.events = self.events
|
|
113
114
|
machine.internalEvents = self.internalEvents
|
|
114
|
-
machine.
|
|
115
|
+
machine.emittedEvents = self.emittedEvents
|
|
116
|
+
machine.parentEvents = self.parentEvents
|
|
115
117
|
machine.input = self.input
|
|
116
118
|
machine.id = self.id
|
|
117
119
|
machine.initial = self.initial
|
|
@@ -726,6 +728,7 @@ const makeTargetBuilder = <const States extends Machine.StateSchemas>(
|
|
|
726
728
|
const history = makeHistoryTargetBuilder(states, "") as Machine.HistoryTargetBuilder<States>
|
|
727
729
|
return <Source extends Machine.StateNodeIdentifier<States>>(source: Source): Machine.TargetBuilder<States, Source> =>
|
|
728
730
|
({
|
|
731
|
+
none: Topology.makeNoTarget,
|
|
729
732
|
local: makeLocalTargetBuilder(states, stateNodes, source),
|
|
730
733
|
branch: makeBranchTargetBuilder(states, stateNodes, source),
|
|
731
734
|
full,
|
|
@@ -765,18 +768,22 @@ type MakeConfig<
|
|
|
765
768
|
Input extends Schema.Top,
|
|
766
769
|
InitialE,
|
|
767
770
|
InitialR,
|
|
768
|
-
InternalEvents extends ReadonlyArray<Machine.TaggedSchema
|
|
771
|
+
InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
|
|
772
|
+
ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
|
|
769
773
|
> = {
|
|
770
774
|
readonly id?: string
|
|
771
775
|
readonly states: States & DefineStateTreeInput<NoInfer<States>>
|
|
772
|
-
readonly events:
|
|
776
|
+
readonly events:
|
|
777
|
+
& Machine.EventProtocol<"public", InputEvents>
|
|
778
|
+
& ValidateInputEventProtocol<NoInfer<InputEvents>>
|
|
773
779
|
readonly internalEvents?:
|
|
774
|
-
& InternalEvents
|
|
780
|
+
& Machine.EventProtocol<"internal", InternalEvents>
|
|
775
781
|
& ValidateInternalEventProtocol<
|
|
776
782
|
NoInfer<InputEvents>,
|
|
777
783
|
NoInfer<InternalEvents>
|
|
778
784
|
>
|
|
779
|
-
readonly
|
|
785
|
+
readonly emittedEvents?: Machine.EventProtocol<"emitted", Emits>
|
|
786
|
+
readonly parentEvents?: Machine.EventProtocol<"public", ParentEvents>
|
|
780
787
|
readonly input?: Input
|
|
781
788
|
readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>
|
|
782
789
|
}
|
|
@@ -788,7 +795,8 @@ type MakeResult<
|
|
|
788
795
|
Input extends Schema.Top,
|
|
789
796
|
InitialE,
|
|
790
797
|
InitialR,
|
|
791
|
-
InternalEvents extends ReadonlyArray<Machine.TaggedSchema
|
|
798
|
+
InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
|
|
799
|
+
ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
|
|
792
800
|
> = Machine<
|
|
793
801
|
States,
|
|
794
802
|
readonly [...InputEvents, ...InternalEvents],
|
|
@@ -802,7 +810,8 @@ type MakeResult<
|
|
|
802
810
|
Machine.TerminalOutput<States>,
|
|
803
811
|
Emits,
|
|
804
812
|
never,
|
|
805
|
-
InputEvents
|
|
813
|
+
InputEvents,
|
|
814
|
+
ParentEvents
|
|
806
815
|
>
|
|
807
816
|
|
|
808
817
|
interface Make {
|
|
@@ -813,11 +822,12 @@ interface Make {
|
|
|
813
822
|
const Input extends Schema.Top = typeof Schema.Void,
|
|
814
823
|
InitialE = never,
|
|
815
824
|
InitialR = never,
|
|
816
|
-
const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
|
|
825
|
+
const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
|
|
826
|
+
const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
|
|
817
827
|
>(
|
|
818
|
-
config: MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>,
|
|
828
|
+
config: MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>,
|
|
819
829
|
..._validation: ValidateDefinedStates<NoInfer<States>>
|
|
820
|
-
): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>
|
|
830
|
+
): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>
|
|
821
831
|
<
|
|
822
832
|
const States extends Machine.StateSchemas,
|
|
823
833
|
const InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
|
|
@@ -825,10 +835,11 @@ interface Make {
|
|
|
825
835
|
const Input extends Schema.Top = typeof Schema.Void,
|
|
826
836
|
InitialE = never,
|
|
827
837
|
InitialR = never,
|
|
828
|
-
const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
|
|
838
|
+
const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
|
|
839
|
+
const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
|
|
829
840
|
>(
|
|
830
841
|
config:
|
|
831
|
-
& Omit<MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>, "states">
|
|
842
|
+
& Omit<MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>, "states">
|
|
832
843
|
& { readonly states: InvalidDefinedStateTreeInput<States> }
|
|
833
844
|
): never
|
|
834
845
|
}
|
|
@@ -840,24 +851,27 @@ export const make: Make = (<
|
|
|
840
851
|
const Input extends Schema.Top = typeof Schema.Void,
|
|
841
852
|
InitialE = never,
|
|
842
853
|
InitialR = never,
|
|
843
|
-
const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
|
|
854
|
+
const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
|
|
855
|
+
const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
|
|
844
856
|
>(
|
|
845
857
|
config: {
|
|
846
858
|
readonly id?: string
|
|
847
859
|
readonly states: States
|
|
848
|
-
readonly events: InputEvents
|
|
849
|
-
readonly internalEvents?: InternalEvents
|
|
850
|
-
readonly
|
|
860
|
+
readonly events: Machine.EventProtocol<"public", InputEvents>
|
|
861
|
+
readonly internalEvents?: Machine.EventProtocol<"internal", InternalEvents>
|
|
862
|
+
readonly emittedEvents?: Machine.EventProtocol<"emitted", Emits>
|
|
863
|
+
readonly parentEvents?: Machine.EventProtocol<"public", ParentEvents>
|
|
851
864
|
readonly input?: Input
|
|
852
865
|
readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>
|
|
853
866
|
}
|
|
854
|
-
): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents> => {
|
|
867
|
+
): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents> => {
|
|
855
868
|
StateDefinition.validateStateDefinitions(config.states, "Machine.make")
|
|
856
869
|
const self = Object.create(Proto)
|
|
857
870
|
self.states = config.states
|
|
858
871
|
self.events = config.events
|
|
859
|
-
self.internalEvents = config.internalEvents ?? []
|
|
860
|
-
self.
|
|
872
|
+
self.internalEvents = config.internalEvents ?? Protocol.makeEventProtocol("internal", [] as const)
|
|
873
|
+
self.emittedEvents = config.emittedEvents ?? Protocol.makeEventProtocol("emitted", [] as const)
|
|
874
|
+
self.parentEvents = config.parentEvents ?? Protocol.makeEventProtocol("public", [] as const)
|
|
861
875
|
self.input = config.input
|
|
862
876
|
self.id = config.id
|
|
863
877
|
self.initial = config.initial
|
|
@@ -869,28 +883,39 @@ export const make: Make = (<
|
|
|
869
883
|
return self
|
|
870
884
|
}) as Make
|
|
871
885
|
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
:
|
|
886
|
+
const flattenEventProtocolInputs = <Kind extends Machine.EventProtocolKind>(
|
|
887
|
+
kind: Kind,
|
|
888
|
+
inputs: ReadonlyArray<Machine.EventProtocolInput<Kind>>
|
|
889
|
+
): ReadonlyArray<Machine.TaggedSchema> =>
|
|
890
|
+
inputs.flatMap((input) =>
|
|
891
|
+
Protocol.isEventProtocol(input, kind)
|
|
892
|
+
? Protocol.eventProtocolSchemas(input)
|
|
893
|
+
: [input as Machine.TaggedSchema]
|
|
894
|
+
)
|
|
875
895
|
|
|
876
|
-
export const
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
): EventSchema["Type"] => Protocol.makeEvent(machine, schema, args.length === 0 ? {} : args[0])
|
|
896
|
+
export const events = <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"public">>>(
|
|
897
|
+
...inputs: Inputs
|
|
898
|
+
): Machine.EventProtocol<"public", Machine.EventProtocolInputSchemasOf<"public", Inputs>> =>
|
|
899
|
+
Protocol.makeEventProtocol(
|
|
900
|
+
"public",
|
|
901
|
+
flattenEventProtocolInputs("public", inputs)
|
|
902
|
+
) as Machine.EventProtocol<"public", Machine.EventProtocolInputSchemasOf<"public", Inputs>>
|
|
884
903
|
|
|
885
|
-
export const
|
|
886
|
-
|
|
887
|
-
): Machine.
|
|
888
|
-
Protocol.
|
|
904
|
+
export const internalEvents = <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"internal">>>(
|
|
905
|
+
...inputs: Inputs
|
|
906
|
+
): Machine.EventProtocol<"internal", Machine.EventProtocolInputSchemasOf<"internal", Inputs>> =>
|
|
907
|
+
Protocol.makeEventProtocol(
|
|
908
|
+
"internal",
|
|
909
|
+
flattenEventProtocolInputs("internal", inputs)
|
|
910
|
+
) as Machine.EventProtocol<"internal", Machine.EventProtocolInputSchemasOf<"internal", Inputs>>
|
|
889
911
|
|
|
890
|
-
export const
|
|
891
|
-
|
|
892
|
-
): Machine.
|
|
893
|
-
Protocol.
|
|
912
|
+
export const emittedEvents = <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"emitted">>>(
|
|
913
|
+
...inputs: Inputs
|
|
914
|
+
): Machine.EventProtocol<"emitted", Machine.EventProtocolInputSchemasOf<"emitted", Inputs>> =>
|
|
915
|
+
Protocol.makeEventProtocol(
|
|
916
|
+
"emitted",
|
|
917
|
+
flattenEventProtocolInputs("emitted", inputs)
|
|
918
|
+
) as Machine.EventProtocol<"emitted", Machine.EventProtocolInputSchemasOf<"emitted", Inputs>>
|
|
894
919
|
|
|
895
920
|
export const encodeSnapshot: <
|
|
896
921
|
const States extends Machine.StateSchemas,
|
|
@@ -966,14 +991,6 @@ export const decodeSnapshot: <
|
|
|
966
991
|
Machine.SnapshotDecodingServices<States>
|
|
967
992
|
> = Serialization.decodeSnapshot as any
|
|
968
993
|
|
|
969
|
-
export const retag = (
|
|
970
|
-
target: Machine.TaggedSchema,
|
|
971
|
-
source: { readonly _tag: PropertyKey },
|
|
972
|
-
patch?: unknown
|
|
973
|
-
): any => {
|
|
974
|
-
const { _tag: _, ...fields } = source
|
|
975
|
-
return target.make({ ...fields, ...((patch ?? {}) as object) } as never)
|
|
976
|
-
}
|
|
977
994
|
export const planInitial: <
|
|
978
995
|
const States extends Machine.StateSchemas,
|
|
979
996
|
const Events extends ReadonlyArray<Machine.TaggedSchema>,
|
|
@@ -1383,7 +1400,8 @@ export const start: <
|
|
|
1383
1400
|
| InfiniteTransitionError
|
|
1384
1401
|
| MachineSchemaDecodeError
|
|
1385
1402
|
| StoppedError,
|
|
1386
|
-
Output
|
|
1403
|
+
Output,
|
|
1404
|
+
Machine.EmittedEventOf<Emits>
|
|
1387
1405
|
>,
|
|
1388
1406
|
| InitialE
|
|
1389
1407
|
| E
|
|
@@ -1441,7 +1459,8 @@ export const resume: <
|
|
|
1441
1459
|
| InfiniteTransitionError
|
|
1442
1460
|
| MachineSchemaDecodeError
|
|
1443
1461
|
| StoppedError,
|
|
1444
|
-
Output
|
|
1462
|
+
Output,
|
|
1463
|
+
Machine.EmittedEventOf<Emits>
|
|
1445
1464
|
>,
|
|
1446
1465
|
MachineSchemaDecodeError,
|
|
1447
1466
|
ExcludeCompatibleRuntime<
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import * as Cause from "effect/Cause"
|
|
8
8
|
import * as Effect from "effect/Effect"
|
|
9
9
|
import type * as Schema from "effect/Schema"
|
|
10
|
-
import type { Enqueue, InitialEvent as MachineInitialEvent, Machine } from "../../Machine.js"
|
|
10
|
+
import type { ActorContext, ActorRef, Enqueue, InitialEvent as MachineInitialEvent, Machine } from "../../Machine.js"
|
|
11
11
|
import { getTargetBuilder, makeCollector, type RuntimeCommand } from "./command.js"
|
|
12
12
|
import {
|
|
13
13
|
type ActiveConfiguration,
|
|
@@ -37,7 +37,7 @@ import {
|
|
|
37
37
|
snapshotFromConfigurationAtPath,
|
|
38
38
|
validateInitialConfiguration
|
|
39
39
|
} from "./configuration.js"
|
|
40
|
-
import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError } from "./errors.js"
|
|
40
|
+
import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError, StoppedError } from "./errors.js"
|
|
41
41
|
import * as InvocationEvent from "./invocationEvent.js"
|
|
42
42
|
import { decodeEventSync, decodeInputSync, decodeStateValueSync } from "./protocol.js"
|
|
43
43
|
import { InitialEventTypeId } from "./symbols.js"
|
|
@@ -45,6 +45,7 @@ import {
|
|
|
45
45
|
getNode,
|
|
46
46
|
isChoiceTarget,
|
|
47
47
|
isHistoryTarget,
|
|
48
|
+
isNoTarget,
|
|
48
49
|
isSnapshot,
|
|
49
50
|
isTarget,
|
|
50
51
|
makeChoiceTarget,
|
|
@@ -93,6 +94,38 @@ export type TransitionHandler<States extends Machine.StateSchemas, E, R, Context
|
|
|
93
94
|
enqueue: Enqueue<any, any>
|
|
94
95
|
) => Machine.HandlerResult<States, E, R>
|
|
95
96
|
|
|
97
|
+
const rootActorScopes = new WeakMap<Machine.Any, ActorContext<any, any>>()
|
|
98
|
+
const machineActorScopes = new WeakMap<Machine.Any, ActorContext<any, any>>()
|
|
99
|
+
|
|
100
|
+
export const withActorScope = (
|
|
101
|
+
machine: Machine.Any,
|
|
102
|
+
actorScope: ActorContext<any, any>
|
|
103
|
+
): Machine.Any => {
|
|
104
|
+
const scoped = Object.create(machine) as Machine.Any
|
|
105
|
+
machineActorScopes.set(scoped, { self: actorScope.self, parent: actorScope.parent })
|
|
106
|
+
return scoped
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const getActorContext = (
|
|
110
|
+
machine: Machine.Any,
|
|
111
|
+
configuration: ActiveConfiguration
|
|
112
|
+
): ActorContext<any, any> => {
|
|
113
|
+
const scope = configuration.actorScope
|
|
114
|
+
if (scope !== undefined) return scope
|
|
115
|
+
const machineScope = machineActorScopes.get(machine)
|
|
116
|
+
if (machineScope !== undefined) return machineScope
|
|
117
|
+
const cached = rootActorScopes.get(machine)
|
|
118
|
+
if (cached !== undefined) return cached
|
|
119
|
+
const self: ActorRef<unknown> = {
|
|
120
|
+
id: machine.id ?? "Machine",
|
|
121
|
+
sessionId: "Machine.plan",
|
|
122
|
+
send: () => Effect.fail(new StoppedError())
|
|
123
|
+
}
|
|
124
|
+
const root = { self, parent: undefined }
|
|
125
|
+
rootActorScopes.set(machine, root)
|
|
126
|
+
return root
|
|
127
|
+
}
|
|
128
|
+
|
|
96
129
|
type EventTransition<States extends Machine.StateSchemas, E, R, Context> =
|
|
97
130
|
| TransitionHandler<States, E, R, Context>
|
|
98
131
|
| {
|
|
@@ -155,9 +188,9 @@ const collectTransition = <
|
|
|
155
188
|
context: Context
|
|
156
189
|
) => {
|
|
157
190
|
const collected = makeCollector<Event>(machine)
|
|
158
|
-
const
|
|
191
|
+
const result = transition(context, collected.enqueue)
|
|
159
192
|
return {
|
|
160
|
-
state,
|
|
193
|
+
state: isNoTarget(result) ? undefined : result,
|
|
161
194
|
commands: collected.commands,
|
|
162
195
|
raisedEvents: collected.raisedEvents,
|
|
163
196
|
emittedEvents: collected.emittedEvents
|
|
@@ -206,7 +239,8 @@ const completeHistoryConfiguration = (
|
|
|
206
239
|
active,
|
|
207
240
|
values,
|
|
208
241
|
outputs: configuration.outputs,
|
|
209
|
-
history: configuration.history
|
|
242
|
+
history: configuration.history,
|
|
243
|
+
actorScope: configuration.actorScope
|
|
210
244
|
} as ActiveConfiguration
|
|
211
245
|
active.add(child.path)
|
|
212
246
|
if (child.schema !== undefined) {
|
|
@@ -215,9 +249,10 @@ const completeHistoryConfiguration = (
|
|
|
215
249
|
throw new Error(`Machine shallow history requires an initial value implementation for state "${path}"`)
|
|
216
250
|
}
|
|
217
251
|
const initialized = collectStateInitializer(machine, initializer, {
|
|
252
|
+
...getActorContext(machine, current),
|
|
218
253
|
state: current.values.get(path),
|
|
219
|
-
|
|
220
|
-
|
|
254
|
+
containingState: getParentValue(machine, current, path),
|
|
255
|
+
ancestors: getParentValues(machine, current, path),
|
|
221
256
|
event
|
|
222
257
|
})
|
|
223
258
|
values.set(child.path, decodeStateValueSync(machine, child, initialized.value))
|
|
@@ -235,16 +270,18 @@ const completeHistoryConfiguration = (
|
|
|
235
270
|
active,
|
|
236
271
|
values,
|
|
237
272
|
outputs: configuration.outputs,
|
|
238
|
-
history: configuration.history
|
|
273
|
+
history: configuration.history,
|
|
274
|
+
actorScope: configuration.actorScope
|
|
239
275
|
} as ActiveConfiguration
|
|
240
276
|
const initializer = valuedMissing.length === 0 ? undefined : machine.handlers[path]?.initial
|
|
241
277
|
if (valuedMissing.length > 0 && initializer === undefined) {
|
|
242
278
|
throw new Error(`Machine shallow history requires an initial value implementation for state "${path}"`)
|
|
243
279
|
}
|
|
244
280
|
const initialized = initializer === undefined ? undefined : collectStateInitializer(machine, initializer, {
|
|
281
|
+
...getActorContext(machine, current),
|
|
245
282
|
state: current.values.get(path),
|
|
246
|
-
|
|
247
|
-
|
|
283
|
+
containingState: getParentValue(machine, current, path),
|
|
284
|
+
ancestors: getParentValues(machine, current, path),
|
|
248
285
|
event
|
|
249
286
|
})
|
|
250
287
|
if (initialized !== undefined && (typeof initialized.value !== "object" || initialized.value === null)) {
|
|
@@ -328,7 +365,7 @@ const resolveHistoryTarget = (
|
|
|
328
365
|
const collected = collectTransition(machine, fallback, {
|
|
329
366
|
event,
|
|
330
367
|
target: getTargetBuilder(machine, target.parent).full,
|
|
331
|
-
|
|
368
|
+
owner: target.parent
|
|
332
369
|
})
|
|
333
370
|
if (collected.state === undefined || isHistoryTarget(collected.state) || !isSnapshot(collected.state)) {
|
|
334
371
|
throw new Error(`Machine history default for "${target.path}" must return a complete snapshot containing its owner`)
|
|
@@ -505,9 +542,10 @@ const makeStateActionContext = <
|
|
|
505
542
|
path: string,
|
|
506
543
|
event: Machine.LifecycleEvent<Events>
|
|
507
544
|
): Machine.StateActionContext<States, Events, Emits, StateId> => ({
|
|
545
|
+
...getActorContext(machine, configuration),
|
|
508
546
|
state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
|
|
509
|
-
|
|
510
|
-
|
|
547
|
+
containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
|
|
548
|
+
ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
|
|
511
549
|
event
|
|
512
550
|
})
|
|
513
551
|
|
|
@@ -524,9 +562,10 @@ const makeTransitionContext = <
|
|
|
524
562
|
event: Machine.EventByTag<Events, EventTag>,
|
|
525
563
|
snapshot: Machine.Snapshot<States>
|
|
526
564
|
): Machine.HandlerContext<States, Events, Emits, StateId, EventTag, any, any> => ({
|
|
565
|
+
...getActorContext(machine, configuration),
|
|
527
566
|
state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
|
|
528
|
-
|
|
529
|
-
|
|
567
|
+
containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
|
|
568
|
+
ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
|
|
530
569
|
event,
|
|
531
570
|
snapshot,
|
|
532
571
|
target: getTargetBuilder(machine, path)
|
|
@@ -545,9 +584,10 @@ const makeDoneContext = <
|
|
|
545
584
|
output: unknown,
|
|
546
585
|
snapshot: Machine.Snapshot<States>
|
|
547
586
|
): Machine.DoneContext<States, Events, Emits, StateId> => ({
|
|
587
|
+
...getActorContext(machine, configuration),
|
|
548
588
|
state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
|
|
549
|
-
|
|
550
|
-
|
|
589
|
+
containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
|
|
590
|
+
ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
|
|
551
591
|
event,
|
|
552
592
|
output: output as Machine.CompletionOutputByIdentifier<States, StateId>,
|
|
553
593
|
snapshot,
|
|
@@ -639,15 +679,16 @@ const selectAlwaysTransitions = <
|
|
|
639
679
|
Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>
|
|
640
680
|
>,
|
|
641
681
|
context: {
|
|
682
|
+
...getActorContext(machine, configuration),
|
|
642
683
|
state: configuration.values.get(path) as Machine.StateByIdentifier<
|
|
643
684
|
States,
|
|
644
685
|
Machine.StateIdentifier<States>
|
|
645
686
|
>,
|
|
646
|
-
|
|
687
|
+
containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<
|
|
647
688
|
States,
|
|
648
689
|
Machine.StateIdentifier<States>
|
|
649
690
|
>,
|
|
650
|
-
|
|
691
|
+
ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<
|
|
651
692
|
States,
|
|
652
693
|
Machine.StateIdentifier<States>
|
|
653
694
|
>,
|
|
@@ -826,9 +867,10 @@ const selectInvocationTransition = <
|
|
|
826
867
|
if (transition === undefined) return []
|
|
827
868
|
const snapshot = snapshotFromConfiguration<States>(machine, configuration)
|
|
828
869
|
const context = {
|
|
870
|
+
...getActorContext(machine, configuration),
|
|
829
871
|
state: configuration.values.get(event.path),
|
|
830
|
-
|
|
831
|
-
|
|
872
|
+
containingState: getParentValue(machine, configuration, event.path),
|
|
873
|
+
ancestors: getParentValues(machine, configuration, event.path),
|
|
832
874
|
snapshot,
|
|
833
875
|
target: getTargetBuilder(machine, event.path),
|
|
834
876
|
id: event.id,
|
|
@@ -1064,11 +1106,13 @@ const resolveChoiceTarget = (
|
|
|
1064
1106
|
...Object.entries(extracted.values)
|
|
1065
1107
|
]),
|
|
1066
1108
|
outputs: configuration.outputs,
|
|
1067
|
-
history: configuration.history
|
|
1109
|
+
history: configuration.history,
|
|
1110
|
+
...(configuration.actorScope === undefined ? {} : { actorScope: configuration.actorScope })
|
|
1068
1111
|
}
|
|
1069
1112
|
const collected = collectTransition(machine, choice.transition, {
|
|
1070
|
-
|
|
1071
|
-
|
|
1113
|
+
...getActorContext(machine, provisional),
|
|
1114
|
+
containingState: getParentValue(machine, provisional, node.path),
|
|
1115
|
+
ancestors: getParentValues(machine, provisional, node.path),
|
|
1072
1116
|
event,
|
|
1073
1117
|
target: getTargetBuilder(machine, node.path)
|
|
1074
1118
|
})
|
|
@@ -131,7 +131,8 @@ const makeChildlessCompiledDrain = (
|
|
|
131
131
|
planned = executionPlan.plan(
|
|
132
132
|
configuration ?? executionPlan.fromConfiguration(Configuration.normalizeConfigurationSync(machine, current)),
|
|
133
133
|
event,
|
|
134
|
-
acknowledged
|
|
134
|
+
acknowledged,
|
|
135
|
+
context.scope
|
|
135
136
|
)
|
|
136
137
|
} catch (error) {
|
|
137
138
|
return error instanceof InfiniteTransitionError || error instanceof MachineSchemaDecodeError
|
|
@@ -255,7 +256,8 @@ const makeInvokingCompiledDrain = (
|
|
|
255
256
|
configuration ??
|
|
256
257
|
executionPlan.fromConfiguration(Configuration.normalizeConfigurationSync(machine, current)),
|
|
257
258
|
event,
|
|
258
|
-
acknowledged
|
|
259
|
+
acknowledged,
|
|
260
|
+
scope
|
|
259
261
|
)
|
|
260
262
|
} catch (error) {
|
|
261
263
|
return error instanceof InfiniteTransitionError || error instanceof MachineSchemaDecodeError
|
|
@@ -426,9 +428,11 @@ const makeProcessLogic: <
|
|
|
426
428
|
const executionPlan = ExecutionPlan.compileExecutionPlan(machine)
|
|
427
429
|
const initialArgs = entry._tag === "Initial" ? entry.args : []
|
|
428
430
|
const compiledInitial = entry._tag === "Initial" ? executionPlan.initial : undefined
|
|
429
|
-
const makeCompiledInitial = compiledInitial === undefined ? undefined : (
|
|
431
|
+
const makeCompiledInitial = compiledInitial === undefined ? undefined : (
|
|
432
|
+
scope: internalRuntime.ProcessScope<Machine.EventOf<Events>>
|
|
433
|
+
) => {
|
|
430
434
|
try {
|
|
431
|
-
const planned = compiledInitial(initialArgs)
|
|
435
|
+
const planned = compiledInitial(initialArgs, scope)
|
|
432
436
|
const result = {
|
|
433
437
|
state: planned.state as Machine.Snapshot<States>,
|
|
434
438
|
done: planned.done,
|
|
@@ -455,7 +459,7 @@ const makeProcessLogic: <
|
|
|
455
459
|
) =>
|
|
456
460
|
compiledInitial === undefined
|
|
457
461
|
? internalRuntime.provideMachineRuntime(
|
|
458
|
-
internalPlanner.planInitial(machine, ...initialArgs).pipe(
|
|
462
|
+
internalPlanner.planInitial(internalPlanner.withActorScope(machine, scope), ...initialArgs).pipe(
|
|
459
463
|
Effect.flatMap((planned) => {
|
|
460
464
|
const commands = planned.commands.length === 0
|
|
461
465
|
? undefined
|
|
@@ -480,7 +484,7 @@ const makeProcessLogic: <
|
|
|
480
484
|
),
|
|
481
485
|
scope
|
|
482
486
|
)
|
|
483
|
-
: Effect.try({ try: makeCompiledInitial
|
|
487
|
+
: Effect.try({ try: () => makeCompiledInitial!(scope), catch: (error) => error as any })
|
|
484
488
|
return ({
|
|
485
489
|
execution: {
|
|
486
490
|
_tag: "Compiled",
|
|
@@ -536,7 +540,10 @@ const makeProcessLogic: <
|
|
|
536
540
|
try {
|
|
537
541
|
planned = internalPlanner.planConfiguration(
|
|
538
542
|
machine,
|
|
539
|
-
|
|
543
|
+
Configuration.withActorScope(
|
|
544
|
+
configuration ?? Configuration.normalizeConfigurationSync(machine, current),
|
|
545
|
+
context
|
|
546
|
+
),
|
|
540
547
|
event
|
|
541
548
|
)
|
|
542
549
|
} catch (error) {
|
|
@@ -644,7 +651,10 @@ const makeProcessLogic: <
|
|
|
644
651
|
try {
|
|
645
652
|
planned = internalPlanner.planConfiguration(
|
|
646
653
|
machine,
|
|
647
|
-
|
|
654
|
+
Configuration.withActorScope(
|
|
655
|
+
configuration ?? Configuration.normalizeConfigurationSync(machine, current),
|
|
656
|
+
context
|
|
657
|
+
),
|
|
648
658
|
event
|
|
649
659
|
)
|
|
650
660
|
} catch (error) {
|