@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
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Runs Effect machines as persisted Cluster entities.
|
|
3
3
|
*
|
|
4
|
+
* Each live entity keeps one machine instance in memory, which applies requests
|
|
5
|
+
* and runs the effects and timers invoked by active states. {@link Storage} holds
|
|
6
|
+
* the truth: every step commits a checkpoint to it, and an entity that comes up
|
|
7
|
+
* anywhere continues from that checkpoint.
|
|
8
|
+
*
|
|
4
9
|
* @since 0.4.0
|
|
5
10
|
*/
|
|
6
|
-
import type { Entity, MessageStorage, Sharding, Snowflake } from "effect/cluster"
|
|
11
|
+
import type { ClusterError, Entity, EntityId, MessageStorage, Sharding, Snowflake } from "effect/cluster"
|
|
12
|
+
import type * as Duration from "effect/Duration"
|
|
7
13
|
import type * as Effect from "effect/Effect"
|
|
8
14
|
import type * as Layer from "effect/Layer"
|
|
9
15
|
import type * as Option from "effect/Option"
|
|
10
16
|
import type { Rpc } from "effect/rpc"
|
|
17
|
+
import type * as Schedule from "effect/Schedule"
|
|
11
18
|
import type * as Schema from "effect/Schema"
|
|
19
|
+
import type * as Stream from "effect/Stream"
|
|
12
20
|
import * as internal from "../../internal/machine/cluster.js"
|
|
13
21
|
import * as Protocol from "../../internal/machine/clusterProtocol.js"
|
|
14
|
-
import { Accepted, Rejected, Storage } from "../../internal/machine/clusterProtocol.js"
|
|
22
|
+
import { Accepted, MachineFailedError, ReadError, Rejected, Storage } from "../../internal/machine/clusterProtocol.js"
|
|
23
|
+
import * as StorageCheck from "../../internal/machine/clusterStorageCheck.js"
|
|
24
|
+
import { StorageCheckFailure } from "../../internal/machine/clusterStorageCheck.js"
|
|
15
25
|
import type { EnsureExecutable } from "../../internal/machine/readiness.js"
|
|
16
|
-
import type { ExcludeCompatibleRuntime } from "../../internal/machine/requirements.js"
|
|
17
26
|
import type * as Machine from "../../Machine.js"
|
|
18
27
|
|
|
19
28
|
type Snowflake = Snowflake.Snowflake
|
|
@@ -23,25 +32,265 @@ type Snowflake = Snowflake.Snowflake
|
|
|
23
32
|
*
|
|
24
33
|
* **Details**
|
|
25
34
|
*
|
|
26
|
-
*
|
|
27
|
-
* encoded snapshot. The
|
|
28
|
-
*
|
|
35
|
+
* The definition id and deployment version are stored around the generic
|
|
36
|
+
* encoded snapshot. The origin records the input that produced the checkpoint.
|
|
37
|
+
* The revision orders the checkpoints of one entity and decides which
|
|
38
|
+
* checkpoint a commit may replace. The status says whether the machine failed.
|
|
39
|
+
*
|
|
40
|
+
* **Gotchas**
|
|
41
|
+
*
|
|
42
|
+
* Store and return every field unchanged. The bridge assigns them all, and
|
|
43
|
+
* rejects a checkpoint whose revision is not a positive integer, or whose
|
|
44
|
+
* status it does not know or contradicts the snapshot or the machine, as
|
|
45
|
+
* `InvalidCheckpoint`.
|
|
29
46
|
*
|
|
30
47
|
* @category models
|
|
31
48
|
* @since 0.4.0
|
|
32
49
|
*/
|
|
33
50
|
export interface Checkpoint {
|
|
34
|
-
/**
|
|
35
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Stable id of the machine definition that produced the snapshot: `machine.id`
|
|
53
|
+
* when present, otherwise the Cluster entity type.
|
|
54
|
+
*/
|
|
55
|
+
readonly definitionId: string
|
|
36
56
|
|
|
37
57
|
/** Application-controlled deployment or migration version. */
|
|
38
58
|
readonly version: string
|
|
39
59
|
|
|
40
|
-
/**
|
|
41
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Position in the entity's checkpoint history: `1` for its first checkpoint,
|
|
62
|
+
* then one more than the checkpoint it replaces.
|
|
63
|
+
*/
|
|
64
|
+
readonly revision: number
|
|
42
65
|
|
|
43
|
-
/**
|
|
66
|
+
/** What produced this checkpoint from the one before it. */
|
|
67
|
+
readonly origin: Checkpoint.Origin
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Encoded logical machine state and completed outputs. For a failed machine,
|
|
71
|
+
* the last state before the failure.
|
|
72
|
+
*/
|
|
44
73
|
readonly snapshot: Machine.Machine.EncodedSnapshot
|
|
74
|
+
|
|
75
|
+
/** Whether the machine can still take steps, or failed. */
|
|
76
|
+
readonly status: Checkpoint.Status
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Types of the parts of a Cluster machine {@link Checkpoint}.
|
|
81
|
+
*
|
|
82
|
+
* @category models
|
|
83
|
+
* @since 0.48.0
|
|
84
|
+
*/
|
|
85
|
+
export declare namespace Checkpoint {
|
|
86
|
+
/**
|
|
87
|
+
* What produced a checkpoint.
|
|
88
|
+
*
|
|
89
|
+
* **Details**
|
|
90
|
+
*
|
|
91
|
+
* Only a request has an id to deduplicate. A checkpoint of any other origin is
|
|
92
|
+
* guarded by its revision alone.
|
|
93
|
+
*
|
|
94
|
+
* @category models
|
|
95
|
+
* @since 0.48.0
|
|
96
|
+
*/
|
|
97
|
+
export type Origin = Origin.Request | Origin.Invocation | Origin.Timers
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Types for what produced a checkpoint.
|
|
101
|
+
*
|
|
102
|
+
* @category models
|
|
103
|
+
* @since 0.48.0
|
|
104
|
+
*/
|
|
105
|
+
export namespace Origin {
|
|
106
|
+
/**
|
|
107
|
+
* A Cluster request.
|
|
108
|
+
*
|
|
109
|
+
* @category models
|
|
110
|
+
* @since 0.48.0
|
|
111
|
+
*/
|
|
112
|
+
export interface Request {
|
|
113
|
+
readonly _tag: "Request"
|
|
114
|
+
|
|
115
|
+
/** Id that `Storage.commit` deduplicates. */
|
|
116
|
+
readonly requestId: Snowflake
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The end of an invocation, or its failure to start.
|
|
121
|
+
*
|
|
122
|
+
* @category models
|
|
123
|
+
* @since 0.48.0
|
|
124
|
+
*/
|
|
125
|
+
export interface Invocation extends InvocationKey {
|
|
126
|
+
readonly _tag: "Invocation"
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Due times for timers of the state that was stored without one.
|
|
131
|
+
*
|
|
132
|
+
* **Details**
|
|
133
|
+
*
|
|
134
|
+
* No step was taken, and the snapshot is the one before it. This origin
|
|
135
|
+
* occurs when an entity comes up in a state that was stored before the
|
|
136
|
+
* machine declared one of its timers.
|
|
137
|
+
*
|
|
138
|
+
* @category models
|
|
139
|
+
* @since 0.48.0
|
|
140
|
+
*/
|
|
141
|
+
export interface Timers {
|
|
142
|
+
readonly _tag: "Timers"
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Names one invocation of a machine.
|
|
148
|
+
*
|
|
149
|
+
* @category models
|
|
150
|
+
* @since 0.48.0
|
|
151
|
+
*/
|
|
152
|
+
export interface InvocationKey {
|
|
153
|
+
/** Path of the state that owns the invocation. */
|
|
154
|
+
readonly path: string
|
|
155
|
+
|
|
156
|
+
/** Id of the invocation within its state. */
|
|
157
|
+
readonly id: string
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Progress of the invocations of active states, by the path of the state and
|
|
162
|
+
* then by the id of the invocation within it.
|
|
163
|
+
*
|
|
164
|
+
* **Details**
|
|
165
|
+
*
|
|
166
|
+
* An effect that should run has no entry. A timer has an entry from the step
|
|
167
|
+
* that enters its state, unless its duration cannot be evaluated, which fails
|
|
168
|
+
* the machine. An entry is removed when its state exits, and no entry is
|
|
169
|
+
* `Waiting` once the machine is final.
|
|
170
|
+
*
|
|
171
|
+
* @category models
|
|
172
|
+
* @since 0.48.0
|
|
173
|
+
*/
|
|
174
|
+
export interface Invocations {
|
|
175
|
+
readonly [path: string]: { readonly [id: string]: Progress }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* How far one invocation of an active state has come.
|
|
180
|
+
*
|
|
181
|
+
* @category models
|
|
182
|
+
* @since 0.48.0
|
|
183
|
+
*/
|
|
184
|
+
export type Progress = Progress.Settled | Progress.Waiting | Progress.Child
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Types for the progress of an invocation.
|
|
188
|
+
*
|
|
189
|
+
* @category models
|
|
190
|
+
* @since 0.48.0
|
|
191
|
+
*/
|
|
192
|
+
export namespace Progress {
|
|
193
|
+
/**
|
|
194
|
+
* The invocation ended while its state stayed active. It does not run again
|
|
195
|
+
* until that state is entered again.
|
|
196
|
+
*
|
|
197
|
+
* @category models
|
|
198
|
+
* @since 0.48.0
|
|
199
|
+
*/
|
|
200
|
+
export interface Settled {
|
|
201
|
+
readonly _tag: "Settled"
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The invocation is a timer that has not completed.
|
|
206
|
+
*
|
|
207
|
+
* @category models
|
|
208
|
+
* @since 0.48.0
|
|
209
|
+
*/
|
|
210
|
+
export interface Waiting {
|
|
211
|
+
readonly _tag: "Waiting"
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* When the timer completes, in milliseconds since the Unix epoch. An
|
|
215
|
+
* integer that a date can name, which is the last such instant for a
|
|
216
|
+
* duration that ends later or never.
|
|
217
|
+
*/
|
|
218
|
+
readonly dueAt: number
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The invocation is a child machine, which runs as an entity of its own
|
|
223
|
+
* and has not ended.
|
|
224
|
+
*
|
|
225
|
+
* @category models
|
|
226
|
+
* @since 0.48.0
|
|
227
|
+
*/
|
|
228
|
+
export interface Child {
|
|
229
|
+
readonly _tag: "Child"
|
|
230
|
+
|
|
231
|
+
/** Entity id of the child, unique to this activation of its state. */
|
|
232
|
+
readonly entityId: string
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Whether the machine can still take steps, or failed.
|
|
238
|
+
*
|
|
239
|
+
* @category models
|
|
240
|
+
* @since 0.48.0
|
|
241
|
+
*/
|
|
242
|
+
export type Status = Status.Active | Status.Failed | Status.Stopped
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Types for the status of a checkpoint.
|
|
246
|
+
*
|
|
247
|
+
* @category models
|
|
248
|
+
* @since 0.48.0
|
|
249
|
+
*/
|
|
250
|
+
export namespace Status {
|
|
251
|
+
/**
|
|
252
|
+
* The machine has not failed. A machine in a final state is active too: its
|
|
253
|
+
* snapshot says that it is final.
|
|
254
|
+
*
|
|
255
|
+
* @category models
|
|
256
|
+
* @since 0.48.0
|
|
257
|
+
*/
|
|
258
|
+
export interface Active {
|
|
259
|
+
readonly _tag: "Active"
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Timers that wait, and invocations that ended while their state stayed
|
|
263
|
+
* active.
|
|
264
|
+
*/
|
|
265
|
+
readonly invocations: Invocations
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* The machine failed. It is terminal, runs no work, and rejects every later
|
|
270
|
+
* request as `MachineFailed`.
|
|
271
|
+
*
|
|
272
|
+
* @category models
|
|
273
|
+
* @since 0.48.0
|
|
274
|
+
*/
|
|
275
|
+
export interface Failed {
|
|
276
|
+
readonly _tag: "Failed"
|
|
277
|
+
|
|
278
|
+
/** Human-readable rendering of the failure, of bounded length. */
|
|
279
|
+
readonly failure: string
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* The machine is a child that its parent stopped, because the state that
|
|
284
|
+
* invoked it exited. It is terminal, runs no work, and rejects every later
|
|
285
|
+
* request as `MachineFailed`.
|
|
286
|
+
*
|
|
287
|
+
* @category models
|
|
288
|
+
* @since 0.48.0
|
|
289
|
+
*/
|
|
290
|
+
export interface Stopped {
|
|
291
|
+
readonly _tag: "Stopped"
|
|
292
|
+
}
|
|
293
|
+
}
|
|
45
294
|
}
|
|
46
295
|
|
|
47
296
|
/**
|
|
@@ -51,7 +300,8 @@ export interface Checkpoint {
|
|
|
51
300
|
*
|
|
52
301
|
* `processed` reports whether the exact Cluster request id was already
|
|
53
302
|
* committed. Storage implementations must retain enough request ids to detect
|
|
54
|
-
* redelivery even after later requests have advanced the
|
|
303
|
+
* redelivery even after later requests and invocations have advanced the
|
|
304
|
+
* checkpoint.
|
|
55
305
|
*
|
|
56
306
|
* @category models
|
|
57
307
|
* @since 0.4.0
|
|
@@ -65,12 +315,19 @@ export interface LoadResult {
|
|
|
65
315
|
}
|
|
66
316
|
|
|
67
317
|
/**
|
|
68
|
-
* Result of atomically committing a Cluster machine
|
|
318
|
+
* Result of atomically committing a Cluster machine checkpoint.
|
|
319
|
+
*
|
|
320
|
+
* **Details**
|
|
321
|
+
*
|
|
322
|
+
* The results are ordered. `Duplicate` takes precedence over
|
|
323
|
+
* `RevisionConflict`, so a redelivered request is recognized after later
|
|
324
|
+
* checkpoints have advanced the revision. Only a checkpoint whose origin is a
|
|
325
|
+
* request can be a `Duplicate`.
|
|
69
326
|
*
|
|
70
327
|
* @category models
|
|
71
328
|
* @since 0.4.0
|
|
72
329
|
*/
|
|
73
|
-
export type CommitResult = CommitResult.Committed | CommitResult.Duplicate
|
|
330
|
+
export type CommitResult = CommitResult.Committed | CommitResult.Duplicate | CommitResult.RevisionConflict
|
|
74
331
|
|
|
75
332
|
/**
|
|
76
333
|
* Constructors and types for Cluster machine commit results.
|
|
@@ -81,6 +338,7 @@ export type CommitResult = CommitResult.Committed | CommitResult.Duplicate
|
|
|
81
338
|
export const CommitResult: {
|
|
82
339
|
Committed: () => CommitResult.Committed
|
|
83
340
|
Duplicate: () => CommitResult.Duplicate
|
|
341
|
+
RevisionConflict: () => CommitResult.RevisionConflict
|
|
84
342
|
} = Protocol.CommitResult
|
|
85
343
|
|
|
86
344
|
/**
|
|
@@ -91,7 +349,9 @@ export const CommitResult: {
|
|
|
91
349
|
*/
|
|
92
350
|
export declare namespace CommitResult {
|
|
93
351
|
/**
|
|
94
|
-
* Indicates that the
|
|
352
|
+
* Indicates that the checkpoint was committed, atomically with the request id
|
|
353
|
+
* of its origin when it has one. The checkpoint directly followed the stored
|
|
354
|
+
* one.
|
|
95
355
|
*
|
|
96
356
|
* @category models
|
|
97
357
|
* @since 0.4.0
|
|
@@ -101,7 +361,8 @@ export declare namespace CommitResult {
|
|
|
101
361
|
}
|
|
102
362
|
|
|
103
363
|
/**
|
|
104
|
-
* Indicates that the request id was already committed
|
|
364
|
+
* Indicates that the request id was already committed, whatever the revision
|
|
365
|
+
* of the checkpoint. Nothing was written.
|
|
105
366
|
*
|
|
106
367
|
* @category models
|
|
107
368
|
* @since 0.4.0
|
|
@@ -109,6 +370,17 @@ export declare namespace CommitResult {
|
|
|
109
370
|
export interface Duplicate {
|
|
110
371
|
readonly _tag: "Duplicate"
|
|
111
372
|
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Indicates that the origin was not a request committed before, and that the
|
|
376
|
+
* stored checkpoint is not the one this commit replaces. Nothing was written.
|
|
377
|
+
*
|
|
378
|
+
* @category models
|
|
379
|
+
* @since 0.48.0
|
|
380
|
+
*/
|
|
381
|
+
export interface RevisionConflict {
|
|
382
|
+
readonly _tag: "RevisionConflict"
|
|
383
|
+
}
|
|
112
384
|
}
|
|
113
385
|
|
|
114
386
|
/**
|
|
@@ -117,10 +389,48 @@ export declare namespace CommitResult {
|
|
|
117
389
|
* **When to use**
|
|
118
390
|
*
|
|
119
391
|
* Use to connect `ClusterMachine` to a durable checkpoint store that can
|
|
120
|
-
* atomically deduplicate request ids and
|
|
392
|
+
* atomically deduplicate request ids and compare-and-set the current
|
|
393
|
+
* checkpoint.
|
|
394
|
+
*
|
|
395
|
+
* **Details**
|
|
396
|
+
*
|
|
397
|
+
* `loadForRequest` returns the latest checkpoint of an entity and whether a
|
|
398
|
+
* request id was already committed for it. `load` returns the checkpoint alone,
|
|
399
|
+
* for the inputs that are not requests: an entity that wakes up, and the end of
|
|
400
|
+
* an invocation.
|
|
401
|
+
*
|
|
402
|
+
* `commit` decides and writes atomically, in this order:
|
|
403
|
+
*
|
|
404
|
+
* 1. `Duplicate` when `checkpoint.origin` is a request whose id was already
|
|
405
|
+
* committed, whatever the revision. A checkpoint of any other origin is never
|
|
406
|
+
* a duplicate.
|
|
407
|
+
* 2. `RevisionConflict` when the stored revision is not exactly
|
|
408
|
+
* `checkpoint.revision - 1`. An entity without a checkpoint has revision
|
|
409
|
+
* `0`, so its first checkpoint has revision `1`.
|
|
410
|
+
* 3. `Committed` otherwise. The checkpoint replaces the stored one, and the
|
|
411
|
+
* request id of its origin, when it has one, is recorded with it.
|
|
412
|
+
*
|
|
413
|
+
* Each live entity loads its checkpoint once and keeps the decoded state in
|
|
414
|
+
* memory, so most inputs only call `commit`. Storage remains the truth: memory
|
|
415
|
+
* advances only after the whole transaction succeeded, and is loaded again
|
|
416
|
+
* after any other outcome. Before a request is rejected, `loadForRequest` is
|
|
417
|
+
* called to recognize a redelivery that was already committed.
|
|
418
|
+
*
|
|
419
|
+
* The due time of a timer is part of the checkpoint that enters its state, so
|
|
420
|
+
* storing the checkpoint whole is all that timers require. An entity that loads a
|
|
421
|
+
* state with a timer without a due time, which an earlier deployment can leave
|
|
422
|
+
* behind, commits one more checkpoint before it starts any work. Its origin is
|
|
423
|
+
* `Checkpoint.Origin.Timers`, and `commit` treats it as any origin that is not a
|
|
424
|
+
* request.
|
|
121
425
|
*
|
|
122
426
|
* **Gotchas**
|
|
123
427
|
*
|
|
428
|
+
* The revision check is what stops an entity that lost its shard, and still
|
|
429
|
+
* holds an older state, from overwriting a newer checkpoint. It must be part of
|
|
430
|
+
* the same atomic write, such as an update conditional on the stored revision
|
|
431
|
+
* together with an insert that fails when a checkpoint exists. Comparing in a
|
|
432
|
+
* separate read is not enough.
|
|
433
|
+
*
|
|
124
434
|
* For checkpoint and emitted-message persistence to commit atomically, this
|
|
125
435
|
* service must join the transaction opened through the configured
|
|
126
436
|
* `MessageStorage`. The reply is persisted after that transaction; storing the
|
|
@@ -128,6 +438,9 @@ export declare namespace CommitResult {
|
|
|
128
438
|
* applying the transition again. A separate transaction or database cannot
|
|
129
439
|
* provide checkpoint-and-emission atomicity.
|
|
130
440
|
*
|
|
441
|
+
* A failed operation never fails the machine. An input that is not a request is
|
|
442
|
+
* retried for as long as the entity lives.
|
|
443
|
+
*
|
|
131
444
|
* @category services
|
|
132
445
|
* @since 0.4.0
|
|
133
446
|
*/
|
|
@@ -146,6 +459,21 @@ export { Accepted }
|
|
|
146
459
|
* Schema for reasons a Cluster machine request can be rejected without
|
|
147
460
|
* advancing its checkpoint.
|
|
148
461
|
*
|
|
462
|
+
* **Details**
|
|
463
|
+
*
|
|
464
|
+
* `InvalidEvent` reports an event that {@link Client} refused before sending
|
|
465
|
+
* it, because its schema or the protocol of the machine refuses it. The
|
|
466
|
+
* machine never sees such an event.
|
|
467
|
+
*
|
|
468
|
+
* `DefinitionIdMismatch` reports a checkpoint whose `definitionId` is not the id
|
|
469
|
+
* of the machine that loaded it. `PersistenceFailure` reports storage that
|
|
470
|
+
* failed. `RevisionConflict` reports storage that worked and refused the request,
|
|
471
|
+
* because another instance of the entity advanced the checkpoint both times the
|
|
472
|
+
* request was applied.
|
|
473
|
+
* `MachineFailed` reports a machine whose stored status is `Failed`, and its
|
|
474
|
+
* message is the stored failure. A request that was committed before the
|
|
475
|
+
* failure is still answered as accepted.
|
|
476
|
+
*
|
|
149
477
|
* @category models
|
|
150
478
|
* @since 0.4.0
|
|
151
479
|
*/
|
|
@@ -163,6 +491,11 @@ export type RejectionReason = typeof RejectionReason.Type
|
|
|
163
491
|
* Rejected Cluster machine request. Transaction-participating durable storage
|
|
164
492
|
* leaves the previous checkpoint in place and suppresses emitted events.
|
|
165
493
|
*
|
|
494
|
+
* **Details**
|
|
495
|
+
*
|
|
496
|
+
* It is a yieldable error, which {@link Client} reports in its error channel,
|
|
497
|
+
* and the raw `send` RPC of the entity returns as a value of {@link SendResult}.
|
|
498
|
+
*
|
|
166
499
|
* @category models
|
|
167
500
|
* @since 0.4.0
|
|
168
501
|
*/
|
|
@@ -186,14 +519,87 @@ type MachineEvents<M extends Machine.Machine.Any> = Machine.Machine.InputEvents<
|
|
|
186
519
|
|
|
187
520
|
type MachineEmits<M extends Machine.Machine.Any> = Machine.Machine.EmittedEvents<M>
|
|
188
521
|
|
|
522
|
+
/**
|
|
523
|
+
* Committed checkpoint passed to {@link LayerOptions.onCommit} inside the same
|
|
524
|
+
* `MessageStorage` transaction as the checkpoint write.
|
|
525
|
+
*
|
|
526
|
+
* @category models
|
|
527
|
+
* @since 0.49.0
|
|
528
|
+
*/
|
|
529
|
+
export interface CommitEvent<M extends Machine.Machine.Any> {
|
|
530
|
+
/** Entity id of the machine whose checkpoint was committed. */
|
|
531
|
+
readonly entityId: string
|
|
532
|
+
|
|
533
|
+
/** Stored checkpoint that was just committed. */
|
|
534
|
+
readonly checkpoint: Checkpoint
|
|
535
|
+
|
|
536
|
+
/** Decoded snapshot matching the committed checkpoint. */
|
|
537
|
+
readonly snapshot: Snapshot<M>
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Options for {@link ClusterMachine.toLayer}, combining transactional commit
|
|
542
|
+
* hooks with standard Effect Cluster `Entity.toLayer` tuning options.
|
|
543
|
+
*
|
|
544
|
+
* @category models
|
|
545
|
+
* @since 0.49.0
|
|
546
|
+
*/
|
|
547
|
+
export interface LayerOptions<M extends Machine.Machine.Any, RE = never, RC = never> {
|
|
548
|
+
/**
|
|
549
|
+
* Durably enqueues emitted events inside the checkpoint's `MessageStorage`
|
|
550
|
+
* transaction.
|
|
551
|
+
*/
|
|
552
|
+
readonly enqueue?: (
|
|
553
|
+
event: Machine.Machine.EmittedEventOf<MachineEmits<M>>
|
|
554
|
+
) => Effect.Effect<void, unknown, RE>
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Runs inside the checkpoint's `MessageStorage` transaction right after a
|
|
558
|
+
* checkpoint is committed, for projections, read models, or audit tables that
|
|
559
|
+
* must commit or roll back atomically with the machine state.
|
|
560
|
+
*/
|
|
561
|
+
readonly onCommit?: (
|
|
562
|
+
event: CommitEvent<M>
|
|
563
|
+
) => Effect.Effect<void, unknown, RC>
|
|
564
|
+
|
|
565
|
+
/** Overrides `ShardingConfig.entityMaxIdleTime` for this entity (and its children). */
|
|
566
|
+
readonly maxIdleTime?: Duration.Input | undefined
|
|
567
|
+
|
|
568
|
+
/** Concurrency forwarded to `Entity.toLayer` for this entity (and its children). */
|
|
569
|
+
readonly concurrency?: number | "unbounded" | undefined
|
|
570
|
+
|
|
571
|
+
/** Overrides `ShardingConfig.entityMailboxCapacity` for this entity (and its children). */
|
|
572
|
+
readonly mailboxCapacity?: number | "unbounded" | undefined
|
|
573
|
+
|
|
574
|
+
/** Enables Cluster mailbox metrics for this entity (and its children). */
|
|
575
|
+
readonly mailboxMetrics?: boolean | undefined
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* When `true`, a handler defect fails only its request instead of restarting
|
|
579
|
+
* the entity and replaying its in-flight requests.
|
|
580
|
+
*/
|
|
581
|
+
readonly disableFatalDefects?: boolean | undefined
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Schedule for restarting an entity after a defect and for retrying failed
|
|
585
|
+
* handler construction.
|
|
586
|
+
*/
|
|
587
|
+
readonly defectRetryPolicy?: Schedule.Schedule<any, unknown> | undefined
|
|
588
|
+
|
|
589
|
+
/** Tracing span attributes attached to entity requests. */
|
|
590
|
+
readonly spanAttributes?: Record<string, string> | undefined
|
|
591
|
+
}
|
|
592
|
+
|
|
189
593
|
/**
|
|
190
594
|
* Cluster adapter for one machine definition and entity type.
|
|
191
595
|
*
|
|
192
596
|
* **Details**
|
|
193
597
|
*
|
|
194
|
-
* The adapter exposes one persisted `send` RPC
|
|
195
|
-
*
|
|
196
|
-
* the
|
|
598
|
+
* The adapter exposes one persisted `send` RPC, and {@link Client}s that send
|
|
599
|
+
* through it as a local reference sends. Each live entity keeps one
|
|
600
|
+
* machine instance in memory, which applies requests and the outcomes of invoked
|
|
601
|
+
* effects and timers one at a time. Every accepted request advances the
|
|
602
|
+
* checkpoint at most once.
|
|
197
603
|
*
|
|
198
604
|
* @category models
|
|
199
605
|
* @since 0.4.0
|
|
@@ -206,35 +612,369 @@ export interface ClusterMachine<
|
|
|
206
612
|
/** Machine definition executed by each entity instance. */
|
|
207
613
|
readonly machine: M
|
|
208
614
|
|
|
209
|
-
/**
|
|
615
|
+
/**
|
|
616
|
+
* Cluster entity exposing the persisted, schema-validated `send` RPC. The
|
|
617
|
+
* entity also answers the delayed messages that wake it for its long timers,
|
|
618
|
+
* which only the machine itself sends, so its type leaves them out.
|
|
619
|
+
*/
|
|
210
620
|
readonly entity: Entity.Entity<Type, SendRpc<MachineEvents<M>>>
|
|
211
621
|
|
|
622
|
+
/**
|
|
623
|
+
* Creates clients for the entities of this machine, one per entity id, like
|
|
624
|
+
* `Entity.client`.
|
|
625
|
+
*
|
|
626
|
+
* **Example** (Sending an event built by a constructor)
|
|
627
|
+
*
|
|
628
|
+
* ```ts import.meta.vitest
|
|
629
|
+
* import { Machine } from "@typeonce/effect-machine"
|
|
630
|
+
* import { ClusterMachine } from "@typeonce/effect-machine/cluster"
|
|
631
|
+
* import { Effect, Schema } from "effect"
|
|
632
|
+
*
|
|
633
|
+
* const Events = Machine.events({ Rename: { name: Schema.NonEmptyString } })
|
|
634
|
+
* const machine = Machine.make({
|
|
635
|
+
* root: Machine.state({ states: { Draft: { fields: { name: Schema.String } } } }),
|
|
636
|
+
* events: Events
|
|
637
|
+
* }).handle({
|
|
638
|
+
* initial: { target: "Draft", data: { name: "untitled" } },
|
|
639
|
+
* states: { Draft: { on: { Rename: { target: "Draft", data: ({ event }) => ({ name: event.name }) } } } }
|
|
640
|
+
* })
|
|
641
|
+
*
|
|
642
|
+
* const adapter = ClusterMachine.make("Document", machine, { version: "1" })
|
|
643
|
+
*
|
|
644
|
+
* // Run with a Cluster runner, which provides `Sharding`.
|
|
645
|
+
* const rename = Effect.gen(function*() {
|
|
646
|
+
* const client = (yield* adapter.client)("document-1")
|
|
647
|
+
* yield* client.send(Events.Rename({ name: "report" })).pipe(
|
|
648
|
+
* Effect.catchTag("Rejected", (rejected) => Effect.logWarning(rejected.reason))
|
|
649
|
+
* )
|
|
650
|
+
* })
|
|
651
|
+
*
|
|
652
|
+
* Effect.isEffect(rename) // => true
|
|
653
|
+
* ```
|
|
654
|
+
*
|
|
655
|
+
* @since 0.48.0
|
|
656
|
+
*/
|
|
657
|
+
readonly client: Effect.Effect<(entityId: string) => Client<M>, never, Sharding.Sharding>
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Names the entity of this machine with `entityId`, as a {@link Target} that
|
|
661
|
+
* another Cluster machine sends to with `enqueue.sendTo`.
|
|
662
|
+
*
|
|
663
|
+
* **Example** (Sending to another Cluster machine)
|
|
664
|
+
*
|
|
665
|
+
* ```ts import.meta.vitest
|
|
666
|
+
* import { Machine } from "@typeonce/effect-machine"
|
|
667
|
+
* import { ClusterMachine } from "@typeonce/effect-machine/cluster"
|
|
668
|
+
* import { Schema } from "effect"
|
|
669
|
+
*
|
|
670
|
+
* const Ledger = ClusterMachine.make(
|
|
671
|
+
* "Ledger",
|
|
672
|
+
* Machine.make({
|
|
673
|
+
* root: Machine.state({ states: { Open: { fields: { total: Schema.Number } } } }),
|
|
674
|
+
* events: Machine.events({ Add: { amount: Schema.Number } })
|
|
675
|
+
* }).handle({
|
|
676
|
+
* initial: { target: "Open", data: { total: 0 } },
|
|
677
|
+
* states: {
|
|
678
|
+
* Open: { on: { Add: { update: { Open: ({ event, state }) => ({ total: state.total + event.amount }) } } } }
|
|
679
|
+
* }
|
|
680
|
+
* }),
|
|
681
|
+
* { version: "1" }
|
|
682
|
+
* )
|
|
683
|
+
*
|
|
684
|
+
* const Order = ClusterMachine.make(
|
|
685
|
+
* "Order",
|
|
686
|
+
* Machine.make({
|
|
687
|
+
* root: Machine.state({ states: { Open: { fields: { ledger: Schema.String } }, Paid: {} } }),
|
|
688
|
+
* events: Machine.events({ Pay: { amount: Schema.Number } })
|
|
689
|
+
* }).handle({
|
|
690
|
+
* initial: { target: "Open", data: { ledger: "ledger-1" } },
|
|
691
|
+
* states: {
|
|
692
|
+
* Open: {
|
|
693
|
+
* on: {
|
|
694
|
+
* Pay: {
|
|
695
|
+
* target: "Paid",
|
|
696
|
+
* // Saved with the checkpoint of this step, and applied once.
|
|
697
|
+
* commands: ({ event, state }, enqueue) =>
|
|
698
|
+
* enqueue.sendTo(Ledger.target(state.ledger), { _tag: "Add", amount: event.amount })
|
|
699
|
+
* }
|
|
700
|
+
* }
|
|
701
|
+
* },
|
|
702
|
+
* Paid: {}
|
|
703
|
+
* }
|
|
704
|
+
* }),
|
|
705
|
+
* { version: "1" }
|
|
706
|
+
* )
|
|
707
|
+
*
|
|
708
|
+
* Order.entity.type // => "Order"
|
|
709
|
+
* ```
|
|
710
|
+
*
|
|
711
|
+
* @since 0.48.0
|
|
712
|
+
*/
|
|
713
|
+
readonly target: (entityId: string) => Target<Machine.Machine.EventInputOf<MachineEvents<M>>>
|
|
714
|
+
|
|
212
715
|
/**
|
|
213
716
|
* Creates the Cluster entity layer for this machine.
|
|
214
717
|
*
|
|
215
718
|
* **Gotchas**
|
|
216
719
|
*
|
|
217
|
-
* `enqueue`
|
|
218
|
-
* transaction.
|
|
720
|
+
* `enqueue` and `onCommit` run after the checkpoint write in the same
|
|
721
|
+
* `MessageStorage` transaction. Use them only for work that joins that
|
|
722
|
+
* transaction (such as durable outbox writes or SQL read-model projections);
|
|
219
723
|
* arbitrary external effects are not atomic with the checkpoint. The reply
|
|
220
724
|
* is persisted after commit and recovered through request-id deduplication if
|
|
221
725
|
* delivery is interrupted. Machines that never emit may omit `enqueue`.
|
|
222
726
|
*
|
|
727
|
+
* An `enqueue` failure rejects the request as `EmissionFailure`; an
|
|
728
|
+
* `onCommit` failure rejects it as `PersistenceFailure` and rolls back the
|
|
729
|
+
* transaction. For steps triggered by an invoked effect or timer, both are
|
|
730
|
+
* retried until stored. Entity tuning options (`maxIdleTime`, `concurrency`,
|
|
731
|
+
* `mailboxCapacity`, `mailboxMetrics`, `disableFatalDefects`,
|
|
732
|
+
* `defectRetryPolicy`, `spanAttributes`) are forwarded to `Entity.toLayer`
|
|
733
|
+
* for this entity and its child entities.
|
|
734
|
+
*
|
|
223
735
|
* @since 0.4.0
|
|
224
736
|
*/
|
|
225
|
-
readonly toLayer: <
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
) => Effect.Effect<void, unknown, R>
|
|
229
|
-
}) => Layer.Layer<never, never, Storage | MessageStorage.MessageStorage | Sharding.Sharding | R | Services>
|
|
737
|
+
readonly toLayer: <RE = never, RC = never>(
|
|
738
|
+
options?: LayerOptions<M, RE, RC>
|
|
739
|
+
) => Layer.Layer<never, never, Storage | MessageStorage.MessageStorage | Sharding.Sharding | RE | RC | Services>
|
|
230
740
|
}
|
|
231
741
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
742
|
+
/**
|
|
743
|
+
* Failures of Cluster that keep a request from reaching an entity or its reply
|
|
744
|
+
* from reaching the caller.
|
|
745
|
+
*
|
|
746
|
+
* @category errors
|
|
747
|
+
* @since 0.48.0
|
|
748
|
+
*/
|
|
749
|
+
export type ClientError =
|
|
750
|
+
| ClusterError.MailboxFull
|
|
751
|
+
| ClusterError.AlreadyProcessingMessage
|
|
752
|
+
| ClusterError.PersistenceError
|
|
753
|
+
| ClusterError.EntityNotAssignedToRunner
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* A Cluster machine named by its entity type and id, as Cluster addresses an
|
|
757
|
+
* entity, that a model sends to.
|
|
758
|
+
*
|
|
759
|
+
* **Details**
|
|
760
|
+
*
|
|
761
|
+
* `target` of an adapter makes one, and a Cluster machine is given one for its
|
|
762
|
+
* own entity as `self`. A step that sends to it with `enqueue.sendTo` saves the
|
|
763
|
+
* event as a persisted request to the entity, in the same `MessageStorage`
|
|
764
|
+
* transaction as its checkpoint, so the event is sent if and only if the step
|
|
765
|
+
* is stored. The request has an id of its own, which the target records when it
|
|
766
|
+
* applies the request, so Cluster may deliver it again and it is applied once.
|
|
767
|
+
* Nobody waits for its answer, so the target treats it as a local machine
|
|
768
|
+
* treats an event sent to it: an event it cannot apply, such as one whose
|
|
769
|
+
* transition throws, fails it, and a target that ended drops the event. A
|
|
770
|
+
* failure of storage is retried.
|
|
771
|
+
*
|
|
772
|
+
* `send` sends through the client of the target, from the work of a Cluster
|
|
773
|
+
* machine or from any effect that has `Sharding`, and waits for the target to
|
|
774
|
+
* accept the event. It sends again when a runner cannot take the request, and
|
|
775
|
+
* fails with `StoppedError` once the target failed. Work runs at least once, so
|
|
776
|
+
* an event that work sends may be sent more than once.
|
|
777
|
+
*
|
|
778
|
+
* **Gotchas**
|
|
779
|
+
*
|
|
780
|
+
* The types check the event against the input events of the target, but not
|
|
781
|
+
* that the target was made by this module: a step on Cluster that sends to a
|
|
782
|
+
* local reference is rejected as `UnsupportedProcessLocal`, and so is
|
|
783
|
+
* `enqueue.stop` with a local child address (whereas `enqueue.stop` with the
|
|
784
|
+
* descriptor of an invoked child stops that child). A target made here is not a
|
|
785
|
+
* local reference either, so a local machine cannot send to it.
|
|
786
|
+
*
|
|
787
|
+
* The runner that stores the step reads the request at once. A target owned by
|
|
788
|
+
* another runner reads it at its next poll of `MessageStorage`, after up to
|
|
789
|
+
* `entityMessagePollInterval`.
|
|
790
|
+
*
|
|
791
|
+
* Its `sessionId` is the address of the entity, `Type/entityId`, which stays
|
|
792
|
+
* the same across runners and restarts.
|
|
793
|
+
*
|
|
794
|
+
* @category models
|
|
795
|
+
* @since 0.48.0
|
|
796
|
+
*/
|
|
797
|
+
export interface Target<in Event> extends Machine.MachineTarget<Event> {
|
|
798
|
+
readonly "~effect/cluster/ClusterMachine/Target": "~effect/cluster/ClusterMachine/Target"
|
|
799
|
+
|
|
800
|
+
/** Entity type of the machine. */
|
|
801
|
+
readonly entityType: string
|
|
802
|
+
|
|
803
|
+
/** Entity id of the machine. */
|
|
804
|
+
readonly entityId: string
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* A client for one Cluster machine, addressed by its entity id.
|
|
809
|
+
*
|
|
810
|
+
* **Details**
|
|
811
|
+
*
|
|
812
|
+
* `send` takes what `send` of a local machine reference takes: an event of the
|
|
813
|
+
* machine's input protocol, as a plain value or as built by a constructor of
|
|
814
|
+
* `Machine.events`. It decodes the event as a local reference does, sends it
|
|
815
|
+
* through the persisted `send` RPC of the entity, and succeeds once the request
|
|
816
|
+
* is committed or recognized as a redelivery.
|
|
817
|
+
*
|
|
818
|
+
* An event that its schema refuses is rejected as `InvalidEvent` before it is
|
|
819
|
+
* sent, and the machine never sees it. A request that the machine rejects fails
|
|
820
|
+
* with the same {@link Rejected}, carrying its reason. A transition that throws
|
|
821
|
+
* is such a rejection, because the client waits for the step: by the rule that
|
|
822
|
+
* `Machine.MachineRef` states for `send`, the error goes to the sender, and the
|
|
823
|
+
* machine goes on.
|
|
824
|
+
*
|
|
825
|
+
* `snapshot`, `changes`, and `join` read the machine from {@link Storage}, which
|
|
826
|
+
* is the truth, and never ask the entity. A read works from any process that
|
|
827
|
+
* reaches the store, whichever runner owns the entity and whether it is in
|
|
828
|
+
* memory or not. It does not wake the entity or keep it alive, so a machine that
|
|
829
|
+
* is passivated, moved, or recovered after a restart is read the same way.
|
|
830
|
+
*
|
|
831
|
+
* **Gotchas**
|
|
832
|
+
*
|
|
833
|
+
* Failures of Cluster itself, such as a full mailbox, are {@link ClientError}s.
|
|
834
|
+
* A request that ended in one may still have been delivered, and is delivered at
|
|
835
|
+
* most once when it is sent again.
|
|
836
|
+
*
|
|
837
|
+
* A remote view is narrower than a local `MachineRef`. Reads see committed
|
|
838
|
+
* checkpoints only, and `changes` reads again after each `readInterval`, so it
|
|
839
|
+
* may skip checkpoints committed between two reads and sees a change up to one
|
|
840
|
+
* interval late. A failed machine carries the text of its failure, not a
|
|
841
|
+
* `Cause`. Only a child machine is ever `stopped`, by its parent, and nothing
|
|
842
|
+
* is read before the first event is committed.
|
|
843
|
+
*
|
|
844
|
+
* @category models
|
|
845
|
+
* @since 0.48.0
|
|
846
|
+
*/
|
|
847
|
+
export interface Client<M extends Machine.Machine.Any> {
|
|
848
|
+
/** Entity id of the machine this client addresses. */
|
|
849
|
+
readonly entityId: string
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Starts the entity with its initial input, committing its initial state when
|
|
853
|
+
* it has no checkpoint yet. Succeeds as a no-op if the entity is already
|
|
854
|
+
* active, and rejects with `MachineFailed` if it failed or was stopped.
|
|
855
|
+
*/
|
|
856
|
+
readonly start: (
|
|
857
|
+
...input: [...Machine.Machine.InputArgs<Machine.Machine.InputSchema<M>>]
|
|
858
|
+
) => Effect.Effect<void, Rejected | ClientError>
|
|
859
|
+
|
|
860
|
+
/** Sends one input event and waits until it is committed or rejected. */
|
|
861
|
+
readonly send: (
|
|
862
|
+
event: Machine.Machine.EventInputOf<MachineEvents<M>>
|
|
863
|
+
) => Effect.Effect<void, Rejected | ClientError>
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* Reads the latest committed checkpoint, or `None` when the machine has not
|
|
867
|
+
* committed one yet.
|
|
868
|
+
*/
|
|
869
|
+
readonly snapshot: Effect.Effect<
|
|
870
|
+
Option.Option<Snapshot<M>>,
|
|
871
|
+
ReadError | ClusterError.PersistenceError,
|
|
872
|
+
ReadServices<M>
|
|
873
|
+
>
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Streams the latest committed checkpoint and the later ones it reads, once
|
|
877
|
+
* each, and ends after a `done` or `failed` one. It waits while the machine
|
|
878
|
+
* has committed none.
|
|
879
|
+
*/
|
|
880
|
+
readonly changes: Stream.Stream<Snapshot<M>, ReadError | ClusterError.PersistenceError, ReadServices<M>>
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Waits for the output of the machine, or fails with
|
|
884
|
+
* {@link MachineFailedError} when it failed.
|
|
885
|
+
*/
|
|
886
|
+
readonly join: Effect.Effect<
|
|
887
|
+
Machine.Machine.Output<M>,
|
|
888
|
+
MachineFailedError | ReadError | ClusterError.PersistenceError,
|
|
889
|
+
ReadServices<M>
|
|
237
890
|
>
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
/**
|
|
894
|
+
* Services that reading a Cluster machine from outside requires: the
|
|
895
|
+
* {@link Storage} that holds its checkpoints, and what its state schemas need
|
|
896
|
+
* to decode.
|
|
897
|
+
*
|
|
898
|
+
* @category models
|
|
899
|
+
* @since 0.48.0
|
|
900
|
+
*/
|
|
901
|
+
export type ReadServices<M extends Machine.Machine.Any> =
|
|
902
|
+
| Storage
|
|
903
|
+
| Machine.Machine.SnapshotDecodingServices<Machine.Machine.States<M>>
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* A committed checkpoint of a Cluster machine, as a {@link Client} reads it.
|
|
907
|
+
*
|
|
908
|
+
* **Details**
|
|
909
|
+
*
|
|
910
|
+
* `revision` orders the checkpoints of one machine. A `done` machine carries
|
|
911
|
+
* its output, as its final state computes it. A `failed` machine carries the
|
|
912
|
+
* text of the failure that ended it, and the last state it committed before.
|
|
913
|
+
* A `stopped` machine is a child machine that its parent stopped.
|
|
914
|
+
*
|
|
915
|
+
* @category models
|
|
916
|
+
* @since 0.48.0
|
|
917
|
+
*/
|
|
918
|
+
export type Snapshot<M extends Machine.Machine.Any> =
|
|
919
|
+
| {
|
|
920
|
+
readonly status: "active"
|
|
921
|
+
readonly revision: number
|
|
922
|
+
readonly state: Machine.Machine.Snapshot<Machine.Machine.States<M>>
|
|
923
|
+
}
|
|
924
|
+
| {
|
|
925
|
+
readonly status: "done"
|
|
926
|
+
readonly revision: number
|
|
927
|
+
readonly state: Machine.Machine.Snapshot<Machine.Machine.States<M>>
|
|
928
|
+
readonly output: Machine.Machine.Output<M>
|
|
929
|
+
}
|
|
930
|
+
| {
|
|
931
|
+
readonly status: "failed"
|
|
932
|
+
readonly revision: number
|
|
933
|
+
readonly state: Machine.Machine.Snapshot<Machine.Machine.States<M>>
|
|
934
|
+
readonly failure: string
|
|
935
|
+
}
|
|
936
|
+
| {
|
|
937
|
+
readonly status: "stopped"
|
|
938
|
+
readonly revision: number
|
|
939
|
+
readonly state: Machine.Machine.Snapshot<Machine.Machine.States<M>>
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* Why a {@link Client} could not read a checkpoint: it was written by another
|
|
944
|
+
* definition or version, or it is not one that this machine can hold.
|
|
945
|
+
*
|
|
946
|
+
* @category errors
|
|
947
|
+
* @since 0.48.0
|
|
948
|
+
*/
|
|
949
|
+
export { ReadError }
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* Reasons for a {@link ReadError}.
|
|
953
|
+
*
|
|
954
|
+
* @category errors
|
|
955
|
+
* @since 0.48.0
|
|
956
|
+
*/
|
|
957
|
+
export const ReadFailureReason: typeof Protocol.ReadFailureReason = Protocol.ReadFailureReason
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* Reason for a {@link ReadError}.
|
|
961
|
+
*
|
|
962
|
+
* @category errors
|
|
963
|
+
* @since 0.48.0
|
|
964
|
+
*/
|
|
965
|
+
export type ReadFailureReason = typeof ReadFailureReason.Type
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* The failure of a Cluster machine that `join` waited for, as the text that
|
|
969
|
+
* the machine stored when it failed.
|
|
970
|
+
*
|
|
971
|
+
* @category errors
|
|
972
|
+
* @since 0.48.0
|
|
973
|
+
*/
|
|
974
|
+
export { MachineFailedError }
|
|
975
|
+
|
|
976
|
+
type MachineServices<M extends Machine.Machine.Any> =
|
|
977
|
+
| Exclude<Machine.Machine.Services<M>, Machine.InvocationSupport<"Child"> | Machine.ChildMachineRuntime>
|
|
238
978
|
| Machine.Machine.SnapshotDecodingServices<Machine.Machine.States<M>>
|
|
239
979
|
| Machine.Machine.SnapshotEncodingServices<Machine.Machine.States<M>>
|
|
240
980
|
|
|
@@ -273,18 +1013,35 @@ type NonJsonInputEvent<Events extends ReadonlyArray<Machine.Machine.TaggedSchema
|
|
|
273
1013
|
: never
|
|
274
1014
|
}[number]
|
|
275
1015
|
|
|
1016
|
+
type NonJsonInput<Input extends Schema.Top> = Input extends typeof Schema.Void ? never
|
|
1017
|
+
: IsJsonEncoded<Input> extends true ? never
|
|
1018
|
+
: Input["Encoded"]
|
|
1019
|
+
|
|
276
1020
|
type EnsureJsonEncoded<
|
|
277
1021
|
States extends Machine.Machine.StateSchemas,
|
|
278
|
-
InputEvents extends ReadonlyArray<Machine.Machine.TaggedSchema
|
|
279
|
-
|
|
1022
|
+
InputEvents extends ReadonlyArray<Machine.Machine.TaggedSchema>,
|
|
1023
|
+
Input extends Schema.Top
|
|
1024
|
+
> = [NonJsonState<States> | NonJsonOutput<States> | NonJsonInputEvent<InputEvents> | NonJsonInput<Input>] extends
|
|
1025
|
+
[never] ? unknown
|
|
280
1026
|
: {
|
|
281
|
-
readonly "~effect/ClusterMachine/NonJsonEncoded": {
|
|
1027
|
+
readonly "~effect/cluster/ClusterMachine/NonJsonEncoded": {
|
|
282
1028
|
readonly states: NonJsonState<States>
|
|
283
1029
|
readonly outputs: NonJsonOutput<States>
|
|
284
1030
|
readonly inputEvents: NonJsonInputEvent<InputEvents>
|
|
1031
|
+
readonly input: NonJsonInput<Input>
|
|
285
1032
|
}
|
|
286
1033
|
}
|
|
287
1034
|
|
|
1035
|
+
// What `Machine.toStep` refuses for a host that runs effects, timers, and child
|
|
1036
|
+
// machines, and supplies the services of children, named as Cluster refuses
|
|
1037
|
+
// it. A child machine runs as an entity of its own, and a kind added to
|
|
1038
|
+
// `InvocationSupport` is refused until this host states that it runs it.
|
|
1039
|
+
type EnsureSupported<M extends Machine.Machine.Any> =
|
|
1040
|
+
Machine.MachineStep.Supported<M, "Child", Machine.ChildMachineRuntime> extends {
|
|
1041
|
+
readonly "~effect/Machine/MachineStep/Unsupported": infer Missing
|
|
1042
|
+
} ? { readonly "~effect/cluster/ClusterMachine/UnsupportedProcessLocal": Missing }
|
|
1043
|
+
: unknown
|
|
1044
|
+
|
|
288
1045
|
/**
|
|
289
1046
|
* Creates an in-memory Cluster machine checkpoint store.
|
|
290
1047
|
*
|
|
@@ -296,7 +1053,9 @@ type EnsureJsonEncoded<
|
|
|
296
1053
|
* **Gotchas**
|
|
297
1054
|
*
|
|
298
1055
|
* This store is not durable and does not provide rollback with the in-memory
|
|
299
|
-
* `MessageStorage` transaction marker.
|
|
1056
|
+
* `MessageStorage` transaction marker. A request rejected as `EmissionFailure`
|
|
1057
|
+
* keeps the checkpoint it had already committed. The outcome of an invoked
|
|
1058
|
+
* effect or timer whose `enqueue` fails keeps it too, so it is not applied again.
|
|
300
1059
|
*
|
|
301
1060
|
* @category constructors
|
|
302
1061
|
* @since 0.4.0
|
|
@@ -311,6 +1070,63 @@ export const makeMemory: Effect.Effect<Storage["Service"]> = internal.makeMemory
|
|
|
311
1070
|
*/
|
|
312
1071
|
export const layerMemory: Layer.Layer<Storage> = internal.layerMemory
|
|
313
1072
|
|
|
1073
|
+
/**
|
|
1074
|
+
* Checks that a {@link Storage} keeps the contract that Cluster machines rely
|
|
1075
|
+
* on, and fails with the first scenario it breaks.
|
|
1076
|
+
*
|
|
1077
|
+
* **When to use**
|
|
1078
|
+
*
|
|
1079
|
+
* Use in the tests of a `Storage` of your own, against the database it runs
|
|
1080
|
+
* on.
|
|
1081
|
+
*
|
|
1082
|
+
* **Details**
|
|
1083
|
+
*
|
|
1084
|
+
* The check commits to new entities of its own entity types, and so runs
|
|
1085
|
+
* against a store that holds other machines. It checks that:
|
|
1086
|
+
*
|
|
1087
|
+
* - every field of a checkpoint is stored unchanged, including a 64-bit request
|
|
1088
|
+
* id;
|
|
1089
|
+
* - only the revision after the stored one commits;
|
|
1090
|
+
* - a committed request is a `Duplicate` whatever the revision, and is
|
|
1091
|
+
* reported as processed with the checkpoint it committed;
|
|
1092
|
+
* - a rejected commit writes nothing and records no request;
|
|
1093
|
+
* - a commit whose origin is not a request is never a `Duplicate`;
|
|
1094
|
+
* - entities of other ids and types are kept apart;
|
|
1095
|
+
* - of two concurrent commits of one revision, one commits.
|
|
1096
|
+
*
|
|
1097
|
+
* With `withTransaction`, it also checks that a commit inside a transaction
|
|
1098
|
+
* that fails is rolled back. Pass the `withTransaction` of the
|
|
1099
|
+
* `MessageStorage` that the store runs beside.
|
|
1100
|
+
*
|
|
1101
|
+
* **Example** (Checking the in-memory store)
|
|
1102
|
+
*
|
|
1103
|
+
* ```ts import.meta.vitest
|
|
1104
|
+
* import { ClusterMachine } from "@typeonce/effect-machine/cluster"
|
|
1105
|
+
* import { Effect } from "effect"
|
|
1106
|
+
*
|
|
1107
|
+
* const program = Effect.flatMap(ClusterMachine.makeMemory, (storage) => ClusterMachine.checkStorage(storage))
|
|
1108
|
+
*
|
|
1109
|
+
* await Effect.runPromise(program) // => undefined
|
|
1110
|
+
* ```
|
|
1111
|
+
*
|
|
1112
|
+
* @category testing
|
|
1113
|
+
* @since 0.48.0
|
|
1114
|
+
*/
|
|
1115
|
+
export const checkStorage: (
|
|
1116
|
+
storage: Storage["Service"],
|
|
1117
|
+
options?: {
|
|
1118
|
+
readonly withTransaction?: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
|
|
1119
|
+
}
|
|
1120
|
+
) => Effect.Effect<void, StorageCheckFailure | ClusterError.PersistenceError> = StorageCheck.check
|
|
1121
|
+
|
|
1122
|
+
/**
|
|
1123
|
+
* The scenario of {@link checkStorage} that a store breaks, and how.
|
|
1124
|
+
*
|
|
1125
|
+
* @category errors
|
|
1126
|
+
* @since 0.48.0
|
|
1127
|
+
*/
|
|
1128
|
+
export { StorageCheckFailure }
|
|
1129
|
+
|
|
314
1130
|
/**
|
|
315
1131
|
* Creates a persisted Cluster entity adapter for a machine.
|
|
316
1132
|
*
|
|
@@ -321,22 +1137,195 @@ export const layerMemory: Layer.Layer<Storage> = internal.layerMemory
|
|
|
321
1137
|
*
|
|
322
1138
|
* **Details**
|
|
323
1139
|
*
|
|
324
|
-
* A missing checkpoint runs initial planning before the first event.
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
1140
|
+
* A missing checkpoint runs initial planning before the first event. An entity
|
|
1141
|
+
* may also be started explicitly via `client.start(...input)`. For a machine
|
|
1142
|
+
* that declares `input`, you may either omit the default input from
|
|
1143
|
+
* `ClusterMachine.make` and pass per-entity input to `client.start(input)`
|
|
1144
|
+
* (sending an event before start rejects as `InvalidEvent`), or pass a default
|
|
1145
|
+
* input to `ClusterMachine.make` so `client.send` can auto-start while
|
|
1146
|
+
* `client.start(input)` still starts an entity with a custom input. Existing
|
|
1147
|
+
* checkpoints are checked for their definition id and version, decoded, and
|
|
1148
|
+
* resumed without rerunning initial entry behavior. Final checkpoints accept
|
|
1149
|
+
* later requests as no-ops. The definition id is `machine.id` when present,
|
|
328
1150
|
* otherwise the Cluster entity type.
|
|
329
1151
|
*
|
|
1152
|
+
* A live entity loads its checkpoint when it comes up, or for its first request
|
|
1153
|
+
* when the machine invokes nothing, and then keeps the decoded state in memory
|
|
1154
|
+
* until the entity is passivated or restarted. Storage remains the truth. A
|
|
1155
|
+
* request whose state turns out to be behind storage is applied again to the
|
|
1156
|
+
* stored state, and is rejected as `RevisionConflict` when storage has moved
|
|
1157
|
+
* again.
|
|
1158
|
+
*
|
|
1159
|
+
* Effects invoked by active states run in the live entity. Work starts once the
|
|
1160
|
+
* step that entered its state is stored, and stops once the step that exited it
|
|
1161
|
+
* is stored. The end of the work is applied as a step of its own, in order with
|
|
1162
|
+
* requests, and only the state it leads to is stored, never the result itself.
|
|
1163
|
+
* While work should run the entity is kept alive, so it is not passivated, and
|
|
1164
|
+
* Cluster brings it up on another runner after a crash. It is kept alive through
|
|
1165
|
+
* `Entity.keepAlive`, so a host that provides an `Entity.KeepAliveHandler` is
|
|
1166
|
+
* told when to keep it, and when to let it go.
|
|
1167
|
+
*
|
|
1168
|
+
* An entity that comes up starts the work of its stored state again. Input
|
|
1169
|
+
* mappers run again against that state, with `Machine.InitialEvent` as their
|
|
1170
|
+
* event, as they do for `LocalMachine.resume`. An invocation that ended while its
|
|
1171
|
+
* state stayed active is not started again until its state is entered again.
|
|
1172
|
+
*
|
|
1173
|
+
* A timer is stored as the time at which it is due, in the same checkpoint as the
|
|
1174
|
+
* step that enters its state. The live entity waits for that time and then
|
|
1175
|
+
* applies the completion as a step of its own. An entity that comes up again,
|
|
1176
|
+
* here or on another runner, waits only for the time that remains, and completes
|
|
1177
|
+
* a timer whose time has passed at once. Leaving the state cancels the timer, and
|
|
1178
|
+
* entering it again stores a new due time. `LocalMachine.resume` differs: it starts the
|
|
1179
|
+
* full duration again.
|
|
1180
|
+
*
|
|
1181
|
+
* The duration of a timer is evaluated after the step that enters its state is
|
|
1182
|
+
* planned and before that step is stored, and not again while its due time is
|
|
1183
|
+
* stored. The local runtime evaluates it once the state is entered, in order with
|
|
1184
|
+
* the other invocations of the state. A duration that cannot be evaluated does
|
|
1185
|
+
* not reject the request. The step is stored, and the machine then fails as it
|
|
1186
|
+
* does locally.
|
|
1187
|
+
*
|
|
1188
|
+
* Work that ends in a way the model does not handle, an outcome that cannot be
|
|
1189
|
+
* applied, and a source that cannot be constructed fail the machine, as they do
|
|
1190
|
+
* locally. The failure is stored with the last state, and every later request is
|
|
1191
|
+
* rejected as `MachineFailed`. There is no retry of failed work: a model that
|
|
1192
|
+
* recovers declares `onFailure`.
|
|
1193
|
+
*
|
|
1194
|
+
* A machine that this host cannot run does not compile: one whose states invoke a
|
|
1195
|
+
* Stream or a `Logic`, or whose invoked work requires `Machine.MachineRuntime`,
|
|
1196
|
+
* as `Machine.spawn` does, and `Machine.sendTo` and `Machine.stopChild` with a
|
|
1197
|
+
* child address. With the descriptor of a child machine, they require only
|
|
1198
|
+
* `Machine.ChildMachineRuntime`, which this host provides: they act on the
|
|
1199
|
+
* child that runs when the work acts, through a persisted request saved outside
|
|
1200
|
+
* the transaction of a step, and ignore a child that does not run. The error
|
|
1201
|
+
* names the property
|
|
1202
|
+
* `~effect/cluster/ClusterMachine/UnsupportedProcessLocal`, and lists there every
|
|
1203
|
+
* invocation kind and capability to replace with an Effect, a timer, or a child
|
|
1204
|
+
* machine. A source that is registered and never invoked is accepted.
|
|
1205
|
+
*
|
|
1206
|
+
* Streams and `Logic` are refused because their progress cannot be stored. A
|
|
1207
|
+
* checkpoint holds the state of a machine, and after a crash, a move to another
|
|
1208
|
+
* runner, or a restart, the machine continues from it and its work runs again
|
|
1209
|
+
* from the start. An Effect or a timer ends with one outcome, so running it
|
|
1210
|
+
* again changes nothing that was applied. A Stream ends after many elements,
|
|
1211
|
+
* and how far it got lives only in memory: run again, it would apply elements
|
|
1212
|
+
* that were applied before, or miss some when it does not produce the same ones.
|
|
1213
|
+
* A `Logic` is a process with a state and a mailbox of its own, and both would
|
|
1214
|
+
* be lost. Model a process, or a source of many events, as a child machine,
|
|
1215
|
+
* whose every step is stored, or as an Effect that ends with one result and a
|
|
1216
|
+
* state that is entered again for the next one.
|
|
1217
|
+
*
|
|
330
1218
|
* **Gotchas**
|
|
331
1219
|
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
1220
|
+
* Work runs at least once and must be safe to repeat. It runs again after a
|
|
1221
|
+
* crash, a move to another runner, a restart of the entity, and when another
|
|
1222
|
+
* instance advanced the checkpoint, and it can run twice at the same time while
|
|
1223
|
+
* a shard moves. One result is applied. Results are not stored, so a result that
|
|
1224
|
+
* was not applied before a crash is computed again.
|
|
1225
|
+
*
|
|
1226
|
+
* A storage or `enqueue` failure never fails the machine. An outcome that cannot
|
|
1227
|
+
* be stored is retried for as long as the entity lives.
|
|
1228
|
+
*
|
|
1229
|
+
* Use `effect` 4.1.5 or later. Earlier versions answer the keep-alive request
|
|
1230
|
+
* of an entity after about ten minutes, so a machine whose work runs or whose
|
|
1231
|
+
* timer waits longer would not come up again after a crash.
|
|
1232
|
+
*
|
|
1233
|
+
* A timer that waits no longer than `DurableClock.InMemoryThreshold`, one
|
|
1234
|
+
* minute by default, keeps its entity in memory, as a workflow keeps a short
|
|
1235
|
+
* sleep. The step that stores a longer wait also saves, in the same
|
|
1236
|
+
* transaction of `MessageStorage`, a message to the entity that Cluster
|
|
1237
|
+
* delivers when the timer is due. Such a timer keeps nothing alive, so the
|
|
1238
|
+
* entity may leave memory, and the message brings it back, here or on another
|
|
1239
|
+
* runner. The path, the id, and the due time of the timer name the message, so
|
|
1240
|
+
* the message of a wait that ended is ignored. A timer that never ends saves no
|
|
1241
|
+
* message. A long wait stored before this version gets its message when its
|
|
1242
|
+
* entity next comes up.
|
|
1243
|
+
*
|
|
1244
|
+
* A message is read by the runner every `entityMessagePollInterval`, so a long
|
|
1245
|
+
* timer of an entity that left memory fires up to that much after it is due.
|
|
1246
|
+
*
|
|
1247
|
+
* Due times are read from the wall clock of the runner, so runners whose clocks
|
|
1248
|
+
* differ complete a timer that moves between them that much earlier or later.
|
|
1249
|
+
*
|
|
1250
|
+
* A state that was stored before the machine declared one of its timers has no
|
|
1251
|
+
* due time for it. The entity evaluates the duration when it comes up, with
|
|
1252
|
+
* `Machine.InitialEvent`, and stores the due time before it starts any work. A
|
|
1253
|
+
* state that was stored with a timer the machine no longer declares is rejected
|
|
1254
|
+
* as `InvalidCheckpoint`, so migrate such checkpoints.
|
|
1255
|
+
*
|
|
1256
|
+
* Machines send to each other through a {@link Target}, which names an entity
|
|
1257
|
+
* by its type and id: `target` of an adapter makes one, and `self` is the one of
|
|
1258
|
+
* the machine's own entity. An event that a step sends with `enqueue.sendTo` is
|
|
1259
|
+
* saved with its checkpoint and applied once, and work sends with `send` of a
|
|
1260
|
+
* target.
|
|
1261
|
+
*
|
|
1262
|
+
* A child machine that a state invokes runs as an entity of its own, of type
|
|
1263
|
+
* `Type/childId`, which the layer of the adapter registers too, with the same
|
|
1264
|
+
* version. Each activation of the state starts a new child, whose id is
|
|
1265
|
+
* `entityId/childId/revision`, with the revision of the step that entered the
|
|
1266
|
+
* state, so a child knows nothing of the one before it. The step that enters
|
|
1267
|
+
* the state saves the start of the child, with its input, in the transaction of
|
|
1268
|
+
* its checkpoint, and the step that exits it saves its stop, so a child runs
|
|
1269
|
+
* exactly while its state is stored. `enqueue.sendTo` with the descriptor of a
|
|
1270
|
+
* child sends to the child that runs once the step is stored, and drops the
|
|
1271
|
+
* event when none does. `enqueue.stop` with the descriptor of a child stops the
|
|
1272
|
+
* running child in the same transaction, settles its invocation progress so it
|
|
1273
|
+
* does not restart while the state stays active, and ignores a child that does
|
|
1274
|
+
* not run. Commands are carried out before the work of the states a step enters
|
|
1275
|
+
* starts, as they are locally, so both drop a child that the same step enters.
|
|
1276
|
+
* Work acts on the child of its state with `Machine.sendTo` and
|
|
1277
|
+
* `Machine.stopChild`, and its input mapper with `children.sendTo` and
|
|
1278
|
+
* `children.stop`. The child's `parent` is the target of its parent. A
|
|
1279
|
+
* child that ends tells its parent, which reads its output or its failure from
|
|
1280
|
+
* `Storage` and applies it as the end of the invocation. A failure of a child
|
|
1281
|
+
* fails its parent, also when the parent declares `onFailure`, because the
|
|
1282
|
+
* failure is stored as text and not as the error of the child. A stopped child
|
|
1283
|
+
* is stored as `Stopped`, and a machine that fails or ends stops its children.
|
|
1284
|
+
*
|
|
1285
|
+
* When a state of the parent takes the snapshots of a child with `onSnapshot`,
|
|
1286
|
+
* each step of the child that plans its initial state or takes a transition, and
|
|
1287
|
+
* leaves it active, sends its parent the encoded snapshot in the transaction of
|
|
1288
|
+
* its checkpoint. The parent applies each one once, as Cluster delivers it,
|
|
1289
|
+
* while it runs that child. A transition on a snapshot that cannot
|
|
1290
|
+
* be applied fails the parent. A child whose parent takes none of its snapshots
|
|
1291
|
+
* sends none.
|
|
1292
|
+
*
|
|
1293
|
+
* The compiler reads only the requirements of a machine, so these uses of a local
|
|
1294
|
+
* process compile and fail at run time:
|
|
1295
|
+
*
|
|
1296
|
+
* - A step whose plan sends to a target that is neither a {@link Target} nor
|
|
1297
|
+
* the descriptor of an invoked child, or stops a child address rather than
|
|
1298
|
+
* the descriptor of an invoked child with `enqueue.stop`. It rejects the
|
|
1299
|
+
* request as `UnsupportedProcessLocal`, or fails the machine when the step
|
|
1300
|
+
* applies an outcome. Planning-time raised events remain part of the current
|
|
1301
|
+
* macrostep.
|
|
1302
|
+
* - Work that spawns a child with `children.spawn` from the context of its
|
|
1303
|
+
* input mapper, or that `children.sendTo` and `children.stop` give a child
|
|
1304
|
+
* address. It fails the machine.
|
|
1305
|
+
* - A machine whose requirements are `any`, such as an erased one. When any of its
|
|
1306
|
+
* states declares an unsupported invocation, it rejects every request as
|
|
1307
|
+
* `UnsupportedProcessLocal`. Work of such a machine that uses
|
|
1308
|
+
* `Machine.MachineRuntime`, such as `Machine.spawn`, or `Machine.sendTo` and
|
|
1309
|
+
* `Machine.stopChild` with a child address, fails the machine with
|
|
1310
|
+
* `ProcessLocalError`.
|
|
1311
|
+
*
|
|
1312
|
+
* A deployment that adds an invocation to a state does not wake the entities
|
|
1313
|
+
* that are already in it. Their work starts when they come up.
|
|
1314
|
+
*
|
|
1315
|
+
* A request redelivered to a live entity is planned again before storage
|
|
1316
|
+
* recognizes it. The plan is discarded, but its resolvers have run again, and so
|
|
1317
|
+
* have the input mappers and sources of the timers it enters, so they must not
|
|
1318
|
+
* perform effects.
|
|
1319
|
+
*
|
|
1320
|
+
* A checkpoint changed outside the bridge while an entity is live is noticed
|
|
1321
|
+
* through its revision, so such a change must also advance the revision.
|
|
1322
|
+
*
|
|
335
1323
|
* Arbitrary action effects may run again after a crash before checkpoint
|
|
336
1324
|
* commit, so the bridge does not provide exactly-once external effects.
|
|
337
1325
|
* State values, completion outputs, and public input events must declare
|
|
338
|
-
* JSON-compatible encoded representations. Local and internal event protocols
|
|
339
|
-
*
|
|
1326
|
+
* JSON-compatible encoded representations. Local and internal event protocols,
|
|
1327
|
+
* and the inputs, outputs, and errors of invoked work, remain unrestricted
|
|
1328
|
+
* because they do not cross the Cluster boundary.
|
|
340
1329
|
*
|
|
341
1330
|
* **Example** (Adapting a machine to an entity)
|
|
342
1331
|
*
|
|
@@ -356,6 +1345,124 @@ export const layerMemory: Layer.Layer<Storage> = internal.layerMemory
|
|
|
356
1345
|
* adapter.entity.type // => "IdleMachine"
|
|
357
1346
|
* ```
|
|
358
1347
|
*
|
|
1348
|
+
* **Example** (Running an invoked effect in an entity)
|
|
1349
|
+
*
|
|
1350
|
+
* ```ts import.meta.vitest
|
|
1351
|
+
* import { Machine } from "@typeonce/effect-machine"
|
|
1352
|
+
* import { ClusterMachine } from "@typeonce/effect-machine/cluster"
|
|
1353
|
+
* import { Effect, Schema } from "effect"
|
|
1354
|
+
*
|
|
1355
|
+
* const machine = Machine.make({
|
|
1356
|
+
* id: "Invoice",
|
|
1357
|
+
* root: Machine.state({
|
|
1358
|
+
* states: {
|
|
1359
|
+
* Draft: {},
|
|
1360
|
+
* Sending: { fields: { invoiceId: Schema.String } },
|
|
1361
|
+
* Sent: { fields: { receipt: Schema.String } },
|
|
1362
|
+
* Rejected: { fields: { reason: Schema.String } }
|
|
1363
|
+
* }
|
|
1364
|
+
* }),
|
|
1365
|
+
* events: Machine.events({ Send: { invoiceId: Schema.String } }),
|
|
1366
|
+
* effects: {
|
|
1367
|
+
* // Can run more than once, so the invoice id makes it safe to repeat.
|
|
1368
|
+
* send: (invoiceId: string) =>
|
|
1369
|
+
* invoiceId === "" ? Effect.fail({ reason: "No invoice" }) : Effect.succeed(`receipt-${invoiceId}`)
|
|
1370
|
+
* }
|
|
1371
|
+
* }).handle({
|
|
1372
|
+
* initial: { target: "Draft" },
|
|
1373
|
+
* states: {
|
|
1374
|
+
* Draft: { on: { Send: { target: "Sending", data: ({ event }) => ({ invoiceId: event.invoiceId }) } } },
|
|
1375
|
+
* Sending: {
|
|
1376
|
+
* invoke: {
|
|
1377
|
+
* src: "send",
|
|
1378
|
+
* input: ({ state }) => state.invoiceId,
|
|
1379
|
+
* onDone: { target: "Sent", data: ({ output }) => ({ receipt: output }) },
|
|
1380
|
+
* onFailure: { target: "Rejected", data: ({ error }) => ({ reason: error.reason }) }
|
|
1381
|
+
* }
|
|
1382
|
+
* }
|
|
1383
|
+
* }
|
|
1384
|
+
* })
|
|
1385
|
+
*
|
|
1386
|
+
* const adapter = ClusterMachine.make("Invoice", machine, { version: "1" })
|
|
1387
|
+
*
|
|
1388
|
+
* // Provide the layer with a Cluster runner and a durable `ClusterMachine.Storage`.
|
|
1389
|
+
* const layer = adapter.toLayer()
|
|
1390
|
+
*
|
|
1391
|
+
* adapter.entity.type // => "Invoice"
|
|
1392
|
+
* ```
|
|
1393
|
+
*
|
|
1394
|
+
* **Example** (Waiting for a timer that survives its runner)
|
|
1395
|
+
*
|
|
1396
|
+
* ```ts import.meta.vitest
|
|
1397
|
+
* import { Machine } from "@typeonce/effect-machine"
|
|
1398
|
+
* import { ClusterMachine } from "@typeonce/effect-machine/cluster"
|
|
1399
|
+
* import { Schema } from "effect"
|
|
1400
|
+
*
|
|
1401
|
+
* const machine = Machine.make({
|
|
1402
|
+
* id: "Trial",
|
|
1403
|
+
* root: Machine.state({
|
|
1404
|
+
* states: {
|
|
1405
|
+
* Inactive: {},
|
|
1406
|
+
* Running: { fields: { days: Schema.Number } },
|
|
1407
|
+
* Expired: {}
|
|
1408
|
+
* }
|
|
1409
|
+
* }),
|
|
1410
|
+
* events: Machine.events({ Start: { days: Schema.Number }, Cancel: {} }),
|
|
1411
|
+
* timers: { trial: (days: number) => ({ days }) }
|
|
1412
|
+
* }).handle({
|
|
1413
|
+
* initial: { target: "Inactive" },
|
|
1414
|
+
* states: {
|
|
1415
|
+
* Inactive: { on: { Start: { target: "Running", data: ({ event }) => ({ days: event.days }) } } },
|
|
1416
|
+
* Running: {
|
|
1417
|
+
* invoke: { src: "trial", input: ({ state }) => state.days, onDone: { target: "Expired" } },
|
|
1418
|
+
* on: { Cancel: { target: "Inactive" } }
|
|
1419
|
+
* }
|
|
1420
|
+
* }
|
|
1421
|
+
* })
|
|
1422
|
+
*
|
|
1423
|
+
* const adapter = ClusterMachine.make("Trial", machine, { version: "1" })
|
|
1424
|
+
*
|
|
1425
|
+
* adapter.entity.type // => "Trial"
|
|
1426
|
+
* ```
|
|
1427
|
+
*
|
|
1428
|
+
* **Example** (Refusing a machine that Cluster cannot run)
|
|
1429
|
+
*
|
|
1430
|
+
* ```ts import.meta.vitest
|
|
1431
|
+
* import { Machine } from "@typeonce/effect-machine"
|
|
1432
|
+
* import { ClusterMachine } from "@typeonce/effect-machine/cluster"
|
|
1433
|
+
* import { Effect, Stream } from "effect"
|
|
1434
|
+
*
|
|
1435
|
+
* const definition = Machine.make({
|
|
1436
|
+
* id: "Sensor",
|
|
1437
|
+
* root: Machine.state({ states: { Reading: {}, Read: {} } }),
|
|
1438
|
+
* events: Machine.events({}),
|
|
1439
|
+
* effects: { read: Effect.succeed(20) },
|
|
1440
|
+
* streams: { readings: Stream.make(20, 21) }
|
|
1441
|
+
* })
|
|
1442
|
+
*
|
|
1443
|
+
* const streaming = definition.handle({
|
|
1444
|
+
* initial: { target: "Reading" },
|
|
1445
|
+
* states: {
|
|
1446
|
+
* Reading: { invoke: { src: "readings", onElement: { stay: true }, onDone: { target: "Read" } } }
|
|
1447
|
+
* }
|
|
1448
|
+
* })
|
|
1449
|
+
*
|
|
1450
|
+
* // @ts-expect-error Invokes a Stream: { invocations: "Stream"; capabilities: never }
|
|
1451
|
+
* ClusterMachine.make("Sensor", streaming, { version: "1" })
|
|
1452
|
+
*
|
|
1453
|
+
* const reading = definition.handle({
|
|
1454
|
+
* initial: { target: "Reading" },
|
|
1455
|
+
* states: {
|
|
1456
|
+
* Reading: { invoke: { src: "read", onDone: { target: "Read" } } }
|
|
1457
|
+
* }
|
|
1458
|
+
* })
|
|
1459
|
+
*
|
|
1460
|
+
* const adapter = ClusterMachine.make("Sensor", reading, { version: "1" })
|
|
1461
|
+
*
|
|
1462
|
+
* adapter.entity.type // => "Sensor"
|
|
1463
|
+
* ```
|
|
1464
|
+
*
|
|
1465
|
+
* @see {@link Machine.InvocationSupport} for the requirement of each invocation kind.
|
|
359
1466
|
* @category constructors
|
|
360
1467
|
* @since 0.4.0
|
|
361
1468
|
*/
|
|
@@ -367,8 +1474,6 @@ export const make: <
|
|
|
367
1474
|
UnhandledStates extends Machine.Machine.StateIdentifier<States>,
|
|
368
1475
|
E,
|
|
369
1476
|
R,
|
|
370
|
-
InitialE,
|
|
371
|
-
InitialR,
|
|
372
1477
|
FinalStates extends Machine.Machine.StateIdentifier<States>,
|
|
373
1478
|
Output,
|
|
374
1479
|
Emits extends ReadonlyArray<Machine.Machine.TaggedSchema>,
|
|
@@ -385,8 +1490,6 @@ export const make: <
|
|
|
385
1490
|
UnhandledStates,
|
|
386
1491
|
E,
|
|
387
1492
|
R,
|
|
388
|
-
InitialE,
|
|
389
|
-
InitialR,
|
|
390
1493
|
FinalStates,
|
|
391
1494
|
Output,
|
|
392
1495
|
Emits,
|
|
@@ -396,11 +1499,37 @@ export const make: <
|
|
|
396
1499
|
>
|
|
397
1500
|
& EnsureExecutable<States, UnhandledStates, OutputStates>
|
|
398
1501
|
& Machine.Machine.RootCompatible<ParentEvents>
|
|
399
|
-
& EnsureJsonEncoded<States, InputEvents
|
|
1502
|
+
& EnsureJsonEncoded<States, InputEvents, Input>
|
|
1503
|
+
& EnsureSupported<
|
|
1504
|
+
Machine.Machine<
|
|
1505
|
+
States,
|
|
1506
|
+
Events,
|
|
1507
|
+
Input,
|
|
1508
|
+
UnhandledStates,
|
|
1509
|
+
E,
|
|
1510
|
+
R,
|
|
1511
|
+
FinalStates,
|
|
1512
|
+
Output,
|
|
1513
|
+
Emits,
|
|
1514
|
+
OutputStates,
|
|
1515
|
+
InputEvents,
|
|
1516
|
+
ParentEvents
|
|
1517
|
+
>
|
|
1518
|
+
>,
|
|
400
1519
|
options: {
|
|
401
1520
|
readonly version: string
|
|
1521
|
+
/**
|
|
1522
|
+
* How long `changes` and `join` of a {@link Client} wait between two reads.
|
|
1523
|
+
* Defaults to one second.
|
|
1524
|
+
*/
|
|
1525
|
+
readonly readInterval?: Duration.Input | undefined
|
|
1526
|
+
/**
|
|
1527
|
+
* Selects the Cluster shard group for each entity id (and for any child
|
|
1528
|
+
* entities invoked by that parent). Defaults to Cluster's `"default"` group.
|
|
1529
|
+
*/
|
|
1530
|
+
readonly shardGroup?: ((entityId: EntityId.EntityId) => string) | undefined
|
|
402
1531
|
},
|
|
403
|
-
...input: [...Machine.Machine.InputArgs<Input>]
|
|
1532
|
+
...input: readonly [] | [...Machine.Machine.InputArgs<Input>]
|
|
404
1533
|
) => ClusterMachine<
|
|
405
1534
|
Type,
|
|
406
1535
|
Machine.Machine<
|
|
@@ -410,8 +1539,6 @@ export const make: <
|
|
|
410
1539
|
UnhandledStates,
|
|
411
1540
|
E,
|
|
412
1541
|
R,
|
|
413
|
-
InitialE,
|
|
414
|
-
InitialR,
|
|
415
1542
|
FinalStates,
|
|
416
1543
|
Output,
|
|
417
1544
|
Emits,
|
|
@@ -419,11 +1546,8 @@ export const make: <
|
|
|
419
1546
|
InputEvents,
|
|
420
1547
|
ParentEvents
|
|
421
1548
|
>,
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
Machine.Machine.EventOf<Events>,
|
|
425
|
-
Machine.Machine.EmittedEventOf<Emits>
|
|
426
|
-
>
|
|
1549
|
+
// A child machine runs as an entity that the layer of its parent registers.
|
|
1550
|
+
| Exclude<R, Machine.InvocationSupport<"Child"> | Machine.ChildMachineRuntime>
|
|
427
1551
|
| Machine.Machine.SnapshotDecodingServices<States>
|
|
428
1552
|
| Machine.Machine.SnapshotEncodingServices<States>
|
|
429
1553
|
> = internal.make
|