@typeonce/effect-machine 0.47.0 → 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.
Files changed (334) hide show
  1. package/AGENTS.md +374 -22
  2. package/CLAUDE.md +374 -22
  3. package/README.md +56 -7
  4. package/ai-docs/src/10_modeling/01_create-a-machine.ts +3 -3
  5. package/ai-docs/src/10_modeling/index.md +64 -17
  6. package/ai-docs/src/20_root-api/index.md +2 -2
  7. package/ai-docs/src/35_cluster/index.md +213 -0
  8. package/ai-docs/src/37_new-host/index.md +90 -0
  9. package/dist/LocalMachine.d.ts +417 -0
  10. package/dist/LocalMachine.d.ts.map +1 -0
  11. package/dist/LocalMachine.js +193 -0
  12. package/dist/LocalMachine.js.map +1 -0
  13. package/dist/Machine.d.ts +1122 -814
  14. package/dist/Machine.d.ts.map +1 -1
  15. package/dist/Machine.js +195 -186
  16. package/dist/Machine.js.map +1 -1
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/internal/machine/atom.d.ts +8 -10
  22. package/dist/internal/machine/atom.d.ts.map +1 -1
  23. package/dist/internal/machine/atom.js +7 -6
  24. package/dist/internal/machine/atom.js.map +1 -1
  25. package/dist/internal/machine/capability.d.ts +101 -0
  26. package/dist/internal/machine/capability.d.ts.map +1 -0
  27. package/dist/internal/machine/capability.js +70 -0
  28. package/dist/internal/machine/capability.js.map +1 -0
  29. package/dist/internal/machine/{invocationDefinition.d.ts → capturedInvocation.d.ts} +11 -11
  30. package/dist/internal/machine/capturedInvocation.d.ts.map +1 -0
  31. package/dist/internal/machine/{invocationDefinition.js → capturedInvocation.js} +2 -2
  32. package/dist/internal/machine/capturedInvocation.js.map +1 -0
  33. package/dist/internal/machine/child.d.ts +14 -0
  34. package/dist/internal/machine/child.d.ts.map +1 -0
  35. package/dist/internal/machine/child.js +19 -0
  36. package/dist/internal/machine/child.js.map +1 -0
  37. package/dist/internal/machine/childRegistry.d.ts +0 -2
  38. package/dist/internal/machine/childRegistry.d.ts.map +1 -1
  39. package/dist/internal/machine/childRegistry.js +2 -2
  40. package/dist/internal/machine/childRegistry.js.map +1 -1
  41. package/dist/internal/machine/cluster.d.ts +19 -4
  42. package/dist/internal/machine/cluster.d.ts.map +1 -1
  43. package/dist/internal/machine/cluster.js +263 -110
  44. package/dist/internal/machine/cluster.js.map +1 -1
  45. package/dist/internal/machine/clusterChild.d.ts +66 -0
  46. package/dist/internal/machine/clusterChild.d.ts.map +1 -0
  47. package/dist/internal/machine/clusterChild.js +105 -0
  48. package/dist/internal/machine/clusterChild.js.map +1 -0
  49. package/dist/internal/machine/clusterFailure.d.ts +14 -0
  50. package/dist/internal/machine/clusterFailure.d.ts.map +1 -0
  51. package/dist/internal/machine/clusterFailure.js +44 -0
  52. package/dist/internal/machine/clusterFailure.js.map +1 -0
  53. package/dist/internal/machine/clusterInstance.d.ts +194 -0
  54. package/dist/internal/machine/clusterInstance.d.ts.map +1 -0
  55. package/dist/internal/machine/clusterInstance.js +1076 -0
  56. package/dist/internal/machine/clusterInstance.js.map +1 -0
  57. package/dist/internal/machine/clusterKeepAlive.d.ts +47 -0
  58. package/dist/internal/machine/clusterKeepAlive.d.ts.map +1 -0
  59. package/dist/internal/machine/clusterKeepAlive.js +72 -0
  60. package/dist/internal/machine/clusterKeepAlive.js.map +1 -0
  61. package/dist/internal/machine/clusterProgress.d.ts +73 -0
  62. package/dist/internal/machine/clusterProgress.d.ts.map +1 -0
  63. package/dist/internal/machine/clusterProgress.js +116 -0
  64. package/dist/internal/machine/clusterProgress.js.map +1 -0
  65. package/dist/internal/machine/clusterProtocol.d.ts +37 -4
  66. package/dist/internal/machine/clusterProtocol.d.ts.map +1 -1
  67. package/dist/internal/machine/clusterProtocol.js +18 -4
  68. package/dist/internal/machine/clusterProtocol.js.map +1 -1
  69. package/dist/internal/machine/clusterProxy.d.ts +46 -0
  70. package/dist/internal/machine/clusterProxy.d.ts.map +1 -0
  71. package/dist/internal/machine/clusterProxy.js +150 -0
  72. package/dist/internal/machine/clusterProxy.js.map +1 -0
  73. package/dist/internal/machine/clusterRead.d.ts +61 -0
  74. package/dist/internal/machine/clusterRead.d.ts.map +1 -0
  75. package/dist/internal/machine/clusterRead.js +108 -0
  76. package/dist/internal/machine/clusterRead.js.map +1 -0
  77. package/dist/internal/machine/clusterSqlStorage.d.ts +16 -0
  78. package/dist/internal/machine/clusterSqlStorage.d.ts.map +1 -0
  79. package/dist/internal/machine/clusterSqlStorage.js +138 -0
  80. package/dist/internal/machine/clusterSqlStorage.js.map +1 -0
  81. package/dist/internal/machine/clusterStorageCheck.d.ts +19 -0
  82. package/dist/internal/machine/clusterStorageCheck.d.ts.map +1 -0
  83. package/dist/internal/machine/clusterStorageCheck.js +186 -0
  84. package/dist/internal/machine/clusterStorageCheck.js.map +1 -0
  85. package/dist/internal/machine/clusterTarget.d.ts +74 -0
  86. package/dist/internal/machine/clusterTarget.d.ts.map +1 -0
  87. package/dist/internal/machine/clusterTarget.js +81 -0
  88. package/dist/internal/machine/clusterTarget.js.map +1 -0
  89. package/dist/internal/machine/clusterTimer.d.ts +21 -0
  90. package/dist/internal/machine/clusterTimer.d.ts.map +1 -0
  91. package/dist/internal/machine/clusterTimer.js +50 -0
  92. package/dist/internal/machine/clusterTimer.js.map +1 -0
  93. package/dist/internal/machine/clusterWake.d.ts +61 -0
  94. package/dist/internal/machine/clusterWake.d.ts.map +1 -0
  95. package/dist/internal/machine/clusterWake.js +81 -0
  96. package/dist/internal/machine/clusterWake.js.map +1 -0
  97. package/dist/internal/machine/clusterWork.d.ts +130 -0
  98. package/dist/internal/machine/clusterWork.d.ts.map +1 -0
  99. package/dist/internal/machine/clusterWork.js +191 -0
  100. package/dist/internal/machine/clusterWork.js.map +1 -0
  101. package/dist/internal/machine/command.d.ts +4 -4
  102. package/dist/internal/machine/command.d.ts.map +1 -1
  103. package/dist/internal/machine/commandRuntime.d.ts +8 -6
  104. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  105. package/dist/internal/machine/commandRuntime.js +1 -5
  106. package/dist/internal/machine/commandRuntime.js.map +1 -1
  107. package/dist/internal/machine/configuration.d.ts +0 -13
  108. package/dist/internal/machine/configuration.d.ts.map +1 -1
  109. package/dist/internal/machine/configuration.js +0 -151
  110. package/dist/internal/machine/configuration.js.map +1 -1
  111. package/dist/internal/machine/errors.d.ts +11 -2
  112. package/dist/internal/machine/errors.d.ts.map +1 -1
  113. package/dist/internal/machine/errors.js +11 -2
  114. package/dist/internal/machine/errors.js.map +1 -1
  115. package/dist/internal/machine/executionPlan.d.ts +45 -3
  116. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  117. package/dist/internal/machine/executionPlan.js +328 -191
  118. package/dist/internal/machine/executionPlan.js.map +1 -1
  119. package/dist/internal/machine/implementation.d.ts +6 -6
  120. package/dist/internal/machine/implementation.d.ts.map +1 -1
  121. package/dist/internal/machine/inspectionRuntime.d.ts +1 -1
  122. package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -1
  123. package/dist/internal/machine/invocationChanges.d.ts +60 -0
  124. package/dist/internal/machine/invocationChanges.d.ts.map +1 -0
  125. package/dist/internal/machine/invocationChanges.js +54 -0
  126. package/dist/internal/machine/invocationChanges.js.map +1 -0
  127. package/dist/internal/machine/{activities.d.ts → invocationDefinitions.d.ts} +8 -8
  128. package/dist/internal/machine/invocationDefinitions.d.ts.map +1 -0
  129. package/dist/internal/machine/{activities.js → invocationDefinitions.js} +5 -5
  130. package/dist/internal/machine/invocationDefinitions.js.map +1 -0
  131. package/dist/internal/machine/invocationEvent.d.ts +16 -7
  132. package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
  133. package/dist/internal/machine/invocationEvent.js +26 -12
  134. package/dist/internal/machine/invocationEvent.js.map +1 -1
  135. package/dist/internal/machine/invocationOutcome.d.ts +89 -0
  136. package/dist/internal/machine/invocationOutcome.d.ts.map +1 -0
  137. package/dist/internal/machine/invocationOutcome.js +76 -0
  138. package/dist/internal/machine/invocationOutcome.js.map +1 -0
  139. package/dist/internal/machine/invocationResolution.d.ts +137 -0
  140. package/dist/internal/machine/invocationResolution.d.ts.map +1 -0
  141. package/dist/internal/machine/invocationResolution.js +150 -0
  142. package/dist/internal/machine/invocationResolution.js.map +1 -0
  143. package/dist/internal/machine/logicScope.d.ts +24 -0
  144. package/dist/internal/machine/logicScope.d.ts.map +1 -0
  145. package/dist/internal/machine/logicScope.js +33 -0
  146. package/dist/internal/machine/logicScope.js.map +1 -0
  147. package/dist/internal/machine/machine.d.ts +17 -23
  148. package/dist/internal/machine/machine.d.ts.map +1 -1
  149. package/dist/internal/machine/machine.js +24 -40
  150. package/dist/internal/machine/machine.js.map +1 -1
  151. package/dist/internal/machine/observation.d.ts +3 -0
  152. package/dist/internal/machine/observation.d.ts.map +1 -1
  153. package/dist/internal/machine/observation.js +56 -2
  154. package/dist/internal/machine/observation.js.map +1 -1
  155. package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -1
  156. package/dist/internal/machine/plannedSnapshot.js +11 -20
  157. package/dist/internal/machine/plannedSnapshot.js.map +1 -1
  158. package/dist/internal/machine/planner.d.ts +36 -23
  159. package/dist/internal/machine/planner.d.ts.map +1 -1
  160. package/dist/internal/machine/planner.js +15 -8
  161. package/dist/internal/machine/planner.js.map +1 -1
  162. package/dist/internal/machine/process.d.ts +17 -9
  163. package/dist/internal/machine/process.d.ts.map +1 -1
  164. package/dist/internal/machine/process.js +43 -503
  165. package/dist/internal/machine/process.js.map +1 -1
  166. package/dist/internal/machine/protocol.d.ts +1 -1
  167. package/dist/internal/machine/protocol.d.ts.map +1 -1
  168. package/dist/internal/machine/protocol.js +48 -10
  169. package/dist/internal/machine/protocol.js.map +1 -1
  170. package/dist/internal/machine/runtime.d.ts +20 -16
  171. package/dist/internal/machine/runtime.d.ts.map +1 -1
  172. package/dist/internal/machine/runtime.js +93 -88
  173. package/dist/internal/machine/runtime.js.map +1 -1
  174. package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -1
  175. package/dist/internal/machine/runtimeCompiled.js +359 -120
  176. package/dist/internal/machine/runtimeCompiled.js.map +1 -1
  177. package/dist/internal/machine/runtimeProtocol.d.ts +151 -82
  178. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
  179. package/dist/internal/machine/runtimeProtocol.js +236 -219
  180. package/dist/internal/machine/runtimeProtocol.js.map +1 -1
  181. package/dist/internal/machine/stateDefinition.d.ts +0 -1
  182. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  183. package/dist/internal/machine/stateDefinition.js +0 -1
  184. package/dist/internal/machine/stateDefinition.js.map +1 -1
  185. package/dist/internal/machine/step.d.ts +486 -0
  186. package/dist/internal/machine/step.d.ts.map +1 -0
  187. package/dist/internal/machine/step.js +957 -0
  188. package/dist/internal/machine/step.js.map +1 -0
  189. package/dist/internal/machine/stepHost.d.ts +33 -0
  190. package/dist/internal/machine/stepHost.d.ts.map +1 -0
  191. package/dist/internal/machine/stepHost.js +501 -0
  192. package/dist/internal/machine/stepHost.js.map +1 -0
  193. package/dist/internal/machine/symbols.d.ts +2 -11
  194. package/dist/internal/machine/symbols.d.ts.map +1 -1
  195. package/dist/internal/machine/symbols.js +2 -2
  196. package/dist/internal/machine/symbols.js.map +1 -1
  197. package/dist/internal/machine/transition.d.ts +7 -7
  198. package/dist/internal/machine/transition.d.ts.map +1 -1
  199. package/dist/internal/testing/machine/check.d.ts.map +1 -1
  200. package/dist/internal/testing/machine/check.js +217 -25
  201. package/dist/internal/testing/machine/check.js.map +1 -1
  202. package/dist/internal/testing/machine/exploration.d.ts +19 -2
  203. package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
  204. package/dist/internal/testing/machine/exploration.js +14 -3
  205. package/dist/internal/testing/machine/exploration.js.map +1 -1
  206. package/dist/internal/testing/machine/format.js.map +1 -1
  207. package/dist/internal/testing/machine/probe.d.ts.map +1 -1
  208. package/dist/internal/testing/machine/probe.js +1 -1
  209. package/dist/internal/testing/machine/probe.js.map +1 -1
  210. package/dist/internal/testing/machine/runtime.d.ts +2 -2
  211. package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
  212. package/dist/internal/testing/machine/runtime.js +10 -3
  213. package/dist/internal/testing/machine/runtime.js.map +1 -1
  214. package/dist/internal/testing/machine/trace.d.ts +38 -3
  215. package/dist/internal/testing/machine/trace.d.ts.map +1 -1
  216. package/dist/internal/testing/machine/trace.js +180 -33
  217. package/dist/internal/testing/machine/trace.js.map +1 -1
  218. package/dist/internal/testing/machine/transitionCoverage.d.ts +1 -1
  219. package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -1
  220. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  221. package/dist/internal/testing/machine/verification.js +9 -1
  222. package/dist/internal/testing/machine/verification.js.map +1 -1
  223. package/dist/internal/testing/machine/work.d.ts +47 -0
  224. package/dist/internal/testing/machine/work.d.ts.map +1 -0
  225. package/dist/internal/testing/machine/work.js +74 -0
  226. package/dist/internal/testing/machine/work.js.map +1 -0
  227. package/dist/testing/MachineTest.d.ts +253 -37
  228. package/dist/testing/MachineTest.d.ts.map +1 -1
  229. package/dist/testing/MachineTest.js +81 -9
  230. package/dist/testing/MachineTest.js.map +1 -1
  231. package/dist/unstable/cluster/ClusterMachine.d.ts +1071 -41
  232. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  233. package/dist/unstable/cluster/ClusterMachine.js +441 -12
  234. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  235. package/dist/unstable/cluster/ClusterMachineProxy.d.ts +257 -0
  236. package/dist/unstable/cluster/ClusterMachineProxy.d.ts.map +1 -0
  237. package/dist/unstable/cluster/ClusterMachineProxy.js +73 -0
  238. package/dist/unstable/cluster/ClusterMachineProxy.js.map +1 -0
  239. package/dist/unstable/cluster/SqlMachineStorage.d.ts +125 -0
  240. package/dist/unstable/cluster/SqlMachineStorage.d.ts.map +1 -0
  241. package/dist/unstable/cluster/SqlMachineStorage.js +62 -0
  242. package/dist/unstable/cluster/SqlMachineStorage.js.map +1 -0
  243. package/dist/unstable/cluster/index.d.ts +13 -0
  244. package/dist/unstable/cluster/index.d.ts.map +1 -1
  245. package/dist/unstable/cluster/index.js +13 -0
  246. package/dist/unstable/cluster/index.js.map +1 -1
  247. package/dist/unstable/reactivity/AtomMachine.d.ts +21 -26
  248. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  249. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  250. package/package.json +5 -3
  251. package/src/LocalMachine.ts +611 -0
  252. package/src/Machine.ts +1383 -1336
  253. package/src/index.ts +1 -0
  254. package/src/internal/machine/atom.ts +20 -49
  255. package/src/internal/machine/capability.ts +123 -0
  256. package/src/internal/machine/{invocationDefinition.ts → capturedInvocation.ts} +13 -13
  257. package/src/internal/machine/child.ts +25 -0
  258. package/src/internal/machine/childRegistry.ts +1 -4
  259. package/src/internal/machine/cluster.ts +420 -205
  260. package/src/internal/machine/clusterChild.ts +117 -0
  261. package/src/internal/machine/clusterFailure.ts +53 -0
  262. package/src/internal/machine/clusterInstance.ts +1779 -0
  263. package/src/internal/machine/clusterKeepAlive.ts +93 -0
  264. package/src/internal/machine/clusterProgress.ts +159 -0
  265. package/src/internal/machine/clusterProtocol.ts +46 -4
  266. package/src/internal/machine/clusterProxy.ts +233 -0
  267. package/src/internal/machine/clusterRead.ts +207 -0
  268. package/src/internal/machine/clusterSqlStorage.ts +181 -0
  269. package/src/internal/machine/clusterStorageCheck.ts +274 -0
  270. package/src/internal/machine/clusterTarget.ts +154 -0
  271. package/src/internal/machine/clusterTimer.ts +58 -0
  272. package/src/internal/machine/clusterWake.ts +100 -0
  273. package/src/internal/machine/clusterWork.ts +269 -0
  274. package/src/internal/machine/command.ts +4 -4
  275. package/src/internal/machine/commandRuntime.ts +12 -24
  276. package/src/internal/machine/configuration.ts +0 -181
  277. package/src/internal/machine/errors.ts +11 -2
  278. package/src/internal/machine/executionPlan.ts +493 -263
  279. package/src/internal/machine/implementation.ts +6 -6
  280. package/src/internal/machine/inspectionRuntime.ts +1 -1
  281. package/src/internal/machine/invocationChanges.ts +89 -0
  282. package/src/internal/machine/{activities.ts → invocationDefinitions.ts} +10 -10
  283. package/src/internal/machine/invocationEvent.ts +35 -12
  284. package/src/internal/machine/invocationOutcome.ts +145 -0
  285. package/src/internal/machine/invocationResolution.ts +307 -0
  286. package/src/internal/machine/logicScope.ts +47 -0
  287. package/src/internal/machine/machine.ts +35 -100
  288. package/src/internal/machine/observation.ts +67 -2
  289. package/src/internal/machine/plannedSnapshot.ts +17 -20
  290. package/src/internal/machine/planner.ts +75 -99
  291. package/src/internal/machine/process.ts +78 -722
  292. package/src/internal/machine/protocol.ts +70 -26
  293. package/src/internal/machine/runtime.ts +198 -195
  294. package/src/internal/machine/runtimeCompiled.ts +496 -171
  295. package/src/internal/machine/runtimeProtocol.ts +416 -305
  296. package/src/internal/machine/stateDefinition.ts +0 -5
  297. package/src/internal/machine/step.ts +1518 -0
  298. package/src/internal/machine/stepHost.ts +634 -0
  299. package/src/internal/machine/symbols.ts +3 -14
  300. package/src/internal/machine/transition.ts +11 -11
  301. package/src/internal/testing/machine/check.ts +240 -36
  302. package/src/internal/testing/machine/exploration.ts +42 -3
  303. package/src/internal/testing/machine/format.ts +1 -1
  304. package/src/internal/testing/machine/probe.ts +2 -1
  305. package/src/internal/testing/machine/runtime.ts +15 -5
  306. package/src/internal/testing/machine/trace.ts +261 -41
  307. package/src/internal/testing/machine/transitionCoverage.ts +1 -1
  308. package/src/internal/testing/machine/verification.ts +11 -5
  309. package/src/internal/testing/machine/work.ts +101 -0
  310. package/src/testing/MachineTest.ts +268 -44
  311. package/src/unstable/cluster/ClusterMachine.ts +1183 -59
  312. package/src/unstable/cluster/ClusterMachineProxy.ts +372 -0
  313. package/src/unstable/cluster/SqlMachineStorage.ts +136 -0
  314. package/src/unstable/cluster/index.ts +15 -0
  315. package/src/unstable/reactivity/AtomMachine.ts +27 -88
  316. package/dist/internal/machine/activities.d.ts.map +0 -1
  317. package/dist/internal/machine/activities.js.map +0 -1
  318. package/dist/internal/machine/invocation.d.ts +0 -33
  319. package/dist/internal/machine/invocation.d.ts.map +0 -1
  320. package/dist/internal/machine/invocation.js +0 -220
  321. package/dist/internal/machine/invocation.js.map +0 -1
  322. package/dist/internal/machine/invocationDefinition.d.ts.map +0 -1
  323. package/dist/internal/machine/invocationDefinition.js.map +0 -1
  324. package/dist/internal/machine/requirements.d.ts +0 -4
  325. package/dist/internal/machine/requirements.d.ts.map +0 -1
  326. package/dist/internal/machine/requirements.js +0 -2
  327. package/dist/internal/machine/requirements.js.map +0 -1
  328. package/dist/internal/machine/runtimeGeneric.d.ts +0 -3
  329. package/dist/internal/machine/runtimeGeneric.d.ts.map +0 -1
  330. package/dist/internal/machine/runtimeGeneric.js +0 -465
  331. package/dist/internal/machine/runtimeGeneric.js.map +0 -1
  332. package/src/internal/machine/invocation.ts +0 -376
  333. package/src/internal/machine/requirements.ts +0 -11
  334. package/src/internal/machine/runtimeGeneric.ts +0 -797
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Targets that name a Cluster machine by its entity type and id, and the
3
+ * messages that carry the events a step sends to them.
4
+ *
5
+ * A step that sends to a target saves the event as a persisted request to the
6
+ * target's entity, in the `MessageStorage` transaction of its checkpoint. The
7
+ * request has an id of its own, which the target records when it applies it, so
8
+ * a request that Cluster delivers again changes nothing.
9
+ *
10
+ * @since 0.48.0
11
+ */
12
+ import { Envelope, Message, Sharding } from "effect/cluster"
13
+ import type { Entity, EntityAddress, MessageStorage } from "effect/cluster"
14
+ import { EntityAddress as Address, EntityId } from "effect/cluster"
15
+ import * as Context from "effect/Context"
16
+ import * as Effect from "effect/Effect"
17
+ import * as Headers from "effect/http/Headers"
18
+ import * as Option from "effect/Option"
19
+ import type { Rpc } from "effect/rpc"
20
+ import * as Schedule from "effect/Schedule"
21
+ import type { Machine, MachineTarget } from "../../Machine.js"
22
+ import type { Rejected } from "./clusterProtocol.js"
23
+ import { type MachineSchemaDecodeError, StoppedError } from "./errors.js"
24
+
25
+ /** @internal */
26
+ export const TypeId = "~effect/cluster/ClusterMachine/Target"
27
+
28
+ /**
29
+ * What a target needs from the machine it names.
30
+ *
31
+ * @internal
32
+ */
33
+ export interface Protocol {
34
+ // oxlint-disable-next-line typescript/no-explicit-any -- any protocol of a machine
35
+ readonly entity: Entity.Entity<any, any>
36
+ readonly machine: Machine.Any
37
+ readonly definitionId: string
38
+ readonly version: string
39
+
40
+ /** Decodes an event as the client of the target does before it sends it. */
41
+ readonly decode: (event: unknown) => Effect.Effect<unknown, Rejected>
42
+
43
+ /** Decodes an event for a waiting caller, failing with `MachineSchemaDecodeError` when refused. */
44
+ readonly decodeEvent: (event: unknown) => Effect.Effect<unknown, MachineSchemaDecodeError>
45
+
46
+ /** Sends a decoded event through the client of the target, from wherever `Sharding` is. */
47
+ readonly send: (entityId: string, event: unknown) => Effect.Effect<void, Rejected | unknown, Sharding.Sharding>
48
+ }
49
+
50
+ /** @internal */
51
+ export class Target implements MachineTarget<any> {
52
+ readonly [TypeId]: typeof TypeId = TypeId
53
+
54
+ constructor(readonly protocol: Protocol, readonly entityId: string) {}
55
+
56
+ get entityType(): string {
57
+ return this.protocol.entity.type
58
+ }
59
+
60
+ get id(): string {
61
+ return this.protocol.definitionId
62
+ }
63
+
64
+ /** A Cluster machine has no session, so the address names it. */
65
+ get sessionId(): string {
66
+ return `${this.entityType}/${this.entityId}`
67
+ }
68
+
69
+ readonly send = (event: unknown): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> =>
70
+ Effect.flatMap(
71
+ Effect.serviceOption(Sharding.Sharding),
72
+ Option.match({
73
+ onNone: () =>
74
+ Effect.die(
75
+ new Error(
76
+ `A ClusterMachine target sends to ${this.sessionId} only where a Cluster runner provides Sharding`
77
+ )
78
+ ),
79
+ onSome: (sharding) =>
80
+ Effect.flatMap(this.protocol.decodeEvent(event), (decoded) =>
81
+ this.protocol.send(this.entityId, decoded).pipe(
82
+ Effect.provideService(Sharding.Sharding, sharding),
83
+ // A runner that cannot take the request now takes it later.
84
+ Effect.retry({ while: (error) => !isRejected(error), schedule: Schedule.spaced("1 second") }),
85
+ Effect.catch((error) =>
86
+ isRejected(error) && error.reason === "MachineFailed"
87
+ ? Effect.fail(new StoppedError())
88
+ : Effect.die(error)
89
+ )
90
+ ))
91
+ })
92
+ )
93
+ }
94
+
95
+ const isRejected = (error: unknown): error is Rejected =>
96
+ typeof error === "object" && error !== null && "_tag" in error && error._tag === "Rejected"
97
+
98
+ /** @internal */
99
+ export const isTarget = (value: unknown): value is Target => value instanceof Target
100
+
101
+ /**
102
+ * An event that a step sends, decoded by the protocol of its target.
103
+ *
104
+ * @internal
105
+ */
106
+ export interface Sent {
107
+ readonly target: Target
108
+ readonly event: unknown
109
+ }
110
+
111
+ /** @internal */
112
+ export const addressOf = (sharding: Sharding.Sharding["Service"], target: Target): EntityAddress.EntityAddress => {
113
+ const id = EntityId.make(target.entityId)
114
+ const { entity } = target.protocol
115
+ return Address.make({
116
+ shardId: sharding.getShardId(id, entity.getShardGroup(id)),
117
+ entityType: entity.type,
118
+ entityId: id
119
+ })
120
+ }
121
+
122
+ /**
123
+ * Saves a persisted request to a target, of one of the RPCs of its entity. It
124
+ * joins an open transaction of `MessageStorage`.
125
+ *
126
+ * @internal
127
+ */
128
+ export type Save = (target: Target, tag: string, payload: unknown) => Effect.Effect<void, unknown>
129
+
130
+ /** @internal */
131
+ export const makeSave = (options: {
132
+ readonly messageStorage: MessageStorage.MessageStorage["Service"]
133
+ readonly sharding: Sharding.Sharding["Service"]
134
+ }): Save =>
135
+ (target, tag, payload) =>
136
+ Effect.flatMap(options.sharding.getSnowflake, (requestId) => {
137
+ const rpc = target.protocol.entity.protocol.requests.get(tag) as Rpc.AnyWithProps
138
+ return options.messageStorage.saveRequest(
139
+ new Message.OutgoingRequest<any>({
140
+ envelope: Envelope.makeRequest<any>({
141
+ requestId,
142
+ address: addressOf(options.sharding, target),
143
+ tag,
144
+ payload,
145
+ headers: Headers.empty
146
+ }),
147
+ context: Context.empty() as Context.Context<unknown>,
148
+ lastReceivedReply: Option.none(),
149
+ rpc,
150
+ respond: () => Effect.void,
151
+ annotations: rpc.annotations
152
+ })
153
+ )
154
+ }).pipe(Effect.asVoid)
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Due times of the timers of a Cluster machine.
3
+ *
4
+ * A timer waits for an instant that is stored with its state, not for a
5
+ * duration. Its duration is read once, from the timer that a step lists, before
6
+ * the checkpoint that stores the due time is committed. Nothing reads it again
7
+ * while that due time is stored.
8
+ *
9
+ * @since 0.48.0
10
+ */
11
+ import * as Duration from "effect/Duration"
12
+ import * as Effect from "effect/Effect"
13
+ import * as Exit from "effect/Exit"
14
+ import * as ClusterProgress from "./clusterProgress.js"
15
+ import type * as Step from "./step.js"
16
+
17
+ const dueAt = (now: number, duration: Duration.Duration): number =>
18
+ Math.min(Math.ceil(now + Math.max(Duration.toMillis(duration), 0)), ClusterProgress.maxDueAt)
19
+
20
+ /**
21
+ * Gives a due time, from now on, to every timer among `running` that has no
22
+ * progress, in start order. A timer whose duration cannot be read keeps none:
23
+ * its host settles it with that failure where it would have started. The
24
+ * progress is returned as it is when no timer needed one.
25
+ *
26
+ * @internal
27
+ */
28
+ export const schedule = (
29
+ progress: ClusterProgress.Progress,
30
+ running: ReadonlyArray<Step.Work>,
31
+ now: number
32
+ ): ClusterProgress.Progress => {
33
+ let scheduled = progress
34
+ for (const work of running) {
35
+ if (work._tag !== "Timer" || ClusterProgress.get(scheduled, work) !== undefined) continue
36
+ const duration = work.duration()
37
+ if (Exit.isSuccess(duration)) {
38
+ scheduled = ClusterProgress.wait(scheduled, work, dueAt(now, duration.value))
39
+ }
40
+ }
41
+ return scheduled
42
+ }
43
+
44
+ /**
45
+ * Waits until the clock has reached a due time, and no longer when it already
46
+ * has. The clock is read again after each sleep, because a wall clock can be set
47
+ * back and a sleep can end early.
48
+ *
49
+ * @internal
50
+ */
51
+ export const waitUntil = (dueAt: number): Effect.Effect<void> =>
52
+ Effect.clockWith((clock) => {
53
+ const wait: Effect.Effect<void> = Effect.suspend(() => {
54
+ const remaining = dueAt - clock.currentTimeMillisUnsafe()
55
+ return remaining <= 0 ? Effect.void : Effect.andThen(clock.sleep(Duration.millis(remaining)), wait)
56
+ })
57
+ return wait
58
+ })
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Delayed messages that bring a Cluster machine back for a long timer.
3
+ *
4
+ * A timer that waits longer than the in-memory threshold of `DurableClock` does
5
+ * not keep its entity alive. The step that stores its due time also saves a
6
+ * message to the entity, delivered by Cluster at that time through `DeliverAt`,
7
+ * so the entity may leave memory and returns when the timer is due, here or on
8
+ * another runner.
9
+ *
10
+ * @since 0.48.0
11
+ */
12
+ import { ClusterSchema, DeliverAt, Envelope, Message } from "effect/cluster"
13
+ import type { EntityAddress, MessageStorage, Sharding } from "effect/cluster"
14
+ import * as Context from "effect/Context"
15
+ import * as DateTime from "effect/DateTime"
16
+ import * as Duration from "effect/Duration"
17
+ import * as Effect from "effect/Effect"
18
+ import * as Headers from "effect/http/Headers"
19
+ import * as Option from "effect/Option"
20
+ import * as PrimaryKey from "effect/PrimaryKey"
21
+ import { Rpc } from "effect/rpc"
22
+ import * as Schema from "effect/Schema"
23
+ import * as DurableClock from "effect/workflow/DurableClock"
24
+ import type { Timer } from "./clusterInstance.js"
25
+
26
+ /**
27
+ * The payload of the message that wakes an entity for one timer. Its path, id,
28
+ * and due time name one activation of the timer, so the message of an
29
+ * activation that ended is recognized and ignored, and saving it again is a
30
+ * duplicate.
31
+ *
32
+ * @internal
33
+ */
34
+ export class Wake extends Schema.Class<Wake>("effect/cluster/ClusterMachine/Wake")({
35
+ path: Schema.String,
36
+ id: Schema.String,
37
+ dueAt: Schema.Number
38
+ }) {
39
+ [PrimaryKey.symbol]() {
40
+ return JSON.stringify([this.path, this.id, this.dueAt])
41
+ }
42
+ [DeliverAt.symbol]() {
43
+ return DateTime.makeUnsafe(this.dueAt)
44
+ }
45
+ }
46
+
47
+ /** @internal */
48
+ export const WakeRpc = Rpc.make("wake", { payload: Wake })
49
+ .annotate(ClusterSchema.Persisted, true)
50
+ .annotate(ClusterSchema.Uninterruptible, true)
51
+
52
+ /** @internal */
53
+ export type WakeRpc = typeof WakeRpc
54
+
55
+ // `effect` 4.1 added the reference, and the published range starts before it.
56
+ const InMemoryThreshold: typeof DurableClock.InMemoryThreshold | undefined =
57
+ (DurableClock as Partial<typeof DurableClock>).InMemoryThreshold
58
+
59
+ /**
60
+ * The longest wait that stays in memory, in milliseconds, as `DurableClock`
61
+ * decides it for a workflow.
62
+ *
63
+ * @internal
64
+ */
65
+ export const threshold: Effect.Effect<number> = InMemoryThreshold === undefined
66
+ ? Effect.succeed(60_000)
67
+ : Effect.gen(function*() {
68
+ return Duration.toMillis(yield* InMemoryThreshold)
69
+ })
70
+
71
+ /**
72
+ * Saves the message that wakes an entity for a timer. It joins an open
73
+ * transaction of `MessageStorage`, and a message that is already saved is a
74
+ * duplicate that changes nothing.
75
+ *
76
+ * @internal
77
+ */
78
+ export const makeSave = (options: {
79
+ readonly address: EntityAddress.EntityAddress
80
+ readonly messageStorage: MessageStorage.MessageStorage["Service"]
81
+ readonly sharding: Sharding.Sharding["Service"]
82
+ }) =>
83
+ (timer: Timer): Effect.Effect<void, unknown> =>
84
+ Effect.flatMap(options.sharding.getSnowflake, (requestId) =>
85
+ options.messageStorage.saveRequest(
86
+ new Message.OutgoingRequest({
87
+ envelope: Envelope.makeRequest<WakeRpc>({
88
+ requestId,
89
+ address: options.address,
90
+ tag: WakeRpc._tag,
91
+ payload: new Wake(timer),
92
+ headers: Headers.empty
93
+ }),
94
+ context: Context.empty(),
95
+ lastReceivedReply: Option.none(),
96
+ rpc: WakeRpc,
97
+ respond: () => Effect.void,
98
+ annotations: WakeRpc.annotations
99
+ })
100
+ )).pipe(Effect.asVoid)
@@ -0,0 +1,269 @@
1
+ /**
2
+ * The work that one live Cluster instance runs: effects, and timers that wait
3
+ * for their stored due time. Which work runs, stops, and starts is what
4
+ * `Machine.toStep` says. This module only runs it.
5
+ *
6
+ * @since 0.48.0
7
+ */
8
+ import type { EntityAddress } from "effect/cluster"
9
+ import type * as Context from "effect/Context"
10
+ import * as Effect from "effect/Effect"
11
+ import * as Exit from "effect/Exit"
12
+ import * as Fiber from "effect/Fiber"
13
+ import * as Scope from "effect/Scope"
14
+ import * as CapturedInvocation from "./capturedInvocation.js"
15
+ import * as ClusterProgress from "./clusterProgress.js"
16
+ import * as ClusterTimer from "./clusterTimer.js"
17
+ import type * as Step from "./step.js"
18
+
19
+ type EntityAddress = EntityAddress.EntityAddress
20
+
21
+ /**
22
+ * One piece of work that this host runs on a fiber, from its start until its
23
+ * outcome is applied or the host stops it.
24
+ *
25
+ * @internal
26
+ */
27
+ export interface Running {
28
+ readonly work: Step.Work
29
+ readonly fiber: Fiber.Fiber<unknown, unknown>
30
+
31
+ /**
32
+ * Whether this host stopped the work. Set before its fiber is interrupted, so
33
+ * no interruption the host caused is read as an outcome.
34
+ */
35
+ stopped: boolean
36
+ }
37
+
38
+ /**
39
+ * Work that could not start: the failed `Exit` of the accessor that evaluated
40
+ * it, which the host settles it with.
41
+ *
42
+ * @internal
43
+ */
44
+ export interface StartFailure {
45
+ readonly work: Step.Work
46
+ readonly exit: Exit.Exit<unknown, unknown>
47
+ }
48
+
49
+ /** @internal */
50
+ export interface Options<R> {
51
+ readonly definitionId: string
52
+ readonly address: EntityAddress
53
+
54
+ /** Services of every work fiber, whatever started it. */
55
+ readonly context: Context.Context<R>
56
+
57
+ /**
58
+ * Called in a fiber of its own once work that was not stopped has ended. It
59
+ * may start before the turn that started the work has finished.
60
+ */
61
+ readonly onEnd: (running: Running, exit: Exit.Exit<unknown, unknown>) => Effect.Effect<void, never, R>
62
+ }
63
+
64
+ /**
65
+ * Owns every fiber that runs work, including the waits of timers and the fibers
66
+ * that report how it ended. All fibers live in one private scope that only
67
+ * `close` closes, so `close` can declare the work stopped before the first
68
+ * interruption reaches it.
69
+ *
70
+ * Nothing here is synchronized. Every method except `close` runs inside a turn
71
+ * of the instance.
72
+ *
73
+ * @internal
74
+ */
75
+ export class ClusterWork<R> {
76
+ /**
77
+ * The stored revision that the running work belongs to, with the step state
78
+ * that lists it. `undefined` means the work matches no revision, so the next
79
+ * state starts it from the beginning.
80
+ */
81
+ current: { readonly revision: number; readonly state: Step.State } | undefined = undefined
82
+
83
+ private readonly scope = Scope.makeUnsafe("parallel")
84
+ private readonly running = new Map<Step.Work, Running>()
85
+ private isClosed = false
86
+
87
+ constructor(private readonly options: Options<R>) {}
88
+
89
+ /** Whether the instance that owns this work is gone. */
90
+ get closed(): boolean {
91
+ return this.isClosed
92
+ }
93
+
94
+ /** Whether some work whose outcome is still expected is not left out. */
95
+ pendingExcept(leftOut: (running: Running) => boolean): boolean {
96
+ for (const running of this.running.values()) {
97
+ if (!leftOut(running)) return true
98
+ }
99
+ return false
100
+ }
101
+
102
+ /** Forks an effect that ends with this work. */
103
+ fork(effect: Effect.Effect<void, never, R>): Effect.Effect<Fiber.Fiber<void>> {
104
+ return Effect.forkIn(Effect.setContext(effect, this.options.context), this.scope, { startImmediately: true })
105
+ }
106
+
107
+ /**
108
+ * Starts work in order, evaluating each piece when it starts. A child machine
109
+ * runs as an entity of its own, which the step that entered its state
110
+ * started. A timer waits for its stored due time, or ends at once when that
111
+ * has passed. The first work that cannot start ends the start.
112
+ */
113
+ readonly start = Effect.fnUntraced(function*<R>(
114
+ this: ClusterWork<R>,
115
+ works: ReadonlyArray<Step.Work>,
116
+ progress: ClusterProgress.Progress
117
+ ): Effect.fn.Return<StartFailure | undefined> {
118
+ for (const work of works) {
119
+ if (this.isClosed) return undefined
120
+ const constructed = construct(work, progress)
121
+ if (constructed === undefined) continue
122
+ if (Exit.isFailure(constructed)) return { work, exit: constructed }
123
+ yield* this.run(work, constructed.value)
124
+ }
125
+ return undefined
126
+ })
127
+
128
+ /** Stops the work that runs among `works`, together. */
129
+ stop(works: ReadonlyArray<Step.Work>): Effect.Effect<void> {
130
+ return Effect.suspend(() => {
131
+ const stopped: Array<Running> = []
132
+ for (const work of works) {
133
+ const running = this.running.get(work)
134
+ if (running === undefined) continue
135
+ this.running.delete(work)
136
+ stopped.push(running)
137
+ }
138
+ return this.interrupt(stopped)
139
+ })
140
+ }
141
+
142
+ /** Stops all work, which then belongs to no revision. */
143
+ readonly stopAll: Effect.Effect<void> = Effect.suspend(() => {
144
+ this.current = undefined
145
+ return this.interrupt(this.takeAll())
146
+ })
147
+
148
+ /** Removes work whose outcome was applied. */
149
+ settle(running: Running): void {
150
+ if (this.running.get(running.work) === running) this.running.delete(running.work)
151
+ }
152
+
153
+ /**
154
+ * Stops everything when the instance ends. The work is declared stopped, and
155
+ * the instance closed, before any fiber is interrupted.
156
+ */
157
+ readonly close: Effect.Effect<void> = Effect.suspend(() => {
158
+ this.isClosed = true
159
+ for (const running of this.takeAll()) running.stopped = true
160
+ return Scope.close(this.scope, Exit.void)
161
+ })
162
+
163
+ private takeAll(): ReadonlyArray<Running> {
164
+ const all = Array.from(this.running.values())
165
+ this.running.clear()
166
+ return all
167
+ }
168
+
169
+ private interrupt(stopped: ReadonlyArray<Running>): Effect.Effect<void> {
170
+ for (const running of stopped) running.stopped = true
171
+ return stopped.length === 0 ? Effect.void : Fiber.interruptAll(stopped.map((running) => running.fiber))
172
+ }
173
+
174
+ private run(work: Step.Work, effect: Effect.Effect<unknown, unknown, unknown>): Effect.Effect<void> {
175
+ const { address, context, definitionId, onEnd } = this.options
176
+ const hosted = Effect.suspend(() => effect).pipe(
177
+ Effect.withSpan("Machine.invoke", {
178
+ attributes: {
179
+ "machine.id": definitionId,
180
+ "machine.state.path": work.path,
181
+ "machine.invoke.id": work.id,
182
+ "machine.invoke.source": work.source ?? work.id,
183
+ "machine.invoke.kind": work._tag,
184
+ "entity.type": address.entityType,
185
+ "entity.id": address.entityId
186
+ }
187
+ }, { captureStackTrace: false })
188
+ // The public machine type owns the services of its invoked effects, which
189
+ // the adapter requires. They are erased in the step.
190
+ ) as Effect.Effect<unknown, unknown, R>
191
+ return Effect.forkIn(Effect.setContext(hosted, context), this.scope, { startImmediately: true }).pipe(
192
+ Effect.flatMap((fiber) => {
193
+ const running: Running = { work, fiber, stopped: false }
194
+ this.running.set(work, running)
195
+ return this.fork(
196
+ Effect.flatMap(Fiber.await(fiber), (exit) => running.stopped ? Effect.void : onEnd(running, exit))
197
+ )
198
+ }),
199
+ Effect.asVoid
200
+ )
201
+ }
202
+ }
203
+
204
+ /**
205
+ * The effect that runs a piece of work, a failed `Exit` when it cannot start,
206
+ * or `undefined` for work that runs elsewhere.
207
+ */
208
+ const construct = (
209
+ work: Step.Work,
210
+ progress: ClusterProgress.Progress
211
+ ): Exit.Exit<Effect.Effect<unknown, unknown, unknown>, unknown> | undefined => {
212
+ switch (work._tag) {
213
+ case "Effect":
214
+ return work.effect()
215
+ case "Timer": {
216
+ const waiting = ClusterProgress.get(progress, work)
217
+ if (waiting?._tag === "Waiting") return Exit.succeed(ClusterTimer.waitUntil(waiting.dueAt))
218
+ // The duration was read before the state was stored. A failure of that
219
+ // is reported here, where the local runtime would have read it.
220
+ const duration = work.duration()
221
+ return Exit.isFailure(duration)
222
+ ? Exit.failCause(duration.cause)
223
+ : Exit.die(new Error(`Timer "${work.id}" of state "${work.path}" has no due time`))
224
+ }
225
+ case "Child":
226
+ return undefined
227
+ case "Stream":
228
+ case "Logic":
229
+ return Exit.die(new Error(`Unexpected ${work._tag} invocation "${work.id}" of a supported machine`))
230
+ }
231
+ }
232
+
233
+ /** What a machine asks of its host. */
234
+ export type Support =
235
+ /** The machine invokes nothing. */
236
+ | { readonly _tag: "Idle" }
237
+ /** Every invocation of the machine is an effect, a timer, or a child machine. */
238
+ | { readonly _tag: "Hosted" }
239
+ | { readonly _tag: "Unsupported"; readonly message: string }
240
+
241
+ const unsupportedKind = (definition: CapturedInvocation.CapturedInvocation): string | undefined => {
242
+ if ("effect" in definition || "after" in definition || "child" in definition) return undefined
243
+ if ("stream" in definition) return "Stream"
244
+ return "Logic"
245
+ }
246
+
247
+ /**
248
+ * Decides for the whole machine, before it runs, so that no step can drive it
249
+ * into a state this host cannot run.
250
+ *
251
+ * @internal
252
+ */
253
+ export const supportOf = (handlers: Readonly<Record<string, { readonly invoke?: unknown } | undefined>>): Support => {
254
+ let support: Support = { _tag: "Idle" }
255
+ for (const path of Object.keys(handlers)) {
256
+ for (const definition of CapturedInvocation.list(handlers[path]?.invoke)) {
257
+ const kind = unsupportedKind(definition)
258
+ if (kind !== undefined) {
259
+ const id = CapturedInvocation.lifecycleId(definition)
260
+ return {
261
+ _tag: "Unsupported",
262
+ message: `${kind} invocation "${id}" of state "${path}" is process-local and is not supported`
263
+ }
264
+ }
265
+ support = { _tag: "Hosted" }
266
+ }
267
+ }
268
+ return support
269
+ }
@@ -4,19 +4,19 @@
4
4
  * @since 0.4.0
5
5
  */
6
6
 
7
- import type { Command, Enqueue, Machine } from "../../Machine.js"
7
+ import type { Command, Commands, Machine } from "../../Machine.js"
8
8
  import { decodeEmitSync, decodeEventSync } from "./protocol.js"
9
9
 
10
10
  export type RuntimeCommand = Command
11
11
 
12
- /** Destination accepted by `Enqueue.sendTo`, erased over the child's event type. */
12
+ /** Destination accepted by `Commands.sendTo`, erased over the child's event type. */
13
13
  export type SendToTarget = Extract<RuntimeCommand, { readonly _tag: "SendTo" }>["target"]
14
14
 
15
- /** Child accepted by `Enqueue.stop`, erased over the child's event type. */
15
+ /** Child accepted by `Commands.stop`, erased over the child's event type. */
16
16
  export type StopTarget = Extract<RuntimeCommand, { readonly _tag: "Stop" }>["child"]
17
17
 
18
18
  export interface Collected<Event> {
19
- readonly enqueue: Enqueue<Event, unknown>
19
+ readonly enqueue: Commands<Event, unknown>
20
20
  readonly commands: Array<RuntimeCommand>
21
21
  readonly raisedEvents: Array<Event>
22
22
  readonly emittedEvents: Array<unknown>
@@ -5,37 +5,25 @@
5
5
  */
6
6
 
7
7
  import * as Effect from "effect/Effect"
8
- import type { Machine, Runtime } from "../../Machine.js"
9
- import type { RuntimeCommand } from "./command.js"
10
- import { decodeEmit, decodeEvent } from "./protocol.js"
11
- import type { ProcessScope } from "./runtimeProtocol.js"
8
+ import type { Machine, MachineSchemaDecodeError, StoppedError } from "../../Machine.js"
9
+ import { decodeEmit } from "./protocol.js"
10
+ import type { ProcessInitialContext } from "./runtimeProtocol.js"
12
11
 
13
- export const makeLiveRuntime = <Events, Emits>(
14
- machine: Machine.Any,
15
- scope: ProcessScope<Events>
16
- ): Runtime<Events, Emits> => ({
17
- raise: (event) =>
18
- decodeEvent(machine, event).pipe(
19
- Effect.flatMap((event) => scope.sendTo(scope.self, event as Events))
20
- ),
12
+ /** Publishes emissions planned by a machine to its process observers. */
13
+ export interface LiveRuntime {
14
+ readonly emit: (event: unknown) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
15
+ }
16
+
17
+ export const makeLiveRuntime = <Events>(machine: Machine.Any, scope: ProcessInitialContext<Events>): LiveRuntime => ({
21
18
  emit: (event) =>
22
19
  decodeEmit(machine, event).pipe(
23
20
  Effect.flatMap((event) => scope.emit(event))
24
21
  )
25
22
  })
26
23
 
27
- export const runCommands = <Event>(
28
- commands: Iterable<RuntimeCommand>,
29
- scope: ProcessScope<Event>
30
- ) =>
31
- Effect.forEach(commands, (command) =>
32
- command._tag === "SendTo"
33
- ? scope.sendTo(command.target as never, command.event)
34
- : scope.stopChild(command.child as never), { discard: true })
35
-
36
- export const runEmittedEvents = <Events, Emits>(
37
- events: Iterable<Emits>,
38
- runtime: Runtime<Events, Emits>
24
+ export const runEmittedEvents = (
25
+ events: Iterable<unknown>,
26
+ runtime: LiveRuntime
39
27
  ) =>
40
28
  Effect.all(
41
29
  Array.from(events, (event) => runtime.emit(event)),