@typeonce/effect-machine 0.47.1 → 0.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +374 -22
- package/CLAUDE.md +374 -22
- package/README.md +56 -7
- package/ai-docs/src/10_modeling/01_create-a-machine.ts +3 -3
- package/ai-docs/src/10_modeling/index.md +64 -17
- package/ai-docs/src/20_root-api/index.md +2 -2
- package/ai-docs/src/35_cluster/index.md +213 -0
- package/ai-docs/src/37_new-host/index.md +90 -0
- package/dist/LocalMachine.d.ts +417 -0
- package/dist/LocalMachine.d.ts.map +1 -0
- package/dist/LocalMachine.js +193 -0
- package/dist/LocalMachine.js.map +1 -0
- package/dist/Machine.d.ts +1120 -812
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +195 -186
- package/dist/Machine.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/machine/atom.d.ts +8 -10
- package/dist/internal/machine/atom.d.ts.map +1 -1
- package/dist/internal/machine/atom.js +7 -6
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/capability.d.ts +101 -0
- package/dist/internal/machine/capability.d.ts.map +1 -0
- package/dist/internal/machine/capability.js +70 -0
- package/dist/internal/machine/capability.js.map +1 -0
- package/dist/internal/machine/{invocationDefinition.d.ts → capturedInvocation.d.ts} +11 -11
- package/dist/internal/machine/capturedInvocation.d.ts.map +1 -0
- package/dist/internal/machine/{invocationDefinition.js → capturedInvocation.js} +2 -2
- package/dist/internal/machine/capturedInvocation.js.map +1 -0
- package/dist/internal/machine/child.d.ts +14 -0
- package/dist/internal/machine/child.d.ts.map +1 -0
- package/dist/internal/machine/child.js +19 -0
- package/dist/internal/machine/child.js.map +1 -0
- package/dist/internal/machine/childRegistry.d.ts +0 -2
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/childRegistry.js +2 -2
- package/dist/internal/machine/childRegistry.js.map +1 -1
- package/dist/internal/machine/cluster.d.ts +19 -4
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +263 -110
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterChild.d.ts +66 -0
- package/dist/internal/machine/clusterChild.d.ts.map +1 -0
- package/dist/internal/machine/clusterChild.js +105 -0
- package/dist/internal/machine/clusterChild.js.map +1 -0
- package/dist/internal/machine/clusterFailure.d.ts +14 -0
- package/dist/internal/machine/clusterFailure.d.ts.map +1 -0
- package/dist/internal/machine/clusterFailure.js +44 -0
- package/dist/internal/machine/clusterFailure.js.map +1 -0
- package/dist/internal/machine/clusterInstance.d.ts +194 -0
- package/dist/internal/machine/clusterInstance.d.ts.map +1 -0
- package/dist/internal/machine/clusterInstance.js +1076 -0
- package/dist/internal/machine/clusterInstance.js.map +1 -0
- package/dist/internal/machine/clusterKeepAlive.d.ts +47 -0
- package/dist/internal/machine/clusterKeepAlive.d.ts.map +1 -0
- package/dist/internal/machine/clusterKeepAlive.js +72 -0
- package/dist/internal/machine/clusterKeepAlive.js.map +1 -0
- package/dist/internal/machine/clusterProgress.d.ts +73 -0
- package/dist/internal/machine/clusterProgress.d.ts.map +1 -0
- package/dist/internal/machine/clusterProgress.js +116 -0
- package/dist/internal/machine/clusterProgress.js.map +1 -0
- package/dist/internal/machine/clusterProtocol.d.ts +37 -4
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -1
- package/dist/internal/machine/clusterProtocol.js +18 -4
- package/dist/internal/machine/clusterProtocol.js.map +1 -1
- package/dist/internal/machine/clusterProxy.d.ts +46 -0
- package/dist/internal/machine/clusterProxy.d.ts.map +1 -0
- package/dist/internal/machine/clusterProxy.js +150 -0
- package/dist/internal/machine/clusterProxy.js.map +1 -0
- package/dist/internal/machine/clusterRead.d.ts +61 -0
- package/dist/internal/machine/clusterRead.d.ts.map +1 -0
- package/dist/internal/machine/clusterRead.js +108 -0
- package/dist/internal/machine/clusterRead.js.map +1 -0
- package/dist/internal/machine/clusterSqlStorage.d.ts +16 -0
- package/dist/internal/machine/clusterSqlStorage.d.ts.map +1 -0
- package/dist/internal/machine/clusterSqlStorage.js +138 -0
- package/dist/internal/machine/clusterSqlStorage.js.map +1 -0
- package/dist/internal/machine/clusterStorageCheck.d.ts +19 -0
- package/dist/internal/machine/clusterStorageCheck.d.ts.map +1 -0
- package/dist/internal/machine/clusterStorageCheck.js +186 -0
- package/dist/internal/machine/clusterStorageCheck.js.map +1 -0
- package/dist/internal/machine/clusterTarget.d.ts +74 -0
- package/dist/internal/machine/clusterTarget.d.ts.map +1 -0
- package/dist/internal/machine/clusterTarget.js +81 -0
- package/dist/internal/machine/clusterTarget.js.map +1 -0
- package/dist/internal/machine/clusterTimer.d.ts +21 -0
- package/dist/internal/machine/clusterTimer.d.ts.map +1 -0
- package/dist/internal/machine/clusterTimer.js +50 -0
- package/dist/internal/machine/clusterTimer.js.map +1 -0
- package/dist/internal/machine/clusterWake.d.ts +61 -0
- package/dist/internal/machine/clusterWake.d.ts.map +1 -0
- package/dist/internal/machine/clusterWake.js +81 -0
- package/dist/internal/machine/clusterWake.js.map +1 -0
- package/dist/internal/machine/clusterWork.d.ts +130 -0
- package/dist/internal/machine/clusterWork.d.ts.map +1 -0
- package/dist/internal/machine/clusterWork.js +191 -0
- package/dist/internal/machine/clusterWork.js.map +1 -0
- package/dist/internal/machine/command.d.ts +4 -4
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.d.ts +8 -6
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.js +1 -5
- package/dist/internal/machine/commandRuntime.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +0 -13
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +0 -151
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/errors.d.ts +11 -2
- package/dist/internal/machine/errors.d.ts.map +1 -1
- package/dist/internal/machine/errors.js +11 -2
- package/dist/internal/machine/errors.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts +45 -3
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +328 -191
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +6 -6
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/inspectionRuntime.d.ts +1 -1
- package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -1
- package/dist/internal/machine/invocationChanges.d.ts +60 -0
- package/dist/internal/machine/invocationChanges.d.ts.map +1 -0
- package/dist/internal/machine/invocationChanges.js +54 -0
- package/dist/internal/machine/invocationChanges.js.map +1 -0
- package/dist/internal/machine/{activities.d.ts → invocationDefinitions.d.ts} +8 -8
- package/dist/internal/machine/invocationDefinitions.d.ts.map +1 -0
- package/dist/internal/machine/{activities.js → invocationDefinitions.js} +5 -5
- package/dist/internal/machine/invocationDefinitions.js.map +1 -0
- package/dist/internal/machine/invocationEvent.d.ts +16 -7
- package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
- package/dist/internal/machine/invocationEvent.js +26 -12
- package/dist/internal/machine/invocationEvent.js.map +1 -1
- package/dist/internal/machine/invocationOutcome.d.ts +89 -0
- package/dist/internal/machine/invocationOutcome.d.ts.map +1 -0
- package/dist/internal/machine/invocationOutcome.js +76 -0
- package/dist/internal/machine/invocationOutcome.js.map +1 -0
- package/dist/internal/machine/invocationResolution.d.ts +137 -0
- package/dist/internal/machine/invocationResolution.d.ts.map +1 -0
- package/dist/internal/machine/invocationResolution.js +150 -0
- package/dist/internal/machine/invocationResolution.js.map +1 -0
- package/dist/internal/machine/logicScope.d.ts +24 -0
- package/dist/internal/machine/logicScope.d.ts.map +1 -0
- package/dist/internal/machine/logicScope.js +33 -0
- package/dist/internal/machine/logicScope.js.map +1 -0
- package/dist/internal/machine/machine.d.ts +17 -23
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +24 -40
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/observation.d.ts +3 -0
- package/dist/internal/machine/observation.d.ts.map +1 -1
- package/dist/internal/machine/observation.js +56 -2
- package/dist/internal/machine/observation.js.map +1 -1
- package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -1
- package/dist/internal/machine/plannedSnapshot.js +11 -20
- package/dist/internal/machine/plannedSnapshot.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +36 -23
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +15 -8
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +17 -9
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +43 -503
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js +48 -10
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +20 -16
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +93 -88
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -1
- package/dist/internal/machine/runtimeCompiled.js +359 -120
- package/dist/internal/machine/runtimeCompiled.js.map +1 -1
- package/dist/internal/machine/runtimeProtocol.d.ts +151 -82
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
- package/dist/internal/machine/runtimeProtocol.js +236 -219
- package/dist/internal/machine/runtimeProtocol.js.map +1 -1
- package/dist/internal/machine/stateDefinition.d.ts +0 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +0 -1
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/step.d.ts +486 -0
- package/dist/internal/machine/step.d.ts.map +1 -0
- package/dist/internal/machine/step.js +957 -0
- package/dist/internal/machine/step.js.map +1 -0
- package/dist/internal/machine/stepHost.d.ts +33 -0
- package/dist/internal/machine/stepHost.d.ts.map +1 -0
- package/dist/internal/machine/stepHost.js +501 -0
- package/dist/internal/machine/stepHost.js.map +1 -0
- package/dist/internal/machine/symbols.d.ts +2 -11
- package/dist/internal/machine/symbols.d.ts.map +1 -1
- package/dist/internal/machine/symbols.js +2 -2
- package/dist/internal/machine/symbols.js.map +1 -1
- package/dist/internal/machine/transition.d.ts +7 -7
- package/dist/internal/machine/transition.d.ts.map +1 -1
- package/dist/internal/testing/machine/check.d.ts.map +1 -1
- package/dist/internal/testing/machine/check.js +217 -25
- package/dist/internal/testing/machine/check.js.map +1 -1
- package/dist/internal/testing/machine/exploration.d.ts +19 -2
- package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
- package/dist/internal/testing/machine/exploration.js +14 -3
- package/dist/internal/testing/machine/exploration.js.map +1 -1
- package/dist/internal/testing/machine/format.js.map +1 -1
- package/dist/internal/testing/machine/probe.d.ts.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.js.map +1 -1
- package/dist/internal/testing/machine/runtime.d.ts +2 -2
- package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
- package/dist/internal/testing/machine/runtime.js +10 -3
- package/dist/internal/testing/machine/runtime.js.map +1 -1
- package/dist/internal/testing/machine/trace.d.ts +38 -3
- package/dist/internal/testing/machine/trace.d.ts.map +1 -1
- package/dist/internal/testing/machine/trace.js +180 -33
- package/dist/internal/testing/machine/trace.js.map +1 -1
- package/dist/internal/testing/machine/transitionCoverage.d.ts +1 -1
- package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +9 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/internal/testing/machine/work.d.ts +47 -0
- package/dist/internal/testing/machine/work.d.ts.map +1 -0
- package/dist/internal/testing/machine/work.js +74 -0
- package/dist/internal/testing/machine/work.js.map +1 -0
- package/dist/testing/MachineTest.d.ts +253 -37
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +81 -9
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +1071 -41
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +441 -12
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachineProxy.d.ts +257 -0
- package/dist/unstable/cluster/ClusterMachineProxy.d.ts.map +1 -0
- package/dist/unstable/cluster/ClusterMachineProxy.js +73 -0
- package/dist/unstable/cluster/ClusterMachineProxy.js.map +1 -0
- package/dist/unstable/cluster/SqlMachineStorage.d.ts +125 -0
- package/dist/unstable/cluster/SqlMachineStorage.d.ts.map +1 -0
- package/dist/unstable/cluster/SqlMachineStorage.js +62 -0
- package/dist/unstable/cluster/SqlMachineStorage.js.map +1 -0
- package/dist/unstable/cluster/index.d.ts +13 -0
- package/dist/unstable/cluster/index.d.ts.map +1 -1
- package/dist/unstable/cluster/index.js +13 -0
- package/dist/unstable/cluster/index.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +21 -26
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/package.json +5 -3
- package/src/LocalMachine.ts +611 -0
- package/src/Machine.ts +1379 -1334
- package/src/index.ts +1 -0
- package/src/internal/machine/atom.ts +20 -49
- package/src/internal/machine/capability.ts +123 -0
- package/src/internal/machine/{invocationDefinition.ts → capturedInvocation.ts} +13 -13
- package/src/internal/machine/child.ts +25 -0
- package/src/internal/machine/childRegistry.ts +1 -4
- package/src/internal/machine/cluster.ts +420 -205
- package/src/internal/machine/clusterChild.ts +117 -0
- package/src/internal/machine/clusterFailure.ts +53 -0
- package/src/internal/machine/clusterInstance.ts +1779 -0
- package/src/internal/machine/clusterKeepAlive.ts +93 -0
- package/src/internal/machine/clusterProgress.ts +159 -0
- package/src/internal/machine/clusterProtocol.ts +46 -4
- package/src/internal/machine/clusterProxy.ts +233 -0
- package/src/internal/machine/clusterRead.ts +207 -0
- package/src/internal/machine/clusterSqlStorage.ts +181 -0
- package/src/internal/machine/clusterStorageCheck.ts +274 -0
- package/src/internal/machine/clusterTarget.ts +154 -0
- package/src/internal/machine/clusterTimer.ts +58 -0
- package/src/internal/machine/clusterWake.ts +100 -0
- package/src/internal/machine/clusterWork.ts +269 -0
- package/src/internal/machine/command.ts +4 -4
- package/src/internal/machine/commandRuntime.ts +12 -24
- package/src/internal/machine/configuration.ts +0 -181
- package/src/internal/machine/errors.ts +11 -2
- package/src/internal/machine/executionPlan.ts +493 -263
- package/src/internal/machine/implementation.ts +6 -6
- package/src/internal/machine/inspectionRuntime.ts +1 -1
- package/src/internal/machine/invocationChanges.ts +89 -0
- package/src/internal/machine/{activities.ts → invocationDefinitions.ts} +10 -10
- package/src/internal/machine/invocationEvent.ts +35 -12
- package/src/internal/machine/invocationOutcome.ts +145 -0
- package/src/internal/machine/invocationResolution.ts +307 -0
- package/src/internal/machine/logicScope.ts +47 -0
- package/src/internal/machine/machine.ts +35 -100
- package/src/internal/machine/observation.ts +67 -2
- package/src/internal/machine/plannedSnapshot.ts +17 -20
- package/src/internal/machine/planner.ts +75 -99
- package/src/internal/machine/process.ts +78 -722
- package/src/internal/machine/protocol.ts +70 -26
- package/src/internal/machine/runtime.ts +198 -195
- package/src/internal/machine/runtimeCompiled.ts +496 -171
- package/src/internal/machine/runtimeProtocol.ts +416 -305
- package/src/internal/machine/stateDefinition.ts +0 -5
- package/src/internal/machine/step.ts +1518 -0
- package/src/internal/machine/stepHost.ts +634 -0
- package/src/internal/machine/symbols.ts +3 -14
- package/src/internal/machine/transition.ts +11 -11
- package/src/internal/testing/machine/check.ts +240 -36
- package/src/internal/testing/machine/exploration.ts +42 -3
- package/src/internal/testing/machine/format.ts +1 -1
- package/src/internal/testing/machine/probe.ts +2 -1
- package/src/internal/testing/machine/runtime.ts +15 -5
- package/src/internal/testing/machine/trace.ts +261 -41
- package/src/internal/testing/machine/transitionCoverage.ts +1 -1
- package/src/internal/testing/machine/verification.ts +11 -5
- package/src/internal/testing/machine/work.ts +101 -0
- package/src/testing/MachineTest.ts +268 -44
- package/src/unstable/cluster/ClusterMachine.ts +1183 -59
- package/src/unstable/cluster/ClusterMachineProxy.ts +372 -0
- package/src/unstable/cluster/SqlMachineStorage.ts +136 -0
- package/src/unstable/cluster/index.ts +15 -0
- package/src/unstable/reactivity/AtomMachine.ts +27 -88
- package/dist/internal/machine/activities.d.ts.map +0 -1
- package/dist/internal/machine/activities.js.map +0 -1
- package/dist/internal/machine/invocation.d.ts +0 -33
- package/dist/internal/machine/invocation.d.ts.map +0 -1
- package/dist/internal/machine/invocation.js +0 -220
- package/dist/internal/machine/invocation.js.map +0 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +0 -1
- package/dist/internal/machine/invocationDefinition.js.map +0 -1
- package/dist/internal/machine/requirements.d.ts +0 -4
- package/dist/internal/machine/requirements.d.ts.map +0 -1
- package/dist/internal/machine/requirements.js +0 -2
- package/dist/internal/machine/requirements.js.map +0 -1
- package/dist/internal/machine/runtimeGeneric.d.ts +0 -3
- package/dist/internal/machine/runtimeGeneric.d.ts.map +0 -1
- package/dist/internal/machine/runtimeGeneric.js +0 -465
- package/dist/internal/machine/runtimeGeneric.js.map +0 -1
- package/src/internal/machine/invocation.ts +0 -376
- package/src/internal/machine/requirements.ts +0 -11
- package/src/internal/machine/runtimeGeneric.ts +0 -797
|
@@ -10,13 +10,15 @@ import * as PubSub from "effect/PubSub"
|
|
|
10
10
|
import * as Scope from "effect/Scope"
|
|
11
11
|
import * as Stream from "effect/Stream"
|
|
12
12
|
import type * as Take from "effect/Take"
|
|
13
|
+
import type { Inspection, Prepared as PublicPrepared } from "../../LocalMachine.js"
|
|
13
14
|
import type {
|
|
14
15
|
MachineRef as PublicMachineRef,
|
|
15
|
-
Prepared as PublicPrepared,
|
|
16
16
|
RuntimeOutcome as PublicRuntimeOutcome,
|
|
17
17
|
RuntimeSnapshot as PublicRuntimeSnapshot
|
|
18
18
|
} from "../../Machine.js"
|
|
19
|
-
import type { ChildMachine,
|
|
19
|
+
import type { ChildMachine, Machine as MachineDefinition, MachineTarget } from "../../Machine.js"
|
|
20
|
+
import { ChildMachineRuntime, MachineRuntime } from "./capability.js"
|
|
21
|
+
import { isChildMachine } from "./child.js"
|
|
20
22
|
import {
|
|
21
23
|
type ChildDescriptor,
|
|
22
24
|
type ChildEntry,
|
|
@@ -32,10 +34,8 @@ import {
|
|
|
32
34
|
takeChildObservations,
|
|
33
35
|
unregisterChild
|
|
34
36
|
} from "./childRegistry.js"
|
|
35
|
-
import { ChildAlreadyExistsError, StoppedError } from "./errors.js"
|
|
37
|
+
import { ChildAlreadyExistsError, MachineSchemaDecodeError, StoppedError } from "./errors.js"
|
|
36
38
|
import * as InspectionRuntime from "./inspectionRuntime.js"
|
|
37
|
-
import type * as InvocationEvent from "./invocationEvent.js"
|
|
38
|
-
import { type ChildMachineLogicSlot, ChildMachineLogicTypeId } from "./symbols.js"
|
|
39
39
|
|
|
40
40
|
/** @internal */
|
|
41
41
|
export const activeSnapshotObserver: unique symbol = Symbol.for("effect/Machine/activeSnapshotObserver")
|
|
@@ -63,8 +63,105 @@ export interface SnapshotObservable {
|
|
|
63
63
|
export const isSnapshotObservable = (value: unknown): value is SnapshotObservable =>
|
|
64
64
|
typeof value === "object" && value !== null && snapshotObserver in value
|
|
65
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Delivery for transition commands and internal work reports inside one
|
|
68
|
+
* runtime. A caller-awaited `send` or `sendTo` rejects an event that the
|
|
69
|
+
* schema of the machine refuses; a delivery has no waiting caller, so the
|
|
70
|
+
* receiving machine fails with that error when the event is planned.
|
|
71
|
+
*
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
export const deliverEvent: unique symbol = Symbol.for("effect/Machine/deliverEvent")
|
|
75
|
+
|
|
76
|
+
/** @internal */
|
|
77
|
+
export const deliverFromProcess: unique symbol = Symbol.for("effect/Machine/deliverFromProcess")
|
|
78
|
+
|
|
66
79
|
/** @internal */
|
|
67
|
-
|
|
80
|
+
export interface Deliverable {
|
|
81
|
+
readonly [deliverEvent]: (
|
|
82
|
+
event: unknown,
|
|
83
|
+
source: Inspection.Subject | undefined,
|
|
84
|
+
causedBy: Inspection.Causation | undefined
|
|
85
|
+
) => Effect.Effect<void, StoppedError>
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The event of a delivery that the schema of the machine refused, or whose
|
|
90
|
+
* decoding threw. Planning it fails the machine with `error`.
|
|
91
|
+
*
|
|
92
|
+
* @internal
|
|
93
|
+
*/
|
|
94
|
+
export class RefusedEvent {
|
|
95
|
+
constructor(readonly error: unknown) {}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* What running work reports to the machine that owns it, as a message of the
|
|
100
|
+
* machine's mailbox, so its step takes the report in the machine's own turn:
|
|
101
|
+
* `Settle` ends the work with the `Exit` in `value`, `Element` delivers an
|
|
102
|
+
* element of a stream, and `Report` a snapshot of a child. It is never decoded.
|
|
103
|
+
* `sent` is the event that inspection reports for it.
|
|
104
|
+
*
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
107
|
+
export class WorkMessage {
|
|
108
|
+
constructor(
|
|
109
|
+
readonly _tag: "Settle" | "Element" | "Report",
|
|
110
|
+
readonly work: unknown,
|
|
111
|
+
readonly value: unknown,
|
|
112
|
+
readonly sent: unknown
|
|
113
|
+
) {}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The event that inspection reports for a message: what a report of work stands for, or the event. */
|
|
117
|
+
export const sentOf = (event: unknown): unknown => event instanceof WorkMessage ? event.sent : event
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Decodes an event that enters a mailbox. An invocation event is the host's
|
|
121
|
+
* own and is not decoded.
|
|
122
|
+
*
|
|
123
|
+
* @internal
|
|
124
|
+
*/
|
|
125
|
+
export type EventDecoder = (event: unknown) => unknown
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Decodes an event that a public `send` offers: a refused event is rejected
|
|
129
|
+
* with its `MachineSchemaDecodeError`, and any other throw is a defect of the
|
|
130
|
+
* machine, which fails when it plans the returned `RefusedEvent`.
|
|
131
|
+
*
|
|
132
|
+
* @internal
|
|
133
|
+
*/
|
|
134
|
+
export const decodeSent = (
|
|
135
|
+
decode: EventDecoder | undefined,
|
|
136
|
+
event: unknown
|
|
137
|
+
): { readonly _tag: "Decoded"; readonly event: unknown } | {
|
|
138
|
+
readonly _tag: "Rejected"
|
|
139
|
+
readonly error: MachineSchemaDecodeError
|
|
140
|
+
} => {
|
|
141
|
+
if (decode === undefined) return { _tag: "Decoded", event }
|
|
142
|
+
try {
|
|
143
|
+
return { _tag: "Decoded", event: decode(event) }
|
|
144
|
+
} catch (error) {
|
|
145
|
+
return error instanceof MachineSchemaDecodeError
|
|
146
|
+
? { _tag: "Rejected", error }
|
|
147
|
+
: { _tag: "Decoded", event: new RefusedEvent(error) }
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Decodes an event that a delivery offers. A refused event becomes a
|
|
153
|
+
* `RefusedEvent`, so the machine fails when it plans it.
|
|
154
|
+
*
|
|
155
|
+
* @internal
|
|
156
|
+
*/
|
|
157
|
+
export const decodeDelivered = (decode: EventDecoder | undefined, event: unknown): unknown => {
|
|
158
|
+
if (decode === undefined || event instanceof WorkMessage) return event
|
|
159
|
+
try {
|
|
160
|
+
return decode(event)
|
|
161
|
+
} catch (error) {
|
|
162
|
+
return new RefusedEvent(error)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
68
165
|
|
|
69
166
|
/** @internal */
|
|
70
167
|
export const acknowledgedSend: unique symbol = Symbol.for("effect/Machine/acknowledgedSend")
|
|
@@ -79,7 +176,7 @@ export interface AcknowledgedDelivery<State> {
|
|
|
79
176
|
export const AcknowledgedMessageTypeId: unique symbol = Symbol("effect/Machine/AcknowledgedMessage")
|
|
80
177
|
|
|
81
178
|
/** @internal */
|
|
82
|
-
interface AcknowledgedMessage<Event> {
|
|
179
|
+
export interface AcknowledgedMessage<Event> {
|
|
83
180
|
readonly [AcknowledgedMessageTypeId]: true
|
|
84
181
|
readonly event: Event
|
|
85
182
|
readonly deferred?: Deferred.Deferred<AcknowledgedDelivery<unknown>, unknown>
|
|
@@ -166,10 +263,12 @@ export interface MachineRef<out State, in Event, out Error = never, out Output =
|
|
|
166
263
|
/** @internal */
|
|
167
264
|
readonly inspectionSubject?: Inspection.Subject
|
|
168
265
|
/** @internal */
|
|
169
|
-
readonly sendInspected?:
|
|
266
|
+
readonly sendInspected?: ProcessEndpoint<Event>["sendInspected"]
|
|
267
|
+
/** @internal */
|
|
268
|
+
readonly [deliverEvent]?: Deliverable[typeof deliverEvent]
|
|
170
269
|
readonly [acknowledgedSend]?: (
|
|
171
270
|
event: Event
|
|
172
|
-
) => Effect.Effect<AcknowledgedDelivery<State>, Error | StoppedError>
|
|
271
|
+
) => Effect.Effect<AcknowledgedDelivery<State>, Error | StoppedError | MachineSchemaDecodeError>
|
|
173
272
|
readonly child: (child: any) => Effect.Effect<Option.Option<any>>
|
|
174
273
|
readonly childChanges: (child: any) => Stream.Stream<Option.Option<any>>
|
|
175
274
|
}
|
|
@@ -186,44 +285,114 @@ export interface PreparedProcess<
|
|
|
186
285
|
readonly start: Effect.Effect<MachineRef<State, Event, Error, Output, Emitted>, StartError, StartRequirements>
|
|
187
286
|
}
|
|
188
287
|
|
|
189
|
-
export interface
|
|
288
|
+
export interface ProcessEndpoint<in Event> {
|
|
190
289
|
readonly id: string
|
|
191
290
|
readonly sessionId: string
|
|
192
|
-
readonly inspectionSubject?: Inspection.Subject
|
|
291
|
+
readonly inspectionSubject?: Inspection.Subject | undefined
|
|
193
292
|
readonly stop: Effect.Effect<void>
|
|
194
|
-
readonly send: (event: Event) => Effect.Effect<void, StoppedError>
|
|
293
|
+
readonly send: (event: Event) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
|
|
195
294
|
readonly sendInspected?: (
|
|
196
295
|
event: Event,
|
|
197
296
|
source: Inspection.Subject | undefined,
|
|
198
297
|
causedBy: Inspection.Causation | undefined
|
|
199
|
-
) => Effect.Effect<void, StoppedError>
|
|
200
|
-
readonly [
|
|
201
|
-
|
|
202
|
-
|
|
298
|
+
) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
|
|
299
|
+
readonly [deliverEvent]?: Deliverable[typeof deliverEvent]
|
|
300
|
+
readonly [deliverFromProcess]?: (
|
|
301
|
+
target: MachineTarget<unknown> | ChildSelector,
|
|
302
|
+
event: unknown
|
|
303
|
+
) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
|
|
304
|
+
readonly [acknowledgedSend]?:
|
|
305
|
+
| ((event: Event) => Effect.Effect<AcknowledgedDelivery<unknown>, unknown | StoppedError>)
|
|
306
|
+
| undefined
|
|
203
307
|
}
|
|
204
308
|
|
|
205
309
|
export const isMachineTarget = (value: unknown): value is MachineTarget<unknown> =>
|
|
206
310
|
typeof value === "object" && value !== null && "send" in value && typeof value.send === "function"
|
|
207
311
|
|
|
312
|
+
/** Sends to a target for a waiting caller: a refused event is rejected and the target keeps running. */
|
|
208
313
|
export const sendMachineTarget = (
|
|
209
314
|
target: MachineTarget<unknown>,
|
|
210
315
|
event: unknown,
|
|
211
316
|
source: Inspection.Subject | undefined,
|
|
212
317
|
causedBy: Inspection.Causation | undefined
|
|
213
|
-
): Effect.Effect<void, StoppedError> =>
|
|
318
|
+
): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> =>
|
|
214
319
|
"inspectionSubject" in target && "sendInspected" in target && typeof target.sendInspected === "function"
|
|
215
320
|
? target.sendInspected(event, source, causedBy)
|
|
216
321
|
: target.send(event)
|
|
217
322
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
323
|
+
/** Delivers a transition command to a target without caller-side schema rejection. */
|
|
324
|
+
export const deliverMachineTarget = (
|
|
325
|
+
target: MachineTarget<unknown>,
|
|
326
|
+
event: unknown,
|
|
327
|
+
source: Inspection.Subject | undefined,
|
|
328
|
+
causedBy: Inspection.Causation | undefined
|
|
329
|
+
): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> =>
|
|
330
|
+
deliverEvent in target
|
|
331
|
+
? (target as unknown as Deliverable)[deliverEvent](event, source, causedBy)
|
|
332
|
+
: sendMachineTarget(target, event, source, causedBy)
|
|
333
|
+
|
|
334
|
+
/** Sends to an endpoint for a waiting caller. */
|
|
335
|
+
export const sendEndpoint = (
|
|
336
|
+
endpoint: ProcessEndpoint<unknown>,
|
|
337
|
+
event: unknown,
|
|
338
|
+
source: Inspection.Subject | undefined,
|
|
339
|
+
causedBy: Inspection.Causation | undefined
|
|
340
|
+
): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> =>
|
|
341
|
+
endpoint.sendInspected === undefined ? endpoint.send(event) : endpoint.sendInspected(event, source, causedBy)
|
|
342
|
+
|
|
343
|
+
/** Delivers an internal message or transition command to an endpoint of this runtime. */
|
|
344
|
+
export const deliverToEndpoint = (
|
|
345
|
+
endpoint: ProcessEndpoint<unknown>,
|
|
346
|
+
event: unknown,
|
|
347
|
+
source: Inspection.Subject | undefined,
|
|
348
|
+
causedBy: Inspection.Causation | undefined
|
|
349
|
+
): Effect.Effect<void, StoppedError> =>
|
|
350
|
+
endpoint[deliverEvent] !== undefined
|
|
351
|
+
? endpoint[deliverEvent](event, source, causedBy)
|
|
352
|
+
: sendEndpoint(endpoint, event, source, causedBy) as Effect.Effect<void, StoppedError>
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Delivers a transition command to a child started by this runtime: a machine
|
|
356
|
+
* takes the delivery and fails when planning a refused event, and other logic
|
|
357
|
+
* decodes nothing.
|
|
358
|
+
*/
|
|
359
|
+
const deliverToChild = (
|
|
360
|
+
ref: MachineRef<any, any, any, any>,
|
|
361
|
+
event: unknown,
|
|
362
|
+
source: Inspection.Subject | undefined,
|
|
363
|
+
causedBy: Inspection.Causation | undefined
|
|
364
|
+
): Effect.Effect<void, StoppedError> =>
|
|
365
|
+
ref[deliverEvent] === undefined
|
|
366
|
+
? sendMachineTarget(ref, event, source, causedBy) as Effect.Effect<void, StoppedError>
|
|
367
|
+
: ref[deliverEvent](event, source, causedBy)
|
|
368
|
+
|
|
369
|
+
/** Delivers a transition command from a running local process to a target or child. */
|
|
370
|
+
export const deliverFromScope = (
|
|
371
|
+
scope: ProcessInitialContext<unknown>,
|
|
372
|
+
target: MachineTarget<unknown> | ChildSelector,
|
|
373
|
+
event: unknown
|
|
374
|
+
): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> =>
|
|
375
|
+
scope.self[deliverFromProcess] !== undefined
|
|
376
|
+
? scope.self[deliverFromProcess](target, event)
|
|
377
|
+
: isMachineTarget(target)
|
|
378
|
+
? deliverMachineTarget(target, event, scope.self.inspectionSubject, undefined)
|
|
379
|
+
: scope.sendTo(target, event)
|
|
380
|
+
|
|
381
|
+
export interface ProcessInitialContext<Event> {
|
|
382
|
+
readonly self: ProcessEndpoint<Event>
|
|
383
|
+
readonly parent: ProcessEndpoint<unknown> | undefined
|
|
221
384
|
readonly spawn: ProcessSpawn<Event>
|
|
222
|
-
readonly sendParent: (event: unknown) => Effect.Effect<void, StoppedError>
|
|
223
385
|
readonly emit: (event: unknown) => Effect.Effect<void>
|
|
224
386
|
readonly sendTo: {
|
|
225
|
-
<TargetEvent>(
|
|
226
|
-
|
|
387
|
+
<TargetEvent>(
|
|
388
|
+
target: ProcessEndpoint<TargetEvent>,
|
|
389
|
+
event: TargetEvent
|
|
390
|
+
): Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
|
|
391
|
+
<TargetEvent>(
|
|
392
|
+
target: MachineTarget<TargetEvent>,
|
|
393
|
+
event: TargetEvent
|
|
394
|
+
): Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
|
|
395
|
+
(child: ChildSelector, event: unknown): Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
|
|
227
396
|
}
|
|
228
397
|
readonly stopChild: (child: ChildSelector) => Effect.Effect<void>
|
|
229
398
|
/** @internal */
|
|
@@ -235,7 +404,7 @@ export interface ProcessScope<Event> {
|
|
|
235
404
|
) => void
|
|
236
405
|
}
|
|
237
406
|
|
|
238
|
-
export interface
|
|
407
|
+
export interface ProcessRunContext<State, Event> extends ProcessInitialContext<Event> {
|
|
239
408
|
readonly receive: Effect.Effect<Event>
|
|
240
409
|
/** @internal */
|
|
241
410
|
readonly poll?: Effect.Effect<Option.Option<Event>>
|
|
@@ -250,14 +419,12 @@ export interface ProcessContext<State, Event> extends ProcessScope<Event> {
|
|
|
250
419
|
readonly updateState: <E, R>(
|
|
251
420
|
f: (state: State) => Effect.Effect<State, E, R>
|
|
252
421
|
) => Effect.Effect<void, E, R>
|
|
253
|
-
/** Present only when a compiled statechart is forced through the generic runtime. @internal */
|
|
254
|
-
readonly ownedChildren?: OwnedChildRuntime
|
|
255
422
|
}
|
|
256
423
|
|
|
257
424
|
/**
|
|
258
425
|
* Owner-local execution context for compiled statecharts.
|
|
259
426
|
*
|
|
260
|
-
* Unlike `
|
|
427
|
+
* Unlike `ProcessRunContext`, synchronous mailbox and state operations do not
|
|
261
428
|
* introduce an Effect boundary. The compiled drain still returns an Effect so
|
|
262
429
|
* machine commands, invokes, observation callbacks, interruption, and the Effect
|
|
263
430
|
* scheduler remain explicit at their actual boundaries.
|
|
@@ -265,12 +432,12 @@ export interface ProcessContext<State, Event> extends ProcessScope<Event> {
|
|
|
265
432
|
* @internal
|
|
266
433
|
*/
|
|
267
434
|
export interface CompiledProcessContext<State, Event> {
|
|
268
|
-
readonly scope:
|
|
435
|
+
readonly scope: ProcessInitialContext<Event>
|
|
269
436
|
readonly ownedChildren: OwnedChildRuntime
|
|
270
437
|
readonly poll: () => Option.Option<Event>
|
|
271
438
|
readonly pollMessage: () => Option.Option<ProcessMessage<Event>>
|
|
272
|
-
/** Marks a message delivered without the mailbox as the one in flight. */
|
|
273
|
-
readonly beginMessage: (message: ProcessMessage<Event>) =>
|
|
439
|
+
/** Marks a message delivered without the mailbox as the one in flight, and reports whether it is acknowledged. */
|
|
440
|
+
readonly beginMessage: (message: ProcessMessage<Event>) => boolean
|
|
274
441
|
readonly state: () => State
|
|
275
442
|
readonly completeMessage: (delivery: AcknowledgedDelivery<State>) => void
|
|
276
443
|
readonly commit: (state: State) => Effect.Effect<void> | undefined
|
|
@@ -296,7 +463,7 @@ type CompiledProcessDrain<State, Event, Error, Requirements, Output> =
|
|
|
296
463
|
| {
|
|
297
464
|
readonly _tag: "Process"
|
|
298
465
|
readonly run: (
|
|
299
|
-
context:
|
|
466
|
+
context: ProcessRunContext<State, Event>
|
|
300
467
|
) => Effect.Effect<Option.Option<Output>, Error, Requirements>
|
|
301
468
|
}
|
|
302
469
|
| OwnedCompiledProcessDrain<State, Event, Error, Requirements, Output>
|
|
@@ -339,11 +506,17 @@ export interface CompiledProcessExecution<
|
|
|
339
506
|
> {
|
|
340
507
|
readonly _tag: "Compiled"
|
|
341
508
|
readonly childless: boolean
|
|
509
|
+
/**
|
|
510
|
+
* Decodes a public event as it enters the mailbox, so the drain plans it
|
|
511
|
+
* without decoding it again. An invocation event passes unchanged. Without
|
|
512
|
+
* it, the process takes events as they are.
|
|
513
|
+
*/
|
|
514
|
+
readonly decodeEvent?: EventDecoder
|
|
342
515
|
readonly initial?: (
|
|
343
|
-
scope:
|
|
516
|
+
scope: ProcessInitialContext<Event>
|
|
344
517
|
) => Effect.Effect<CompiledProcessInitial<State, Output>, InitialError, Requirements>
|
|
345
518
|
readonly initialSync?: (
|
|
346
|
-
scope:
|
|
519
|
+
scope: ProcessInitialContext<Event>
|
|
347
520
|
) => CompiledProcessInitial<State, Output>
|
|
348
521
|
readonly drain: CompiledProcessDrain<State, Event, Error, Requirements, Output>
|
|
349
522
|
}
|
|
@@ -480,8 +653,8 @@ export interface ProcessLogic<
|
|
|
480
653
|
readonly kind: Inspection.Subject["kind"]
|
|
481
654
|
readonly definition?: MachineDefinition.Any
|
|
482
655
|
}
|
|
483
|
-
initial(scope:
|
|
484
|
-
run(context:
|
|
656
|
+
initial(scope: ProcessInitialContext<Event>): Effect.Effect<State, InitialError, Requirements>
|
|
657
|
+
run(context: ProcessRunContext<State, Event>): Effect.Effect<Output, Error, Requirements>
|
|
485
658
|
}
|
|
486
659
|
|
|
487
660
|
interface ProcessSpawn<OwnerEvent = unknown> {
|
|
@@ -511,7 +684,6 @@ interface ProcessSpawn<OwnerEvent = unknown> {
|
|
|
511
684
|
readonly [activeSnapshotObserver]?: (
|
|
512
685
|
snapshot: Extract<RuntimeSnapshot<ChildState, ChildError, ChildOutput>, { readonly status: "active" }>
|
|
513
686
|
) => Effect.Effect<void>
|
|
514
|
-
readonly [sendParentOverride]?: (event: unknown) => Effect.Effect<void, StoppedError>
|
|
515
687
|
}
|
|
516
688
|
): Effect.Effect<
|
|
517
689
|
MachineRef<ChildState, ChildEvent, ChildError, ChildOutput>,
|
|
@@ -520,90 +692,16 @@ interface ProcessSpawn<OwnerEvent = unknown> {
|
|
|
520
692
|
>
|
|
521
693
|
}
|
|
522
694
|
|
|
523
|
-
/**
|
|
524
|
-
* Service required by operations that act on the machine executing them,
|
|
525
|
-
* such as `sendTo`, `stopChild`, and `spawn`.
|
|
526
|
-
*
|
|
527
|
-
* **When to use**
|
|
528
|
-
*
|
|
529
|
-
* Name this requirement when annotating a reusable helper built from those
|
|
530
|
-
* operations, or when exporting a value whose inferred type includes it.
|
|
531
|
-
*
|
|
532
|
-
* **Details**
|
|
533
|
-
*
|
|
534
|
-
* A running machine provides this service to the effects it executes, so it
|
|
535
|
-
* never appears in the requirements of the machine itself. Only the type is
|
|
536
|
-
* public: the service cannot be constructed or provided outside a running
|
|
537
|
-
* machine.
|
|
538
|
-
*
|
|
539
|
-
* @category services
|
|
540
|
-
* @since 0.47.0
|
|
541
|
-
*/
|
|
542
|
-
export class MachineRuntime extends Context.Service<MachineRuntime, ProcessScope<any>>()(
|
|
543
|
-
"effect/Machine/MachineRuntime"
|
|
544
|
-
) {}
|
|
545
|
-
|
|
546
695
|
export const provideMachineRuntime = <A, E, R, Event>(
|
|
547
696
|
effect: Effect.Effect<A, E, R>,
|
|
548
|
-
scope:
|
|
549
|
-
): Effect.Effect<A, E, Exclude<R, MachineRuntime>> =>
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
case "active": {
|
|
557
|
-
return undefined
|
|
558
|
-
}
|
|
559
|
-
case "done": {
|
|
560
|
-
return {
|
|
561
|
-
_tag: "Done",
|
|
562
|
-
output: snapshot.output,
|
|
563
|
-
snapshot
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
case "error": {
|
|
567
|
-
const failure = snapshot.cause.reasons.find(Cause.isFailReason)
|
|
568
|
-
if (failure !== undefined) {
|
|
569
|
-
return {
|
|
570
|
-
_tag: "Failure",
|
|
571
|
-
error: failure.error,
|
|
572
|
-
cause: snapshot.cause,
|
|
573
|
-
snapshot
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
const defect = snapshot.cause.reasons.find(Cause.isDieReason)
|
|
577
|
-
if (defect !== undefined) {
|
|
578
|
-
return {
|
|
579
|
-
_tag: "Defect",
|
|
580
|
-
defect: defect.defect,
|
|
581
|
-
cause: snapshot.cause,
|
|
582
|
-
snapshot
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
const interrupted = snapshot.cause.reasons.find(Cause.isInterruptReason)
|
|
586
|
-
if (interrupted !== undefined) {
|
|
587
|
-
return {
|
|
588
|
-
_tag: "Interrupted",
|
|
589
|
-
cause: snapshot.cause,
|
|
590
|
-
snapshot
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
return {
|
|
594
|
-
_tag: "Cause",
|
|
595
|
-
cause: snapshot.cause,
|
|
596
|
-
snapshot
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
case "stopped": {
|
|
600
|
-
return {
|
|
601
|
-
_tag: "Stopped",
|
|
602
|
-
snapshot
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
}
|
|
697
|
+
scope: ProcessInitialContext<Event>
|
|
698
|
+
): Effect.Effect<A, E, Exclude<R, MachineRuntime | ChildMachineRuntime>> =>
|
|
699
|
+
// One update of the context for both, as each process starts with them.
|
|
700
|
+
Effect.updateContext(
|
|
701
|
+
effect,
|
|
702
|
+
(context: Context.Context<Exclude<R, MachineRuntime | ChildMachineRuntime>>) =>
|
|
703
|
+
Context.add(Context.add(context, MachineRuntime, scope), ChildMachineRuntime, scope) as Context.Context<R>
|
|
704
|
+
)
|
|
607
705
|
|
|
608
706
|
export const notifyActiveSnapshot = <State, Error, Output>(
|
|
609
707
|
onSnapshot: (
|
|
@@ -616,14 +714,11 @@ export const notifyActiveSnapshot = <State, Error, Output>(
|
|
|
616
714
|
Effect.asVoid
|
|
617
715
|
)
|
|
618
716
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
Stream.map((snapshot) => classifyOutcome(snapshot)!),
|
|
625
|
-
Stream.take(1)
|
|
626
|
-
)
|
|
717
|
+
/** Builds the process logic that runs a child machine with the given input. */
|
|
718
|
+
export type MakeChildLogic = (
|
|
719
|
+
machine: MachineDefinition.Any,
|
|
720
|
+
input: unknown
|
|
721
|
+
) => ProcessLogic<any, any, any, any, any, any>
|
|
627
722
|
|
|
628
723
|
export interface ProcessRuntime {
|
|
629
724
|
readonly start: StartProcess
|
|
@@ -633,23 +728,30 @@ export interface ProcessRuntime {
|
|
|
633
728
|
services: Context.Context<any>,
|
|
634
729
|
sessionId: string
|
|
635
730
|
) => Effect.Effect<MachineRef<any, any, any, any>, any, any>
|
|
731
|
+
readonly makeChildLogic: MakeChildLogic
|
|
636
732
|
readonly nextSessionId: Effect.Effect<string>
|
|
733
|
+
readonly nextSessionIdSync: () => string
|
|
637
734
|
inspection?: InspectionRuntime.Runtime
|
|
638
735
|
}
|
|
639
736
|
|
|
640
737
|
export const makeProcessRuntime = (
|
|
641
738
|
start: StartProcess,
|
|
642
|
-
startCompiledSync: ProcessRuntime["startCompiledSync"]
|
|
739
|
+
startCompiledSync: ProcessRuntime["startCompiledSync"],
|
|
740
|
+
makeChildLogic: MakeChildLogic
|
|
643
741
|
): Effect.Effect<ProcessRuntime> => {
|
|
644
742
|
// Strategy hooks belong to the shared prototype. Each root owns only its
|
|
645
743
|
// session counter and optional inspection runtime, independent of other roots.
|
|
646
744
|
class Runtime implements ProcessRuntime {
|
|
647
745
|
declare inspection?: InspectionRuntime.Runtime
|
|
648
|
-
|
|
746
|
+
private sessionIdCounter = 0
|
|
747
|
+
private _nextSessionId: Effect.Effect<string> | undefined = undefined
|
|
748
|
+
|
|
749
|
+
nextSessionIdSync(): string {
|
|
750
|
+
return `machine:${this.sessionIdCounter++}`
|
|
751
|
+
}
|
|
649
752
|
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
this.nextSessionId = Effect.sync(() => `machine:${sessionIdCounter++}`)
|
|
753
|
+
get nextSessionId(): Effect.Effect<string> {
|
|
754
|
+
return this._nextSessionId ??= Effect.sync(() => this.nextSessionIdSync())
|
|
653
755
|
}
|
|
654
756
|
|
|
655
757
|
get start(): StartProcess {
|
|
@@ -659,6 +761,10 @@ export const makeProcessRuntime = (
|
|
|
659
761
|
get startCompiledSync(): ProcessRuntime["startCompiledSync"] {
|
|
660
762
|
return startCompiledSync
|
|
661
763
|
}
|
|
764
|
+
|
|
765
|
+
get makeChildLogic(): MakeChildLogic {
|
|
766
|
+
return makeChildLogic
|
|
767
|
+
}
|
|
662
768
|
}
|
|
663
769
|
return Effect.sync(() => new Runtime())
|
|
664
770
|
}
|
|
@@ -689,15 +795,17 @@ export const makeInspectedMessage = <Event>(
|
|
|
689
795
|
event: Event,
|
|
690
796
|
source: Inspection.Subject | undefined,
|
|
691
797
|
causedBy: Inspection.Causation | undefined,
|
|
692
|
-
deferred?: Deferred.Deferred<AcknowledgedDelivery<unknown>, unknown
|
|
798
|
+
deferred?: Deferred.Deferred<AcknowledgedDelivery<unknown>, unknown>,
|
|
799
|
+
sent: unknown = event
|
|
693
800
|
): AcknowledgedMessage<Event> => {
|
|
694
801
|
const deliveryId = inspection.nextDeliveryId()
|
|
695
802
|
const macrostepId = inspection.nextMacrostepId()
|
|
803
|
+
// Inspection reports the event as it was sent, before decoding.
|
|
696
804
|
const inspected: InspectedDelivery = {
|
|
697
805
|
deliveryId,
|
|
698
806
|
macrostepId,
|
|
699
807
|
source,
|
|
700
|
-
event,
|
|
808
|
+
event: sent,
|
|
701
809
|
causedBy,
|
|
702
810
|
complete: (delivery) => {
|
|
703
811
|
const microsteps = InspectionRuntime.microsteps(delivery.plan)
|
|
@@ -707,7 +815,7 @@ export const makeInspectedMessage = <Event>(
|
|
|
707
815
|
macrostepId,
|
|
708
816
|
deliveryId,
|
|
709
817
|
source,
|
|
710
|
-
event,
|
|
818
|
+
event: sent,
|
|
711
819
|
before: { status: "active", state: delivery.before },
|
|
712
820
|
after: { status: "active", state: delivery.after },
|
|
713
821
|
handled: microsteps.some((microstep) => microstep.transitions.length > 0),
|
|
@@ -755,61 +863,62 @@ export interface StartInternalOptions {
|
|
|
755
863
|
ref: MachineRef<any, any, any, any>,
|
|
756
864
|
requestStop: Effect.Effect<void>
|
|
757
865
|
) => Effect.Effect<void>
|
|
758
|
-
readonly onReadySync?: (ref: MachineRef<any, any, any, any>) => boolean
|
|
866
|
+
readonly onReadySync?: ((ref: MachineRef<any, any, any, any>) => boolean) | undefined
|
|
759
867
|
readonly onStop?: Effect.Effect<void>
|
|
760
868
|
/** Runs synchronously once the process has terminated, with its reference. */
|
|
761
869
|
readonly onStopSync?: (ref: MachineRef<any, any, any, any>) => void
|
|
762
870
|
readonly skipStoppedOutcome?: boolean
|
|
763
|
-
readonly parent?:
|
|
871
|
+
readonly parent?: ProcessEndpoint<unknown>
|
|
764
872
|
readonly runtime: ProcessRuntime
|
|
765
|
-
readonly sendParent?: (event: unknown) => Effect.Effect<void, StoppedError>
|
|
766
873
|
readonly origin?: Inspection.Origin
|
|
767
|
-
readonly
|
|
874
|
+
readonly programInvocation?: {
|
|
768
875
|
readonly id: string
|
|
769
876
|
readonly owner: Inspection.Subject
|
|
770
877
|
readonly ownerPath: string
|
|
771
|
-
readonly kind: Inspection.
|
|
878
|
+
readonly kind: Inspection.ProgramInvocation["kind"]
|
|
772
879
|
}
|
|
773
880
|
readonly inspectionRoot?: boolean
|
|
774
881
|
}
|
|
775
882
|
|
|
883
|
+
/**
|
|
884
|
+
* Work started by an owner: lazily built process logic, or a child machine
|
|
885
|
+
* whose logic the runtime builds from its definition. A child machine is named
|
|
886
|
+
* rather than wrapped, so a running owner retains no closure for it.
|
|
887
|
+
*
|
|
888
|
+
* @internal
|
|
889
|
+
*/
|
|
890
|
+
export type OwnedChildSource = (() => ProcessLogic<any, any, any, any, any, any>) | {
|
|
891
|
+
readonly child: ChildMachine.Any
|
|
892
|
+
/** Constructs the child's input, or `undefined` when none is declared. */
|
|
893
|
+
readonly input: (() => unknown) | undefined
|
|
894
|
+
}
|
|
895
|
+
|
|
776
896
|
/** @internal */
|
|
777
897
|
interface OwnedChildSpawnOptions {
|
|
778
898
|
readonly key: string
|
|
779
899
|
readonly path: string
|
|
780
900
|
readonly id: string
|
|
781
901
|
readonly duplicateId: string
|
|
782
|
-
readonly descriptor?: ChildDescriptor
|
|
783
902
|
readonly onOutcome: (
|
|
784
903
|
isCurrent: () => boolean,
|
|
785
904
|
outcome: RuntimeOutcome<any, any, any>,
|
|
786
|
-
|
|
905
|
+
invocationSessionId: string | undefined
|
|
787
906
|
) => Effect.Effect<void>
|
|
788
907
|
readonly onSnapshot?: (
|
|
789
908
|
isCurrent: () => boolean,
|
|
790
909
|
snapshot: Extract<RuntimeSnapshot<any, any, any>, { readonly status: "active" }>
|
|
791
910
|
) => Effect.Effect<void>
|
|
792
|
-
readonly
|
|
793
|
-
isCurrent: () => boolean,
|
|
794
|
-
event: unknown
|
|
795
|
-
) => Effect.Effect<void, StoppedError>
|
|
796
|
-
readonly activityKind?: Inspection.Activity["kind"]
|
|
911
|
+
readonly programKind?: Inspection.ProgramInvocation["kind"]
|
|
797
912
|
}
|
|
798
913
|
|
|
799
914
|
/** @internal */
|
|
800
915
|
export interface OwnedChildRuntime {
|
|
801
916
|
readonly spawn: (
|
|
802
|
-
|
|
917
|
+
source: OwnedChildSource,
|
|
803
918
|
options: OwnedChildSpawnOptions
|
|
804
919
|
) => Effect.Effect<void, any, any>
|
|
805
|
-
/**
|
|
806
|
-
|
|
807
|
-
* replaces. The generation stays current after its children finish and is
|
|
808
|
-
* retired only when the state exits, by `stopPaths` or `stopAll`.
|
|
809
|
-
*/
|
|
810
|
-
readonly beginGeneration: (path: string) => InvocationEvent.Generation
|
|
811
|
-
readonly stopAll: () => Effect.Effect<void>
|
|
812
|
-
readonly stopPaths: (paths: ReadonlyArray<string>) => Effect.Effect<void> | undefined
|
|
920
|
+
/** Stops the child that was spawned with `key`, while its owner holds it. */
|
|
921
|
+
readonly stopKey: (key: string) => Effect.Effect<void>
|
|
813
922
|
}
|
|
814
923
|
|
|
815
924
|
export interface ChildRuntime {
|
|
@@ -826,27 +935,95 @@ export interface ChildRuntime {
|
|
|
826
935
|
event: unknown,
|
|
827
936
|
source?: Inspection.Subject,
|
|
828
937
|
causedBy?: Inspection.Causation
|
|
938
|
+
) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
|
|
939
|
+
readonly deliverTo: (
|
|
940
|
+
child: ChildSelector,
|
|
941
|
+
event: unknown,
|
|
942
|
+
source?: Inspection.Subject,
|
|
943
|
+
causedBy?: Inspection.Causation
|
|
829
944
|
) => Effect.Effect<void, StoppedError>
|
|
830
945
|
readonly stop: (child: ChildSelector) => Effect.Effect<void>
|
|
831
946
|
readonly owned: OwnedChildRuntime
|
|
832
947
|
}
|
|
833
948
|
|
|
949
|
+
/**
|
|
950
|
+
* Parent endpoint exposed to an owned invocation. Prototype getters and methods
|
|
951
|
+
* keep each running child free of per-child parent endpoint closures.
|
|
952
|
+
*/
|
|
953
|
+
class OwnedChildParent implements ProcessEndpoint<unknown>, Deliverable {
|
|
954
|
+
startedChild: MachineRef<any, any, any, any> | undefined = undefined
|
|
955
|
+
|
|
956
|
+
constructor(
|
|
957
|
+
readonly owner: OwnedChildRuntimeImpl,
|
|
958
|
+
readonly childId: string,
|
|
959
|
+
readonly token: symbol,
|
|
960
|
+
readonly ownerKey: string
|
|
961
|
+
) {}
|
|
962
|
+
|
|
963
|
+
get id(): string {
|
|
964
|
+
return this.owner.self.id
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
get sessionId(): string {
|
|
968
|
+
return this.owner.self.sessionId
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
get inspectionSubject(): Inspection.Subject | undefined {
|
|
972
|
+
return this.owner.self.inspectionSubject
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
get stop(): Effect.Effect<void> {
|
|
976
|
+
return this.owner.self.stop
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
isCurrent(): boolean {
|
|
980
|
+
const entry = this.owner.registry.children.get(this.childId)
|
|
981
|
+
return entry?.token === this.token && entry.ownerKey === this.ownerKey && entry.ownerActive === true
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
unregister(): void {
|
|
985
|
+
unregisterChild(this.owner.registry, this.childId, this.token)
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
send(event: unknown): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> {
|
|
989
|
+
return this.isCurrent() ? this.owner.self.send(event) : Effect.void
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
sendInspected(
|
|
993
|
+
event: unknown,
|
|
994
|
+
source: Inspection.Subject | undefined,
|
|
995
|
+
causedBy: Inspection.Causation | undefined
|
|
996
|
+
): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> {
|
|
997
|
+
return this.isCurrent() ? sendEndpoint(this.owner.self, event, source, causedBy) : Effect.void
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
[deliverEvent](
|
|
1001
|
+
event: unknown,
|
|
1002
|
+
source: Inspection.Subject | undefined,
|
|
1003
|
+
causedBy: Inspection.Causation | undefined
|
|
1004
|
+
): Effect.Effect<void, StoppedError> {
|
|
1005
|
+
return this.isCurrent() ? deliverToEndpoint(this.owner.self, event, source, causedBy) : Effect.void
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
get [acknowledgedSend]():
|
|
1009
|
+
| ((event: unknown) => Effect.Effect<AcknowledgedDelivery<unknown>, unknown | StoppedError>)
|
|
1010
|
+
| undefined
|
|
1011
|
+
{
|
|
1012
|
+
const send = this.owner.sendAcknowledged
|
|
1013
|
+
if (send === undefined) return undefined
|
|
1014
|
+
return (event: unknown) => this.isCurrent() ? send(event) : Effect.interrupt
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
|
|
834
1018
|
class OwnedChildRuntimeImpl implements OwnedChildRuntime {
|
|
835
1019
|
private scopedServices: Context.Context<any> | undefined
|
|
836
|
-
// The current generation of each active invoking state path. Most
|
|
837
|
-
// configurations have one such path, held inline; further paths use the map.
|
|
838
|
-
// Entries are removed when their state exits, so neither outgrows the active
|
|
839
|
-
// configuration.
|
|
840
|
-
private generationPath: string | undefined
|
|
841
|
-
private generation: InvocationEvent.Generation | undefined
|
|
842
|
-
private generations: Map<string, InvocationEvent.Generation> | undefined
|
|
843
1020
|
|
|
844
1021
|
constructor(
|
|
845
|
-
|
|
846
|
-
|
|
1022
|
+
readonly registry: ChildRegistry,
|
|
1023
|
+
readonly self: ProcessEndpoint<any>,
|
|
847
1024
|
private readonly runtime: ProcessRuntime,
|
|
848
1025
|
private readonly services?: Context.Context<any>,
|
|
849
|
-
|
|
1026
|
+
readonly sendAcknowledged?: (
|
|
850
1027
|
event: unknown
|
|
851
1028
|
) => Effect.Effect<AcknowledgedDelivery<unknown>, unknown | StoppedError>
|
|
852
1029
|
) {}
|
|
@@ -864,173 +1041,98 @@ class OwnedChildRuntimeImpl implements OwnedChildRuntime {
|
|
|
864
1041
|
}
|
|
865
1042
|
|
|
866
1043
|
spawn(
|
|
867
|
-
|
|
1044
|
+
source: OwnedChildSource,
|
|
868
1045
|
options: OwnedChildSpawnOptions
|
|
869
1046
|
): Effect.Effect<void, any, any> {
|
|
870
1047
|
const token = Symbol()
|
|
871
|
-
|
|
872
|
-
const
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
1048
|
+
const id = options.id
|
|
1049
|
+
const key = options.key
|
|
1050
|
+
const onOutcome = options.onOutcome
|
|
1051
|
+
const onSnapshot = options.onSnapshot
|
|
1052
|
+
const parent = new OwnedChildParent(this, id, token, key)
|
|
876
1053
|
return Effect.suspend(() => {
|
|
877
1054
|
if (this.registry.closed) return Effect.interrupt
|
|
878
|
-
if (this.has(
|
|
1055
|
+
if (this.registry.children.size > 0 && (this.has(key) || this.registry.children.has(id))) {
|
|
879
1056
|
return Effect.fail(new ChildAlreadyExistsError({ id: options.duplicateId }))
|
|
880
1057
|
}
|
|
881
|
-
const logic =
|
|
1058
|
+
const logic = typeof source === "function"
|
|
1059
|
+
? source()
|
|
1060
|
+
: this.runtime.makeChildLogic(source.child.machine, source.input?.())
|
|
1061
|
+
const descriptor = typeof source === "function" ? undefined : source.child
|
|
882
1062
|
const scope = this.registry.scope ??= Scope.makeUnsafe("parallel")
|
|
883
|
-
this.registry.children.set(
|
|
1063
|
+
this.registry.children.set(id, {
|
|
884
1064
|
_tag: "Starting",
|
|
885
1065
|
token,
|
|
886
|
-
ownerKey:
|
|
887
|
-
ownerPath: options.path,
|
|
1066
|
+
ownerKey: key,
|
|
888
1067
|
ownerActive: true
|
|
889
1068
|
})
|
|
890
|
-
const
|
|
891
|
-
|
|
892
|
-
send: (event) => options.sendParent(isCurrent, event),
|
|
893
|
-
sendInspected: (event, source, causedBy) =>
|
|
894
|
-
isCurrent() ? sendMachineTarget(this.self, event, source, causedBy) : Effect.void,
|
|
895
|
-
...(this.sendAcknowledged === undefined
|
|
896
|
-
? undefined
|
|
897
|
-
: {
|
|
898
|
-
[acknowledgedSend]: (event: unknown) =>
|
|
899
|
-
isCurrent()
|
|
900
|
-
? this.sendAcknowledged!(event)
|
|
901
|
-
: Effect.interrupt
|
|
902
|
-
})
|
|
903
|
-
}
|
|
904
|
-
const startOptions: StartInternalOptions = {
|
|
1069
|
+
const isCurrent = () => parent.isCurrent()
|
|
1070
|
+
const startOptions: { -readonly [K in keyof StartInternalOptions]: StartInternalOptions[K] } = {
|
|
905
1071
|
detached: true,
|
|
906
|
-
id
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
...(options.onSnapshot === undefined
|
|
1072
|
+
id,
|
|
1073
|
+
onOutcome: (outcome) => onOutcome(isCurrent, outcome, parent.startedChild?.sessionId),
|
|
1074
|
+
...(onSnapshot === undefined
|
|
910
1075
|
? undefined
|
|
911
|
-
: { onSnapshot: (snapshot) =>
|
|
1076
|
+
: { onSnapshot: (snapshot) => onSnapshot(isCurrent, snapshot) }),
|
|
912
1077
|
onReadySync: (child) => {
|
|
913
|
-
startedChild = child
|
|
914
|
-
|
|
1078
|
+
parent.startedChild = child
|
|
1079
|
+
startOptions.onReadySync = undefined
|
|
1080
|
+
return registerChild(this.registry, id, token, child, descriptor)
|
|
915
1081
|
},
|
|
916
|
-
onStopSync: () =>
|
|
1082
|
+
onStopSync: () => parent.unregister(),
|
|
917
1083
|
skipStoppedOutcome: true,
|
|
918
1084
|
parent,
|
|
919
1085
|
runtime: this.runtime,
|
|
920
|
-
|
|
921
|
-
...(options.activityKind === undefined || this.runtime.inspection === undefined ||
|
|
922
|
-
this.self.inspectionSubject === undefined
|
|
1086
|
+
...(this.runtime.inspection === undefined
|
|
923
1087
|
? undefined
|
|
924
1088
|
: {
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
1089
|
+
origin: { _tag: "Invoke" as const, ownerPath: options.path, invokeId: options.duplicateId },
|
|
1090
|
+
...(options.programKind === undefined || this.self.inspectionSubject === undefined
|
|
1091
|
+
? undefined
|
|
1092
|
+
: {
|
|
1093
|
+
programInvocation: {
|
|
1094
|
+
id: options.duplicateId,
|
|
1095
|
+
owner: this.self.inspectionSubject,
|
|
1096
|
+
ownerPath: options.path,
|
|
1097
|
+
kind: options.programKind
|
|
1098
|
+
}
|
|
1099
|
+
})
|
|
931
1100
|
})
|
|
932
1101
|
}
|
|
933
1102
|
const execution = logic.execution
|
|
934
|
-
const synchronous = this.services !== undefined &&
|
|
1103
|
+
const synchronous = this.services !== undefined && onSnapshot === undefined &&
|
|
935
1104
|
execution?._tag === "Compiled" && execution.childless && execution.drain._tag === "Owned" &&
|
|
936
1105
|
execution.initialSync !== undefined
|
|
937
1106
|
const start = synchronous
|
|
938
|
-
?
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
startOptions,
|
|
944
|
-
this.scopedServices ??= Context.add(this.services!, Scope.Scope, scope),
|
|
945
|
-
sessionId
|
|
946
|
-
)
|
|
1107
|
+
? this.runtime.startCompiledSync(
|
|
1108
|
+
logic,
|
|
1109
|
+
startOptions,
|
|
1110
|
+
this.scopedServices ??= Context.add(this.services, Scope.Scope, scope),
|
|
1111
|
+
this.runtime.nextSessionIdSync()
|
|
947
1112
|
)
|
|
948
1113
|
: this.runtime.start(logic, startOptions)
|
|
949
1114
|
const guarded = start.pipe(
|
|
950
1115
|
Effect.onExit((exit) => {
|
|
951
1116
|
if (Exit.isSuccess(exit)) return Effect.void
|
|
952
|
-
|
|
953
|
-
return startedChild === undefined ? Effect.void : startedChild.stop
|
|
1117
|
+
parent.unregister()
|
|
1118
|
+
return parent.startedChild === undefined ? Effect.void : parent.startedChild.stop
|
|
954
1119
|
})
|
|
955
1120
|
)
|
|
956
1121
|
return (synchronous ? guarded : Scope.provide(guarded, scope)).pipe(Effect.asVoid)
|
|
957
1122
|
})
|
|
958
1123
|
}
|
|
959
1124
|
|
|
960
|
-
|
|
961
|
-
this.retireGeneration(path)
|
|
962
|
-
const generation: InvocationEvent.Generation = { current: true }
|
|
963
|
-
if (this.generationPath === undefined) {
|
|
964
|
-
this.generationPath = path
|
|
965
|
-
this.generation = generation
|
|
966
|
-
} else {
|
|
967
|
-
;(this.generations ??= new Map()).set(path, generation)
|
|
968
|
-
}
|
|
969
|
-
return generation
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
private retireGeneration(path: string): void {
|
|
973
|
-
const inline = this.generation
|
|
974
|
-
if (inline !== undefined && this.generationPath === path) {
|
|
975
|
-
inline.current = false
|
|
976
|
-
this.generationPath = undefined
|
|
977
|
-
this.generation = undefined
|
|
978
|
-
return
|
|
979
|
-
}
|
|
980
|
-
const generations = this.generations
|
|
981
|
-
const generation = generations?.get(path)
|
|
982
|
-
if (generations !== undefined && generation !== undefined) {
|
|
983
|
-
generation.current = false
|
|
984
|
-
generations.delete(path)
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
|
|
988
|
-
stopAll(): Effect.Effect<void> {
|
|
1125
|
+
stopKey(key: string): Effect.Effect<void> {
|
|
989
1126
|
return Effect.suspend(() => {
|
|
990
|
-
if (this.generation !== undefined) {
|
|
991
|
-
this.generation.current = false
|
|
992
|
-
this.generationPath = undefined
|
|
993
|
-
this.generation = undefined
|
|
994
|
-
}
|
|
995
|
-
const generations = this.generations
|
|
996
|
-
if (generations !== undefined && generations.size > 0) {
|
|
997
|
-
for (const generation of generations.values()) generation.current = false
|
|
998
|
-
generations.clear()
|
|
999
|
-
}
|
|
1000
|
-
const effects: Array<Effect.Effect<void>> = []
|
|
1001
1127
|
for (const entry of this.registry.children.values()) {
|
|
1002
|
-
if (entry.ownerActive)
|
|
1128
|
+
if (entry.ownerActive && entry.ownerKey === key) return this.stopEntry(entry)
|
|
1003
1129
|
}
|
|
1004
|
-
return
|
|
1005
|
-
? Effect.void
|
|
1006
|
-
: effects.length === 1
|
|
1007
|
-
? effects[0]!
|
|
1008
|
-
: Effect.all(effects, { concurrency: "unbounded", discard: true })
|
|
1130
|
+
return Effect.void
|
|
1009
1131
|
})
|
|
1010
1132
|
}
|
|
1011
|
-
|
|
1012
|
-
stopPaths(paths: ReadonlyArray<string>): Effect.Effect<void> | undefined {
|
|
1013
|
-
if (paths.length === 0) return undefined
|
|
1014
|
-
if (this.generation !== undefined || (this.generations !== undefined && this.generations.size > 0)) {
|
|
1015
|
-
for (const path of paths) this.retireGeneration(path)
|
|
1016
|
-
}
|
|
1017
|
-
const pathSet = new Set(paths)
|
|
1018
|
-
const effects: Array<Effect.Effect<void>> = []
|
|
1019
|
-
for (const entry of this.registry.children.values()) {
|
|
1020
|
-
if (entry.ownerActive && entry.ownerPath !== undefined && pathSet.has(entry.ownerPath)) {
|
|
1021
|
-
effects.push(this.stopEntry(entry))
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
return effects.length === 0
|
|
1025
|
-
? undefined
|
|
1026
|
-
: effects.length === 1
|
|
1027
|
-
? effects[0]!
|
|
1028
|
-
: Effect.all(effects, { concurrency: "unbounded", discard: true })
|
|
1029
|
-
}
|
|
1030
1133
|
}
|
|
1031
1134
|
|
|
1032
1135
|
const noChildChanges = Stream.succeed(Option.none()).pipe(Stream.concat(Stream.never))
|
|
1033
|
-
export const noParentSend = (_event: unknown): Effect.Effect<void, StoppedError> => Effect.void
|
|
1034
1136
|
export const noInspectInitial = (_paths: ReadonlyArray<string>, _microsteps?: ReadonlyArray<unknown>): void => {}
|
|
1035
1137
|
export const noCausation = (): Inspection.Causation | undefined => undefined
|
|
1036
1138
|
export const EmissionsClosed: unique symbol = Symbol("effect/Machine/EmissionsClosed")
|
|
@@ -1091,18 +1193,16 @@ export const childlessRuntime: ChildRuntime = {
|
|
|
1091
1193
|
get: () => Effect.succeed(Option.none()),
|
|
1092
1194
|
changes: () => noChildChanges,
|
|
1093
1195
|
sendTo: () => Effect.void,
|
|
1196
|
+
deliverTo: () => Effect.void,
|
|
1094
1197
|
stop: () => Effect.void,
|
|
1095
1198
|
owned: {
|
|
1096
1199
|
spawn: () => Effect.die(new Error("Childless machine logic cannot spawn an owned process")),
|
|
1097
|
-
|
|
1098
|
-
beginGeneration: () => ({ current: true }),
|
|
1099
|
-
stopAll: () => Effect.void,
|
|
1100
|
-
stopPaths: () => undefined
|
|
1200
|
+
stopKey: () => Effect.void
|
|
1101
1201
|
}
|
|
1102
1202
|
}
|
|
1103
1203
|
|
|
1104
1204
|
export const makeChildRuntimeSync = (
|
|
1105
|
-
self:
|
|
1205
|
+
self: ProcessEndpoint<any>,
|
|
1106
1206
|
runtime: ProcessRuntime,
|
|
1107
1207
|
services?: Context.Context<any>,
|
|
1108
1208
|
sendAcknowledged?: (
|
|
@@ -1223,7 +1323,7 @@ export const makeChildRuntimeSync = (
|
|
|
1223
1323
|
event: unknown,
|
|
1224
1324
|
source?: Inspection.Subject,
|
|
1225
1325
|
causedBy?: Inspection.Causation
|
|
1226
|
-
): Effect.Effect<void, StoppedError> => {
|
|
1326
|
+
): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> => {
|
|
1227
1327
|
const id = typeof child === "string" ? child : child.id
|
|
1228
1328
|
return Effect.suspend(() => {
|
|
1229
1329
|
if (registry.closed) {
|
|
@@ -1236,6 +1336,24 @@ export const makeChildRuntimeSync = (
|
|
|
1236
1336
|
})
|
|
1237
1337
|
}
|
|
1238
1338
|
|
|
1339
|
+
const deliverTo = (
|
|
1340
|
+
child: ChildSelector,
|
|
1341
|
+
event: unknown,
|
|
1342
|
+
source?: Inspection.Subject,
|
|
1343
|
+
causedBy?: Inspection.Causation
|
|
1344
|
+
): Effect.Effect<void, StoppedError> => {
|
|
1345
|
+
const id = typeof child === "string" ? child : child.id
|
|
1346
|
+
return Effect.suspend(() => {
|
|
1347
|
+
if (registry.closed) {
|
|
1348
|
+
return Effect.void
|
|
1349
|
+
}
|
|
1350
|
+
const entry = registry.children.get(id)
|
|
1351
|
+
return entry !== undefined && matchesChild(entry, child)
|
|
1352
|
+
? deliverToChild(entry.ref, event, source, causedBy)
|
|
1353
|
+
: Effect.void
|
|
1354
|
+
})
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1239
1357
|
const stop = (child: ChildSelector): Effect.Effect<void> => {
|
|
1240
1358
|
const id = typeof child === "string" ? child : child.id
|
|
1241
1359
|
return Effect.suspend(() => {
|
|
@@ -1275,7 +1393,6 @@ export const makeChildRuntimeSync = (
|
|
|
1275
1393
|
readonly [activeSnapshotObserver]?: (
|
|
1276
1394
|
snapshot: Extract<RuntimeSnapshot<ChildState, ChildError, ChildOutput>, { readonly status: "active" }>
|
|
1277
1395
|
) => Effect.Effect<void>
|
|
1278
|
-
readonly [sendParentOverride]?: (event: unknown) => Effect.Effect<void, StoppedError>
|
|
1279
1396
|
}
|
|
1280
1397
|
): Effect.Effect<
|
|
1281
1398
|
MachineRef<ChildState, ChildEvent, ChildError, ChildOutput>,
|
|
@@ -1293,18 +1410,15 @@ export const makeChildRuntimeSync = (
|
|
|
1293
1410
|
readonly [activeSnapshotObserver]?: (
|
|
1294
1411
|
snapshot: Extract<RuntimeSnapshot<any, any, any>, { readonly status: "active" }>
|
|
1295
1412
|
) => Effect.Effect<void>
|
|
1296
|
-
readonly [sendParentOverride]?: (event: unknown) => Effect.Effect<void, StoppedError>
|
|
1297
1413
|
} | { readonly input?: unknown }
|
|
1298
1414
|
): Effect.Effect<MachineRef<any, any, any, any>, any, any> {
|
|
1299
|
-
const descriptor =
|
|
1300
|
-
ChildMachineLogicTypeId in logicOrChild
|
|
1301
|
-
? logicOrChild as unknown as ChildMachine.Any & ChildMachineLogicSlot
|
|
1302
|
-
: undefined
|
|
1415
|
+
const descriptor = isChildMachine(logicOrChild) ? logicOrChild : undefined
|
|
1303
1416
|
const logic = descriptor === undefined
|
|
1304
1417
|
? logicOrChild as ProcessLogic<any, any, any, any, any, any>
|
|
1305
|
-
:
|
|
1418
|
+
: runtime.makeChildLogic(
|
|
1419
|
+
descriptor.machine,
|
|
1306
1420
|
(options as { readonly input?: unknown } | undefined)?.input
|
|
1307
|
-
)
|
|
1421
|
+
)
|
|
1308
1422
|
const spawnOptions = descriptor === undefined
|
|
1309
1423
|
? options as {
|
|
1310
1424
|
readonly id: string
|
|
@@ -1313,7 +1427,6 @@ export const makeChildRuntimeSync = (
|
|
|
1313
1427
|
readonly [activeSnapshotObserver]?: (
|
|
1314
1428
|
snapshot: Extract<RuntimeSnapshot<any, any, any>, { readonly status: "active" }>
|
|
1315
1429
|
) => Effect.Effect<void>
|
|
1316
|
-
readonly [sendParentOverride]?: (event: unknown) => Effect.Effect<void, StoppedError>
|
|
1317
1430
|
} | undefined
|
|
1318
1431
|
: { id: descriptor.id, descriptor }
|
|
1319
1432
|
const token = Symbol()
|
|
@@ -1335,9 +1448,6 @@ export const makeChildRuntimeSync = (
|
|
|
1335
1448
|
...(spawnOptions?.[activeSnapshotObserver] === undefined
|
|
1336
1449
|
? undefined
|
|
1337
1450
|
: { onSnapshot: spawnOptions[activeSnapshotObserver] }),
|
|
1338
|
-
...(spawnOptions?.[sendParentOverride] === undefined
|
|
1339
|
-
? undefined
|
|
1340
|
-
: { sendParent: spawnOptions[sendParentOverride] }),
|
|
1341
1451
|
onReady: (child, requestChildStop) =>
|
|
1342
1452
|
Effect.sync(() => {
|
|
1343
1453
|
startedChild = child
|
|
@@ -1368,13 +1478,14 @@ export const makeChildRuntimeSync = (
|
|
|
1368
1478
|
get,
|
|
1369
1479
|
changes,
|
|
1370
1480
|
sendTo,
|
|
1481
|
+
deliverTo,
|
|
1371
1482
|
stop,
|
|
1372
1483
|
owned: new OwnedChildRuntimeImpl(registry, self, runtime, services, sendAcknowledged)
|
|
1373
1484
|
}
|
|
1374
1485
|
}
|
|
1375
1486
|
|
|
1376
1487
|
export const makeChildRuntime = (
|
|
1377
|
-
self:
|
|
1488
|
+
self: ProcessEndpoint<any>,
|
|
1378
1489
|
runtime: ProcessRuntime,
|
|
1379
1490
|
services?: Context.Context<any>,
|
|
1380
1491
|
sendAcknowledged?: (
|