@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,307 @@
1
+ /**
2
+ * Host-neutral resolution of the work owned by entered states.
3
+ *
4
+ * `resolve` answers which invocations a step starts, in which order, and what
5
+ * each one is. It runs nothing. `step.ts` is its one caller: it resolves the
6
+ * work of entered states against the configuration the step settled in, stops
7
+ * the work of exited paths before it starts the work of entered ones, evaluates
8
+ * each lazy source at most once per start, and routes what the work reports
9
+ * through `InvocationOutcome`. Every host, the local runtime included, starts
10
+ * and owns the work that a step lists.
11
+ *
12
+ * @since 0.47.2
13
+ */
14
+
15
+ import * as Duration from "effect/Duration"
16
+ import type * as Effect from "effect/Effect"
17
+ import type * as Stream from "effect/Stream"
18
+ import type { ChildMachine, ChildOwner, Logic, Machine, MachineTarget } from "../../Machine.js"
19
+ import * as CapturedInvocation from "./capturedInvocation.js"
20
+ import * as Configuration from "./configuration.js"
21
+ import * as Planner from "./planner.js"
22
+
23
+ /**
24
+ * Outcome channels an invocation declares a transition for.
25
+ *
26
+ * @internal
27
+ */
28
+ export interface OutcomeChannels {
29
+ readonly done: boolean
30
+ readonly failure: boolean
31
+ readonly element: boolean
32
+ readonly snapshot: boolean
33
+ }
34
+
35
+ interface Base {
36
+ /** Path of the state that owns the invocation. */
37
+ readonly path: string
38
+ /** Lifecycle id that routes the invocation's outcomes within its state. */
39
+ readonly id: string
40
+ readonly channels: OutcomeChannels
41
+ }
42
+
43
+ interface NamedBase extends Base {
44
+ /** Name the source is registered with, when it has one. */
45
+ readonly sourceName: string | undefined
46
+ }
47
+
48
+ /** @internal */
49
+ export interface EffectInvocation extends NamedBase {
50
+ readonly _tag: "Effect"
51
+ /** Constructs the effect with its input applied. */
52
+ readonly effect: () => Effect.Effect<unknown, unknown, unknown>
53
+ }
54
+
55
+ /**
56
+ * A timer completes with `undefined` once its duration has elapsed.
57
+ *
58
+ * `duration` is lazy because a parameterized timer runs its input mapper and
59
+ * source function. It throws when the timer cannot start: when either throws,
60
+ * and when the input names no duration. A throw belongs to the host's startup
61
+ * boundary, so a timer that cannot start fails in one way.
62
+ *
63
+ * @internal
64
+ */
65
+ export interface TimerInvocation extends NamedBase {
66
+ readonly _tag: "Timer"
67
+ readonly duration: () => Duration.Duration
68
+ }
69
+
70
+ /** @internal */
71
+ export interface StreamInvocation extends NamedBase {
72
+ readonly _tag: "Stream"
73
+ /** Constructs the stream with its input applied. */
74
+ readonly stream: () => Stream.Stream<unknown, unknown, unknown>
75
+ }
76
+
77
+ /** @internal */
78
+ export interface LogicInvocation extends NamedBase {
79
+ readonly _tag: "Logic"
80
+ /** Caller-chosen address of the started process within its owner. */
81
+ readonly address: string
82
+ /** Constructs the logic with its input applied. */
83
+ readonly logic: () => Logic<any, any, any, any, any>
84
+ }
85
+
86
+ /**
87
+ * A child machine is named by its descriptor rather than by its source.
88
+ *
89
+ * @internal
90
+ */
91
+ export interface ChildInvocation extends Base {
92
+ readonly _tag: "Child"
93
+ readonly child: ChildMachine.Any
94
+ /** Constructs the child's input, or `undefined` when none is declared. */
95
+ readonly input: (() => unknown) | undefined
96
+ }
97
+
98
+ /**
99
+ * One invocation to start, described without any host capability.
100
+ *
101
+ * @internal
102
+ */
103
+ export type ResolvedInvocation =
104
+ | EffectInvocation
105
+ | TimerInvocation
106
+ | StreamInvocation
107
+ | LogicInvocation
108
+ | ChildInvocation
109
+
110
+ /**
111
+ * The invocations started by one entry of a state, in start order. They share
112
+ * one generation.
113
+ *
114
+ * @internal
115
+ */
116
+ export interface InvocationGroup {
117
+ readonly path: string
118
+ readonly invocations: ReadonlyArray<ResolvedInvocation>
119
+ }
120
+
121
+ /**
122
+ * Host capabilities exposed to input mappers.
123
+ *
124
+ * @internal
125
+ */
126
+ export interface InvocationCapabilities {
127
+ readonly self: MachineTarget<any>
128
+ readonly parent?: MachineTarget<any> | undefined
129
+ readonly children: ChildOwner<any>
130
+ }
131
+
132
+ const channelCombinations: ReadonlyArray<OutcomeChannels> = Array.from({ length: 16 }, (_, bits) =>
133
+ Object.freeze({
134
+ done: (bits & 1) !== 0,
135
+ failure: (bits & 2) !== 0,
136
+ element: (bits & 4) !== 0,
137
+ snapshot: (bits & 8) !== 0
138
+ }))
139
+
140
+ // Descriptions live as long as the work they start, so equal channels share one
141
+ // immutable value.
142
+ const outcomeChannels = (definition: CapturedInvocation.CapturedInvocation): OutcomeChannels =>
143
+ channelCombinations[
144
+ (definition.onDone === undefined ? 0 : 1) |
145
+ (definition.onFailure === undefined ? 0 : 2) |
146
+ (definition.onElement === undefined ? 0 : 4) |
147
+ (definition.onSnapshot === undefined ? 0 : 8)
148
+ ]!
149
+
150
+ type CapturedChild = Extract<CapturedInvocation.CapturedInvocation, { readonly child: ChildMachine.Any }>
151
+
152
+ const staticChildInvocations = new WeakMap<CapturedChild, ChildInvocation>()
153
+
154
+ // A child without input does not depend on its entry context, so neither
155
+ // building nor retaining one is worthwhile.
156
+ const resolveChild = (
157
+ definition: CapturedChild,
158
+ path: string,
159
+ input: (() => unknown) | undefined
160
+ ): ChildInvocation => ({
161
+ _tag: "Child",
162
+ path,
163
+ id: CapturedInvocation.lifecycleId(definition),
164
+ channels: outcomeChannels(definition),
165
+ child: definition.child,
166
+ input
167
+ })
168
+
169
+ const resolveStaticChild = (definition: CapturedChild, path: string): ChildInvocation => {
170
+ const cached = staticChildInvocations.get(definition)
171
+ if (cached !== undefined && cached.path === path) return cached
172
+ const resolved = resolveChild(definition, path, undefined)
173
+ if (cached === undefined) staticChildInvocations.set(definition, resolved)
174
+ return resolved
175
+ }
176
+
177
+ const resolveDefinition = (
178
+ definition: CapturedInvocation.CapturedInvocation,
179
+ path: string,
180
+ context: CapturedInvocation.Context
181
+ ): ResolvedInvocation => {
182
+ const id = CapturedInvocation.lifecycleId(definition)
183
+ const sourceName = definition.sourceName
184
+ const channels = outcomeChannels(definition)
185
+ if ("effect" in definition) {
186
+ return { _tag: "Effect", path, id, sourceName, channels, effect: () => definition.effect(context) }
187
+ }
188
+ if ("after" in definition) {
189
+ return {
190
+ _tag: "Timer",
191
+ path,
192
+ id,
193
+ sourceName,
194
+ channels,
195
+ duration: () =>
196
+ Duration.fromInputUnsafe(typeof definition.after === "function" ? definition.after(context) : definition.after)
197
+ }
198
+ }
199
+ if ("stream" in definition) {
200
+ return { _tag: "Stream", path, id, sourceName, channels, stream: () => definition.stream(context) }
201
+ }
202
+ if ("logic" in definition) {
203
+ return {
204
+ _tag: "Logic",
205
+ path,
206
+ id,
207
+ sourceName,
208
+ channels,
209
+ address: definition.address,
210
+ logic: () => typeof definition.logic === "function" ? definition.logic(context) : definition.logic
211
+ }
212
+ }
213
+ if ("child" in definition) {
214
+ return resolveChild(
215
+ definition,
216
+ path,
217
+ () => typeof definition.input === "function" ? definition.input(context) : definition.input
218
+ )
219
+ }
220
+ const unsupported: never = definition
221
+ throw new Error(`Unsupported captured machine invocation: ${String(unsupported)}`)
222
+ }
223
+
224
+ const noGroups: ReadonlyArray<InvocationGroup> = []
225
+
226
+ /**
227
+ * Resolves the invocations started by the states a step entered.
228
+ *
229
+ * Groups follow the planner's entry order and contain only paths active in the
230
+ * configuration. Invocations keep their declaration order. A state without
231
+ * invocations has no group.
232
+ *
233
+ * @internal
234
+ */
235
+ export const resolve = (
236
+ machine: Machine.Any,
237
+ configuration: Configuration.ActiveConfiguration,
238
+ entryPaths: ReadonlyArray<string>,
239
+ event: Machine.LifecycleEvent<any>,
240
+ capabilities: InvocationCapabilities | (() => InvocationCapabilities)
241
+ ): ReadonlyArray<InvocationGroup> => {
242
+ let groups: Array<InvocationGroup> | undefined
243
+ let resolvedCapabilities: InvocationCapabilities | undefined
244
+ const ordered = entryPaths.length <= 1 ? entryPaths : Planner.sortEntryPaths(machine, entryPaths)
245
+ for (const path of ordered) {
246
+ if (!configuration.active.has(path)) continue
247
+ const definitions = CapturedInvocation.list(Configuration.getStateConfigByPath(machine, path)?.invoke)
248
+ if (definitions.length === 0) continue
249
+ let context: CapturedInvocation.Context | undefined
250
+ ;(groups ??= []).push({
251
+ path,
252
+ invocations: definitions.map((definition) =>
253
+ "child" in definition && !("input" in definition)
254
+ ? resolveStaticChild(definition, path)
255
+ : resolveDefinition(
256
+ definition,
257
+ path,
258
+ context ??= {
259
+ ...(resolvedCapabilities ??= typeof capabilities === "function" ? capabilities() : capabilities),
260
+ state: configuration.values.get(path),
261
+ containingState: Configuration.getParentValue(machine, configuration, path),
262
+ ancestors: Configuration.getParentValues(machine, configuration, path),
263
+ event
264
+ }
265
+ )
266
+ )
267
+ })
268
+ }
269
+ return groups ?? noGroups
270
+ }
271
+
272
+ /**
273
+ * Resolves the groups started by several entries of one step, in start order:
274
+ * each entry's groups follow `resolve`, and entries keep their order.
275
+ *
276
+ * @internal
277
+ */
278
+ export const resolveEntries = (
279
+ machine: Machine.Any,
280
+ configuration: Configuration.ActiveConfiguration,
281
+ entries: Iterable<readonly [ReadonlyArray<string>, Machine.LifecycleEvent<any>]>,
282
+ capabilities: InvocationCapabilities | (() => InvocationCapabilities)
283
+ ): ReadonlyArray<InvocationGroup> => {
284
+ let groups: Array<InvocationGroup> | undefined
285
+ let resolvedCapabilities: InvocationCapabilities | undefined
286
+ const getCapabilities = (): InvocationCapabilities =>
287
+ resolvedCapabilities ??= typeof capabilities === "function" ? capabilities() : capabilities
288
+ for (const [paths, event] of entries) {
289
+ const resolved = resolve(machine, configuration, paths, event, getCapabilities)
290
+ if (resolved.length > 0) (groups ??= []).push(...resolved)
291
+ }
292
+ return groups ?? noGroups
293
+ }
294
+
295
+ /**
296
+ * The entries a machine starts in: every active path of the configuration,
297
+ * entered with `InitialEvent`, as when a machine starts or resumes.
298
+ *
299
+ * @internal
300
+ */
301
+ export const initialEntries = (
302
+ machine: Machine.Any,
303
+ configuration: Configuration.ActiveConfiguration
304
+ ): ReadonlyArray<readonly [ReadonlyArray<string>, Machine.LifecycleEvent<any>]> => [[
305
+ Configuration.getInitialEntryPaths(machine, configuration),
306
+ Planner.InitialEvent
307
+ ]]
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The `Scope` that a `Logic` process owns, for every host that runs one.
3
+ *
4
+ * @since 0.48.0
5
+ */
6
+
7
+ import * as Effect from "effect/Effect"
8
+ import * as Exit from "effect/Exit"
9
+ import * as Scope from "effect/Scope"
10
+
11
+ /** The members of process logic that run, whatever else a host attaches to it. */
12
+ interface Runnable {
13
+ initial(context: never): Effect.Effect<unknown, unknown, unknown>
14
+ run(context: never): Effect.Effect<unknown, unknown, unknown>
15
+ }
16
+
17
+ /**
18
+ * Gives a `Logic` process one `Scope` from its `initial` through the end of its
19
+ * `run`. The scope is forked from the one that `initial` runs in, so a process
20
+ * stopped before its `run` starts is still released when that scope closes.
21
+ *
22
+ * Each call returns logic for one process: its `initial` and `run` share the
23
+ * scope of that process.
24
+ *
25
+ * @internal
26
+ */
27
+ export const scoped = <L extends Runnable>(logic: L): L => {
28
+ let scope: Scope.Closeable | undefined
29
+ return {
30
+ ...logic,
31
+ initial: (context: never) =>
32
+ Effect.flatMap(Effect.scope, (owner) => {
33
+ const own = scope = Scope.forkUnsafe(owner, "sequential")
34
+ return logic.initial(context).pipe(
35
+ Scope.provide(own),
36
+ Effect.onError((cause) => Scope.close(own, Exit.failCause(cause)))
37
+ )
38
+ }),
39
+ run: (context: never) => {
40
+ const own = scope!
41
+ return logic.run(context).pipe(
42
+ Scope.provide(own),
43
+ Effect.onExit((exit) => Scope.close(own, exit))
44
+ )
45
+ }
46
+ }
47
+ }
@@ -4,41 +4,39 @@ import * as Option from "effect/Option"
4
4
  import { Prototype as PipeablePrototype } from "effect/Pipeable"
5
5
  import { hasProperty } from "effect/Predicate"
6
6
  import * as Schema from "effect/Schema"
7
- import type * as Stream from "effect/Stream"
8
7
  import type {
9
8
  ChildAddress,
10
9
  ChildMachine,
11
10
  Definition,
12
11
  Logic,
13
12
  Machine,
14
- MachineRef,
15
13
  MachineSchemaDecodeError,
16
14
  MachineSchemaEncodeError,
17
15
  Parent,
18
16
  ParentMode,
19
- RuntimeOutcome,
20
17
  SpawnOptions,
21
18
  State,
22
19
  StoppedError
23
20
  } from "../../Machine.js"
24
- import * as Activities from "./activities.js"
21
+ import { ChildMachineRuntime, MachineRuntime } from "./capability.js"
22
+ import * as CapturedInvocation from "./capturedInvocation.js"
23
+ import * as Child from "./child.js"
25
24
  import * as Configuration from "./configuration.js"
26
25
  import * as Construction from "./construction.js"
27
26
  import * as Declaration from "./declaration.js"
28
27
  import type { PlanningError, StartupError } from "./errors.js"
29
28
  import type { CapturedStateConfig } from "./implementation.js"
30
29
  import * as InitialDeclaration from "./initialDeclaration.js"
31
- import * as InvocationDefinition from "./invocationDefinition.js"
30
+ import * as InvocationDefinitions from "./invocationDefinitions.js"
32
31
  import * as Observation from "./observation.js"
33
32
  import * as internalPlanner from "./planner.js"
34
- import * as internalProcess from "./process.js"
35
33
  import * as Protocol from "./protocol.js"
36
- import * as internalRuntime from "./runtimeProtocol.js"
37
34
  import * as Serialization from "./serialization.js"
38
35
  import * as StateDefinition from "./stateDefinition.js"
39
- import { type ChildMachineLogicSlot, ChildMachineLogicTypeId } from "./symbols.js"
36
+ import * as Step from "./step.js"
40
37
  import * as Topology from "./topology.js"
41
38
 
39
+ export { child, childAddress, childFamily } from "./child.js"
42
40
  export {
43
41
  ChildAlreadyExistsError,
44
42
  InfiniteTransitionError,
@@ -48,14 +46,12 @@ export {
48
46
  StartupError,
49
47
  StoppedError
50
48
  } from "./errors.js"
51
- export { InitialEventTypeId } from "./symbols.js"
49
+ export { InitialEventTypeId, InvocationEventTypeId } from "./symbols.js"
52
50
 
53
51
  /** Internal seam for the public target-reference factory and its opaque brand. */
54
52
 
55
53
  const TypeId = "~effect/Machine"
56
54
  const ParentTypeId = "~effect/Machine/Parent"
57
- const ChildMachineTypeId = "~effect/Machine/ChildMachine"
58
- type MachineRuntimeRequirement = internalRuntime.MachineRuntime
59
55
 
60
56
  const Proto = {
61
57
  ...Inspectable.BaseProto,
@@ -643,7 +639,7 @@ const captureInvokeDefinition = (
643
639
  declaration: Declaration.Declaration
644
640
  ): unknown => {
645
641
  const definitions = Array.isArray(invoke) ? invoke : [invoke]
646
- const capturedDefinitions = definitions.map((definition) => {
642
+ const capturedInvocations = definitions.map((definition) => {
647
643
  const captured = Declaration.invocation(
648
644
  declaration,
649
645
  definition,
@@ -655,11 +651,11 @@ const captureInvokeDefinition = (
655
651
  captured[key] = captureTransition(captured[key], stateNodes, path, declaration, key, "required")
656
652
  }
657
653
  }
658
- return InvocationDefinition.capture(captured, path)
654
+ return CapturedInvocation.capture(captured, path)
659
655
  })
660
- InvocationDefinition.assertDistinctIds(capturedDefinitions, path)
661
- if (Array.isArray(invoke)) return Object.freeze(capturedDefinitions)
662
- return capturedDefinitions[0]
656
+ CapturedInvocation.assertDistinctIds(capturedInvocations, path)
657
+ if (Array.isArray(invoke)) return Object.freeze(capturedInvocations)
658
+ return capturedInvocations[0]
663
659
  }
664
660
 
665
661
  const flattenHandlers = (
@@ -1025,11 +1021,11 @@ export const transitionDefinitions = <M extends Machine.Any>(
1025
1021
  >
1026
1022
  >
1027
1023
 
1028
- export const activityDefinitions = <M extends Machine.Any>(
1024
+ export const invocationDefinitions = <M extends Machine.Any>(
1029
1025
  machine: M
1030
- ): ReadonlyArray<Machine.ActivityDefinition<Machine.StateIdentifier<Machine.States<M>>>> =>
1031
- Activities.activityDefinitions(machine) as ReadonlyArray<
1032
- Machine.ActivityDefinition<Machine.StateIdentifier<Machine.States<M>>>
1026
+ ): ReadonlyArray<Machine.InvocationDefinition<Machine.StateIdentifier<Machine.States<M>>>> =>
1027
+ InvocationDefinitions.invocationDefinitions(machine) as ReadonlyArray<
1028
+ Machine.InvocationDefinition<Machine.StateIdentifier<Machine.States<M>>>
1033
1029
  >
1034
1030
 
1035
1031
  export const configuration = <M extends Machine.Any>(
@@ -1063,6 +1059,8 @@ export const plan: (
1063
1059
  event: any
1064
1060
  ) => Effect.Effect<any, PlanningError> = internalPlanner.plan
1065
1061
 
1062
+ export const toStep: (machine: Machine.Any, options: Step.Options) => Step.MachineStep = Step.make
1063
+
1066
1064
  export const logic = <
1067
1065
  State,
1068
1066
  Event = never,
@@ -1076,111 +1074,48 @@ export const logic = <
1076
1074
  readonly initial:
1077
1075
  | State
1078
1076
  | ((
1079
- scope: Logic.Scope<Event>
1077
+ scope: Logic.InitialContext<Event>
1080
1078
  ) => Effect.Effect<State, InitialError, InitialRequirements>)
1081
1079
  readonly run: (
1082
- context: Logic.Context<State, Event>
1080
+ context: Logic.RunContext<State, Event>
1083
1081
  ) => Effect.Effect<Output, Error, Requirements>
1084
1082
  }
1085
1083
  ): Logic<State, Event, Error, Requirements | InitialRequirements, Output, InitialError> => ({
1086
1084
  initial: (scope) =>
1087
1085
  typeof options.initial === "function"
1088
1086
  ? (options.initial as (
1089
- scope: Logic.Scope<Event>
1087
+ scope: Logic.InitialContext<Event>
1090
1088
  ) => Effect.Effect<State, InitialError, InitialRequirements>)(scope)
1091
1089
  : Effect.succeed(options.initial),
1092
1090
  run: options.run
1093
1091
  })
1094
1092
 
1095
- export const transition = <State, Event, Error = never, Requirements = never>(
1096
- initial: State,
1097
- transition: (state: State, event: Event) => Effect.Effect<State, Error, Requirements>
1098
- ): Logic<State, Event, Error, Requirements, never> =>
1099
- logic<State, Event, never, Error, Requirements>({
1100
- initial,
1101
- run: ({ receive, updateState }) =>
1102
- receive.pipe(
1103
- Effect.flatMap((event) => updateState((state) => transition(state, event))),
1104
- Effect.forever
1105
- )
1106
- })
1107
-
1108
- export const child = <const Id extends string, M extends Machine.Any>(
1109
- id: Id,
1110
- machine: M
1111
- ): ChildMachine<Id, M> =>
1112
- makeChild(id, machine, (input) =>
1113
- machine.input === undefined
1114
- ? internalProcess.toProcessLogic(machine)
1115
- : internalProcess.toProcessLogic(machine, input))
1116
-
1117
- const makeChild = <const Id extends string, M extends Machine.Any>(
1118
- id: Id,
1119
- machine: M,
1120
- makeLogic: (input?: unknown) => Logic<any, any, any, any, any, any>
1121
- ): ChildMachine<Id, M> =>
1122
- ({
1123
- [ChildMachineTypeId]: ChildMachineTypeId,
1124
- id,
1125
- machine,
1126
- [ChildMachineLogicTypeId]: makeLogic
1127
- } satisfies
1128
- & Pick<ChildMachine<Id, M>, typeof ChildMachineTypeId | "id" | "machine">
1129
- & ChildMachineLogicSlot) as unknown as ChildMachine<Id, M>
1130
-
1131
- export const childFamily = <M extends Machine.Any>(machine: M): ChildMachine.Family<M> => {
1132
- const makeLogic = (input?: unknown): Logic<any, any, any, any, any, any> =>
1133
- machine.input === undefined
1134
- ? internalProcess.toProcessLogic(machine)
1135
- : internalProcess.toProcessLogic(machine, input)
1136
- return (id) => makeChild(id, machine, makeLogic)
1137
- }
1138
-
1139
- export const childAddress = <Event = never>(id: string): ChildAddress<Event> => id as ChildAddress<Event>
1140
-
1141
1093
  // The public `Machine` module declares the typed overloads and checks these
1142
1094
  // erased implementations against them structurally.
1143
1095
  export const spawn = (logic: Logic<any, any, any, any, any, any>, options?: SpawnOptions) =>
1144
1096
  Effect.flatMap(
1145
- internalRuntime.MachineRuntime,
1097
+ MachineRuntime,
1146
1098
  // `SpawnOptions` declares only `id`; an absent id spawns an anonymous child.
1147
1099
  (runtime) => options?.id === undefined ? runtime.spawn(logic) : runtime.spawn(logic, { id: options.id })
1148
1100
  )
1149
1101
 
1102
+ // A child machine needs only the capability of child machines, which more hosts
1103
+ // provide than the one of processes.
1150
1104
  export const sendTo = (
1151
- child: string | ChildMachine.Any,
1105
+ child: ChildAddress<never> | ChildMachine.Any,
1152
1106
  event: unknown
1153
- ): Effect.Effect<void, StoppedError, MachineRuntimeRequirement> =>
1154
- Effect.flatMap(
1155
- internalRuntime.MachineRuntime,
1156
- (runtime) => runtime.sendTo(child, event)
1157
- )
1107
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError, MachineRuntime | ChildMachineRuntime> =>
1108
+ Child.isChildMachine(child)
1109
+ ? Effect.flatMap(ChildMachineRuntime, (runtime) => runtime.sendTo(child, event))
1110
+ : Effect.flatMap(MachineRuntime, (runtime) => runtime.sendTo(child, event))
1158
1111
 
1159
1112
  export const stopChild = (
1160
- child: string | ChildMachine.Any
1161
- ): Effect.Effect<void, never, MachineRuntimeRequirement> =>
1162
- Effect.flatMap(
1163
- internalRuntime.MachineRuntime,
1164
- (runtime) => runtime.stopChild(child)
1165
- )
1113
+ child: ChildAddress<never> | ChildMachine.Any
1114
+ ): Effect.Effect<void, never, MachineRuntime | ChildMachineRuntime> =>
1115
+ Child.isChildMachine(child)
1116
+ ? Effect.flatMap(ChildMachineRuntime, (runtime) => runtime.stopChild(child))
1117
+ : Effect.flatMap(MachineRuntime, (runtime) => runtime.stopChild(child))
1166
1118
 
1167
- export const watch = <State, Event, Error = never, Output = never>(
1168
- ref: MachineRef<State, Event, Error, Output>
1169
- ): Stream.Stream<RuntimeOutcome<State, Error, Output>> => internalRuntime.watch(ref)
1119
+ export const watch = Observation.watch
1170
1120
 
1171
1121
  export const waitFor = Observation.waitFor
1172
-
1173
- export const prepare: (
1174
- machine: Machine.Any,
1175
- ...args: ReadonlyArray<any>
1176
- ) => Effect.Effect<internalRuntime.PreparedProcess<any, any, any, any, any, any, any>> = internalProcess.prepare
1177
-
1178
- export const start: (
1179
- machine: Machine.Any,
1180
- ...args: ReadonlyArray<any>
1181
- ) => Effect.Effect<MachineRef<any, any, any, any, any>, any, any> = internalProcess.start
1182
-
1183
- export const resume: (
1184
- machine: Machine.Any,
1185
- snapshot: any
1186
- ) => Effect.Effect<MachineRef<any, any, any, any, any>, MachineSchemaDecodeError, any> = internalProcess.resume
@@ -1,4 +1,4 @@
1
- /** Observation of current and subsequent published runtime snapshots. */
1
+ /** Observation of the snapshots and outcomes published through the reference contract. */
2
2
  import * as Cause from "effect/Cause"
3
3
  import * as Effect from "effect/Effect"
4
4
  import { dual } from "effect/Function"
@@ -7,6 +7,71 @@ import * as Stream from "effect/Stream"
7
7
  import type * as Machine from "../../Machine.js"
8
8
  import { StoppedError } from "./errors.js"
9
9
 
10
+ export const classifyOutcome = <State, Error, Output>(
11
+ snapshot: Machine.RuntimeSnapshot<State, Error, Output>
12
+ ): Machine.RuntimeOutcome<State, Error, Output> | undefined => {
13
+ switch (snapshot.status) {
14
+ case "active": {
15
+ return undefined
16
+ }
17
+ case "done": {
18
+ return {
19
+ _tag: "Done",
20
+ output: snapshot.output,
21
+ snapshot
22
+ }
23
+ }
24
+ case "failed": {
25
+ const failure = snapshot.cause.reasons.find(Cause.isFailReason)
26
+ if (failure !== undefined) {
27
+ return {
28
+ _tag: "Failure",
29
+ error: failure.error,
30
+ cause: snapshot.cause,
31
+ snapshot
32
+ }
33
+ }
34
+ const defect = snapshot.cause.reasons.find(Cause.isDieReason)
35
+ if (defect !== undefined) {
36
+ return {
37
+ _tag: "Defect",
38
+ defect: defect.defect,
39
+ cause: snapshot.cause,
40
+ snapshot
41
+ }
42
+ }
43
+ const interrupted = snapshot.cause.reasons.find(Cause.isInterruptReason)
44
+ if (interrupted !== undefined) {
45
+ return {
46
+ _tag: "Interrupted",
47
+ cause: snapshot.cause,
48
+ snapshot
49
+ }
50
+ }
51
+ return {
52
+ _tag: "Cause",
53
+ cause: snapshot.cause,
54
+ snapshot
55
+ }
56
+ }
57
+ case "stopped": {
58
+ return {
59
+ _tag: "Stopped",
60
+ snapshot
61
+ }
62
+ }
63
+ }
64
+ }
65
+
66
+ export const watch = <State, Event, Error = never, Output = never>(
67
+ ref: Machine.MachineRef<State, Event, Error, Output>
68
+ ): Stream.Stream<Machine.RuntimeOutcome<State, Error, Output>> =>
69
+ ref.changes.pipe(
70
+ Stream.filter((snapshot) => snapshot.status !== "active"),
71
+ Stream.map((snapshot) => classifyOutcome(snapshot)!),
72
+ Stream.take(1)
73
+ )
74
+
10
75
  export const waitFor: typeof Machine.waitFor = dual(2, <State, Event, Error, Output, Emitted>(
11
76
  ref: Machine.MachineRef<State, Event, Error, Output, Emitted>,
12
77
  predicate: (snapshot: Machine.RuntimeSnapshot<State, Error, Output>) => boolean
@@ -25,7 +90,7 @@ export const waitFor: typeof Machine.waitFor = dual(2, <State, Event, Error, Out
25
90
  switch (snapshot.status) {
26
91
  case "active":
27
92
  return Effect.succeed(true)
28
- case "error":
93
+ case "failed":
29
94
  return Effect.failCause(snapshot.cause)
30
95
  case "stopped":
31
96
  return Effect.fail(new StoppedError())