@raphy-js/raph 1.0.0-dev.2 → 1.0.1-dev.25

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 (246) hide show
  1. package/README.md +1096 -0
  2. package/dist/api/Raph.d.ts +59 -190
  3. package/dist/api/RaphFacadeSession.d.ts +90 -0
  4. package/dist/api/RaphReactivityInstance.d.ts +72 -0
  5. package/dist/api/errors.d.ts +9 -4
  6. package/dist/api/types.d.ts +162 -17
  7. package/dist/core-CClhT8WR.js +1044 -0
  8. package/dist/core-cPQWYD4K.cjs +1 -0
  9. package/dist/debug/api/Raph.cjs +1 -1
  10. package/dist/debug/api/Raph.js +368 -367
  11. package/dist/debug/api/RaphFacadeSession.cjs +1 -0
  12. package/dist/debug/api/RaphFacadeSession.js +302 -0
  13. package/dist/debug/api/RaphReactivityInstance.cjs +1 -0
  14. package/dist/debug/api/RaphReactivityInstance.js +146 -0
  15. package/dist/debug/api/errors.cjs +1 -1
  16. package/dist/debug/api/errors.js +10 -7
  17. package/dist/debug/path/DataPath.cjs +1 -0
  18. package/dist/debug/path/DataPath.js +390 -0
  19. package/dist/debug/path/PathRouter.cjs +1 -0
  20. package/dist/debug/path/PathRouter.js +80 -0
  21. package/dist/debug/path/RaphKernel.cjs +1 -1
  22. package/dist/debug/path/RaphKernel.js +316 -236
  23. package/dist/debug/path/data-path.utils.cjs +1 -0
  24. package/dist/debug/path/data-path.utils.js +56 -0
  25. package/dist/debug/path/derived/DerivedRegistry.cjs +1 -0
  26. package/dist/debug/path/derived/DerivedRegistry.js +143 -0
  27. package/dist/debug/path/derived/RaphDerivedHandle.cjs +1 -0
  28. package/dist/debug/path/{RaphDerivedHandle.js → derived/RaphDerivedHandle.js} +19 -22
  29. package/dist/debug/path/derived/derived-collection.utils.cjs +1 -0
  30. package/dist/debug/path/{derived-collection.js → derived/derived-collection.utils.js} +71 -71
  31. package/dist/debug/path/derived/derived-strategy.types.cjs +1 -0
  32. package/dist/debug/path/derived/derived-strategy.types.js +1 -0
  33. package/dist/debug/path/meta/MetaController.cjs +1 -0
  34. package/dist/debug/path/{MetaController.js → meta/MetaController.js} +58 -66
  35. package/dist/debug/path/meta/RaphMeta.cjs +1 -0
  36. package/dist/debug/path/meta/RaphMeta.js +62 -0
  37. package/dist/debug/path/meta/RaphMetaStore.cjs +1 -0
  38. package/dist/debug/path/{RaphMetaStore.js → meta/RaphMetaStore.js} +24 -30
  39. package/dist/debug/path/meta/meta.types.cjs +1 -0
  40. package/dist/debug/path/meta/meta.types.js +1 -0
  41. package/dist/debug/path/storage/AdapterPathStore.cjs +1 -0
  42. package/dist/debug/path/storage/AdapterPathStore.js +128 -0
  43. package/dist/debug/path/storage/DefaultDataAdapter.cjs +1 -0
  44. package/dist/debug/path/{data-adapter.js → storage/DefaultDataAdapter.js} +47 -57
  45. package/dist/debug/path/storage/PathEventHub.cjs +1 -0
  46. package/dist/debug/path/storage/PathEventHub.js +161 -0
  47. package/dist/debug/path/storage/PathStore.cjs +1 -0
  48. package/dist/debug/path/{PathStore.js → storage/PathStore.js} +88 -98
  49. package/dist/debug/path/storage/createDataDraft.cjs +1 -0
  50. package/dist/debug/path/{data-draft.js → storage/createDataDraft.js} +24 -28
  51. package/dist/debug/path/storage/data-adapter.types.cjs +1 -0
  52. package/dist/debug/path/storage/data-adapter.types.js +1 -0
  53. package/dist/debug/reactivity/disposal.cjs +1 -0
  54. package/dist/debug/reactivity/disposal.js +32 -0
  55. package/dist/debug/reactivity/engine.cjs +1 -1
  56. package/dist/debug/reactivity/engine.js +888 -321
  57. package/dist/debug/reactivity/reactive-object.cjs +1 -0
  58. package/dist/debug/reactivity/reactive-object.js +265 -0
  59. package/dist/debug/reactivity/scope.cjs +1 -1
  60. package/dist/debug/reactivity/scope.js +221 -117
  61. package/dist/debug/reactivity/watch.cjs +1 -1
  62. package/dist/debug/reactivity/watch.js +1 -1
  63. package/dist/debug/runtime/RaphRuntime.cjs +1 -1
  64. package/dist/debug/runtime/RaphRuntime.js +1134 -502
  65. package/dist/debug/runtime/diagnostics/RaphDebug.cjs +1 -0
  66. package/dist/debug/runtime/diagnostics/RaphDebug.js +287 -0
  67. package/dist/debug/runtime/errors.cjs +1 -1
  68. package/dist/debug/runtime/errors.js +6 -3
  69. package/dist/debug/runtime/ownership/RaphEffectScope.cjs +1 -0
  70. package/dist/debug/runtime/ownership/RaphEffectScope.js +304 -0
  71. package/dist/debug/runtime/ownership/RaphNode.cjs +1 -0
  72. package/dist/debug/runtime/ownership/RaphNode.js +665 -0
  73. package/dist/debug/runtime/ownership/RaphScope.cjs +1 -0
  74. package/dist/debug/runtime/ownership/RaphScope.js +236 -0
  75. package/dist/debug/runtime/ownership/parent-graph.types.cjs +1 -0
  76. package/dist/debug/runtime/ownership/parent-graph.types.js +1 -0
  77. package/dist/debug/runtime/phases/RaphFrameLoop.cjs +1 -0
  78. package/dist/debug/runtime/{RaphFrameLoop.js → phases/RaphFrameLoop.js} +13 -17
  79. package/dist/debug/runtime/phases/RaphPhasePipeline.cjs +1 -0
  80. package/dist/debug/runtime/phases/RaphPhasePipeline.js +848 -0
  81. package/dist/debug/runtime/phases/RaphPhaseTaskRecord.cjs +1 -0
  82. package/dist/debug/runtime/phases/RaphPhaseTaskRecord.js +176 -0
  83. package/dist/debug/runtime/phases/RaphPhaseWatch.cjs +1 -0
  84. package/dist/debug/runtime/phases/RaphPhaseWatch.js +20 -0
  85. package/dist/debug/runtime/phases/orderPhaseTasks.cjs +1 -0
  86. package/dist/debug/runtime/phases/orderPhaseTasks.js +59 -0
  87. package/dist/debug/runtime/reactivity/PathHandle.cjs +1 -0
  88. package/dist/debug/runtime/reactivity/PathHandle.js +165 -0
  89. package/dist/debug/runtime/reactivity/RaphMetaSource.cjs +1 -0
  90. package/dist/debug/runtime/reactivity/RaphMetaSource.js +108 -0
  91. package/dist/debug/runtime/reactivity/RaphSelector.cjs +1 -0
  92. package/dist/debug/runtime/reactivity/RaphSelector.js +503 -0
  93. package/dist/debug/runtime/reactivity/RaphWorkGraph.cjs +1 -0
  94. package/dist/debug/runtime/reactivity/RaphWorkGraph.js +1157 -0
  95. package/dist/debug/runtime/reactivity/RuntimeReactivity.cjs +1 -0
  96. package/dist/debug/runtime/reactivity/RuntimeReactivity.js +550 -0
  97. package/dist/debug/runtime/reactivity/RuntimeWatch.cjs +1 -0
  98. package/dist/debug/runtime/reactivity/RuntimeWatch.js +77 -0
  99. package/dist/debug/runtime/reactivity/disposal-sweep.utils.cjs +1 -0
  100. package/dist/debug/runtime/reactivity/disposal-sweep.utils.js +42 -0
  101. package/dist/debug/runtime/reactivity/runtime-reactivity.types.cjs +1 -0
  102. package/dist/debug/runtime/reactivity/runtime-reactivity.types.js +1 -0
  103. package/dist/entrypoints/machine-testing.d.ts +2 -0
  104. package/dist/entrypoints/machine.d.ts +5 -0
  105. package/dist/entrypoints/path.d.ts +14 -0
  106. package/dist/entrypoints/reactivity.d.ts +1 -0
  107. package/dist/entrypoints/runtime.d.ts +18 -0
  108. package/dist/index.cjs +1 -1
  109. package/dist/index.js +46 -38
  110. package/dist/machine/compiler.d.ts +71 -0
  111. package/dist/machine/core.d.ts +50 -0
  112. package/dist/machine/persistence.d.ts +5 -0
  113. package/dist/machine/runtime.d.ts +4 -0
  114. package/dist/machine/testing.d.ts +43 -0
  115. package/dist/machine/types.d.ts +155 -0
  116. package/dist/machine-testing.cjs +1 -0
  117. package/dist/machine-testing.js +188 -0
  118. package/dist/machine.cjs +1 -0
  119. package/dist/machine.js +822 -0
  120. package/dist/main.d.ts +20 -0
  121. package/dist/path/{data-path.d.ts → DataPath.d.ts} +16 -40
  122. package/dist/path/{path-router.d.ts → PathRouter.d.ts} +1 -1
  123. package/dist/path/RaphKernel.d.ts +37 -10
  124. package/dist/path/data-path.types.d.ts +34 -0
  125. package/dist/path/data-path.utils.d.ts +9 -0
  126. package/dist/path/{DerivedRegistry.d.ts → derived/DerivedRegistry.d.ts} +2 -2
  127. package/dist/path/{RaphDerivedHandle.d.ts → derived/RaphDerivedHandle.d.ts} +3 -3
  128. package/dist/path/derived/derived-collection.types.d.ts +12 -0
  129. package/dist/path/{derived-collection.d.ts → derived/derived-collection.utils.d.ts} +3 -13
  130. package/dist/path/{derived-strategies.d.ts → derived/derived-strategy.types.d.ts} +0 -3
  131. package/dist/path/derived/derived-strategy.utils.d.ts +4 -0
  132. package/dist/path/{MetaController.d.ts → meta/MetaController.d.ts} +4 -4
  133. package/dist/path/{RaphMeta.d.ts → meta/RaphMeta.d.ts} +2 -2
  134. package/dist/path/{RaphMetaStore.d.ts → meta/RaphMetaStore.d.ts} +1 -1
  135. package/dist/path/{meta.types.d.ts → meta/meta.types.d.ts} +2 -4
  136. package/dist/path/path.types.d.ts +3 -2
  137. package/dist/path/{segment-key.d.ts → pathSegmentKey.d.ts} +1 -1
  138. package/dist/path/{AdapterPathStore.d.ts → storage/AdapterPathStore.d.ts} +4 -4
  139. package/dist/path/{data-adapter.d.ts → storage/DefaultDataAdapter.d.ts} +2 -17
  140. package/dist/path/storage/PathEventHub.d.ts +62 -0
  141. package/dist/path/{PathStore.d.ts → storage/PathStore.d.ts} +7 -7
  142. package/dist/path/storage/data-adapter.types.d.ts +17 -0
  143. package/dist/path.cjs +1 -1
  144. package/dist/path.js +16 -16
  145. package/dist/raph.cjs +1 -1
  146. package/dist/raph.js +46 -38
  147. package/dist/reactivity/disposal.d.ts +14 -0
  148. package/dist/reactivity/engine.d.ts +193 -6
  149. package/dist/reactivity/reactive-object.d.ts +36 -0
  150. package/dist/reactivity/scope.d.ts +108 -30
  151. package/dist/reactivity/types.d.ts +25 -0
  152. package/dist/reactivity.cjs +1 -1
  153. package/dist/reactivity.js +12 -12
  154. package/dist/runtime/RaphRuntime.d.ts +319 -160
  155. package/dist/runtime/diagnostics/RaphDebug.d.ts +239 -0
  156. package/dist/runtime/errors.d.ts +5 -0
  157. package/dist/runtime/ownership/RaphEffectScope.d.ts +98 -0
  158. package/dist/runtime/ownership/RaphNode.d.ts +310 -0
  159. package/dist/runtime/ownership/RaphScope.d.ts +168 -0
  160. package/dist/runtime/{parent-graph.d.ts → ownership/parent-graph.types.d.ts} +0 -1
  161. package/dist/runtime/ownership/parentGraph.d.ts +3 -0
  162. package/dist/runtime/phases/RaphPhasePipeline.d.ts +253 -0
  163. package/dist/runtime/phases/RaphPhaseTaskRecord.d.ts +195 -0
  164. package/dist/runtime/phases/RaphPhaseWatch.d.ts +22 -0
  165. package/dist/runtime/phases/orderPhaseTasks.d.ts +6 -0
  166. package/dist/runtime/phases/phase.types.d.ts +171 -0
  167. package/dist/runtime/{PathHandle.d.ts → reactivity/PathHandle.d.ts} +14 -6
  168. package/dist/runtime/reactivity/RaphMetaSource.d.ts +32 -0
  169. package/dist/runtime/reactivity/RaphSelector.d.ts +338 -0
  170. package/dist/runtime/reactivity/RaphWorkGraph.d.ts +309 -0
  171. package/dist/runtime/reactivity/RuntimeReactivity.d.ts +293 -0
  172. package/dist/runtime/{RuntimeWatch.d.ts → reactivity/RuntimeWatch.d.ts} +12 -6
  173. package/dist/runtime/reactivity/disposal-sweep.utils.d.ts +19 -0
  174. package/dist/runtime/reactivity/runtime-reactivity.types.d.ts +56 -0
  175. package/dist/runtime.cjs +1 -1
  176. package/dist/runtime.js +21 -16
  177. package/package.json +76 -35
  178. package/ReadMe.md +0 -153
  179. package/dist/api/public.d.ts +0 -22
  180. package/dist/debug/api/public.cjs +0 -1
  181. package/dist/debug/api/public.js +0 -40
  182. package/dist/debug/path/AdapterPathStore.cjs +0 -1
  183. package/dist/debug/path/AdapterPathStore.js +0 -133
  184. package/dist/debug/path/DerivedRegistry.cjs +0 -1
  185. package/dist/debug/path/DerivedRegistry.js +0 -157
  186. package/dist/debug/path/MetaController.cjs +0 -1
  187. package/dist/debug/path/PathEventHub.cjs +0 -1
  188. package/dist/debug/path/PathEventHub.js +0 -148
  189. package/dist/debug/path/PathStore.cjs +0 -1
  190. package/dist/debug/path/RaphDerivedHandle.cjs +0 -1
  191. package/dist/debug/path/RaphMeta.cjs +0 -1
  192. package/dist/debug/path/RaphMeta.js +0 -68
  193. package/dist/debug/path/RaphMetaStore.cjs +0 -1
  194. package/dist/debug/path/data-adapter.cjs +0 -1
  195. package/dist/debug/path/data-draft.cjs +0 -1
  196. package/dist/debug/path/data-path.cjs +0 -1
  197. package/dist/debug/path/data-path.js +0 -423
  198. package/dist/debug/path/derived-collection.cjs +0 -1
  199. package/dist/debug/path/path-router.cjs +0 -1
  200. package/dist/debug/path/path-router.js +0 -86
  201. package/dist/debug/path/public.cjs +0 -1
  202. package/dist/debug/path/public.js +0 -18
  203. package/dist/debug/runtime/PathHandle.cjs +0 -1
  204. package/dist/debug/runtime/PathHandle.js +0 -139
  205. package/dist/debug/runtime/RaphDebug.cjs +0 -1
  206. package/dist/debug/runtime/RaphDebug.js +0 -15
  207. package/dist/debug/runtime/RaphFrameLoop.cjs +0 -1
  208. package/dist/debug/runtime/RaphNode.cjs +0 -1
  209. package/dist/debug/runtime/RaphNode.js +0 -143
  210. package/dist/debug/runtime/RaphPhasePipeline.cjs +0 -1
  211. package/dist/debug/runtime/RaphPhasePipeline.js +0 -526
  212. package/dist/debug/runtime/RaphScope.cjs +0 -1
  213. package/dist/debug/runtime/RaphScope.js +0 -64
  214. package/dist/debug/runtime/RuntimeReactivity.cjs +0 -1
  215. package/dist/debug/runtime/RuntimeReactivity.js +0 -154
  216. package/dist/debug/runtime/RuntimeWatch.cjs +0 -1
  217. package/dist/debug/runtime/RuntimeWatch.js +0 -71
  218. package/dist/debug/runtime/public.cjs +0 -1
  219. package/dist/debug/runtime/public.js +0 -18
  220. package/dist/path/PathEventHub.d.ts +0 -48
  221. package/dist/path/public.d.ts +0 -14
  222. package/dist/runtime/RaphDebug.d.ts +0 -32
  223. package/dist/runtime/RaphNode.d.ts +0 -68
  224. package/dist/runtime/RaphPhasePipeline.d.ts +0 -133
  225. package/dist/runtime/RaphScope.d.ts +0 -50
  226. package/dist/runtime/RuntimeReactivity.d.ts +0 -40
  227. package/dist/runtime/phase.types.d.ts +0 -49
  228. package/dist/runtime/public.d.ts +0 -13
  229. package/dist/runtime/runtime-reactivity.types.d.ts +0 -9
  230. /package/dist/debug/path/{meta.types.cjs → data-path.types.cjs} +0 -0
  231. /package/dist/debug/path/{meta.types.js → data-path.types.js} +0 -0
  232. /package/dist/debug/{runtime/runtime-reactivity.types.cjs → path/derived/derived-collection.types.cjs} +0 -0
  233. /package/dist/debug/{runtime/runtime-reactivity.types.js → path/derived/derived-collection.types.js} +0 -0
  234. /package/dist/debug/path/{derived-strategies.cjs → derived/derived-strategy.utils.cjs} +0 -0
  235. /package/dist/debug/path/{derived-strategies.js → derived/derived-strategy.utils.js} +0 -0
  236. /package/dist/debug/path/{segment-key.cjs → pathSegmentKey.cjs} +0 -0
  237. /package/dist/debug/path/{segment-key.js → pathSegmentKey.js} +0 -0
  238. /package/dist/debug/runtime/{parent-graph.cjs → ownership/parentGraph.cjs} +0 -0
  239. /package/dist/debug/runtime/{parent-graph.js → ownership/parentGraph.js} +0 -0
  240. /package/dist/debug/runtime/{disposal.cjs → ownership/throwDisposalErrors.cjs} +0 -0
  241. /package/dist/debug/runtime/{disposal.js → ownership/throwDisposalErrors.js} +0 -0
  242. /package/dist/debug/runtime/{phase.types.cjs → phases/phase.types.cjs} +0 -0
  243. /package/dist/debug/runtime/{phase.types.js → phases/phase.types.js} +0 -0
  244. /package/dist/path/{data-draft.d.ts → storage/createDataDraft.d.ts} +0 -0
  245. /package/dist/runtime/{disposal.d.ts → ownership/throwDisposalErrors.d.ts} +0 -0
  246. /package/dist/runtime/{RaphFrameLoop.d.ts → phases/RaphFrameLoop.d.ts} +0 -0
@@ -0,0 +1,822 @@
1
+ import { b as I, g as M, h as j, c as P, r as k, d as A, s as z } from "./core-CClhT8WR.js";
2
+ import { e as K, a as U, t as X } from "./core-CClhT8WR.js";
3
+ import { RaphScope as D } from "./debug/runtime/ownership/RaphScope.js";
4
+ class _ extends Error {
5
+ }
6
+ function N(o, t) {
7
+ return I(
8
+ o,
9
+ t
10
+ ), v(t.context, "context"), t.output !== void 0 && v(t.output, "output"), Object.freeze({
11
+ active: Object.freeze([...t.active]),
12
+ context: O(t.context),
13
+ history: Object.freeze(Object.fromEntries(
14
+ Object.entries(t.history).map(([e, r]) => [e, Object.freeze([...r])])
15
+ )),
16
+ modelId: o.id,
17
+ ...t.output === void 0 ? {} : { output: O(t.output) },
18
+ revision: t.revision,
19
+ status: t.status,
20
+ version: o.version
21
+ });
22
+ }
23
+ function B(o, t, e) {
24
+ const r = M(o);
25
+ try {
26
+ if (typeof t != "object" || t === null || Array.isArray(t))
27
+ throw new _("[Raph machine] Persisted snapshot must be an object.");
28
+ if (t.modelId !== o.id)
29
+ throw new _(
30
+ `[Raph machine] Snapshot model "${t.modelId}" is incompatible with "${o.id}".`
31
+ );
32
+ if (!Number.isSafeInteger(t.version) || t.version < 1)
33
+ throw new _("[Raph machine] Persisted version must be a positive integer.");
34
+ let i = t;
35
+ for (; i.version < o.version; ) {
36
+ const s = r.migrations.get(i.version);
37
+ if (s === void 0)
38
+ throw new _(
39
+ `[Raph machine] Missing migration from version ${i.version} for "${o.id}".`
40
+ );
41
+ const n = s(i);
42
+ if (n.version !== i.version + 1 || n.modelId !== o.id)
43
+ throw new _(
44
+ `[Raph machine] Migration from version ${i.version} must produce version ${i.version + 1}.`
45
+ );
46
+ i = n;
47
+ }
48
+ if (i.version !== o.version)
49
+ throw new _(
50
+ `[Raph machine] Snapshot version ${i.version} is newer than model version ${o.version}.`
51
+ );
52
+ return v(i.context, "context"), i.output !== void 0 && v(i.output, "output"), j(
53
+ o,
54
+ i,
55
+ e
56
+ );
57
+ } catch (i) {
58
+ throw i instanceof _ ? i : new _(
59
+ `[Raph machine] Persisted snapshot is invalid: ${i instanceof Error ? i.message : String(i)}`
60
+ );
61
+ }
62
+ }
63
+ function v(o, t, e = /* @__PURE__ */ new Set()) {
64
+ if (o === null)
65
+ return;
66
+ const r = typeof o;
67
+ if (r === "string" || r === "boolean")
68
+ return;
69
+ if (r === "number") {
70
+ if (!Number.isFinite(o))
71
+ throw new _(`[Raph machine] ${t} contains a non-finite number.`);
72
+ return;
73
+ }
74
+ if (r !== "object")
75
+ throw new _(`[Raph machine] ${t} is not JSON-safe.`);
76
+ const i = o;
77
+ if (e.has(i))
78
+ throw new _(`[Raph machine] ${t} contains a cycle.`);
79
+ if (e.add(i), Array.isArray(o))
80
+ o.forEach((s, n) => v(s, `${t}[${n}]`, e));
81
+ else {
82
+ const s = Object.getPrototypeOf(o);
83
+ if (s !== Object.prototype && s !== null)
84
+ throw new _(`[Raph machine] ${t} contains a non-plain object.`);
85
+ for (const [n, h] of Object.entries(o))
86
+ v(h, `${t}.${n}`, e);
87
+ }
88
+ e.delete(i);
89
+ }
90
+ function O(o) {
91
+ return JSON.parse(JSON.stringify(o));
92
+ }
93
+ const E = /* @__PURE__ */ new WeakMap(), y = Symbol("RaphMachineActivationCancelled"), C = Object.freeze({ type: "@@init" }), L = Object.freeze({
94
+ clearTimeout: (o) => clearTimeout(o),
95
+ now: () => Date.now(),
96
+ setTimeout: (o, t) => setTimeout(o, t)
97
+ });
98
+ class S {
99
+ constructor(t, e, r, i) {
100
+ if (this.id = void 0, this.model = void 0, this.snapshot = void 0, this._owner = void 0, this._runtime = void 0, this._compiled = void 0, this._bindings = void 0, this._clock = void 0, this._input = void 0, this._errorPolicy = void 0, this._releaseSnapshot = R, this._writeSnapshotInBoundary = void 0, this._currentSnapshot = void 0, this._queue = void 0, this._listeners = void 0, this._stateCleanups = void 0, this._invocations = void 0, this._actors = void 0, this._releaseOwnership = R, this._releaseDebug = R, this._restored = !1, this._initialPlan = void 0, this._onError = void 0, this._queueCursor = 0, this._processing = !1, this._started = !1, this._stopped = !1, this._lastIgnoredSnapshot = void 0, this._lastIgnoredResult = void 0, !(t instanceof D))
101
+ throw new TypeError("[Raph machine] Machine owner must be a Raph Runtime scope or node.");
102
+ if (t._assertAlive(), this._owner = t, this._runtime = t.runtime, this._compiled = M(
103
+ e
104
+ ), this.model = e, this._bindings = r, this._clock = i.clock ?? L, this._input = i.input, this._errorPolicy = i.errorPolicy ?? "throw", this._errorPolicy !== "ignore" && this._errorPolicy !== "stop" && this._errorPolicy !== "throw")
105
+ throw new TypeError(`[Raph machine] Unknown error policy "${String(this._errorPolicy)}".`);
106
+ this._onError = i.onError;
107
+ const s = (E.get(this._runtime) ?? 0) + 1;
108
+ if (E.set(this._runtime, s), this.id = i.id ?? `${e.id}#${s}`, !this.id.trim())
109
+ throw new TypeError("[Raph machine] Instance id must not be empty.");
110
+ if (this._runtime._hasMachineDebug(this.id))
111
+ throw new Error(`[Raph] Machine diagnostic id "${this.id}" is already registered.`);
112
+ const n = i.persisted === void 0 ? this._runtime.untracked(() => P(
113
+ e,
114
+ this._input,
115
+ r,
116
+ this._runtime._traces("machine") ? this._traceHooks() : void 0
117
+ )) : void 0, h = n?.snapshot ?? B(e, i.persisted, this._input);
118
+ this._initialPlan = n?.plan, this._restored = i.persisted !== void 0;
119
+ const c = J(this._runtime, t, h);
120
+ this._releaseSnapshot = c.dispose, this._writeSnapshotInBoundary = c.writeInBoundary, this._currentSnapshot = h, this.snapshot = c.source;
121
+ try {
122
+ this._releaseDebug = this._runtime._registerMachineDebug({
123
+ id: this.id,
124
+ ownerId: t.id,
125
+ ...i.parentId === void 0 ? {} : { parentId: i.parentId },
126
+ snapshot: () => {
127
+ const a = this._currentSnapshot, u = Object.fromEntries(this._activeClosure(a).flatMap((d) => {
128
+ const g = this._compiled.states[d].meta;
129
+ return g === void 0 ? [] : [[this._compiled.states[d].path, g]];
130
+ }));
131
+ return Object.freeze({
132
+ active: a.active,
133
+ actors: Object.freeze([...this._actors?.keys() ?? []]),
134
+ contextKeys: Object.freeze(T(a.context) ? Object.keys(a.context) : []),
135
+ id: this.id,
136
+ meta: Object.freeze(u),
137
+ modelId: e.id,
138
+ ownerId: t.id,
139
+ ...i.parentId === void 0 ? {} : { parentId: i.parentId },
140
+ revision: a.revision,
141
+ status: a.status
142
+ });
143
+ }
144
+ }), this._releaseOwnership = t.own(() => this._dispose(!1));
145
+ } catch (a) {
146
+ const u = [a];
147
+ try {
148
+ this._releaseDebug();
149
+ } catch (d) {
150
+ u.push(d);
151
+ }
152
+ try {
153
+ this._releaseSnapshot();
154
+ } catch (d) {
155
+ u.push(d);
156
+ }
157
+ throw u.length > 1 ? new AggregateError(u, `[Raph machine] Machine "${this.id}" construction rollback failed.`) : a;
158
+ }
159
+ }
160
+ get started() {
161
+ return this._started && !this._stopped;
162
+ }
163
+ start() {
164
+ if (this._stopped)
165
+ throw new Error(`[Raph machine] Machine "${this.id}" is stopped.`);
166
+ if (this._started)
167
+ return this;
168
+ this._owner._assertAlive();
169
+ const t = this._currentSnapshot;
170
+ if (t.status === "stopped")
171
+ throw new Error(`[Raph machine] Persisted machine "${this.id}" is stopped.`);
172
+ if (this._started = !0, this._compiled.flat)
173
+ return this;
174
+ const e = this._activeClosure(t).sort((s, n) => this._compiled.states[s].depth - this._compiled.states[n].depth);
175
+ let r = !1, i;
176
+ this._processing = !0;
177
+ try {
178
+ const s = [];
179
+ if (!this._restored) {
180
+ try {
181
+ this._runOutputs(
182
+ this._initialPlan?.exitOutputs ?? [],
183
+ t,
184
+ this._runtime._currentTraceCauseId()
185
+ );
186
+ } catch (n) {
187
+ s.push(n);
188
+ }
189
+ if (this.started)
190
+ try {
191
+ this._runOutputs(
192
+ this._initialPlan?.transitionOutputs ?? [],
193
+ t,
194
+ this._runtime._currentTraceCauseId()
195
+ );
196
+ } catch (n) {
197
+ s.push(n);
198
+ }
199
+ if (this.started) {
200
+ const n = e.flatMap((h) => this._compiled.states[h].entry.map((c) => ({ event: C, ref: c })));
201
+ try {
202
+ this._runOutputs(n, t, this._runtime._currentTraceCauseId());
203
+ } catch (h) {
204
+ s.push(h);
205
+ }
206
+ }
207
+ }
208
+ if (this.started && t.status === "active")
209
+ for (const n of e) {
210
+ if (!this.started)
211
+ break;
212
+ try {
213
+ this._activateState(n, C, t);
214
+ } catch (h) {
215
+ s.push(h);
216
+ }
217
+ }
218
+ l(s, `[Raph machine] Machine "${this.id}" start work failed.`);
219
+ } catch (s) {
220
+ try {
221
+ this._handlePostCommitError(s, "start", this._runtime._currentTraceCauseId());
222
+ } catch (n) {
223
+ r = !0, i = n;
224
+ }
225
+ } finally {
226
+ this._processing = !1, r ? (this._queue !== void 0 && (this._queue.length = 0), this._queueCursor = 0) : this._drainQueued();
227
+ }
228
+ if (r)
229
+ throw i;
230
+ return this;
231
+ }
232
+ send(t) {
233
+ return this._enqueue(t);
234
+ }
235
+ sendTo(t, e) {
236
+ const r = this._actors?.get(t);
237
+ if (r === void 0)
238
+ throw new Error(`[Raph machine] Actor "${t}" is not active in "${this.id}".`);
239
+ return r.send(e);
240
+ }
241
+ matches(t) {
242
+ return this.snapshot.get().active.some((e) => e === t || e.startsWith(`${t}.`));
243
+ }
244
+ hasTag(t) {
245
+ for (const e of this._activeClosure(this.snapshot.get()))
246
+ if (this._compiled.states[e].tags.includes(t))
247
+ return !0;
248
+ return !1;
249
+ }
250
+ subscribe(t) {
251
+ if (typeof t != "function")
252
+ throw new TypeError("[Raph machine] Subscriber must be a function.");
253
+ if (this._stopped)
254
+ return () => {
255
+ };
256
+ const e = { listener: t }, r = this._listeners;
257
+ return this._listeners = r === void 0 ? [e] : [...r, e], () => this._removeListener(e);
258
+ }
259
+ persist() {
260
+ return N(this.model, this.snapshot.get());
261
+ }
262
+ stop() {
263
+ let t;
264
+ try {
265
+ this._dispose(!0);
266
+ } catch (e) {
267
+ (t ??= []).push(e);
268
+ }
269
+ try {
270
+ this._releaseOwnership();
271
+ } catch (e) {
272
+ (t ??= []).push(e);
273
+ }
274
+ l(t, `[Raph machine] Machine "${this.id}" stop failed.`);
275
+ }
276
+ _enqueue(t, e) {
277
+ if (!this.started)
278
+ throw new Error(`[Raph machine] Machine "${this.id}" must be started before send().`);
279
+ if (Q(t), this._processing)
280
+ return (this._queue ??= []).push({ event: t, ...e === void 0 ? {} : { transitions: e } }), { snapshot: this._currentSnapshot, status: "queued" };
281
+ this._processing = !0;
282
+ let r;
283
+ try {
284
+ r = this._process(t, e), this._drainQueuedItems();
285
+ } finally {
286
+ this._queue !== void 0 && (this._queue.length = 0), this._queueCursor = 0, this._processing = !1;
287
+ }
288
+ return r;
289
+ }
290
+ _process(t, e) {
291
+ const r = this._currentSnapshot, i = this._runtime._nextTraceOperation("machine"), s = this._runtime._currentTraceCauseId() ?? i;
292
+ let n;
293
+ i !== void 0 && this._trace(t.type, "start", i, s, r);
294
+ try {
295
+ const h = e === void 0 ? this._compiled.eventIndexes.get(t.type) : void 0;
296
+ if (e === void 0 && h === void 0)
297
+ return i !== void 0 && this._trace(t.type, "ignored", i, s, r), this._ignored(r);
298
+ const c = i === void 0 ? void 0 : this._traceHooks(i, s), a = this._runtime._hasActiveReactiveContext() ? this._runtime.untracked(() => this._evaluate(r, t, h, e, c)) : this._evaluate(r, t, h, e, c);
299
+ if (!a.changed)
300
+ return i !== void 0 && this._trace(t.type, "ignored", i, s, r), this._ignored(r);
301
+ this._runtime._batchReactive(() => {
302
+ this._writeSnapshotInBoundary(a.snapshot), this._currentSnapshot = a.snapshot, n = a.snapshot;
303
+ });
304
+ let u;
305
+ try {
306
+ this._applyPlan(a.plan, a.snapshot, s);
307
+ } catch (m) {
308
+ (u ??= []).push(m);
309
+ }
310
+ const d = this._listeners;
311
+ if (this.started && d !== void 0) {
312
+ const m = this._notifyListeners(d, a.snapshot);
313
+ m !== void 0 && (u ??= []).push(...m);
314
+ }
315
+ if (this.started && a.snapshot.status === "done")
316
+ try {
317
+ this._stopStateResources();
318
+ } catch (m) {
319
+ (u ??= []).push(m);
320
+ }
321
+ u !== void 0 && l(u, `[Raph machine] Machine "${this.id}" post-commit work failed.`);
322
+ const g = this.started ? a.snapshot : this._currentSnapshot;
323
+ return i !== void 0 && this._trace(t.type, "end", i, s, g), { snapshot: g, status: "transitioned" };
324
+ } catch (h) {
325
+ if (n !== void 0)
326
+ return this._handlePostCommitError(h, t.type, s), { snapshot: this._currentSnapshot, status: "transitioned" };
327
+ if (this._traceError(t.type, h, i, s), this._onError !== void 0)
328
+ return this._onError(h), this._ignored(this._currentSnapshot);
329
+ if (this._errorPolicy === "stop" && this._dispose(!0), this._errorPolicy === "ignore")
330
+ return this._ignored(this._currentSnapshot);
331
+ throw h;
332
+ }
333
+ }
334
+ _ignored(t) {
335
+ return this._lastIgnoredSnapshot !== t && (this._lastIgnoredSnapshot = t, this._lastIgnoredResult = { snapshot: t, status: "ignored" }), this._lastIgnoredResult;
336
+ }
337
+ _evaluate(t, e, r, i, s) {
338
+ return i === void 0 ? k(
339
+ this.model,
340
+ t,
341
+ e,
342
+ this._bindings,
343
+ this._input,
344
+ s,
345
+ this._compiled,
346
+ r
347
+ ) : A(
348
+ this.model,
349
+ t,
350
+ e,
351
+ i,
352
+ this._bindings,
353
+ this._input,
354
+ s
355
+ );
356
+ }
357
+ _drainQueued() {
358
+ if (this._queue === void 0 || this._queue.length === 0 || !this.started) {
359
+ this._queue !== void 0 && (this._queue.length = 0), this._queueCursor = 0;
360
+ return;
361
+ }
362
+ this._processing = !0;
363
+ try {
364
+ this._drainQueuedItems();
365
+ } finally {
366
+ this._queue !== void 0 && (this._queue.length = 0), this._queueCursor = 0, this._processing = !1;
367
+ }
368
+ }
369
+ _drainQueuedItems() {
370
+ const t = this._queue;
371
+ if (t !== void 0)
372
+ for (; this._queueCursor < t.length && this.started; ) {
373
+ const e = t[this._queueCursor++];
374
+ this._process(e.event, e.transitions);
375
+ }
376
+ }
377
+ _applyPlan(t, e, r) {
378
+ if (t.exited.length === 0 && t.exitOutputs.length === 0 && t.transitionOutputs.length === 0 && t.transitionOutputRefs === void 0 && t.entryOutputs.length === 0 && t.entered.length === 0)
379
+ return;
380
+ const i = [];
381
+ for (const s of t.exited) {
382
+ try {
383
+ this._cleanupState(s);
384
+ } catch (n) {
385
+ i.push(n);
386
+ }
387
+ if (!this.started)
388
+ break;
389
+ }
390
+ if (this.started && t.exitOutputs.length !== 0)
391
+ try {
392
+ this._runOutputs(t.exitOutputs, e, r);
393
+ } catch (s) {
394
+ i.push(s);
395
+ }
396
+ if (this.started && t.transitionOutputRefs !== void 0)
397
+ try {
398
+ this._runOutputRefs(t.transitionOutputRefs, t.event, e, r);
399
+ } catch (s) {
400
+ i.push(s);
401
+ }
402
+ else if (this.started && t.transitionOutputs.length !== 0)
403
+ try {
404
+ this._runOutputs(t.transitionOutputs, e, r);
405
+ } catch (s) {
406
+ i.push(s);
407
+ }
408
+ if (this.started && t.entryOutputs.length !== 0)
409
+ try {
410
+ this._runOutputs(t.entryOutputs, e, r);
411
+ } catch (s) {
412
+ i.push(s);
413
+ }
414
+ if (this.started)
415
+ for (const s of t.entered) {
416
+ if (!this.started)
417
+ break;
418
+ try {
419
+ this._activateState(s, t.event, e);
420
+ } catch (n) {
421
+ i.push(n);
422
+ }
423
+ }
424
+ l(i, `[Raph machine] Machine "${this.id}" post-commit work failed.`);
425
+ }
426
+ _activateState(t, e, r) {
427
+ if (!this.started || this._stateCleanups?.has(t))
428
+ return;
429
+ const i = this._compiled.states[t];
430
+ if (i.resources.length === 0 && i.after.size === 0 && i.invokes.length === 0)
431
+ return;
432
+ const s = [], n = this._stateCleanups ??= /* @__PURE__ */ new Map();
433
+ n.set(t, s);
434
+ try {
435
+ for (const h of i.resources) {
436
+ const c = this._bindings.resources?.[h];
437
+ if (c === void 0)
438
+ throw new Error(`[Raph machine] Resource binding "${h}" is not defined.`);
439
+ const a = new AbortController();
440
+ let u;
441
+ try {
442
+ const d = this._runtime.untracked(() => c({
443
+ context: r.context,
444
+ event: e,
445
+ input: this._input,
446
+ owner: this._owner,
447
+ signal: a.signal,
448
+ snapshot: r
449
+ }));
450
+ typeof d == "function" && (u = d);
451
+ } catch (d) {
452
+ throw a.abort(), d;
453
+ }
454
+ if (s.push(b(() => {
455
+ a.abort(), u?.();
456
+ })), !this.started)
457
+ throw y;
458
+ }
459
+ for (const [h, c] of i.after) {
460
+ const a = this._resolveDelay(h, r, e);
461
+ if (!this.started)
462
+ throw y;
463
+ const u = this._clock.setTimeout(() => {
464
+ !this.started || !this._stateCleanups?.has(t) || this._enqueue(Object.freeze({ type: `@@after:${i.path}:${h}` }), c);
465
+ }, a);
466
+ if (s.push(b(() => this._clock.clearTimeout(u))), !this.started)
467
+ throw y;
468
+ }
469
+ for (const h of i.invokes)
470
+ if (this._startInvoke(i, h, e, r, s), !this.started)
471
+ throw y;
472
+ } catch (h) {
473
+ n.get(t) === s && n.delete(t), n.size === 0 && this._stateCleanups === n && (this._stateCleanups = void 0);
474
+ const c = x(s);
475
+ l(
476
+ h === y ? c : [h, ...c],
477
+ `[Raph machine] State "${i.path}" activation failed.`
478
+ );
479
+ }
480
+ }
481
+ _startInvoke(t, e, r, i, s) {
482
+ const n = this._invocations ??= /* @__PURE__ */ new Map();
483
+ if (n.has(e.id))
484
+ throw new Error(`[Raph machine] Invoke id "${e.id}" is already active.`);
485
+ const h = new AbortController(), c = {}, a = { controller: h, invoke: e, token: c };
486
+ n.set(e.id, a);
487
+ const u = () => {
488
+ h.abort(), n.get(e.id)?.token === c && n.delete(e.id), n.size === 0 && this._invocations === n && (this._invocations = void 0);
489
+ const p = this._actors;
490
+ p?.delete(e.id), p?.size === 0 && this._actors === p && (this._actors = void 0), a.actor?.stop();
491
+ };
492
+ s.push(b(u));
493
+ const d = typeof e.input == "function" ? e.input({ context: i.context, event: r, input: this._input }) : e.input;
494
+ if (!this.started)
495
+ return;
496
+ if (e.type === "machine") {
497
+ const f = typeof e.src == "string" ? this._bindings.actors?.[e.src] : e.src;
498
+ if (f === void 0)
499
+ throw new Error(`[Raph machine] Actor source "${String(e.src)}" is not defined.`);
500
+ const p = new S(
501
+ this._owner,
502
+ f,
503
+ {},
504
+ {
505
+ errorPolicy: e.onError.length === 0 ? "throw" : "ignore",
506
+ id: `${this.id}/${e.id}`,
507
+ input: d,
508
+ onError: e.onError.length === 0 ? void 0 : (w) => {
509
+ !this.started || this._invocations?.get(e.id)?.token !== c || this._enqueue(
510
+ Object.freeze({ error: w, type: `@@error.actor:${e.id}` }),
511
+ e.onError
512
+ );
513
+ },
514
+ parentId: this.id
515
+ }
516
+ );
517
+ if (!this.started || this._invocations?.get(e.id)?.token !== c || h.signal.aborted) {
518
+ p.stop();
519
+ return;
520
+ }
521
+ a.actor = p, (this._actors ??= /* @__PURE__ */ new Map()).set(e.id, p);
522
+ const q = p.subscribe((w) => {
523
+ w.status === "done" && this._invocations?.get(e.id)?.token === c && this._enqueue(
524
+ Object.freeze({ output: w.output, type: `@@done.actor:${e.id}` }),
525
+ e.onDone
526
+ );
527
+ });
528
+ if (s.push(b(q)), p.start(), !this.started)
529
+ return;
530
+ const $ = p.snapshot.peek();
531
+ $.status === "done" && this._invocations?.get(e.id)?.token === c && this._enqueue(
532
+ Object.freeze({ output: $.output, type: `@@done.actor:${e.id}` }),
533
+ e.onDone
534
+ );
535
+ return;
536
+ }
537
+ if (typeof e.src != "string")
538
+ throw new TypeError(`[Raph machine] Task invoke "${e.id}" requires a string source.`);
539
+ const g = this._bindings.tasks?.[e.src];
540
+ if (g === void 0)
541
+ throw new Error(`[Raph machine] Task binding "${e.src}" is not defined.`);
542
+ let m;
543
+ try {
544
+ m = this._runtime.untracked(() => g({
545
+ context: i.context,
546
+ event: r,
547
+ input: this._input,
548
+ send: (f) => {
549
+ const p = this._invocations?.get(e.id);
550
+ !this.started || p?.token !== c || h.signal.aborted || this.send(f);
551
+ },
552
+ signal: h.signal
553
+ }));
554
+ } catch (f) {
555
+ this._completeInvoke(e, c, !1, f);
556
+ return;
557
+ }
558
+ this.started && Promise.resolve(m).then(
559
+ (f) => this._completeInvoke(e, c, !0, f),
560
+ (f) => this._completeInvoke(e, c, !1, f)
561
+ );
562
+ }
563
+ _completeInvoke(t, e, r, i) {
564
+ const s = this._invocations?.get(t.id);
565
+ !this.started || s?.token !== e || s.controller.signal.aborted || this._enqueue(
566
+ Object.freeze(r ? { data: i, type: `@@done.invoke:${t.id}` } : { error: i, type: `@@error.invoke:${t.id}` }),
567
+ r ? t.onDone : t.onError
568
+ );
569
+ }
570
+ _resolveDelay(t, e, r) {
571
+ const i = Number(t), s = Number.isFinite(i) && t.trim() !== "" ? i : this._bindings.delays?.[t];
572
+ if (s === void 0)
573
+ throw new Error(`[Raph machine] Delay binding "${t}" is not defined.`);
574
+ const n = typeof s == "function" ? s({ context: e.context, event: r, input: this._input }) : s;
575
+ if (!Number.isFinite(n) || n < 0)
576
+ throw new TypeError(`[Raph machine] Delay "${t}" must resolve to a non-negative finite number.`);
577
+ return n;
578
+ }
579
+ _runOutputs(t, e, r) {
580
+ if (t.length === 0)
581
+ return;
582
+ let i;
583
+ for (const s of t) {
584
+ try {
585
+ const n = this._resolveOutput(s.ref);
586
+ this._runtime._runWithTraceCause(r, () => this._runtime.untracked(() => n({
587
+ context: e.context,
588
+ event: s.event,
589
+ input: this._input,
590
+ snapshot: e
591
+ })));
592
+ } catch (n) {
593
+ (i ??= []).push(n);
594
+ }
595
+ if (!this.started)
596
+ break;
597
+ }
598
+ i !== void 0 && l(i, `[Raph machine] Machine "${this.id}" output work failed.`);
599
+ }
600
+ _notifyListeners(t, e) {
601
+ let r;
602
+ for (let i = 0; i < t.length; i += 1)
603
+ try {
604
+ t[i].listener(e);
605
+ } catch (s) {
606
+ (r ??= []).push(s);
607
+ }
608
+ return r;
609
+ }
610
+ _removeListener(t) {
611
+ const e = this._listeners;
612
+ if (e === void 0)
613
+ return;
614
+ const r = e.indexOf(t);
615
+ r !== -1 && (this._listeners = e.length === 1 ? void 0 : [...e.slice(0, r), ...e.slice(r + 1)]);
616
+ }
617
+ _runOutputRefs(t, e, r, i) {
618
+ let s;
619
+ for (const n of t) {
620
+ try {
621
+ const h = this._resolveOutput(n);
622
+ this._runtime._runWithTraceCause(i, () => this._runtime.untracked(() => h({
623
+ context: r.context,
624
+ event: e,
625
+ input: this._input,
626
+ snapshot: r
627
+ })));
628
+ } catch (h) {
629
+ (s ??= []).push(h);
630
+ }
631
+ if (!this.started)
632
+ break;
633
+ }
634
+ s !== void 0 && l(s, `[Raph machine] Machine "${this.id}" output work failed.`);
635
+ }
636
+ _resolveOutput(t) {
637
+ if (typeof t == "function")
638
+ return t;
639
+ const e = this._bindings.outputs?.[t];
640
+ if (e === void 0)
641
+ throw new Error(`[Raph machine] Output binding "${t}" is not defined.`);
642
+ return e;
643
+ }
644
+ _cleanupState(t) {
645
+ const e = this._stateCleanups, r = e?.get(t);
646
+ e === void 0 || r === void 0 || (e.delete(t), e.size === 0 && this._stateCleanups === e && (this._stateCleanups = void 0), l(
647
+ x(r),
648
+ `[Raph machine] State "${this._compiled.states[t].path}" cleanup failed.`
649
+ ));
650
+ }
651
+ _stopStateResources() {
652
+ if (this._stateCleanups === void 0)
653
+ return;
654
+ const t = [...this._stateCleanups.keys()].sort((r, i) => this._compiled.states[i].depth - this._compiled.states[r].depth), e = [];
655
+ for (const r of t)
656
+ try {
657
+ this._cleanupState(r);
658
+ } catch (i) {
659
+ e.push(i);
660
+ }
661
+ l(e, `[Raph machine] Machine "${this.id}" final cleanup failed.`);
662
+ }
663
+ _dispose(t) {
664
+ if (this._stopped)
665
+ return;
666
+ this._stopped = !0, this._started = !1;
667
+ let e;
668
+ try {
669
+ this._stopStateResources();
670
+ } catch (r) {
671
+ (e ??= []).push(r);
672
+ }
673
+ this._queue !== void 0 && (this._queue.length = 0), this._queueCursor = 0, this._listeners = void 0;
674
+ try {
675
+ this._releaseDebug();
676
+ } catch (r) {
677
+ (e ??= []).push(r);
678
+ }
679
+ if (t && !this._runtime.disposed) {
680
+ const r = this._currentSnapshot, i = z(this.model, r);
681
+ try {
682
+ this._runtime._batchReactive(() => {
683
+ this._writeSnapshotInBoundary(i), this._currentSnapshot = i;
684
+ });
685
+ } catch (s) {
686
+ (e ??= []).push(s);
687
+ }
688
+ }
689
+ try {
690
+ this._releaseSnapshot();
691
+ } catch (r) {
692
+ (e ??= []).push(r);
693
+ }
694
+ l(e, `[Raph machine] Machine "${this.id}" stop failed.`);
695
+ }
696
+ _activeClosure(t) {
697
+ const e = /* @__PURE__ */ new Set();
698
+ for (const r of t.active) {
699
+ let i = this._compiled.pathToState.get(r);
700
+ for (; i !== void 0; )
701
+ e.add(i), i = this._compiled.states[i].parent;
702
+ }
703
+ return [...e];
704
+ }
705
+ _traceHooks(t, e = this._runtime._currentTraceCauseId() ?? t) {
706
+ return {
707
+ guard: (r, i, s) => this._runtime._recordTrace({
708
+ ...e === void 0 ? {} : { causeId: e },
709
+ ...s === void 0 ? {} : { context: s },
710
+ guard: i ? "passed" : "failed",
711
+ kind: "machine",
712
+ name: `${this.id}:guard:${r}`,
713
+ ...t === void 0 ? {} : { operationId: t }
714
+ }),
715
+ transition: (r) => this._runtime._recordTrace({
716
+ ...e === void 0 ? {} : { causeId: e },
717
+ details: r,
718
+ kind: "machine",
719
+ name: `${this.id}:transition`,
720
+ ...t === void 0 ? {} : { operationId: t }
721
+ })
722
+ };
723
+ }
724
+ _trace(t, e, r, i, s) {
725
+ this._runtime._recordTrace({
726
+ ...i === void 0 ? {} : { causeId: i },
727
+ context: T(s.context) ? s.context : void 0,
728
+ details: { active: s.active, revision: s.revision },
729
+ kind: "machine",
730
+ name: `${this.id}:${t}`,
731
+ ...r === void 0 ? {} : { operationId: r },
732
+ status: e
733
+ });
734
+ }
735
+ _traceError(t, e, r, i) {
736
+ const s = { message: e instanceof Error ? e.message : String(e) };
737
+ this._runtime._recordTrace({
738
+ ...i === void 0 ? {} : { causeId: i },
739
+ details: s,
740
+ kind: "machine",
741
+ name: `${this.id}:${t}`,
742
+ ...r === void 0 ? {} : { operationId: r },
743
+ status: "error"
744
+ }), this._runtime._recordTrace({
745
+ ...i === void 0 ? {} : { causeId: i },
746
+ details: s,
747
+ kind: "error",
748
+ name: `machine:${this.id}`,
749
+ ...r === void 0 ? {} : { operationId: r },
750
+ status: "error"
751
+ });
752
+ }
753
+ _handlePostCommitError(t, e, r) {
754
+ if (this._traceError(e, t, void 0, r), this._onError !== void 0) {
755
+ this._onError(t);
756
+ return;
757
+ }
758
+ if (this._errorPolicy === "stop" && this._dispose(!0), this._errorPolicy === "throw")
759
+ throw t;
760
+ }
761
+ }
762
+ function F(o, t, e = {}, r = {}) {
763
+ return new S(o, t, e, r);
764
+ }
765
+ function J(o, t, e) {
766
+ const r = o._getWorkGraph(), i = r.createProducer(t), s = o._getReactivity()._ownedSignal(e, r.producerHooks(i));
767
+ let n = s.source, h = e;
768
+ const c = Object.freeze({
769
+ get: () => (o._assertSourceReadable(t), n === void 0 ? h : n.get()),
770
+ peek: () => (t._assertAlive(), n === void 0 ? h : n.peek())
771
+ });
772
+ return r.attachProducer(i, c), {
773
+ dispose: () => {
774
+ const a = n;
775
+ a !== void 0 && (h = a.peek(), n = void 0, s.dispose());
776
+ },
777
+ source: c,
778
+ writeInBoundary: (a) => {
779
+ t._assertAlive(), n.set(a), h = a;
780
+ }
781
+ };
782
+ }
783
+ function x(o) {
784
+ const t = [];
785
+ for (let e = o.length - 1; e >= 0; e--)
786
+ try {
787
+ o[e]();
788
+ } catch (r) {
789
+ t.push(r);
790
+ }
791
+ return t;
792
+ }
793
+ function l(o, t) {
794
+ if (o?.length === 1)
795
+ throw o[0];
796
+ if (o !== void 0 && o.length > 1)
797
+ throw new AggregateError(o, t);
798
+ }
799
+ function Q(o) {
800
+ if (typeof o != "object" || o === null || typeof o.type != "string" || !o.type)
801
+ throw new TypeError("[Raph machine] Event must contain a non-empty string type.");
802
+ }
803
+ function T(o) {
804
+ return typeof o == "object" && o !== null && !Array.isArray(o);
805
+ }
806
+ function R() {
807
+ }
808
+ function b(o) {
809
+ let t = !0;
810
+ return () => {
811
+ t && (t = !1, o());
812
+ };
813
+ }
814
+ export {
815
+ _ as MachinePersistenceError,
816
+ F as createMachine,
817
+ K as defineMachine,
818
+ U as getInitialSnapshot,
819
+ N as persistMachineSnapshot,
820
+ B as restoreMachineSnapshot,
821
+ X as transition
822
+ };