@raphy-js/raph 1.0.1-dev.20 → 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 (107) hide show
  1. package/dist/{core-CyilRD7s.js → core-CClhT8WR.js} +389 -400
  2. package/dist/core-cPQWYD4K.cjs +1 -0
  3. package/dist/debug/api/Raph.cjs +1 -1
  4. package/dist/debug/api/Raph.js +46 -47
  5. package/dist/debug/api/RaphFacadeSession.cjs +1 -1
  6. package/dist/debug/api/RaphFacadeSession.js +85 -89
  7. package/dist/debug/api/RaphReactivityInstance.cjs +1 -1
  8. package/dist/debug/api/RaphReactivityInstance.js +27 -30
  9. package/dist/debug/path/DataPath.cjs +1 -1
  10. package/dist/debug/path/DataPath.js +198 -202
  11. package/dist/debug/path/PathRouter.cjs +1 -1
  12. package/dist/debug/path/PathRouter.js +44 -50
  13. package/dist/debug/path/RaphKernel.cjs +1 -1
  14. package/dist/debug/path/RaphKernel.js +257 -228
  15. package/dist/debug/path/data-path.utils.cjs +1 -1
  16. package/dist/debug/path/data-path.utils.js +41 -41
  17. package/dist/debug/path/derived/DerivedRegistry.cjs +1 -1
  18. package/dist/debug/path/derived/DerivedRegistry.js +52 -66
  19. package/dist/debug/path/derived/RaphDerivedHandle.cjs +1 -1
  20. package/dist/debug/path/derived/RaphDerivedHandle.js +19 -22
  21. package/dist/debug/path/derived/derived-collection.utils.cjs +1 -1
  22. package/dist/debug/path/derived/derived-collection.utils.js +70 -70
  23. package/dist/debug/path/meta/MetaController.cjs +1 -1
  24. package/dist/debug/path/meta/MetaController.js +57 -65
  25. package/dist/debug/path/meta/RaphMeta.cjs +1 -1
  26. package/dist/debug/path/meta/RaphMeta.js +26 -32
  27. package/dist/debug/path/meta/RaphMetaStore.cjs +1 -1
  28. package/dist/debug/path/meta/RaphMetaStore.js +24 -30
  29. package/dist/debug/path/storage/AdapterPathStore.cjs +1 -1
  30. package/dist/debug/path/storage/AdapterPathStore.js +49 -54
  31. package/dist/debug/path/storage/DefaultDataAdapter.cjs +1 -1
  32. package/dist/debug/path/storage/DefaultDataAdapter.js +46 -56
  33. package/dist/debug/path/storage/PathEventHub.cjs +1 -1
  34. package/dist/debug/path/storage/PathEventHub.js +74 -61
  35. package/dist/debug/path/storage/PathStore.cjs +1 -1
  36. package/dist/debug/path/storage/PathStore.js +89 -99
  37. package/dist/debug/path/storage/createDataDraft.cjs +1 -1
  38. package/dist/debug/path/storage/createDataDraft.js +24 -28
  39. package/dist/debug/reactivity/disposal.cjs +1 -0
  40. package/dist/debug/reactivity/disposal.js +32 -0
  41. package/dist/debug/reactivity/engine.cjs +1 -1
  42. package/dist/debug/reactivity/engine.js +787 -392
  43. package/dist/debug/reactivity/reactive-object.cjs +1 -1
  44. package/dist/debug/reactivity/reactive-object.js +81 -96
  45. package/dist/debug/reactivity/scope.cjs +1 -1
  46. package/dist/debug/reactivity/scope.js +221 -117
  47. package/dist/debug/reactivity/watch.cjs +1 -1
  48. package/dist/debug/reactivity/watch.js +1 -1
  49. package/dist/debug/runtime/RaphRuntime.cjs +1 -1
  50. package/dist/debug/runtime/RaphRuntime.js +764 -411
  51. package/dist/debug/runtime/diagnostics/RaphDebug.cjs +1 -1
  52. package/dist/debug/runtime/diagnostics/RaphDebug.js +90 -100
  53. package/dist/debug/runtime/ownership/RaphEffectScope.cjs +1 -1
  54. package/dist/debug/runtime/ownership/RaphEffectScope.js +215 -82
  55. package/dist/debug/runtime/ownership/RaphNode.cjs +1 -1
  56. package/dist/debug/runtime/ownership/RaphNode.js +595 -136
  57. package/dist/debug/runtime/ownership/RaphScope.cjs +1 -1
  58. package/dist/debug/runtime/ownership/RaphScope.js +203 -68
  59. package/dist/debug/runtime/phases/RaphFrameLoop.cjs +1 -1
  60. package/dist/debug/runtime/phases/RaphFrameLoop.js +13 -17
  61. package/dist/debug/runtime/phases/RaphPhasePipeline.cjs +1 -1
  62. package/dist/debug/runtime/phases/RaphPhasePipeline.js +618 -357
  63. package/dist/debug/runtime/phases/RaphPhaseTaskRecord.cjs +1 -0
  64. package/dist/debug/runtime/phases/RaphPhaseTaskRecord.js +176 -0
  65. package/dist/debug/runtime/phases/RaphPhaseWatch.cjs +1 -0
  66. package/dist/debug/runtime/phases/RaphPhaseWatch.js +20 -0
  67. package/dist/debug/runtime/reactivity/PathHandle.cjs +1 -1
  68. package/dist/debug/runtime/reactivity/PathHandle.js +50 -60
  69. package/dist/debug/runtime/reactivity/RaphMetaSource.cjs +1 -1
  70. package/dist/debug/runtime/reactivity/RaphMetaSource.js +28 -32
  71. package/dist/debug/runtime/reactivity/RaphSelector.cjs +1 -0
  72. package/dist/debug/runtime/reactivity/RaphSelector.js +503 -0
  73. package/dist/debug/runtime/reactivity/RaphWorkGraph.cjs +1 -1
  74. package/dist/debug/runtime/reactivity/RaphWorkGraph.js +541 -379
  75. package/dist/debug/runtime/reactivity/RuntimeReactivity.cjs +1 -1
  76. package/dist/debug/runtime/reactivity/RuntimeReactivity.js +484 -90
  77. package/dist/debug/runtime/reactivity/RuntimeWatch.cjs +1 -1
  78. package/dist/debug/runtime/reactivity/RuntimeWatch.js +33 -44
  79. package/dist/debug/runtime/reactivity/disposal-sweep.utils.cjs +1 -0
  80. package/dist/debug/runtime/reactivity/disposal-sweep.utils.js +42 -0
  81. package/dist/entrypoints/runtime.d.ts +2 -1
  82. package/dist/machine-testing.cjs +1 -1
  83. package/dist/machine-testing.js +1 -1
  84. package/dist/machine.cjs +1 -1
  85. package/dist/machine.js +216 -231
  86. package/dist/path/RaphKernel.d.ts +24 -0
  87. package/dist/path/storage/PathEventHub.d.ts +14 -0
  88. package/dist/path/storage/PathStore.d.ts +4 -4
  89. package/dist/reactivity/disposal.d.ts +14 -0
  90. package/dist/reactivity/engine.d.ts +177 -4
  91. package/dist/reactivity/scope.d.ts +108 -30
  92. package/dist/runtime/RaphRuntime.d.ts +236 -17
  93. package/dist/runtime/ownership/RaphEffectScope.d.ts +70 -1
  94. package/dist/runtime/ownership/RaphNode.d.ts +276 -6
  95. package/dist/runtime/ownership/RaphScope.d.ts +144 -14
  96. package/dist/runtime/phases/RaphPhasePipeline.d.ts +202 -28
  97. package/dist/runtime/phases/RaphPhaseTaskRecord.d.ts +195 -0
  98. package/dist/runtime/phases/RaphPhaseWatch.d.ts +22 -0
  99. package/dist/runtime/phases/phase.types.d.ts +83 -4
  100. package/dist/runtime/reactivity/RaphSelector.d.ts +338 -0
  101. package/dist/runtime/reactivity/RaphWorkGraph.d.ts +163 -25
  102. package/dist/runtime/reactivity/RuntimeReactivity.d.ts +258 -10
  103. package/dist/runtime/reactivity/RuntimeWatch.d.ts +8 -8
  104. package/dist/runtime/reactivity/disposal-sweep.utils.d.ts +19 -0
  105. package/dist/runtime/reactivity/runtime-reactivity.types.d.ts +48 -3
  106. package/package.json +9 -1
  107. package/dist/core-ugchZNqC.cjs +0 -1
@@ -1,84 +1,80 @@
1
1
  const Z = /* @__PURE__ */ new WeakMap(), mt = 16384;
2
2
  function Jt(t) {
3
- var j;
4
- if ($t(t.id, t.version), typeof t.states != "object" || t.states === null || Array.isArray(t.states))
3
+ if (Tt(t.id, t.version), typeof t.states != "object" || t.states === null || Array.isArray(t.states))
5
4
  throw new TypeError("[Raph machine] states must be an object.");
6
- const e = t, n = [], r = /* @__PURE__ */ new Map(), i = [];
7
- for (const [h, b] of Object.entries(e.states))
8
- i.push(ct(h, b, void 0, 0, n, r));
9
- const o = r.get(t.initial);
10
- if (o === void 0 || ((j = n[o]) == null ? void 0 : j.parent) !== void 0)
5
+ const e = t, n = [], r = /* @__PURE__ */ new Map(), o = [];
6
+ for (const [f, w] of Object.entries(e.states))
7
+ o.push(ct(f, w, void 0, 0, n, r));
8
+ const i = r.get(t.initial);
9
+ if (i === void 0 || n[i]?.parent !== void 0)
11
10
  throw new Error(`[Raph machine] Initial state "${t.initial}" must be a root state key.`);
12
- const s = /* @__PURE__ */ new Set(), a = n.map((h) => {
13
- const b = h.initialKey === void 0 ? void 0 : h.children.find((z) => {
14
- var l;
15
- return ((l = n[z]) == null ? void 0 : l.key) === h.initialKey;
16
- });
17
- if (h.kind === "compound" && b === void 0)
18
- throw new Error(`[Raph machine] Compound state "${h.path}" requires a valid direct initial child.`);
19
- if (b !== void 0 && n[b].kind === "history")
20
- throw new Error(`[Raph machine] Compound state "${h.path}" cannot use history as its initial child.`);
21
- const E = h.historyFallbackPath === void 0 ? void 0 : r.get(h.historyFallbackPath);
22
- if (h.historyFallbackPath !== void 0 && E === void 0)
11
+ const s = /* @__PURE__ */ new Set(), a = n.map((f) => {
12
+ const w = f.initialKey === void 0 ? void 0 : f.children.find((g) => n[g]?.key === f.initialKey);
13
+ if (f.kind === "compound" && w === void 0)
14
+ throw new Error(`[Raph machine] Compound state "${f.path}" requires a valid direct initial child.`);
15
+ if (w !== void 0 && n[w].kind === "history")
16
+ throw new Error(`[Raph machine] Compound state "${f.path}" cannot use history as its initial child.`);
17
+ const b = f.historyFallbackPath === void 0 ? void 0 : r.get(f.historyFallbackPath);
18
+ if (f.historyFallbackPath !== void 0 && b === void 0)
23
19
  throw new Error(
24
- `[Raph machine] History state "${h.path}" has unknown fallback "${h.historyFallbackPath}".`
20
+ `[Raph machine] History state "${f.path}" has unknown fallback "${f.historyFallbackPath}".`
25
21
  );
26
- for (const z of h.on.keys())
27
- s.add(z);
22
+ for (const g of f.on.keys())
23
+ s.add(g);
28
24
  return {
29
- after: h.after,
30
- always: h.always,
31
- children: Object.freeze([...h.children]),
32
- depth: h.depth,
33
- entry: h.entry,
34
- exit: h.exit,
35
- history: h.history,
36
- ...E === void 0 ? {} : { historyFallback: E },
37
- id: h.id,
38
- ...b === void 0 ? {} : { initial: b },
39
- invokes: h.invokes,
40
- key: h.key,
41
- kind: h.kind,
42
- ...h.meta === void 0 ? {} : { meta: h.meta },
43
- on: h.on,
44
- onDone: h.onDone,
45
- ...h.output === void 0 ? {} : { output: h.output },
46
- ...h.parent === void 0 ? {} : { parent: h.parent },
47
- path: h.path,
48
- resources: h.resources,
49
- tags: h.tags
25
+ after: f.after,
26
+ always: f.always,
27
+ children: Object.freeze([...f.children]),
28
+ depth: f.depth,
29
+ entry: f.entry,
30
+ exit: f.exit,
31
+ history: f.history,
32
+ ...b === void 0 ? {} : { historyFallback: b },
33
+ id: f.id,
34
+ ...w === void 0 ? {} : { initial: w },
35
+ invokes: f.invokes,
36
+ key: f.key,
37
+ kind: f.kind,
38
+ ...f.meta === void 0 ? {} : { meta: f.meta },
39
+ on: f.on,
40
+ onDone: f.onDone,
41
+ ...f.output === void 0 ? {} : { output: f.output },
42
+ ...f.parent === void 0 ? {} : { parent: f.parent },
43
+ path: f.path,
44
+ resources: f.resources,
45
+ tags: f.tags
50
46
  };
51
47
  });
52
- for (const h of a)
53
- zt(h, r, a);
54
- const c = t.version ?? 1;
55
- kt(a);
56
- const u = Object.freeze([...s]), f = new Map(u.map((h, b) => [h, b])), d = Object.freeze(a.map((h) => Object.freeze(Et(a, h.id)))), p = xt(a), v = p && u.length <= Math.floor(
48
+ for (const f of a)
49
+ St(f, r, a);
50
+ const u = t.version ?? 1;
51
+ $t(a);
52
+ const h = Object.freeze([...s]), c = new Map(h.map((f, w) => [f, w])), d = Object.freeze(a.map((f) => Object.freeze(Et(a, f.id)))), l = gt(a), y = l && h.length <= Math.floor(
57
53
  mt / a.length
58
- ) ? jt(a, f, u.length) : void 0, w = Object.freeze({
54
+ ) ? jt(a, c, h.length) : void 0, v = Object.freeze({
59
55
  ancestry: d,
60
56
  context: It(e.context),
61
- eventIndexes: f,
62
- eventTypes: u,
63
- flat: p,
64
- ...v === void 0 ? {} : { flatDispatch: v },
57
+ eventIndexes: c,
58
+ eventTypes: h,
59
+ flat: l,
60
+ ...y === void 0 ? {} : { flatDispatch: y },
65
61
  id: t.id,
66
- initial: o,
67
- migrations: At(t.migrations, c),
62
+ initial: i,
63
+ migrations: At(t.migrations, u),
68
64
  ...e.output === void 0 ? {} : { output: e.output },
69
65
  pathToState: r,
70
- rootChildren: Object.freeze(i),
66
+ rootChildren: Object.freeze(o),
71
67
  states: Object.freeze(a),
72
- version: c
73
- }), m = Object.freeze({ id: t.id, version: c });
74
- return Z.set(m, w), m;
68
+ version: u
69
+ }), m = Object.freeze({ id: t.id, version: u });
70
+ return Z.set(m, v), m;
75
71
  }
76
72
  function jt(t, e, n) {
77
73
  const r = Array.from({ length: t.length * n }).fill(void 0);
78
- for (const i of t) {
79
- const o = i.id * n;
80
- for (const [s, a] of i.on)
81
- r[o + e.get(s)] = a;
74
+ for (const o of t) {
75
+ const i = o.id * n;
76
+ for (const [s, a] of o.on)
77
+ r[i + e.get(s)] = a;
82
78
  }
83
79
  return Object.freeze(r);
84
80
  }
@@ -88,7 +84,7 @@ function Et(t, e) {
88
84
  n.push(r);
89
85
  return n;
90
86
  }
91
- function xt(t) {
87
+ function gt(t) {
92
88
  return t.every((e) => {
93
89
  if (e.parent !== void 0 || e.kind !== "atomic" && e.kind !== "final" || e.always.length !== 0 || e.onDone.length !== 0 || e.after.size !== 0 || e.entry.length !== 0 || e.exit.length !== 0 || e.resources.length !== 0 || e.invokes.length !== 0)
94
90
  return !1;
@@ -104,14 +100,14 @@ function F(t) {
104
100
  throw new TypeError("[Raph machine] Model was not created by defineMachine().");
105
101
  return e;
106
102
  }
107
- function ct(t, e, n, r, i, o) {
103
+ function ct(t, e, n, r, o, i) {
108
104
  if (typeof e != "object" || e === null || Array.isArray(e))
109
105
  throw new TypeError(`[Raph machine] State "${t}" must be an object.`);
110
- Tt(e, t), Mt(t);
111
- const s = n === void 0 ? "" : i[n].path, a = s ? `${s}.${t}` : t, c = i.length;
112
- if (o.has(a))
106
+ kt(e, t), Mt(t);
107
+ const s = n === void 0 ? "" : o[n].path, a = s ? `${s}.${t}` : t, u = o.length;
108
+ if (i.has(a))
113
109
  throw new Error(`[Raph machine] Duplicate state path "${a}".`);
114
- const u = gt(e), f = {
110
+ const h = zt(e), c = {
115
111
  after: /* @__PURE__ */ new Map(),
116
112
  always: Object.freeze([]),
117
113
  children: [],
@@ -120,11 +116,11 @@ function ct(t, e, n, r, i, o) {
120
116
  exit: P(e.exit),
121
117
  history: e.history ?? "shallow",
122
118
  ...e.target === void 0 ? {} : { historyFallbackPath: e.target },
123
- id: c,
119
+ id: u,
124
120
  ...e.initial === void 0 ? {} : { initialKey: e.initial },
125
121
  invokes: Object.freeze([]),
126
122
  key: t,
127
- kind: u,
123
+ kind: h,
128
124
  ...e.meta === void 0 ? {} : { meta: Object.freeze({ ...e.meta }) },
129
125
  on: /* @__PURE__ */ new Map(),
130
126
  onDone: Object.freeze([]),
@@ -134,43 +130,43 @@ function ct(t, e, n, r, i, o) {
134
130
  resources: ot(e.resources),
135
131
  tags: ot(e.tags)
136
132
  };
137
- i.push(f), o.set(a, c);
138
- for (const [v, w] of Object.entries(e.states ?? {}))
139
- f.children.push(ct(v, w, c, r + 1, i, o));
133
+ o.push(c), i.set(a, u);
134
+ for (const [y, v] of Object.entries(e.states ?? {}))
135
+ c.children.push(ct(y, v, u, r + 1, o, i));
140
136
  let d = 0;
141
- for (const [v, w] of Object.entries(e.on ?? {})) {
142
- if (!v.trim())
137
+ for (const [y, v] of Object.entries(e.on ?? {})) {
138
+ if (!y.trim())
143
139
  throw new Error(`[Raph machine] State "${a}" has an empty event type.`);
144
- f.on.set(v, M(w, c, () => d++));
140
+ c.on.set(y, M(v, u, () => d++));
145
141
  }
146
- f.always = M(e.always, c, () => d++), f.onDone = M(e.onDone, c, () => d++);
147
- for (const [v, w] of Object.entries(e.after ?? {})) {
148
- if (!v.trim())
142
+ c.always = M(e.always, u, () => d++), c.onDone = M(e.onDone, u, () => d++);
143
+ for (const [y, v] of Object.entries(e.after ?? {})) {
144
+ if (!y.trim())
149
145
  throw new Error(`[Raph machine] State "${a}" has an empty delay key.`);
150
- f.after.set(v, M(w, c, () => d++));
146
+ c.after.set(y, M(v, u, () => d++));
151
147
  }
152
- const p = Pt(e.invoke);
153
- return f.invokes = Object.freeze(p.map((v, w) => St(
148
+ const l = Pt(e.invoke);
149
+ return c.invokes = Object.freeze(l.map((y, v) => xt(
150
+ y,
151
+ c,
154
152
  v,
155
- f,
156
- w,
157
153
  () => d++
158
- ))), Rt(f, i), c;
154
+ ))), Rt(c, o), u;
159
155
  }
160
- function St(t, e, n, r) {
156
+ function xt(t, e, n, r) {
161
157
  if (typeof t != "object" || t === null || Array.isArray(t))
162
158
  throw new TypeError(`[Raph machine] Invoke in "${e.path}" must be an object.`);
163
- const i = typeof t.src == "string" ? "task" : "machine", o = t.type ?? i, s = t.id ?? `${e.path}:invoke#${n + 1}`;
159
+ const o = typeof t.src == "string" ? "task" : "machine", i = t.type ?? o, s = t.id ?? `${e.path}:invoke#${n + 1}`;
164
160
  if (!s.trim())
165
161
  throw new Error(`[Raph machine] Invoke id in "${e.path}" must not be empty.`);
166
- if (o !== "machine" && o !== "task")
167
- throw new Error(`[Raph machine] Invoke "${s}" has unsupported type "${String(o)}".`);
162
+ if (i !== "machine" && i !== "task")
163
+ throw new Error(`[Raph machine] Invoke "${s}" has unsupported type "${String(i)}".`);
168
164
  if (typeof t.src == "string") {
169
165
  if (!t.src.trim())
170
166
  throw new Error(`[Raph machine] Invoke "${s}" source must not be empty.`);
171
167
  } else if (!Z.has(t.src))
172
168
  throw new TypeError(`[Raph machine] Invoke "${s}" actor source must come from defineMachine().`);
173
- if (o === "task" && typeof t.src != "string")
169
+ if (i === "task" && typeof t.src != "string")
174
170
  throw new TypeError(`[Raph machine] Task invoke "${s}" requires a string source.`);
175
171
  return Object.freeze({
176
172
  id: s,
@@ -178,54 +174,53 @@ function St(t, e, n, r) {
178
174
  onDone: M(t.onDone, e.id, r),
179
175
  onError: M(t.onError, e.id, r),
180
176
  src: t.src,
181
- type: o
177
+ type: i
182
178
  });
183
179
  }
184
180
  function M(t, e, n) {
185
181
  if (t === void 0)
186
182
  return Object.freeze([]);
187
183
  const r = Array.isArray(t) ? t : [t];
188
- return Object.freeze(r.map((i) => {
189
- const o = typeof i == "string" ? { target: i } : i;
190
- if (o === null || typeof o != "object")
184
+ return Object.freeze(r.map((o) => {
185
+ const i = typeof o == "string" ? { target: o } : o;
186
+ if (i === null || typeof i != "object")
191
187
  throw new TypeError("[Raph machine] Transition must be a target string or object.");
192
- if (o.reenter === !0 && o.target === void 0)
188
+ if (i.reenter === !0 && i.target === void 0)
193
189
  throw new Error("[Raph machine] A targetless transition cannot declare reenter: true.");
194
- if (o.reason !== void 0 && (typeof o.reason != "string" || !o.reason.trim()))
190
+ if (i.reason !== void 0 && (typeof i.reason != "string" || !i.reason.trim()))
195
191
  throw new Error("[Raph machine] Transition reason must be a non-empty string.");
196
- return W(o.guard === void 0 ? [] : [o.guard], "guard"), W(P(o.reduce), "reducer"), W(P(o.emit), "output"), {
197
- emit: P(o.emit),
198
- ...o.guard === void 0 ? {} : { guard: o.guard },
192
+ return W(i.guard === void 0 ? [] : [i.guard], "guard"), W(P(i.reduce), "reducer"), W(P(i.emit), "output"), {
193
+ emit: P(i.emit),
194
+ ...i.guard === void 0 ? {} : { guard: i.guard },
199
195
  index: n(),
200
- ...o.raise === void 0 ? {} : { raise: o.raise },
201
- ...o.reason === void 0 ? {} : { reason: o.reason },
202
- reduce: P(o.reduce),
203
- reenter: o.reenter === !0,
196
+ ...i.raise === void 0 ? {} : { raise: i.raise },
197
+ ...i.reason === void 0 ? {} : { reason: i.reason },
198
+ reduce: P(i.reduce),
199
+ reenter: i.reenter === !0,
204
200
  source: e,
205
- ...o.target === void 0 ? {} : { target: o.target }
201
+ ...i.target === void 0 ? {} : { target: i.target }
206
202
  };
207
203
  }));
208
204
  }
209
- function zt(t, e, n) {
210
- var i;
205
+ function St(t, e, n) {
211
206
  const r = [t.always, t.onDone, ...t.on.values(), ...t.after.values()];
212
207
  for (const o of t.invokes)
213
208
  r.push(o.onDone, o.onError);
214
209
  for (const o of r)
215
- for (const s of o) {
216
- if (s.target !== void 0) {
217
- const a = s.target, c = e.get(a);
218
- if (c === void 0)
210
+ for (const i of o) {
211
+ if (i.target !== void 0) {
212
+ const s = i.target, a = e.get(s);
213
+ if (a === void 0)
219
214
  throw new Error(
220
- `[Raph machine] Transition from "${t.path}" targets unknown state "${a}".`
215
+ `[Raph machine] Transition from "${t.path}" targets unknown state "${s}".`
221
216
  );
222
- s.target = c;
217
+ i.target = a;
223
218
  }
224
- Object.freeze(s);
219
+ Object.freeze(i);
225
220
  }
226
221
  if (t.kind === "history" && t.historyFallback !== void 0) {
227
- const o = (i = [...e].find(([, a]) => a === t.historyFallback)) == null ? void 0 : i[0], s = t.path.slice(0, t.path.lastIndexOf("."));
228
- if (o === void 0 || o === t.path || t.historyFallback === void 0 || !o.startsWith(`${s}.`))
222
+ const o = [...e].find(([, s]) => s === t.historyFallback)?.[0], i = t.path.slice(0, t.path.lastIndexOf("."));
223
+ if (o === void 0 || o === t.path || t.historyFallback === void 0 || !o.startsWith(`${i}.`))
229
224
  throw new Error(
230
225
  `[Raph machine] History fallback for "${t.path}" must stay inside its parent state.`
231
226
  );
@@ -233,7 +228,7 @@ function zt(t, e, n) {
233
228
  throw new Error(`[Raph machine] History fallback for "${t.path}" cannot target another history state.`);
234
229
  }
235
230
  }
236
- function gt(t) {
231
+ function zt(t) {
237
232
  if (t.type !== void 0) {
238
233
  if (t.type !== "atomic" && t.type !== "compound" && t.type !== "final" && t.type !== "history" && t.type !== "parallel")
239
234
  throw new TypeError(`[Raph machine] Unsupported state type "${String(t.type)}".`);
@@ -257,7 +252,7 @@ function Rt(t, e) {
257
252
  if ((t.kind === "final" || t.kind === "history") && (t.on.size !== 0 || t.always.length !== 0 || t.after.size !== 0 || t.onDone.length !== 0 || t.invokes.length !== 0))
258
253
  throw new Error(`[Raph machine] ${t.kind} state "${t.path}" cannot declare transitions or invokes.`);
259
254
  }
260
- function Tt(t, e) {
255
+ function kt(t, e) {
261
256
  if (V(t.states, `State "${e}" states`), V(t.on, `State "${e}" on`), V(t.after, `State "${e}" after`), t.history !== void 0 && t.history !== "deep" && t.history !== "shallow")
262
257
  throw new TypeError(`[Raph machine] State "${e}" has unsupported history mode.`);
263
258
  }
@@ -265,14 +260,14 @@ function V(t, e) {
265
260
  if (t !== void 0 && (typeof t != "object" || t === null || Array.isArray(t)))
266
261
  throw new TypeError(`[Raph machine] ${e} must be an object.`);
267
262
  }
268
- function $t(t, e) {
263
+ function Tt(t, e) {
269
264
  if (typeof t != "string" || !t.trim())
270
265
  throw new TypeError("[Raph machine] Model id must be a non-empty string.");
271
266
  const n = e ?? 1;
272
267
  if (!Number.isSafeInteger(n) || n < 1)
273
268
  throw new TypeError("[Raph machine] Model version must be a positive safe integer.");
274
269
  }
275
- function kt(t) {
270
+ function $t(t) {
276
271
  const e = /* @__PURE__ */ new Set();
277
272
  for (const n of t)
278
273
  for (const r of n.invokes) {
@@ -283,13 +278,13 @@ function kt(t) {
283
278
  }
284
279
  function At(t, e) {
285
280
  const n = /* @__PURE__ */ new Map();
286
- for (const [r, i] of Object.entries(t ?? {})) {
287
- const o = Number(r);
288
- if (!Number.isSafeInteger(o) || o < 1 || o >= e)
281
+ for (const [r, o] of Object.entries(t ?? {})) {
282
+ const i = Number(r);
283
+ if (!Number.isSafeInteger(i) || i < 1 || i >= e)
289
284
  throw new Error(`[Raph machine] Migration version "${r}" must be a positive integer below ${e}.`);
290
- if (typeof i != "function")
291
- throw new TypeError(`[Raph machine] Migration from version ${o} must be a function.`);
292
- n.set(o, i);
285
+ if (typeof o != "function")
286
+ throw new TypeError(`[Raph machine] Migration from version ${i} must be a function.`);
287
+ n.set(i, o);
293
288
  }
294
289
  return n;
295
290
  }
@@ -321,33 +316,33 @@ function It(t) {
321
316
  return Array.isArray(t) ? Object.freeze([...t]) : typeof t == "object" && t !== null && Object.getPrototypeOf(t) === Object.prototype ? Object.freeze({ ...t }) : t;
322
317
  }
323
318
  const T = Symbol("RaphMachineSnapshot"), it = /* @__PURE__ */ new WeakMap();
324
- function ft(t) {
319
+ function ut(t) {
325
320
  const e = Object.getPrototypeOf(t);
326
321
  return e !== null && Object.hasOwn(e, T) ? e : void 0;
327
322
  }
328
- function ut(t) {
323
+ function ft(t) {
329
324
  return t[T];
330
325
  }
331
326
  function L(t, e, n) {
332
- const r = n ?? Object.freeze({ [T]: e }), i = Object.create(r);
333
- return i.active = t.active, i.context = t.context, i.history = t.history, t.output !== void 0 && (i.output = t.output), i.revision = t.revision, i.status = t.status, Object.freeze(i);
327
+ const r = n ?? Object.freeze({ [T]: e }), o = Object.create(r);
328
+ return o.active = t.active, o.context = t.context, o.history = t.history, t.output !== void 0 && (o.output = t.output), o.revision = t.revision, o.status = t.status, Object.freeze(o);
334
329
  }
335
- function J(t, e, n, r, i) {
336
- let o = t == null ? void 0 : t.flatPrototypes;
337
- o === void 0 && (n === void 0 ? (o = it.get(r), o === void 0 && (o = [], it.set(r, o))) : o = []);
338
- const s = o[e];
330
+ function J(t, e, n, r, o) {
331
+ let i = t?.flatPrototypes;
332
+ i === void 0 && (n === void 0 ? (i = it.get(r), i === void 0 && (i = [], it.set(r, i))) : i = []);
333
+ const s = i[e];
339
334
  if (s !== void 0)
340
335
  return s;
341
336
  const a = Object.freeze({
342
337
  [T]: Object.freeze({
343
338
  active: Object.freeze([e]),
344
- flatPrototypes: o,
339
+ flatPrototypes: i,
345
340
  input: n,
346
341
  model: r,
347
- publicActive: Object.freeze([i])
342
+ publicActive: Object.freeze([o])
348
343
  })
349
344
  });
350
- return o[e] = a, a;
345
+ return i[e] = a, a;
351
346
  }
352
347
  const Dt = 100, G = Object.freeze({ type: "@@init" }), ht = Object.freeze([]), q = Object.freeze({}), K = Object.freeze([]), D = Object.freeze([]), Ft = Object.freeze([]), Q = Object.freeze({
353
348
  entered: K,
@@ -360,9 +355,9 @@ const Dt = 100, G = Object.freeze({ type: "@@init" }), ht = Object.freeze([]), q
360
355
  function Qt(t, e) {
361
356
  return _t(t, e, void 0).snapshot;
362
357
  }
363
- function te(t, e, n, r, i) {
364
- const o = ut(e);
365
- if (o !== void 0 && o.model !== t)
358
+ function te(t, e, n, r, o) {
359
+ const i = ft(e);
360
+ if (i !== void 0 && i.model !== t)
366
361
  throw new TypeError("[Raph machine] Snapshot belongs to another model.");
367
362
  const s = F(t);
368
363
  return tt(
@@ -370,14 +365,13 @@ function te(t, e, n, r, i) {
370
365
  e,
371
366
  n,
372
367
  r,
373
- o == null ? void 0 : o.input,
374
- i,
368
+ i?.input,
369
+ o,
375
370
  s
376
371
  ).snapshot;
377
372
  }
378
373
  function Ct(t, e) {
379
- var n;
380
- if (((n = ft(e)) == null ? void 0 : n[T].model) !== t)
374
+ if (ut(e)?.[T].model !== t)
381
375
  throw new TypeError("[Raph machine] Snapshot belongs to another model or was not created by Raph.");
382
376
  }
383
377
  function ee(t, e) {
@@ -386,7 +380,7 @@ function ee(t, e) {
386
380
  e
387
381
  ), e.status === "stopped")
388
382
  return e;
389
- const n = ft(e);
383
+ const n = ut(e);
390
384
  return L({
391
385
  active: e.active,
392
386
  context: e.context,
@@ -397,139 +391,139 @@ function ee(t, e) {
397
391
  }, n[T], n);
398
392
  }
399
393
  function _t(t, e, n, r) {
400
- const i = F(t), o = i.context, s = typeof o == "function" ? o(e) : o;
401
- if (i.flat) {
402
- const u = i.states[i.initial], f = u.kind === "final" ? "done" : "active", d = f === "done" ? u.output ?? i.output : void 0, p = typeof d == "function" ? d({ context: s, event: G, input: e }) : d, v = J(
394
+ const o = F(t), i = o.context, s = typeof i == "function" ? i(e) : i;
395
+ if (o.flat) {
396
+ const h = o.states[o.initial], c = h.kind === "final" ? "done" : "active", d = c === "done" ? h.output ?? o.output : void 0, l = typeof d == "function" ? d({ context: s, event: G, input: e }) : d, y = J(
403
397
  void 0,
404
- i.initial,
398
+ o.initial,
405
399
  e,
406
400
  t,
407
- u.path
408
- ), w = v[T], m = L({
409
- active: w.publicActive,
401
+ h.path
402
+ ), v = y[T], m = L({
403
+ active: v.publicActive,
410
404
  context: rt(s),
411
405
  history: q,
412
- ...p === void 0 ? {} : { output: p },
406
+ ...l === void 0 ? {} : { output: l },
413
407
  revision: 0,
414
- status: f
415
- }, w, v);
408
+ status: c
409
+ }, v, y);
416
410
  return { changed: !1, plan: Q, snapshot: m };
417
411
  }
418
- const a = $(i, i.initial, q), c = C(t, e, {
412
+ const a = $(o, o.initial, q), u = C(t, e, {
419
413
  active: a,
420
414
  context: s,
421
415
  history: q
422
- }, 0, "active", void 0, i);
423
- return tt(t, c, G, n, e, r, i, !0);
416
+ }, 0, "active", void 0, o);
417
+ return tt(t, u, G, n, e, r, o, !0);
424
418
  }
425
- function tt(t, e, n, r, i, o, s, a = !1, c = ht) {
419
+ function tt(t, e, n, r, o, i, s, a = !1, u = ht) {
426
420
  return Ot(n), Nt(
427
421
  t,
428
422
  e,
429
423
  n,
430
424
  r,
431
- i,
432
425
  o,
426
+ i,
433
427
  s,
434
428
  void 0,
435
429
  a,
436
- c
430
+ u
437
431
  );
438
432
  }
439
- function Nt(t, e, n, r, i, o, s, a, c = !1, u = ht) {
440
- const f = s ?? F(t);
433
+ function Nt(t, e, n, r, o, i, s, a, u = !1, h = ht) {
434
+ const c = s ?? F(t);
441
435
  if (e.status !== "active")
442
436
  return I(e);
443
- if (!c && f.flat) {
444
- const y = a ?? f.eventIndexes.get(n.type);
445
- return y === void 0 ? I(e) : Ht(t, f, e, n, y, r, i, o);
437
+ if (!u && c.flat) {
438
+ const E = a ?? c.eventIndexes.get(n.type);
439
+ return E === void 0 ? I(e) : Ht(t, c, e, n, E, r, o, i);
446
440
  }
447
- if (!c && !Gt(f, e.active, n.type))
441
+ if (!u && !Gt(c, e.active, n.type))
448
442
  return I(e);
449
443
  const d = {
450
- active: e.active.map((y) => N(f, y)),
444
+ active: e.active.map((E) => N(c, E)),
451
445
  context: e.context,
452
446
  history: { ...e.history }
453
- }, p = r, v = /* @__PURE__ */ new Set(), w = /* @__PURE__ */ new Set(), m = [], j = [], h = [], b = [...u];
454
- let E = !1;
455
- if (!c) {
456
- const y = st(f, d, n, p, i, o);
457
- at(y, v, w, m, j, h), E = y.changed, b.push(...y.raised);
447
+ }, l = r, y = /* @__PURE__ */ new Set(), v = /* @__PURE__ */ new Set(), m = [], f = [], w = [], b = [...h];
448
+ let g = !1;
449
+ if (!u) {
450
+ const E = st(c, d, n, l, o, i);
451
+ at(E, y, v, m, f, w), g = E.changed, b.push(...E.raised);
458
452
  }
459
- const z = /* @__PURE__ */ new Set();
460
- let l = 0;
453
+ const p = /* @__PURE__ */ new Set();
454
+ let O = 0;
461
455
  for (; ; ) {
462
- const y = b.shift();
456
+ const E = b.shift();
463
457
  let x;
464
- if (y !== void 0 ? x = st(f, d, y, p, i, o) : (x = Kt(f, d, n, p, i, o), x.changed || (x = Lt(
465
- f,
458
+ if (E !== void 0 ? x = st(c, d, E, l, o, i) : (x = Kt(c, d, n, l, o, i), x.changed || (x = Lt(
459
+ c,
466
460
  d,
467
461
  n,
462
+ l,
463
+ o,
468
464
  p,
469
- i,
470
- z,
471
- o
465
+ i
472
466
  ))), !x.changed) {
473
467
  if (b.length !== 0)
474
468
  continue;
475
469
  break;
476
470
  }
477
- if (++l > Dt)
471
+ if (++O > Dt)
478
472
  throw new Error("[Raph machine] Macrostep exceeded 100 stabilization microsteps.");
479
473
  for (const U of x.exited)
480
- z.delete(U);
474
+ p.delete(U);
481
475
  for (const U of x.entered)
482
- z.delete(U);
483
- E = !0, at(x, v, w, m, j, h), b.push(...x.raised);
476
+ p.delete(U);
477
+ g = !0, at(x, y, v, m, f, w), b.push(...x.raised);
484
478
  }
485
- const O = nt(f, d.active) ? "done" : "active";
486
- if (!E && O === e.status)
479
+ const R = nt(c, d.active) ? "done" : "active";
480
+ if (!g && R === e.status)
487
481
  return I(e);
488
- const k = O === "done" ? yt(f, d, n, i) : void 0, g = C(
482
+ const z = R === "done" ? yt(c, d, n, o) : void 0, k = C(
489
483
  t,
490
- i,
484
+ o,
491
485
  d,
492
486
  e.revision + 1,
493
- O,
494
- k,
495
- f
496
- ), R = new Set(_(f, d.active));
487
+ R,
488
+ z,
489
+ c
490
+ ), j = new Set(_(c, d.active));
497
491
  return Object.freeze({
498
492
  changed: !0,
499
493
  plan: Object.freeze({
500
- entered: Object.freeze([...w].filter((y) => R.has(y)).sort((y, x) => f.states[y].depth - f.states[x].depth)),
501
- entryOutputs: Object.freeze(h),
494
+ entered: Object.freeze([...v].filter((E) => j.has(E)).sort((E, x) => c.states[E].depth - c.states[x].depth)),
495
+ entryOutputs: Object.freeze(w),
502
496
  event: n,
503
- exited: Object.freeze([...v].sort((y, x) => f.states[x].depth - f.states[y].depth)),
497
+ exited: Object.freeze([...y].sort((E, x) => c.states[x].depth - c.states[E].depth)),
504
498
  exitOutputs: Object.freeze(m),
505
- transitionOutputs: Object.freeze(j)
499
+ transitionOutputs: Object.freeze(f)
506
500
  }),
507
- snapshot: g
501
+ snapshot: k
508
502
  });
509
503
  }
510
- function Ht(t, e, n, r, i, o, s, a) {
511
- const c = ut(n), u = (c == null ? void 0 : c.active[0]) ?? N(e, n.active[0]), f = e.flatDispatch === void 0 ? e.states[u].on.get(r.type) : e.flatDispatch[u * e.eventTypes.length + i], d = H(
512
- f ?? Ft,
504
+ function Ht(t, e, n, r, o, i, s, a) {
505
+ const u = ft(n), h = u?.active[0] ?? N(e, n.active[0]), c = e.flatDispatch === void 0 ? e.states[h].on.get(r.type) : e.flatDispatch[h * e.eventTypes.length + o], d = H(
506
+ c ?? Ft,
513
507
  n.context,
514
508
  r,
515
- o,
509
+ i,
516
510
  s,
517
511
  a
518
512
  );
519
513
  if (d === void 0)
520
514
  return I(n);
521
515
  dt(e, d, a);
522
- let p = n.context;
523
- for (const z of d.reduce)
524
- p = vt(
525
- z,
526
- o
527
- )({ context: p, event: r, input: s });
528
- const v = d.target ?? u, w = e.states[v], m = w.kind === "final" ? "done" : "active", j = m === "done" ? w.output ?? e.output : void 0, h = typeof j == "function" ? j({ context: p, event: r, input: s }) : j, b = J(c, v, s, t, w.path), E = L({
529
- active: v === u ? n.active : b[T].publicActive,
530
- context: p === n.context ? n.context : rt(p),
516
+ let l = n.context;
517
+ for (const p of d.reduce)
518
+ l = vt(
519
+ p,
520
+ i
521
+ )({ context: l, event: r, input: s });
522
+ const y = d.target ?? h, v = e.states[y], m = v.kind === "final" ? "done" : "active", f = m === "done" ? v.output ?? e.output : void 0, w = typeof f == "function" ? f({ context: l, event: r, input: s }) : f, b = J(u, y, s, t, v.path), g = L({
523
+ active: y === h ? n.active : b[T].publicActive,
524
+ context: l === n.context ? n.context : rt(l),
531
525
  history: n.history,
532
- ...h === void 0 ? {} : { output: h },
526
+ ...w === void 0 ? {} : { output: w },
533
527
  revision: n.revision + 1,
534
528
  status: m
535
529
  }, b[T], b);
@@ -544,77 +538,77 @@ function Ht(t, e, n, r, i, o, s, a) {
544
538
  transitionOutputRefs: d.emit,
545
539
  transitionOutputs: D
546
540
  },
547
- snapshot: E
541
+ snapshot: g
548
542
  };
549
543
  }
550
- function ne(t, e, n, r, i, o, s) {
551
- const a = F(t), c = H(
544
+ function ne(t, e, n, r, o, i, s) {
545
+ const a = F(t), u = H(
552
546
  r,
553
547
  e.context,
554
548
  n,
555
- i,
556
549
  o,
550
+ i,
557
551
  s
558
552
  );
559
- if (c === void 0)
553
+ if (u === void 0)
560
554
  return I(e);
561
- const u = {
555
+ const h = {
562
556
  active: e.active.map((b) => N(a, b)),
563
557
  context: e.context,
564
558
  history: { ...e.history }
565
- }, f = Y(
559
+ }, c = Y(
566
560
  a,
567
- u,
568
- [c],
561
+ h,
562
+ [u],
569
563
  n,
570
- i,
571
564
  o,
565
+ i,
572
566
  s
573
567
  ), d = C(
574
568
  t,
575
- o,
576
- u,
569
+ i,
570
+ h,
577
571
  e.revision,
578
572
  "active",
579
573
  void 0,
580
574
  a
581
- ), p = tt(
575
+ ), l = tt(
582
576
  t,
583
577
  d,
584
578
  n,
585
- i,
586
579
  o,
580
+ i,
587
581
  s,
588
582
  a,
589
583
  !0,
590
- f.raised
591
- ), v = p.changed ? {
592
- active: p.snapshot.active.map((b) => N(a, b)),
593
- context: p.snapshot.context,
594
- history: { ...p.snapshot.history }
595
- } : u, w = p.changed ? p.snapshot.status : nt(a, v.active) ? "done" : "active", m = w === "done" ? p.snapshot.output ?? yt(a, v, n, o) : void 0, j = C(
584
+ c.raised
585
+ ), y = l.changed ? {
586
+ active: l.snapshot.active.map((b) => N(a, b)),
587
+ context: l.snapshot.context,
588
+ history: { ...l.snapshot.history }
589
+ } : h, v = l.changed ? l.snapshot.status : nt(a, y.active) ? "done" : "active", m = v === "done" ? l.snapshot.output ?? yt(a, y, n, i) : void 0, f = C(
596
590
  t,
597
- o,
598
- v,
591
+ i,
592
+ y,
599
593
  e.revision + 1,
600
- w,
594
+ v,
601
595
  m,
602
596
  a
603
- ), h = new Set(_(a, v.active));
597
+ ), w = new Set(_(a, y.active));
604
598
  return Object.freeze({
605
599
  changed: !0,
606
600
  plan: Object.freeze({
607
- entered: Object.freeze(A([...f.entered, ...p.plan.entered]).filter((b) => h.has(b)).sort((b, E) => a.states[b].depth - a.states[E].depth)),
608
- entryOutputs: Object.freeze([...f.entryOutputs, ...p.plan.entryOutputs]),
601
+ entered: Object.freeze(A([...c.entered, ...l.plan.entered]).filter((b) => w.has(b)).sort((b, g) => a.states[b].depth - a.states[g].depth)),
602
+ entryOutputs: Object.freeze([...c.entryOutputs, ...l.plan.entryOutputs]),
609
603
  event: n,
610
- exited: Object.freeze(A([...f.exited, ...p.plan.exited]).sort((b, E) => a.states[E].depth - a.states[b].depth)),
611
- exitOutputs: Object.freeze([...f.exitOutputs, ...p.plan.exitOutputs]),
604
+ exited: Object.freeze(A([...c.exited, ...l.plan.exited]).sort((b, g) => a.states[g].depth - a.states[b].depth)),
605
+ exitOutputs: Object.freeze([...c.exitOutputs, ...l.plan.exitOutputs]),
612
606
  transitionOutputs: Object.freeze([
613
- ...f.transitionOutputs,
614
- ...p.plan.transitionOutputs
607
+ ...c.transitionOutputs,
608
+ ...l.plan.transitionOutputs
615
609
  ])
616
610
  }),
617
- snapshot: j
611
+ snapshot: f
618
612
  });
619
613
  }
620
614
  function re(t, e, n) {
@@ -623,12 +617,12 @@ function re(t, e, n) {
623
617
  throw new Error(`[Raph machine] Unknown persisted status "${String(e.status)}".`);
624
618
  if (!Number.isSafeInteger(e.revision) || e.revision < 0)
625
619
  throw new Error("[Raph machine] Persisted revision must be a non-negative safe integer.");
626
- const i = e.active.map((o) => N(r, o));
627
- return qt(r, i, e.history, e.status), C(
620
+ const o = e.active.map((i) => N(r, i));
621
+ return qt(r, o, e.history, e.status), C(
628
622
  t,
629
623
  n,
630
624
  {
631
- active: A(i),
625
+ active: A(o),
632
626
  context: e.context,
633
627
  history: { ...e.history }
634
628
  },
@@ -651,45 +645,45 @@ function qt(t, e, n, r) {
651
645
  for (const s of t.states) {
652
646
  if (s.kind !== "compound" && s.kind !== "parallel")
653
647
  continue;
654
- const a = s.children.filter((u) => t.states[u].kind !== "history" && e.some((f) => f === u || S(t, u, f)));
655
- if (e.some((u) => S(t, s.id, u))) {
648
+ const a = s.children.filter((h) => t.states[h].kind !== "history" && e.some((c) => c === h || S(t, h, c)));
649
+ if (e.some((h) => S(t, s.id, h))) {
656
650
  if (s.kind === "compound" && a.length !== 1)
657
651
  throw new Error(`[Raph machine] Compound state "${s.path}" requires one active child.`);
658
- if (s.kind === "parallel" && a.length !== s.children.filter((u) => t.states[u].kind !== "history").length)
652
+ if (s.kind === "parallel" && a.length !== s.children.filter((h) => t.states[h].kind !== "history").length)
659
653
  throw new Error(`[Raph machine] Parallel state "${s.path}" requires every region.`);
660
654
  }
661
655
  }
662
- const o = nt(t, e);
663
- if (r === "done" && !o)
656
+ const i = nt(t, e);
657
+ if (r === "done" && !i)
664
658
  throw new Error("[Raph machine] Done persisted snapshot is not in a final configuration.");
665
- if (r === "active" && o)
659
+ if (r === "active" && i)
666
660
  throw new Error("[Raph machine] Active persisted snapshot is already final.");
667
661
  for (const [s, a] of Object.entries(n)) {
668
- const c = t.pathToState.get(s);
669
- if (c === void 0)
662
+ const u = t.pathToState.get(s);
663
+ if (u === void 0)
670
664
  throw new Error(`[Raph machine] Persisted history has unknown owner "${s}".`);
671
- const u = t.states[c];
672
- if (u.kind !== "compound" && u.kind !== "parallel")
665
+ const h = t.states[u];
666
+ if (h.kind !== "compound" && h.kind !== "parallel")
673
667
  throw new Error(`[Raph machine] Persisted history owner "${s}" cannot own history.`);
674
668
  if (!Array.isArray(a))
675
669
  throw new TypeError(`[Raph machine] Persisted history for "${s}" must be an array.`);
676
- for (const f of a) {
677
- const d = t.pathToState.get(f);
678
- if (d === void 0 || !S(t, c, d))
679
- throw new Error(`[Raph machine] Persisted history path "${f}" is invalid for "${s}".`);
670
+ for (const c of a) {
671
+ const d = t.pathToState.get(c);
672
+ if (d === void 0 || !S(t, u, d))
673
+ throw new Error(`[Raph machine] Persisted history path "${c}" is invalid for "${s}".`);
680
674
  }
681
675
  }
682
676
  }
683
- function st(t, e, n, r, i, o) {
684
- const s = [], a = /* @__PURE__ */ new Set(), c = [...e.active].sort((u, f) => t.states[f].depth - t.states[u].depth || u - f);
685
- for (const u of c)
686
- for (let f = u; f !== void 0 && !a.has(f); f = t.states[f].parent) {
687
- const d = t.states[f].on.get(n.type);
677
+ function st(t, e, n, r, o, i) {
678
+ const s = [], a = /* @__PURE__ */ new Set(), u = [...e.active].sort((h, c) => t.states[c].depth - t.states[h].depth || h - c);
679
+ for (const h of u)
680
+ for (let c = h; c !== void 0 && !a.has(c); c = t.states[c].parent) {
681
+ const d = t.states[c].on.get(n.type);
688
682
  if (d === void 0)
689
683
  continue;
690
- const p = H(d, e.context, n, r, i, o);
691
- if (p !== void 0) {
692
- a.add(f), s.push(p);
684
+ const l = H(d, e.context, n, r, o, i);
685
+ if (l !== void 0) {
686
+ a.add(c), s.push(l);
693
687
  break;
694
688
  }
695
689
  }
@@ -699,26 +693,26 @@ function st(t, e, n, r, i, o) {
699
693
  pt(t, s),
700
694
  n,
701
695
  r,
702
- i,
703
- o
696
+ o,
697
+ i
704
698
  );
705
699
  }
706
- function Kt(t, e, n, r, i, o) {
700
+ function Kt(t, e, n, r, o, i) {
707
701
  const s = [], a = /* @__PURE__ */ new Set();
708
- for (const c of e.active)
709
- for (let u = c; u !== void 0; u = t.states[u].parent) {
710
- if (a.has(u))
702
+ for (const u of e.active)
703
+ for (let h = u; h !== void 0; h = t.states[h].parent) {
704
+ if (a.has(h))
711
705
  continue;
712
- a.add(u);
713
- const f = H(
714
- t.states[u].always,
706
+ a.add(h);
707
+ const c = H(
708
+ t.states[h].always,
715
709
  e.context,
716
710
  n,
717
711
  r,
718
- i,
719
- o
712
+ o,
713
+ i
720
714
  );
721
- f !== void 0 && s.push(f);
715
+ c !== void 0 && s.push(c);
722
716
  }
723
717
  return Y(
724
718
  t,
@@ -726,65 +720,64 @@ function Kt(t, e, n, r, i, o) {
726
720
  pt(t, s),
727
721
  n,
728
722
  r,
729
- i,
730
- o
723
+ o,
724
+ i
731
725
  );
732
726
  }
733
- function Lt(t, e, n, r, i, o, s) {
734
- const a = _(t, e.active).filter((c) => !o.has(c) && et(t, c, e.active)).sort((c, u) => t.states[u].depth - t.states[c].depth);
735
- for (const c of a) {
736
- o.add(c);
737
- const u = H(
738
- t.states[c].onDone,
727
+ function Lt(t, e, n, r, o, i, s) {
728
+ const a = _(t, e.active).filter((u) => !i.has(u) && et(t, u, e.active)).sort((u, h) => t.states[h].depth - t.states[u].depth);
729
+ for (const u of a) {
730
+ i.add(u);
731
+ const h = H(
732
+ t.states[u].onDone,
739
733
  e.context,
740
734
  n,
741
735
  r,
742
- i,
736
+ o,
743
737
  s
744
738
  );
745
- if (u !== void 0)
746
- return Y(t, e, [u], n, r, i, s);
739
+ if (h !== void 0)
740
+ return Y(t, e, [h], n, r, o, s);
747
741
  }
748
- return wt();
742
+ return bt();
749
743
  }
750
- function Y(t, e, n, r, i, o, s) {
751
- var z;
744
+ function Y(t, e, n, r, o, i, s) {
752
745
  if (n.length === 0)
753
- return wt();
754
- const a = [...e.active], c = new Set(_(t, a)), u = /* @__PURE__ */ new Set(), f = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Set(), p = [], v = [];
755
- let w = !1;
756
- for (const l of n) {
757
- dt(t, l, s);
758
- for (const y of l.reduce)
759
- e.context = vt(y, i)({
746
+ return bt();
747
+ const a = [...e.active], u = new Set(_(t, a)), h = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Set(), l = [], y = [];
748
+ let v = !1;
749
+ for (const p of n) {
750
+ dt(t, p, s);
751
+ for (const j of p.reduce)
752
+ e.context = vt(j, o)({
760
753
  context: e.context,
761
754
  event: r,
762
- input: o
763
- }), w = !0;
764
- for (const y of l.emit)
765
- p.push({ event: r, ref: y });
766
- if (l.emit.length !== 0 && (w = !0), l.raise !== void 0) {
767
- const y = typeof l.raise == "function" ? l.raise({ context: e.context, event: r, input: o }) : l.raise;
768
- Ot(y), v.push(y), w = !0;
755
+ input: i
756
+ }), v = !0;
757
+ for (const j of p.emit)
758
+ l.push({ event: r, ref: j });
759
+ if (p.emit.length !== 0 && (v = !0), p.raise !== void 0) {
760
+ const j = typeof p.raise == "function" ? p.raise({ context: e.context, event: r, input: i }) : p.raise;
761
+ Ot(j), y.push(j), v = !0;
769
762
  }
770
- if (l.target === void 0) {
771
- w = !0;
763
+ if (p.target === void 0) {
764
+ v = !0;
772
765
  continue;
773
766
  }
774
- const O = l.source, k = l.target;
775
- if (O === k && !l.reenter) {
776
- w = !0;
767
+ const O = p.source, R = p.target;
768
+ if (O === R && !p.reenter) {
769
+ v = !0;
777
770
  continue;
778
771
  }
779
- const g = X(t, O, k);
780
- if (l.reenter) {
781
- d.add(g);
782
- for (const y of c)
783
- (y === g || S(t, g, y)) && u.add(y);
772
+ const z = X(t, O, R);
773
+ if (p.reenter) {
774
+ d.add(z);
775
+ for (const j of u)
776
+ (j === z || S(t, z, j)) && h.add(j);
784
777
  }
785
- e.active = e.active.filter((y) => y !== g && !S(t, g, y));
786
- const R = lt(t, k, e.history);
787
- e.active.push(...R), w = !0;
778
+ e.active = e.active.filter((j) => j !== z && !S(t, z, j));
779
+ const k = lt(t, R, e.history);
780
+ e.active.push(...k), v = !0;
788
781
  }
789
782
  e.active = Bt(
790
783
  t,
@@ -792,98 +785,96 @@ function Y(t, e, n, r, i, o, s) {
792
785
  e.history
793
786
  );
794
787
  const m = new Set(_(t, e.active));
795
- for (const l of d)
788
+ for (const p of d)
796
789
  for (const O of m)
797
- (O === l || S(t, l, O)) && f.add(O);
798
- const j = [...c].filter((l) => !m.has(l) || u.has(l)).sort((l, O) => t.states[O].depth - t.states[l].depth), h = [...m].filter((l) => !c.has(l) || f.has(l)).sort((l, O) => t.states[l].depth - t.states[O].depth);
799
- for (const l of j) {
800
- const O = t.states[l];
790
+ (O === p || S(t, p, O)) && c.add(O);
791
+ const f = [...u].filter((p) => !m.has(p) || h.has(p)).sort((p, O) => t.states[O].depth - t.states[p].depth), w = [...m].filter((p) => !u.has(p) || c.has(p)).sort((p, O) => t.states[p].depth - t.states[O].depth);
792
+ for (const p of f) {
793
+ const O = t.states[p];
801
794
  if (O.kind === "compound" || O.kind === "parallel") {
802
- const k = a.filter((g) => S(t, l, g));
803
- if (k.length !== 0) {
804
- const g = ((z = O.children.map((R) => t.states[R]).find((R) => R.kind === "history")) == null ? void 0 : z.history) ?? O.history;
805
- e.history[O.path] = Object.freeze(k.map((R) => {
806
- if (g === "deep")
807
- return t.states[R].path;
808
- const y = bt(t, l, R);
809
- return t.states[y].path;
795
+ const R = a.filter((z) => S(t, p, z));
796
+ if (R.length !== 0) {
797
+ const z = O.children.map((k) => t.states[k]).find((k) => k.kind === "history")?.history ?? O.history;
798
+ e.history[O.path] = Object.freeze(R.map((k) => {
799
+ if (z === "deep")
800
+ return t.states[k].path;
801
+ const j = wt(t, p, k);
802
+ return t.states[j].path;
810
803
  }));
811
804
  }
812
805
  }
813
806
  }
814
- const b = j.flatMap((l) => t.states[l].exit.map((O) => ({ event: r, ref: O }))), E = h.flatMap((l) => t.states[l].entry.map((O) => ({ event: r, ref: O })));
807
+ const b = f.flatMap((p) => t.states[p].exit.map((O) => ({ event: r, ref: O }))), g = w.flatMap((p) => t.states[p].entry.map((O) => ({ event: r, ref: O })));
815
808
  return Object.freeze({
816
- changed: w || j.length !== 0 || h.length !== 0,
817
- entered: Object.freeze(h),
818
- entryOutputs: Object.freeze(E),
819
- exited: Object.freeze(j),
809
+ changed: v || f.length !== 0 || w.length !== 0,
810
+ entered: Object.freeze(w),
811
+ entryOutputs: Object.freeze(g),
812
+ exited: Object.freeze(f),
820
813
  exitOutputs: Object.freeze(b),
821
- raised: Object.freeze(v),
822
- transitionOutputs: Object.freeze(p)
814
+ raised: Object.freeze(y),
815
+ transitionOutputs: Object.freeze(l)
823
816
  });
824
817
  }
825
818
  function dt(t, e, n) {
826
- (n == null ? void 0 : n.transition) !== void 0 && n.transition({
819
+ n?.transition !== void 0 && n.transition({
827
820
  ...e.reason === void 0 ? {} : { reason: e.reason },
828
821
  source: t.states[e.source].path,
829
822
  ...e.target === void 0 ? {} : { target: t.states[e.target].path }
830
823
  });
831
824
  }
832
- function H(t, e, n, r, i, o) {
833
- var s;
834
- for (const a of t) {
835
- if (a.guard === void 0)
836
- return a;
837
- const c = Ut(a.guard, r), u = c({ context: e, event: n, input: i });
838
- if ((s = o == null ? void 0 : o.guard) == null || s.call(
839
- o,
840
- typeof a.guard == "string" ? a.guard : c.name || "inline",
825
+ function H(t, e, n, r, o, i) {
826
+ for (const s of t) {
827
+ if (s.guard === void 0)
828
+ return s;
829
+ const a = Ut(s.guard, r), u = a({ context: e, event: n, input: o });
830
+ if (i?.guard?.(
831
+ typeof s.guard == "string" ? s.guard : a.name || "inline",
841
832
  u,
842
833
  Zt(e) ? e : void 0
843
834
  ), u)
844
- return a;
835
+ return s;
845
836
  }
846
837
  }
847
838
  function pt(t, e) {
848
- const n = [...e].sort((i, o) => t.states[o.source].depth - t.states[i.source].depth || i.source - o.source || i.index - o.index), r = [];
849
- for (const i of n)
850
- r.some((s) => Yt(t, i, s)) || r.push(i);
839
+ const n = [...e].sort((o, i) => t.states[i.source].depth - t.states[o.source].depth || o.source - i.source || o.index - i.index), r = [];
840
+ for (const o of n)
841
+ r.some((s) => Yt(t, o, s)) || r.push(o);
851
842
  return r;
852
843
  }
853
844
  function Yt(t, e, n) {
854
- const r = X(t, e.source, e.target ?? e.source), i = X(t, n.source, n.target ?? n.source);
855
- return e.source === n.source || S(t, e.source, n.source) || S(t, n.source, e.source) || r === i || S(t, r, i) || S(t, i, r);
845
+ const r = X(t, e.source, e.target ?? e.source), o = X(t, n.source, n.target ?? n.source);
846
+ return e.source === n.source || S(t, e.source, n.source) || S(t, n.source, e.source) || r === o || S(t, r, o) || S(t, o, r);
856
847
  }
857
848
  function X(t, e, n) {
858
849
  if (e === n || S(t, e, n))
859
850
  return e;
860
851
  const r = Vt(t, e, n);
861
- return r !== void 0 && t.states[r].kind === "parallel" ? r : r === void 0 ? B(t, e) : bt(t, r, e);
852
+ return r !== void 0 && t.states[r].kind === "parallel" ? r : r === void 0 ? B(t, e) : wt(t, r, e);
862
853
  }
863
854
  function lt(t, e, n) {
864
855
  const r = t.states[e];
865
856
  if (r.kind !== "history")
866
857
  return $(t, e, n);
867
- const i = r.parent, o = n[t.states[i].path];
868
- if (o !== void 0 && o.length !== 0) {
858
+ const o = r.parent, i = n[t.states[o].path];
859
+ if (i !== void 0 && i.length !== 0) {
869
860
  const s = [];
870
- for (const a of o) {
871
- const c = t.pathToState.get(a);
872
- c !== void 0 && s.push(...r.history === "deep" ? $(t, c, n) : $(t, c, {}));
861
+ for (const a of i) {
862
+ const u = t.pathToState.get(a);
863
+ u !== void 0 && s.push(...r.history === "deep" ? $(t, u, n) : $(t, u, {}));
873
864
  }
874
865
  if (s.length !== 0)
875
866
  return A(s);
876
867
  }
877
- return r.historyFallback !== void 0 ? $(t, r.historyFallback, n) : $(t, i, {});
868
+ return r.historyFallback !== void 0 ? $(t, r.historyFallback, n) : $(t, o, {});
878
869
  }
879
870
  function Bt(t, e, n) {
880
871
  const r = [...e];
881
- for (let i = 0; i < r.length; i++)
882
- for (const o of t.ancestry[r[i]].slice(1)) {
883
- const s = t.states[o];
872
+ for (let o = 0; o < r.length; o++)
873
+ for (const i of t.ancestry[r[o]].slice(1)) {
874
+ const s = t.states[i];
884
875
  if (s.kind === "parallel")
885
876
  for (const a of s.children)
886
- t.states[a].kind !== "history" && (r.some((c) => c === a || S(t, a, c)) || r.push(...$(t, a, n)));
877
+ t.states[a].kind !== "history" && (r.some((u) => u === a || S(t, a, u)) || r.push(...$(t, a, n)));
887
878
  }
888
879
  return A(r);
889
880
  }
@@ -895,55 +886,53 @@ function $(t, e, n) {
895
886
  return [e];
896
887
  if (r.kind === "compound")
897
888
  return $(t, r.initial, n);
898
- const i = [];
899
- for (const o of r.children)
900
- t.states[o].kind !== "history" && i.push(...$(t, o, n));
901
- return A(i);
889
+ const o = [];
890
+ for (const i of r.children)
891
+ t.states[i].kind !== "history" && o.push(...$(t, i, n));
892
+ return A(o);
902
893
  }
903
894
  function et(t, e, n) {
904
895
  const r = t.states[e];
905
- return r.kind === "final" ? n.includes(e) : r.kind === "compound" ? r.children.some((i) => t.states[i].kind === "final" && n.includes(i)) : r.kind === "parallel" ? r.children.filter((i) => t.states[i].kind !== "history").every((i) => et(t, i, n)) : !1;
896
+ return r.kind === "final" ? n.includes(e) : r.kind === "compound" ? r.children.some((o) => t.states[o].kind === "final" && n.includes(o)) : r.kind === "parallel" ? r.children.filter((o) => t.states[o].kind !== "history").every((o) => et(t, o, n)) : !1;
906
897
  }
907
898
  function nt(t, e) {
908
899
  const n = Wt(t, e[0]);
909
900
  return n === void 0 ? !1 : et(t, n, e);
910
901
  }
911
902
  function yt(t, e, n, r) {
912
- const i = B(t, e.active[0]), o = t.states[i], s = o.kind === "compound" ? e.active.find((c) => t.states[c].parent === i && t.states[c].kind === "final") : o.kind === "final" ? i : void 0, a = s === void 0 ? t.output : t.states[s].output ?? t.output;
903
+ const o = B(t, e.active[0]), i = t.states[o], s = i.kind === "compound" ? e.active.find((u) => t.states[u].parent === o && t.states[u].kind === "final") : i.kind === "final" ? o : void 0, a = s === void 0 ? t.output : t.states[s].output ?? t.output;
913
904
  return typeof a == "function" ? a({ context: e.context, event: n, input: r }) : a;
914
905
  }
915
906
  function Ut(t, e) {
916
- var r;
917
907
  if (typeof t == "function")
918
908
  return t;
919
- const n = (r = e == null ? void 0 : e.guards) == null ? void 0 : r[t];
909
+ const n = e?.guards?.[t];
920
910
  if (n === void 0)
921
911
  throw new Error(`[Raph machine] Guard binding "${t}" is not defined.`);
922
912
  return n;
923
913
  }
924
914
  function vt(t, e) {
925
- var r;
926
915
  if (typeof t == "function")
927
916
  return t;
928
- const n = (r = e == null ? void 0 : e.reducers) == null ? void 0 : r[t];
917
+ const n = e?.reducers?.[t];
929
918
  if (n === void 0)
930
919
  throw new Error(`[Raph machine] Reducer binding "${t}" is not defined.`);
931
920
  return n;
932
921
  }
933
- function C(t, e, n, r, i, o, s) {
934
- const a = s ?? F(t), c = a.flat && n.active.length === 1 ? n.active[0] : void 0, u = c === void 0 ? void 0 : J(void 0, c, e, t, a.states[c].path), f = u === void 0 ? Object.freeze([...n.active]) : void 0, d = (u == null ? void 0 : u[T]) ?? Object.freeze({
935
- active: f,
922
+ function C(t, e, n, r, o, i, s) {
923
+ const a = s ?? F(t), u = a.flat && n.active.length === 1 ? n.active[0] : void 0, h = u === void 0 ? void 0 : J(void 0, u, e, t, a.states[u].path), c = h === void 0 ? Object.freeze([...n.active]) : void 0, d = h?.[T] ?? Object.freeze({
924
+ active: c,
936
925
  input: e,
937
926
  model: t
938
927
  });
939
928
  return L({
940
- active: d.publicActive ?? Object.freeze(f.map((p) => a.states[p].path)),
929
+ active: d.publicActive ?? Object.freeze(c.map((l) => a.states[l].path)),
941
930
  context: rt(n.context),
942
931
  history: a.flat ? q : Xt(n.history),
943
- ...o === void 0 ? {} : { output: o },
932
+ ...i === void 0 ? {} : { output: i },
944
933
  revision: r,
945
- status: i
946
- }, d, u);
934
+ status: o
935
+ }, d, h);
947
936
  }
948
937
  function I(t, e) {
949
938
  return {
@@ -952,7 +941,7 @@ function I(t, e) {
952
941
  snapshot: t
953
942
  };
954
943
  }
955
- function wt() {
944
+ function bt() {
956
945
  return Object.freeze({
957
946
  changed: !1,
958
947
  entered: Object.freeze([]),
@@ -963,37 +952,37 @@ function wt() {
963
952
  transitionOutputs: Object.freeze([])
964
953
  });
965
954
  }
966
- function at(t, e, n, r, i, o) {
955
+ function at(t, e, n, r, o, i) {
967
956
  for (const s of t.exited)
968
957
  e.add(s);
969
958
  for (const s of t.entered)
970
959
  n.add(s);
971
- r.push(...t.exitOutputs), i.push(...t.transitionOutputs), o.push(...t.entryOutputs);
960
+ r.push(...t.exitOutputs), o.push(...t.transitionOutputs), i.push(...t.entryOutputs);
972
961
  }
973
962
  function _(t, e) {
974
963
  const n = /* @__PURE__ */ new Set();
975
964
  for (const r of e)
976
- for (const i of t.ancestry[r])
977
- n.add(i);
965
+ for (const o of t.ancestry[r])
966
+ n.add(o);
978
967
  return [...n];
979
968
  }
980
969
  function Vt(t, e, n) {
981
970
  const r = new Set(t.ancestry[e]);
982
- for (const i of t.ancestry[n])
983
- if (r.has(i))
984
- return i;
971
+ for (const o of t.ancestry[n])
972
+ if (r.has(o))
973
+ return o;
985
974
  }
986
975
  function S(t, e, n) {
987
976
  const r = t.ancestry[n];
988
977
  return r.length > 1 && r.includes(e, 1);
989
978
  }
990
- function bt(t, e, n) {
979
+ function wt(t, e, n) {
991
980
  let r = n;
992
981
  for (; t.states[r].parent !== e; ) {
993
- const i = t.states[r].parent;
994
- if (i === void 0)
982
+ const o = t.states[r].parent;
983
+ if (o === void 0)
995
984
  return r;
996
- r = i;
985
+ r = o;
997
986
  }
998
987
  return r;
999
988
  }
@@ -1013,10 +1002,10 @@ function Gt(t, e, n) {
1013
1002
  if (!t.eventIndexes.has(n))
1014
1003
  return !1;
1015
1004
  for (const r of e) {
1016
- const i = t.pathToState.get(r);
1017
- if (i !== void 0) {
1018
- for (const o of t.ancestry[i])
1019
- if (t.states[o].on.has(n))
1005
+ const o = t.pathToState.get(r);
1006
+ if (o !== void 0) {
1007
+ for (const i of t.ancestry[o])
1008
+ if (t.states[i].on.has(n))
1020
1009
  return !0;
1021
1010
  }
1022
1011
  }