@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
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps one Cluster entity alive while it has work to run.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.48.0
|
|
5
|
+
*/
|
|
6
|
+
import * as Effect from "effect/Effect"
|
|
7
|
+
import * as Schedule from "effect/Schedule"
|
|
8
|
+
|
|
9
|
+
/** Bounded backoff of everything an instance retries on its own. */
|
|
10
|
+
export const retrySchedule = Schedule.min([
|
|
11
|
+
Schedule.exponential("100 millis"),
|
|
12
|
+
Schedule.spaced("10 seconds")
|
|
13
|
+
])
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Runs an attempt until it reports that nothing is left to do.
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export const untilDone = <R>(attempt: Effect.Effect<boolean, never, R>): Effect.Effect<void, never, R> =>
|
|
21
|
+
Effect.asVoid(Effect.repeat(attempt, { until: (done) => done, schedule: retrySchedule }))
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The keep-alive request of one entity.
|
|
25
|
+
*
|
|
26
|
+
* While enabled, message storage holds one persisted request of the entity
|
|
27
|
+
* without a reply. That request is what brings the entity up on another runner,
|
|
28
|
+
* and it keeps this runner from passivating the entity. Disabling answers it.
|
|
29
|
+
*
|
|
30
|
+
* Nothing here is synchronized. `switchTo` runs inside a turn of the instance,
|
|
31
|
+
* and `close` after its last one.
|
|
32
|
+
*
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export class ClusterKeepAlive {
|
|
36
|
+
// Unknown until the first switch, and after one that did not finish.
|
|
37
|
+
private enabled: boolean | undefined = undefined
|
|
38
|
+
private wanted = false
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
/** `Entity.keepAlive` for the entity. */
|
|
42
|
+
private readonly set: (enabled: boolean) => Effect.Effect<void>
|
|
43
|
+
) {}
|
|
44
|
+
|
|
45
|
+
/** Whether the state of the request is unknown. */
|
|
46
|
+
get unknown(): boolean {
|
|
47
|
+
return this.enabled === undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Enables or disables the request, until that succeeds. */
|
|
51
|
+
switchTo(enabled: boolean): Effect.Effect<void> {
|
|
52
|
+
return Effect.suspend(() => {
|
|
53
|
+
this.wanted = enabled
|
|
54
|
+
if (this.enabled === enabled) return Effect.void
|
|
55
|
+
this.enabled = undefined
|
|
56
|
+
return untilDone(
|
|
57
|
+
this.set(enabled).pipe(
|
|
58
|
+
// The entity lets go of a released request in a later task. It answers
|
|
59
|
+
// a request that reaches it before that as a second one, and would
|
|
60
|
+
// then hold none.
|
|
61
|
+
Effect.andThen(enabled ? Effect.void : Effect.yieldNow),
|
|
62
|
+
Effect.as(true),
|
|
63
|
+
// Does not run when this fiber is interrupted.
|
|
64
|
+
Effect.catchCause((cause) =>
|
|
65
|
+
Effect.as(Effect.logWarning("Could not switch the keep-alive of a Cluster machine", cause), false)
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
).pipe(
|
|
69
|
+
Effect.andThen(Effect.sync(() => {
|
|
70
|
+
this.enabled = enabled
|
|
71
|
+
}))
|
|
72
|
+
)
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Stores the request when the instance ends before a switch that enables it
|
|
78
|
+
* has finished. A stored step whose caller was interrupted is not delivered
|
|
79
|
+
* again, so nothing else would bring the entity up.
|
|
80
|
+
*/
|
|
81
|
+
readonly close: Effect.Effect<void> = Effect.suspend(() =>
|
|
82
|
+
this.wanted && this.enabled === undefined
|
|
83
|
+
? this.set(true).pipe(
|
|
84
|
+
Effect.interruptible,
|
|
85
|
+
Effect.timeoutOption("10 seconds"),
|
|
86
|
+
Effect.catchCause((cause) =>
|
|
87
|
+
Effect.logWarning("Could not restore the keep-alive of a Cluster machine that ended", cause)
|
|
88
|
+
),
|
|
89
|
+
Effect.asVoid
|
|
90
|
+
)
|
|
91
|
+
: Effect.void
|
|
92
|
+
)
|
|
93
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What one stored state knows about the invocations it owns.
|
|
3
|
+
*
|
|
4
|
+
* An effect without progress should run, and a timer without progress needs a due
|
|
5
|
+
* time. Progress is keyed by state and id, so one invocation never has two, and
|
|
6
|
+
* every function returns a new value.
|
|
7
|
+
*
|
|
8
|
+
* @since 0.48.0
|
|
9
|
+
*/
|
|
10
|
+
import * as Predicate from "effect/Predicate"
|
|
11
|
+
import type { Checkpoint } from "../../unstable/cluster/ClusterMachine.js"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Progress by the path of the state and the id of the invocation. Names are
|
|
15
|
+
* chosen by the model, so lookups go through maps and not object properties.
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export type Progress = ReadonlyMap<string, ReadonlyMap<string, Checkpoint.Progress>>
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The last instant a date can name. A later due time, such as that of an
|
|
23
|
+
* infinite duration, is stored as this one, so every stored due time is a date.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export const maxDueAt = 8.64e15
|
|
28
|
+
|
|
29
|
+
/** @internal */
|
|
30
|
+
export const empty: Progress = new Map()
|
|
31
|
+
|
|
32
|
+
/** @internal */
|
|
33
|
+
export const get = (progress: Progress, key: Checkpoint.InvocationKey): Checkpoint.Progress | undefined =>
|
|
34
|
+
progress.get(key.path)?.get(key.id)
|
|
35
|
+
|
|
36
|
+
const set = (progress: Progress, key: Checkpoint.InvocationKey, value: Checkpoint.Progress): Progress =>
|
|
37
|
+
new Map(progress).set(key.path, new Map(progress.get(key.path) ?? []).set(key.id, value))
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* An invocation that ended while its state stayed active.
|
|
41
|
+
*
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export const settle = (progress: Progress, key: Checkpoint.InvocationKey): Progress =>
|
|
45
|
+
set(progress, key, { _tag: "Settled" })
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A timer that waits until a due time.
|
|
49
|
+
*
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
export const wait = (progress: Progress, key: Checkpoint.InvocationKey, dueAt: number): Progress =>
|
|
53
|
+
set(progress, key, { _tag: "Waiting", dueAt })
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A child machine that runs as the entity `entityId`.
|
|
57
|
+
*
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
export const child = (progress: Progress, key: Checkpoint.InvocationKey, entityId: string): Progress =>
|
|
61
|
+
set(progress, key, { _tag: "Child", entityId })
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The children that run, with their keys.
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
export const children = (
|
|
69
|
+
progress: Progress
|
|
70
|
+
): ReadonlyArray<readonly [key: Checkpoint.InvocationKey, entityId: string]> =>
|
|
71
|
+
entries(progress).flatMap(([key, value]) => value._tag === "Child" ? [[key, value.entityId] as const] : [])
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Removes every child, as a machine that ends stops them.
|
|
75
|
+
*
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
78
|
+
export const stopChildren = (progress: Progress): Progress =>
|
|
79
|
+
new Map(
|
|
80
|
+
Array.from(progress, ([path, invocations]) =>
|
|
81
|
+
[
|
|
82
|
+
path,
|
|
83
|
+
new Map(Array.from(invocations).filter(([, { _tag }]) => _tag !== "Child"))
|
|
84
|
+
] as const)
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Removes the invocations of exited states.
|
|
89
|
+
*
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
export const exit = (progress: Progress, paths: ReadonlyArray<string>): Progress =>
|
|
93
|
+
new Map(Array.from(progress).filter(([path]) => !paths.includes(path)))
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Removes every timer that waits.
|
|
97
|
+
*
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
export const cancelTimers = (progress: Progress): Progress =>
|
|
101
|
+
new Map(
|
|
102
|
+
Array.from(progress, ([path, invocations]) =>
|
|
103
|
+
[
|
|
104
|
+
path,
|
|
105
|
+
new Map(Array.from(invocations).filter(([, { _tag }]) => _tag !== "Waiting"))
|
|
106
|
+
] as const)
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
/** @internal */
|
|
110
|
+
export const entries = (
|
|
111
|
+
progress: Progress
|
|
112
|
+
): ReadonlyArray<readonly [key: Checkpoint.InvocationKey, progress: Checkpoint.Progress]> =>
|
|
113
|
+
Array.from(progress).flatMap(([path, invocations]) =>
|
|
114
|
+
Array.from(invocations, ([id, value]) => [{ path, id }, value] as const)
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
/** @internal */
|
|
118
|
+
export const toStored = (progress: Progress): Checkpoint.Invocations =>
|
|
119
|
+
Object.fromEntries(
|
|
120
|
+
Array.from(progress)
|
|
121
|
+
.filter(([, invocations]) => invocations.size > 0)
|
|
122
|
+
.map(([path, invocations]) => [path, Object.fromEntries(invocations)])
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
const readOne = (value: unknown): Checkpoint.Progress | undefined => {
|
|
126
|
+
if (!Predicate.hasProperty(value, "_tag")) return undefined
|
|
127
|
+
if (value._tag === "Settled") return { _tag: "Settled" }
|
|
128
|
+
if (value._tag === "Child") {
|
|
129
|
+
return Predicate.hasProperty(value, "entityId") && Predicate.isString(value.entityId)
|
|
130
|
+
? { _tag: "Child", entityId: value.entityId }
|
|
131
|
+
: undefined
|
|
132
|
+
}
|
|
133
|
+
return value._tag === "Waiting" && Predicate.hasProperty(value, "dueAt") && Predicate.isNumber(value.dueAt) &&
|
|
134
|
+
Number.isInteger(value.dueAt) && Math.abs(value.dueAt) <= maxDueAt
|
|
135
|
+
? { _tag: "Waiting", dueAt: value.dueAt }
|
|
136
|
+
: undefined
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Reads stored progress, whose store may hold anything. `undefined` means it is
|
|
141
|
+
* not progress.
|
|
142
|
+
*
|
|
143
|
+
* @internal
|
|
144
|
+
*/
|
|
145
|
+
export const fromStored = (stored: unknown): Progress | undefined => {
|
|
146
|
+
if (!Predicate.isObject(stored)) return undefined
|
|
147
|
+
const progress = new Map<string, ReadonlyMap<string, Checkpoint.Progress>>()
|
|
148
|
+
for (const [path, invocations] of Object.entries(stored)) {
|
|
149
|
+
if (!Predicate.isObject(invocations)) return undefined
|
|
150
|
+
const read = new Map<string, Checkpoint.Progress>()
|
|
151
|
+
for (const [id, value] of Object.entries(invocations)) {
|
|
152
|
+
const one = readOne(value)
|
|
153
|
+
if (one === undefined) return undefined
|
|
154
|
+
read.set(id, one)
|
|
155
|
+
}
|
|
156
|
+
progress.set(path, read)
|
|
157
|
+
}
|
|
158
|
+
return progress
|
|
159
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import type { ClusterError, EntityAddress, Snowflake } from "effect/cluster"
|
|
3
3
|
import * as Context from "effect/Context"
|
|
4
4
|
import type * as Effect from "effect/Effect"
|
|
5
|
+
import type * as Option from "effect/Option"
|
|
5
6
|
import * as Schema from "effect/Schema"
|
|
6
7
|
import type * as Public from "../../unstable/cluster/ClusterMachine.js"
|
|
7
8
|
import type { Checkpoint, LoadResult } from "../../unstable/cluster/ClusterMachine.js"
|
|
@@ -13,14 +14,36 @@ export type CommitResult = Public.CommitResult
|
|
|
13
14
|
|
|
14
15
|
export const CommitResult = {
|
|
15
16
|
Committed: (): Public.CommitResult.Committed => ({ _tag: "Committed" }),
|
|
16
|
-
Duplicate: (): Public.CommitResult.Duplicate => ({ _tag: "Duplicate" })
|
|
17
|
+
Duplicate: (): Public.CommitResult.Duplicate => ({ _tag: "Duplicate" }),
|
|
18
|
+
RevisionConflict: (): Public.CommitResult.RevisionConflict => ({ _tag: "RevisionConflict" })
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export class Storage extends Context.Service<Storage, {
|
|
22
|
+
/** Reads the latest checkpoint of an entity. */
|
|
20
23
|
readonly load: (
|
|
24
|
+
address: EntityAddress
|
|
25
|
+
) => Effect.Effect<Option.Option<Checkpoint>, PersistenceError>
|
|
26
|
+
/**
|
|
27
|
+
* Reads the latest checkpoint of an entity, and whether `requestId` was
|
|
28
|
+
* already committed for it, from one consistent view.
|
|
29
|
+
*/
|
|
30
|
+
readonly loadForRequest: (
|
|
21
31
|
address: EntityAddress,
|
|
22
32
|
requestId: Snowflake
|
|
23
33
|
) => Effect.Effect<LoadResult, PersistenceError>
|
|
34
|
+
/**
|
|
35
|
+
* Replaces the checkpoint of an entity in one atomic decision:
|
|
36
|
+
*
|
|
37
|
+
* 1. `Duplicate` when the origin is a request whose id was already committed,
|
|
38
|
+
* whatever the revision. A checkpoint of any other origin is never a
|
|
39
|
+
* duplicate.
|
|
40
|
+
* 2. `RevisionConflict` when the stored revision is not exactly
|
|
41
|
+
* `checkpoint.revision - 1`, where no stored checkpoint counts as `0`.
|
|
42
|
+
* 3. `Committed` otherwise, with the checkpoint stored, and the request id
|
|
43
|
+
* recorded when the origin is a request.
|
|
44
|
+
*
|
|
45
|
+
* Only `Committed` writes.
|
|
46
|
+
*/
|
|
24
47
|
readonly commit: (
|
|
25
48
|
address: EntityAddress,
|
|
26
49
|
checkpoint: Checkpoint
|
|
@@ -33,19 +56,22 @@ export class Accepted extends Schema.TaggedClass<Accepted>("effect/cluster/Clust
|
|
|
33
56
|
) {}
|
|
34
57
|
|
|
35
58
|
export const RejectionReason = Schema.Literals([
|
|
36
|
-
"
|
|
59
|
+
"InvalidEvent",
|
|
60
|
+
"DefinitionIdMismatch",
|
|
37
61
|
"VersionMismatch",
|
|
38
62
|
"InvalidCheckpoint",
|
|
39
63
|
"UnsupportedProcessLocal",
|
|
40
64
|
"TransitionFailure",
|
|
41
65
|
"SnapshotEncodeFailure",
|
|
42
66
|
"PersistenceFailure",
|
|
43
|
-
"
|
|
67
|
+
"RevisionConflict",
|
|
68
|
+
"EmissionFailure",
|
|
69
|
+
"MachineFailed"
|
|
44
70
|
])
|
|
45
71
|
|
|
46
72
|
export type RejectionReason = typeof RejectionReason.Type
|
|
47
73
|
|
|
48
|
-
export class Rejected extends Schema.
|
|
74
|
+
export class Rejected extends Schema.TaggedError<Rejected>("effect/cluster/ClusterMachine/Rejected")(
|
|
49
75
|
"Rejected",
|
|
50
76
|
{
|
|
51
77
|
reason: RejectionReason,
|
|
@@ -54,3 +80,19 @@ export class Rejected extends Schema.TaggedClass<Rejected>("effect/cluster/Clust
|
|
|
54
80
|
) {}
|
|
55
81
|
|
|
56
82
|
export const SendResult = Schema.Union([Accepted, Rejected])
|
|
83
|
+
|
|
84
|
+
export const ReadFailureReason = Schema.Literals(["DefinitionIdMismatch", "VersionMismatch", "InvalidCheckpoint"])
|
|
85
|
+
|
|
86
|
+
export class ReadError extends Schema.TaggedError<ReadError>("effect/cluster/ClusterMachine/ReadError")(
|
|
87
|
+
"ReadError",
|
|
88
|
+
{
|
|
89
|
+
reason: ReadFailureReason,
|
|
90
|
+
message: Schema.String
|
|
91
|
+
}
|
|
92
|
+
) {}
|
|
93
|
+
|
|
94
|
+
export class MachineFailedError extends Schema.TaggedError<MachineFailedError>(
|
|
95
|
+
"effect/cluster/ClusterMachine/MachineFailedError"
|
|
96
|
+
)("MachineFailedError", {
|
|
97
|
+
failure: Schema.String
|
|
98
|
+
}) {}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport-independent RPC proxy for `ClusterMachine`.
|
|
3
|
+
*
|
|
4
|
+
* Exposes `send`, `snapshot`, and `changes` of a `ClusterMachine` as an
|
|
5
|
+
* `effect/rpc` `RpcGroup`, so clients without direct `Sharding` or `Storage`
|
|
6
|
+
* access (such as a browser main thread talking to a Web Worker or
|
|
7
|
+
* `SharedWorker`, a Node worker thread, or a remote HTTP/WebSocket client) can
|
|
8
|
+
* drive and observe Cluster machines with full type safety.
|
|
9
|
+
*
|
|
10
|
+
* @since 0.48.0
|
|
11
|
+
*/
|
|
12
|
+
import { ClusterError, EntityAddress, EntityId, Sharding } from "effect/cluster"
|
|
13
|
+
import * as Effect from "effect/Effect"
|
|
14
|
+
import * as Layer from "effect/Layer"
|
|
15
|
+
import * as Option from "effect/Option"
|
|
16
|
+
import { Rpc, RpcClient, RpcGroup, RpcServer } from "effect/rpc"
|
|
17
|
+
import * as Schema from "effect/Schema"
|
|
18
|
+
import type * as Scope from "effect/Scope"
|
|
19
|
+
import * as Stream from "effect/Stream"
|
|
20
|
+
import type * as Machine from "../../Machine.js"
|
|
21
|
+
import type * as ClusterMachine from "../../unstable/cluster/ClusterMachine.js"
|
|
22
|
+
import type * as Public from "../../unstable/cluster/ClusterMachineProxy.js"
|
|
23
|
+
import * as Cluster from "./cluster.js"
|
|
24
|
+
import { ReadError, Rejected } from "./clusterProtocol.js"
|
|
25
|
+
import * as ClusterRead from "./clusterRead.js"
|
|
26
|
+
import * as Protocol from "./protocol.js"
|
|
27
|
+
|
|
28
|
+
/** Wire schema for a validated committed checkpoint returned by proxy reads. */
|
|
29
|
+
export const EncodedSnapshot = Schema.Union([
|
|
30
|
+
Schema.Struct({
|
|
31
|
+
status: Schema.Literal("active"),
|
|
32
|
+
revision: Schema.Number,
|
|
33
|
+
snapshot: Schema.Unknown
|
|
34
|
+
}),
|
|
35
|
+
Schema.Struct({
|
|
36
|
+
status: Schema.Literal("done"),
|
|
37
|
+
revision: Schema.Number,
|
|
38
|
+
snapshot: Schema.Unknown
|
|
39
|
+
}),
|
|
40
|
+
Schema.Struct({
|
|
41
|
+
status: Schema.Literal("failed"),
|
|
42
|
+
revision: Schema.Number,
|
|
43
|
+
snapshot: Schema.Unknown,
|
|
44
|
+
failure: Schema.String
|
|
45
|
+
}),
|
|
46
|
+
Schema.Struct({
|
|
47
|
+
status: Schema.Literal("stopped"),
|
|
48
|
+
revision: Schema.Number,
|
|
49
|
+
snapshot: Schema.Unknown
|
|
50
|
+
})
|
|
51
|
+
])
|
|
52
|
+
|
|
53
|
+
export const SendErrorSchema = Schema.Union([
|
|
54
|
+
Rejected,
|
|
55
|
+
ClusterError.MailboxFull,
|
|
56
|
+
ClusterError.AlreadyProcessingMessage,
|
|
57
|
+
ClusterError.PersistenceError,
|
|
58
|
+
ClusterError.EntityNotAssignedToRunner
|
|
59
|
+
])
|
|
60
|
+
|
|
61
|
+
export const ReadErrorSchema = Schema.Union([
|
|
62
|
+
ReadError,
|
|
63
|
+
ClusterError.PersistenceError
|
|
64
|
+
])
|
|
65
|
+
|
|
66
|
+
const rpcGroups = new WeakMap<object, RpcGroup.RpcGroup<any>>()
|
|
67
|
+
|
|
68
|
+
export const toRpcGroup = <
|
|
69
|
+
const Type extends string,
|
|
70
|
+
M extends Machine.Machine.Any,
|
|
71
|
+
Services
|
|
72
|
+
>(
|
|
73
|
+
adapter: ClusterMachine.ClusterMachine<Type, M, Services>
|
|
74
|
+
): RpcGroup.RpcGroup<Public.Rpcs<Type, M>> => {
|
|
75
|
+
const cached = rpcGroups.get(adapter)
|
|
76
|
+
if (cached !== undefined) return cached
|
|
77
|
+
const meta = Cluster.getAdapterMeta(adapter)
|
|
78
|
+
const eventSchema = Schema.Union(Protocol.inputEventSchemas(meta.machine))
|
|
79
|
+
const startRpc = Rpc.make(`${meta.type}.start`, {
|
|
80
|
+
payload: meta.machine.input === undefined
|
|
81
|
+
? Schema.Struct({ entityId: Schema.String })
|
|
82
|
+
: Schema.Struct({
|
|
83
|
+
entityId: Schema.String,
|
|
84
|
+
input: meta.machine.input
|
|
85
|
+
}),
|
|
86
|
+
success: Schema.Void,
|
|
87
|
+
error: SendErrorSchema
|
|
88
|
+
})
|
|
89
|
+
const sendRpc = Rpc.make(`${meta.type}.send`, {
|
|
90
|
+
payload: Schema.Struct({
|
|
91
|
+
entityId: Schema.String,
|
|
92
|
+
event: eventSchema
|
|
93
|
+
}),
|
|
94
|
+
success: Schema.Void,
|
|
95
|
+
error: SendErrorSchema
|
|
96
|
+
})
|
|
97
|
+
const snapshotRpc = Rpc.make(`${meta.type}.snapshot`, {
|
|
98
|
+
payload: Schema.Struct({ entityId: Schema.String }),
|
|
99
|
+
success: Schema.Option(EncodedSnapshot),
|
|
100
|
+
error: ReadErrorSchema
|
|
101
|
+
})
|
|
102
|
+
const changesRpc = Rpc.make(`${meta.type}.changes`, {
|
|
103
|
+
payload: Schema.Struct({ entityId: Schema.String }),
|
|
104
|
+
success: EncodedSnapshot,
|
|
105
|
+
error: ReadErrorSchema,
|
|
106
|
+
stream: true
|
|
107
|
+
})
|
|
108
|
+
const group = RpcGroup.make(startRpc, sendRpc, snapshotRpc, changesRpc)
|
|
109
|
+
rpcGroups.set(adapter, group)
|
|
110
|
+
return group as never
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export const layerHandlers = <
|
|
114
|
+
const Type extends string,
|
|
115
|
+
M extends Machine.Machine.Any,
|
|
116
|
+
Services
|
|
117
|
+
>(
|
|
118
|
+
adapter: ClusterMachine.ClusterMachine<Type, M, Services>
|
|
119
|
+
): Layer.Layer<
|
|
120
|
+
Public.RpcHandlers<Type, M>,
|
|
121
|
+
never,
|
|
122
|
+
Sharding.Sharding | ClusterMachine.ReadServices<M>
|
|
123
|
+
> => {
|
|
124
|
+
const meta = Cluster.getAdapterMeta(adapter)
|
|
125
|
+
const group = toRpcGroup(adapter)
|
|
126
|
+
return group.toLayer(
|
|
127
|
+
Effect.gen(function*() {
|
|
128
|
+
const makeClient = yield* adapter.client
|
|
129
|
+
const sharding = yield* Sharding.Sharding
|
|
130
|
+
const context = yield* Effect.context<ClusterMachine.ReadServices<M>>()
|
|
131
|
+
|
|
132
|
+
const readerOf = (entityId: string) => {
|
|
133
|
+
const id = EntityId.make(entityId)
|
|
134
|
+
return ClusterRead.makeEncoded<M>({
|
|
135
|
+
machine: meta.machine,
|
|
136
|
+
definitionId: meta.definitionId,
|
|
137
|
+
version: meta.version,
|
|
138
|
+
address: EntityAddress.make({
|
|
139
|
+
shardId: sharding.getShardId(id, adapter.entity.getShardGroup(id)),
|
|
140
|
+
entityType: adapter.entity.type,
|
|
141
|
+
entityId: id
|
|
142
|
+
}),
|
|
143
|
+
interval: meta.readInterval
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
[`${meta.type}.start`]: (payload: { readonly entityId: string; readonly input?: unknown }) =>
|
|
149
|
+
meta.machine.input === undefined
|
|
150
|
+
? (makeClient(payload.entityId).start as () => Effect.Effect<void, any>)()
|
|
151
|
+
: (makeClient(payload.entityId).start as (input: unknown) => Effect.Effect<void, any>)(payload.input),
|
|
152
|
+
[`${meta.type}.send`]: ({ entityId, event }: { readonly entityId: string; readonly event: never }) =>
|
|
153
|
+
makeClient(entityId).send(event),
|
|
154
|
+
[`${meta.type}.snapshot`]: ({ entityId }: { readonly entityId: string }) =>
|
|
155
|
+
readerOf(entityId).snapshot.pipe(Effect.provideContext(context)),
|
|
156
|
+
[`${meta.type}.changes`]: ({ entityId }: { readonly entityId: string }) =>
|
|
157
|
+
readerOf(entityId).changes.pipe(Stream.provideContext(context))
|
|
158
|
+
} as never
|
|
159
|
+
})
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export const toLayer = <
|
|
164
|
+
const Type extends string,
|
|
165
|
+
M extends Machine.Machine.Any,
|
|
166
|
+
Services
|
|
167
|
+
>(
|
|
168
|
+
adapter: ClusterMachine.ClusterMachine<Type, M, Services>,
|
|
169
|
+
options?: Public.ServerOptions
|
|
170
|
+
): Layer.Layer<
|
|
171
|
+
never,
|
|
172
|
+
never,
|
|
173
|
+
| RpcServer.Protocol
|
|
174
|
+
| Sharding.Sharding
|
|
175
|
+
| ClusterMachine.ReadServices<M>
|
|
176
|
+
| Public.ServerServices<M>
|
|
177
|
+
> =>
|
|
178
|
+
RpcServer.layer(toRpcGroup(adapter), options).pipe(
|
|
179
|
+
Layer.provide(layerHandlers(adapter))
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
export const client = <
|
|
183
|
+
const Type extends string,
|
|
184
|
+
M extends Machine.Machine.Any,
|
|
185
|
+
Services
|
|
186
|
+
>(
|
|
187
|
+
adapter: ClusterMachine.ClusterMachine<Type, M, Services>
|
|
188
|
+
): Effect.Effect<
|
|
189
|
+
(entityId: string) => Public.Client<M>,
|
|
190
|
+
never,
|
|
191
|
+
RpcClient.Protocol | Scope.Scope
|
|
192
|
+
> =>
|
|
193
|
+
Effect.gen(function*() {
|
|
194
|
+
const meta = Cluster.getAdapterMeta(adapter)
|
|
195
|
+
const group = toRpcGroup(adapter)
|
|
196
|
+
const rpc = (yield* RpcClient.make(group)) as Record<string, (payload: any) => any>
|
|
197
|
+
const startTag = `${meta.type}.start`
|
|
198
|
+
const sendTag = `${meta.type}.send`
|
|
199
|
+
const snapshotTag = `${meta.type}.snapshot`
|
|
200
|
+
const changesTag = `${meta.type}.changes`
|
|
201
|
+
|
|
202
|
+
return (entityId: string): Public.Client<M> => {
|
|
203
|
+
const start = (...args: ReadonlyArray<unknown>) =>
|
|
204
|
+
meta.decodeInput(args[0]).pipe(
|
|
205
|
+
Effect.flatMap((decoded) =>
|
|
206
|
+
rpc[startTag]!(meta.machine.input === undefined ? { entityId } : { entityId, input: decoded })
|
|
207
|
+
)
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
const send = (event: unknown) =>
|
|
211
|
+
meta.decodeEvent(event).pipe(
|
|
212
|
+
Effect.flatMap((decoded) => rpc[sendTag]!({ entityId, event: decoded }))
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
const snapshot = (rpc[snapshotTag]!({ entityId }) as Effect.Effect<
|
|
216
|
+
Option.Option<ClusterRead.WireSnapshot>,
|
|
217
|
+
any
|
|
218
|
+
>).pipe(
|
|
219
|
+
Effect.flatMap(Option.match({
|
|
220
|
+
onNone: () => Effect.succeedNone,
|
|
221
|
+
onSome: (wire) => Effect.asSome(ClusterRead.decodeWire<M>(meta.machine as M, wire))
|
|
222
|
+
}))
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
const changes = (rpc[changesTag]!({ entityId }) as Stream.Stream<ClusterRead.WireSnapshot, any>).pipe(
|
|
226
|
+
Stream.mapEffect((wire) => ClusterRead.decodeWire<M>(meta.machine as M, wire))
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
const join = ClusterRead.joinChanges<M, any, any>(changes)
|
|
230
|
+
|
|
231
|
+
return { entityId, start, send, snapshot, changes, join } as Public.Client<M>
|
|
232
|
+
}
|
|
233
|
+
})
|