@modular-react/journeys 1.0.0 → 1.0.2

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.
@@ -1,1422 +0,0 @@
1
- import { isDevEnv as e } from "@modular-react/core";
2
- //#region src/semver.ts
3
- var t = /* @__PURE__ */ function(e) {
4
- return e[e.GT = 0] = "GT", e[e.GTE = 1] = "GTE", e[e.LT = 2] = "LT", e[e.LTE = 3] = "LTE", e[e.EQ = 4] = "EQ", e;
5
- }(t || {}), n = class extends Error {
6
- constructor(e) {
7
- super(`[@modular-react/journeys] ${e}`), this.name = "SemverParseError";
8
- }
9
- };
10
- function r(e) {
11
- let t = b(x(e.trim()));
12
- if (!t) throw new n(`invalid version "${e}"`);
13
- return t;
14
- }
15
- function i(e) {
16
- let t = e.trim();
17
- if (t === "" || t === "*" || t === "x" || t === "X") return { sets: [[]] };
18
- let n = t.split("||"), r = [];
19
- for (let e of n) {
20
- let t = u(e);
21
- r.push(t);
22
- }
23
- return { sets: r };
24
- }
25
- function a(e, t) {
26
- for (let n of t.sets) {
27
- let t = !0;
28
- for (let r of n) if (!l(e, r)) {
29
- t = !1;
30
- break;
31
- }
32
- if (t) return !0;
33
- }
34
- return !1;
35
- }
36
- function o(e, t) {
37
- return a(r(e), i(t));
38
- }
39
- function s(e, t) {
40
- return e[0] === t[0] ? e[1] === t[1] ? e[2] === t[2] ? 0 : e[2] < t[2] ? -1 : 1 : e[1] < t[1] ? -1 : 1 : e[0] < t[0] ? -1 : 1;
41
- }
42
- function c(e, t) {
43
- return s(r(e), r(t));
44
- }
45
- function l(e, n) {
46
- let r = s(e, n.v);
47
- switch (n.op) {
48
- case t.GT: return r > 0;
49
- case t.GTE: return r >= 0;
50
- case t.LT: return r < 0;
51
- case t.LTE: return r <= 0;
52
- case t.EQ: return r === 0;
53
- }
54
- }
55
- function u(e) {
56
- let t = [], n = d(e);
57
- if (n) return f(n[0], n[1], t), t;
58
- let r = e.split(/\s+/).filter((e) => e.length > 0);
59
- if (r.length === 0) return t;
60
- for (let e of r) p(e, t);
61
- return t;
62
- }
63
- function d(e) {
64
- let t = e.match(/^\s*(\S+)\s+-\s+(\S+)\s*$/);
65
- return t ? [t[1], t[2]] : null;
66
- }
67
- function f(e, r, i) {
68
- let a = _(x(e));
69
- if (!a) throw new n(`invalid lower bound in hyphen range "${e}"`);
70
- i.push({
71
- op: t.GTE,
72
- v: v(a, 0)
73
- });
74
- let o = _(x(r));
75
- if (!o || o.major === null) throw new n(`invalid upper bound in hyphen range "${r}"`);
76
- o.minor === null ? i.push({
77
- op: t.LT,
78
- v: [
79
- o.major + 1,
80
- 0,
81
- 0
82
- ]
83
- }) : o.patch === null ? i.push({
84
- op: t.LT,
85
- v: [
86
- o.major,
87
- o.minor + 1,
88
- 0
89
- ]
90
- }) : i.push({
91
- op: t.LTE,
92
- v: [
93
- o.major,
94
- o.minor,
95
- o.patch
96
- ]
97
- });
98
- }
99
- function p(e, n) {
100
- if (!(e === "*" || e === "x" || e === "X")) {
101
- if (e.startsWith("^")) {
102
- m(e.slice(1), n);
103
- return;
104
- }
105
- if (e.startsWith("~")) {
106
- h(e.slice(1), n);
107
- return;
108
- }
109
- if (e.startsWith(">=")) {
110
- n.push({
111
- op: t.GTE,
112
- v: y(e.slice(2), e)
113
- });
114
- return;
115
- }
116
- if (e.startsWith("<=")) {
117
- n.push({
118
- op: t.LTE,
119
- v: y(e.slice(2), e)
120
- });
121
- return;
122
- }
123
- if (e.startsWith(">")) {
124
- n.push({
125
- op: t.GT,
126
- v: y(e.slice(1), e)
127
- });
128
- return;
129
- }
130
- if (e.startsWith("<")) {
131
- n.push({
132
- op: t.LT,
133
- v: y(e.slice(1), e)
134
- });
135
- return;
136
- }
137
- if (e.startsWith("=")) {
138
- g(e.slice(1), e, n);
139
- return;
140
- }
141
- g(e, e, n);
142
- }
143
- }
144
- function m(e, r) {
145
- let i = _(x(e));
146
- if (!i) throw new n(`invalid caret range "^${e}"`);
147
- let { major: a, minor: o, patch: s } = i;
148
- if (a === null) throw new n(`invalid caret range "^${e}"`);
149
- if (a > 0) {
150
- r.push({
151
- op: t.GTE,
152
- v: [
153
- a,
154
- o ?? 0,
155
- s ?? 0
156
- ]
157
- }), r.push({
158
- op: t.LT,
159
- v: [
160
- a + 1,
161
- 0,
162
- 0
163
- ]
164
- });
165
- return;
166
- }
167
- if (o === null) {
168
- r.push({
169
- op: t.GTE,
170
- v: [
171
- 0,
172
- 0,
173
- 0
174
- ]
175
- }), r.push({
176
- op: t.LT,
177
- v: [
178
- 1,
179
- 0,
180
- 0
181
- ]
182
- });
183
- return;
184
- }
185
- if (o > 0) {
186
- r.push({
187
- op: t.GTE,
188
- v: [
189
- 0,
190
- o,
191
- s ?? 0
192
- ]
193
- }), r.push({
194
- op: t.LT,
195
- v: [
196
- 0,
197
- o + 1,
198
- 0
199
- ]
200
- });
201
- return;
202
- }
203
- if (s === null) {
204
- r.push({
205
- op: t.GTE,
206
- v: [
207
- 0,
208
- 0,
209
- 0
210
- ]
211
- }), r.push({
212
- op: t.LT,
213
- v: [
214
- 0,
215
- 1,
216
- 0
217
- ]
218
- });
219
- return;
220
- }
221
- r.push({
222
- op: t.GTE,
223
- v: [
224
- 0,
225
- 0,
226
- s
227
- ]
228
- }), r.push({
229
- op: t.LT,
230
- v: [
231
- 0,
232
- 0,
233
- s + 1
234
- ]
235
- });
236
- }
237
- function h(e, r) {
238
- let i = _(x(e));
239
- if (!i) throw new n(`invalid tilde range "~${e}"`);
240
- let { major: a, minor: o, patch: s } = i;
241
- if (a === null) throw new n(`invalid tilde range "~${e}"`);
242
- if (o === null) {
243
- r.push({
244
- op: t.GTE,
245
- v: [
246
- a,
247
- 0,
248
- 0
249
- ]
250
- }), r.push({
251
- op: t.LT,
252
- v: [
253
- a + 1,
254
- 0,
255
- 0
256
- ]
257
- });
258
- return;
259
- }
260
- r.push({
261
- op: t.GTE,
262
- v: [
263
- a,
264
- o,
265
- s ?? 0
266
- ]
267
- }), r.push({
268
- op: t.LT,
269
- v: [
270
- a,
271
- o + 1,
272
- 0
273
- ]
274
- });
275
- }
276
- function g(e, r, i) {
277
- let a = _(x(e));
278
- if (!a) throw new n(`invalid version or range "${r}"`);
279
- let { major: o, minor: s, patch: c } = a;
280
- if (o !== null) {
281
- if (s === null) {
282
- i.push({
283
- op: t.GTE,
284
- v: [
285
- o,
286
- 0,
287
- 0
288
- ]
289
- }), i.push({
290
- op: t.LT,
291
- v: [
292
- o + 1,
293
- 0,
294
- 0
295
- ]
296
- });
297
- return;
298
- }
299
- if (c === null) {
300
- i.push({
301
- op: t.GTE,
302
- v: [
303
- o,
304
- s,
305
- 0
306
- ]
307
- }), i.push({
308
- op: t.LT,
309
- v: [
310
- o,
311
- s + 1,
312
- 0
313
- ]
314
- });
315
- return;
316
- }
317
- i.push({
318
- op: t.EQ,
319
- v: [
320
- o,
321
- s,
322
- c
323
- ]
324
- });
325
- }
326
- }
327
- function _(e) {
328
- if (e === "" || e === "*" || e === "x" || e === "X") return {
329
- major: null,
330
- minor: null,
331
- patch: null
332
- };
333
- let t = e.split(".");
334
- if (t.length > 3) return null;
335
- let n = [];
336
- for (let e of t) {
337
- if (e === "" || e === "x" || e === "X" || e === "*") {
338
- n.push(null);
339
- continue;
340
- }
341
- if (!/^\d+$/.test(e)) return null;
342
- let t = Number(e);
343
- if (!Number.isFinite(t)) return null;
344
- n.push(t);
345
- }
346
- for (; n.length < 3;) n.push(null);
347
- let r = !1;
348
- for (let e of n) if (e === null) r = !0;
349
- else if (r) return null;
350
- return {
351
- major: n[0],
352
- minor: n[1],
353
- patch: n[2]
354
- };
355
- }
356
- function v(e, t) {
357
- return [
358
- e.major ?? t,
359
- e.minor ?? t,
360
- e.patch ?? t
361
- ];
362
- }
363
- function y(e, t) {
364
- let r = b(x(e));
365
- if (!r) throw new n(`invalid version in "${t}"`);
366
- return r;
367
- }
368
- function b(e) {
369
- let t = /^(\d+)\.(\d+)\.(\d+)$/.exec(e);
370
- if (!t) return null;
371
- let n = [
372
- Number(t[1]),
373
- Number(t[2]),
374
- Number(t[3])
375
- ];
376
- return n.every((e) => Number.isFinite(e)) ? n : null;
377
- }
378
- function x(e) {
379
- if (e.length === 0) return e;
380
- let t = e.charCodeAt(0);
381
- return t === 118 || t === 86 || t === 61 ? e.slice(1) : e;
382
- }
383
- //#endregion
384
- //#region src/validation.ts
385
- var S = class extends Error {
386
- issues;
387
- constructor(e) {
388
- super(`[@modular-react/journeys] Invalid journey registration:\n - ${e.join("\n - ")}`), this.name = "JourneyValidationError", this.issues = e;
389
- }
390
- }, C = class extends Error {
391
- constructor(e, t) {
392
- super(`[@modular-react/journeys] ${e}`, t), this.name = "JourneyHydrationError";
393
- }
394
- }, w = class extends Error {
395
- journeyId;
396
- constructor(e, t) {
397
- super(`[@modular-react/journeys] Unknown journey id "${e}". Registered: ${t.join(", ") || "(none)"}`), this.name = "UnknownJourneyError", this.journeyId = e;
398
- }
399
- };
400
- function T(e, t) {
401
- let o = [], s = /* @__PURE__ */ new Map();
402
- for (let e of t) s.set(e.id, e);
403
- for (let t of D(e)) o.push(t);
404
- for (let e of t) e.exitPoints && Object.prototype.hasOwnProperty.call(e.exitPoints, "allowBack") && o.push(`module "${e.id}" declares an exit named "allowBack", which collides with the reserved per-entry transition control key. Rename the exit (e.g. "allowBackExit").`);
405
- let c = /* @__PURE__ */ new Set();
406
- for (let t of e) {
407
- let e = t.definition;
408
- c.has(e.id) && o.push(`journey "${e.id}" is registered more than once`), c.add(e.id);
409
- let l = e.transitions ?? {};
410
- for (let [t, n] of Object.entries(l)) {
411
- let r = s.get(t);
412
- if (!r) {
413
- o.push(`journey "${e.id}" references unknown module id "${t}" in transitions`);
414
- continue;
415
- }
416
- if (!n || typeof n != "object") {
417
- o.push(`journey "${e.id}" has malformed transitions for module "${t}" (expected an object)`);
418
- continue;
419
- }
420
- for (let [i, a] of Object.entries(n)) {
421
- let n = r.entryPoints?.[i];
422
- if (!n) {
423
- o.push(`journey "${e.id}" references unknown entry "${t}.${i}"`);
424
- continue;
425
- }
426
- if (!a || typeof a != "object") {
427
- o.push(`journey "${e.id}" has malformed transitions for entry "${t}.${i}" (expected an object)`);
428
- continue;
429
- }
430
- let s = a;
431
- for (let n of Object.keys(s)) n !== "allowBack" && (!r.exitPoints || !(n in r.exitPoints)) && o.push(`journey "${e.id}" references unknown exit "${t}.${i}.${n}"`);
432
- if (s.allowBack === !0) {
433
- let r = n.allowBack;
434
- r !== "preserve-state" && r !== "rollback" && o.push(`journey "${e.id}" sets allowBack on "${t}.${i}" but the module entry does not declare allowBack`);
435
- }
436
- }
437
- }
438
- if (e.moduleCompat) for (let [t, c] of Object.entries(e.moduleCompat)) {
439
- if (typeof c != "string") {
440
- o.push(`journey "${e.id}" declares a non-string version range for module "${t}" in moduleCompat`);
441
- continue;
442
- }
443
- let l = c.trim();
444
- if (l.length === 0) {
445
- o.push(`journey "${e.id}" declares an empty version range for module "${t}" in moduleCompat`);
446
- continue;
447
- }
448
- let u = s.get(t);
449
- if (!u) {
450
- o.push(`journey "${e.id}" requires module "${t}" (range "${l}") in moduleCompat but it is not registered`);
451
- continue;
452
- }
453
- let d;
454
- try {
455
- d = i(l);
456
- } catch (r) {
457
- let i = r instanceof n ? r.message : String(r);
458
- o.push(`journey "${e.id}" has an unparseable moduleCompat range for "${t}": ${i}`);
459
- continue;
460
- }
461
- let f;
462
- try {
463
- f = r(u.version);
464
- } catch (r) {
465
- let i = r instanceof n ? r.message : String(r);
466
- o.push(`module "${t}" declares an unparseable version "${u.version}" (referenced by journey "${e.id}"): ${i}`);
467
- continue;
468
- }
469
- a(f, d) || o.push(`journey "${e.id}" requires module "${t}" "${l}" but registered version is "${u.version}"`);
470
- }
471
- let u = e.resumes ?? {};
472
- for (let [t, n] of Object.entries(u)) {
473
- let r = s.get(t);
474
- if (!r) {
475
- o.push(`journey "${e.id}" references unknown module id "${t}" in resumes`);
476
- continue;
477
- }
478
- if (!n || typeof n != "object") {
479
- o.push(`journey "${e.id}" has malformed resumes for module "${t}" (expected an object)`);
480
- continue;
481
- }
482
- for (let [i, a] of Object.entries(n)) {
483
- if (!r.entryPoints?.[i]) {
484
- o.push(`journey "${e.id}" references unknown entry "${t}.${i}" in resumes`);
485
- continue;
486
- }
487
- if (!a || typeof a != "object") {
488
- o.push(`journey "${e.id}" has malformed resumes for entry "${t}.${i}" (expected an object)`);
489
- continue;
490
- }
491
- for (let n of Object.keys(a)) r.exitPoints && Object.prototype.hasOwnProperty.call(r.exitPoints, n) && o.push(`journey "${e.id}" declares resume "${t}.${i}.${n}" but "${n}" is also an exit name on that module — rename one to avoid silent confusion`), typeof a[n] != "function" && o.push(`journey "${e.id}" has non-function resume "${t}.${i}.${n}"`);
492
- }
493
- }
494
- }
495
- if (o.length > 0) throw new S(o);
496
- }
497
- function E(e) {
498
- let t = D(e);
499
- if (t.length > 0) throw new S(t);
500
- }
501
- function D(e) {
502
- let t = /* @__PURE__ */ new Set();
503
- for (let n of e) t.add(n.definition.id);
504
- let n = /* @__PURE__ */ new Map();
505
- for (let r of e) {
506
- let e = [], i = r.definition.invokes;
507
- if (Array.isArray(i)) for (let n of i) !n || typeof n.id != "string" || t.has(n.id) && e.push(n.id);
508
- n.set(r.definition.id, e);
509
- }
510
- let r = [], i = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set(), s = [], c = [];
511
- for (let t of e) {
512
- let e = t.definition.id;
513
- if (!a.has(e)) for (c.push({
514
- id: e,
515
- nextIndex: 0
516
- }), o.add(e), s.push(e); c.length > 0;) {
517
- let e = c[c.length - 1], t = n.get(e.id);
518
- if (!t || e.nextIndex >= t.length) {
519
- c.pop(), s.pop(), o.delete(e.id), a.add(e.id);
520
- continue;
521
- }
522
- let l = t[e.nextIndex];
523
- if (e.nextIndex++, o.has(l)) {
524
- let e = s.indexOf(l), t = s.slice(e), n = O(t);
525
- if (!i.has(n)) {
526
- i.add(n);
527
- let e = [...t, l].map(k).join(" → ");
528
- r.push(`journey invoke cycle detected: ${e}`);
529
- }
530
- continue;
531
- }
532
- a.has(l) || (c.push({
533
- id: l,
534
- nextIndex: 0
535
- }), o.add(l), s.push(l));
536
- }
537
- }
538
- return r;
539
- }
540
- function O(e) {
541
- let t = 0;
542
- for (let n = 1; n < e.length; n++) e[n] < e[t] && (t = n);
543
- return e.slice(t).concat(e.slice(0, t)).join("→");
544
- }
545
- function k(e) {
546
- return `"${e}"`;
547
- }
548
- function A(e) {
549
- let t = [];
550
- return (!e.id || typeof e.id != "string") && t.push("journey is missing a string id"), (!e.version || typeof e.version != "string") && t.push(`journey "${e.id ?? "(unknown)"}" is missing a string version`), typeof e.initialState != "function" && t.push(`journey "${e.id}" must declare initialState as a function`), typeof e.start != "function" && t.push(`journey "${e.id}" must declare start as a function`), (!e.transitions || typeof e.transitions != "object") && t.push(`journey "${e.id}" must declare transitions`), t;
551
- }
552
- //#endregion
553
- //#region src/runtime.ts
554
- var j = 16, M = 8, N = /* @__PURE__ */ new WeakMap();
555
- function P(t, n = {}) {
556
- let r = n.debug ?? e(), i = n.modules ?? {}, a = /* @__PURE__ */ new Map();
557
- for (let e of t) a.set(e.definition.id, e);
558
- let o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map();
559
- function l(e, t) {
560
- return `${e}${t}`;
561
- }
562
- function u(e) {
563
- e.revision += 1, e.cachedSnapshot = null;
564
- for (let t of e.listeners) try {
565
- t();
566
- } catch (e) {
567
- r && console.error("[@modular-react/journeys] listener threw", e);
568
- }
569
- }
570
- function d() {
571
- return (/* @__PURE__ */ new Date()).toISOString();
572
- }
573
- function f() {
574
- try {
575
- let e = globalThis.crypto;
576
- if (e?.randomUUID) return `ji_${e.randomUUID()}`;
577
- } catch {}
578
- let e = Math.random().toString(36).slice(2, 10);
579
- return `ji_${Date.now().toString(36)}_${e}`;
580
- }
581
- function p(e) {
582
- return {
583
- id: e.definition.id,
584
- version: e.definition.version,
585
- meta: e.definition.meta
586
- };
587
- }
588
- function m(e) {
589
- let t = a.get(e);
590
- if (!t) throw new w(e, [...a.keys()]);
591
- return t;
592
- }
593
- function h(e) {
594
- return {
595
- moduleId: e.module,
596
- entry: e.entry,
597
- input: e.input
598
- };
599
- }
600
- function g(e) {
601
- if (!e) return !1;
602
- let t = i[e.moduleId]?.entryPoints?.[e.entry]?.allowBack;
603
- return t === "rollback" || t === "preserve-state" ? t : !1;
604
- }
605
- function _(e, t) {
606
- return t ? e.transitions?.[t.moduleId]?.[t.entry]?.allowBack === !0 : !1;
607
- }
608
- function v(e) {
609
- if (typeof e != "object" || !e) return e;
610
- let t = Array.isArray(e) ? [...e] : { ...e };
611
- try {
612
- Object.freeze(t);
613
- } catch {}
614
- return t;
615
- }
616
- function y(e, t) {
617
- let n = t.options?.maxHistory;
618
- if (!(n === void 0 || n <= 0)) {
619
- for (; e.history.length > n;) e.history.shift(), e.rollbackSnapshots.shift();
620
- e.hasRollbackSnapshot = e.rollbackSnapshots.some((e) => e !== void 0);
621
- }
622
- }
623
- function b(e, t) {
624
- let n = D(e);
625
- if (e.saveInFlight) {
626
- e.pendingSave = n;
627
- return;
628
- }
629
- x(e, t, n);
630
- }
631
- async function x(e, t, n) {
632
- e.saveInFlight = !0;
633
- try {
634
- if (!e.persistenceKey) return;
635
- await t.save(e.persistenceKey, n);
636
- } catch (t) {
637
- r && console.error(`[@modular-react/journeys] Failed to persist "${e.journeyId}" instance ${e.id}`, t);
638
- } finally {
639
- if (e.saveInFlight = !1, e.pendingRemove) e.pendingRemove = !1, e.pendingSave = null, e.persistenceKey && E(t, e.persistenceKey);
640
- else if (e.pendingSave) {
641
- let n = e.pendingSave;
642
- e.pendingSave = null, x(e, t, n);
643
- }
644
- }
645
- }
646
- function S(e, t) {
647
- if (!e.persistenceKey) return;
648
- e.pendingSave = null;
649
- let n = e.persistenceKey;
650
- if (s.delete(l(e.journeyId, n)), e.saveInFlight) {
651
- e.pendingRemove = !0;
652
- return;
653
- }
654
- E(t, n);
655
- }
656
- function T(e, t) {
657
- E(e, t);
658
- }
659
- function E(e, t) {
660
- try {
661
- let n = e.remove(t);
662
- n && typeof n.catch == "function" && n.catch((e) => {
663
- r && console.error("[@modular-react/journeys] persistence.remove rejected", e);
664
- });
665
- } catch (e) {
666
- r && console.error("[@modular-react/journeys] persistence.remove threw", e);
667
- }
668
- }
669
- function D(e) {
670
- let t;
671
- if (e.activeChildId) {
672
- let n = o.get(e.activeChildId);
673
- if (n) {
674
- let r = n.parent;
675
- r && r.instanceId === e.id && (t = {
676
- childJourneyId: n.journeyId,
677
- childInstanceId: n.id,
678
- childPersistenceKey: n.persistenceKey,
679
- resumeName: r.resumeName
680
- });
681
- }
682
- }
683
- let n = e.parent ? {
684
- parentInstanceId: e.parent.instanceId,
685
- resumeName: e.parent.resumeName
686
- } : void 0;
687
- return {
688
- definitionId: e.journeyId,
689
- version: a.get(e.journeyId).definition.version,
690
- instanceId: e.id,
691
- status: e.status === "loading" ? "active" : e.status,
692
- step: e.step,
693
- history: [...e.history],
694
- rollbackSnapshots: e.hasRollbackSnapshot ? e.rollbackSnapshots.map((e) => e === void 0 ? null : e) : void 0,
695
- terminalPayload: e.status === "completed" || e.status === "aborted" ? e.terminalPayload : void 0,
696
- state: e.state,
697
- startedAt: e.startedAt,
698
- updatedAt: e.updatedAt,
699
- pendingInvoke: t,
700
- parentLink: n,
701
- resumeBouncesAtStep: e.resumeBouncesAtStep ?? void 0
702
- };
703
- }
704
- function O(e, t, n, i, a, o) {
705
- let s = {
706
- journeyId: t.journeyId,
707
- instanceId: t.id,
708
- from: n,
709
- to: i,
710
- exit: a,
711
- state: t.state,
712
- history: [...t.history],
713
- kind: o?.kind ?? "step",
714
- child: o?.child,
715
- outcome: o?.outcome,
716
- resume: o?.resume
717
- };
718
- try {
719
- e.definition.onTransition?.(s);
720
- } catch (e) {
721
- r && console.error("[@modular-react/journeys] onTransition (definition) threw", e);
722
- }
723
- try {
724
- e.options?.onTransition?.(s);
725
- } catch (e) {
726
- r && console.error("[@modular-react/journeys] onTransition (registration) threw", e);
727
- }
728
- }
729
- function k(e, t, n) {
730
- if (t.terminalFired) return;
731
- t.terminalFired = !0;
732
- let i = {
733
- journeyId: t.journeyId,
734
- instanceId: t.id,
735
- state: t.state,
736
- history: t.history
737
- };
738
- try {
739
- e.definition.onComplete?.(i, n);
740
- } catch (e) {
741
- r && console.error("[@modular-react/journeys] onComplete (definition) threw", e);
742
- }
743
- try {
744
- e.options?.onComplete?.(i, n);
745
- } catch (e) {
746
- r && console.error("[@modular-react/journeys] onComplete (registration) threw", e);
747
- }
748
- }
749
- function A(e, t, n) {
750
- if (t.terminalFired) return;
751
- t.terminalFired = !0;
752
- let i = {
753
- journeyId: t.journeyId,
754
- instanceId: t.id,
755
- state: t.state,
756
- history: t.history
757
- };
758
- try {
759
- e.definition.onAbort?.(i, n);
760
- } catch (e) {
761
- r && console.error("[@modular-react/journeys] onAbort (definition) threw", e);
762
- }
763
- try {
764
- e.options?.onAbort?.(i, n);
765
- } catch (e) {
766
- r && console.error("[@modular-react/journeys] onAbort (registration) threw", e);
767
- }
768
- }
769
- function P(e, t, n, i, a = "step") {
770
- try {
771
- e.options?.onError?.(n, {
772
- step: i,
773
- phase: a
774
- });
775
- } catch (e) {
776
- r && console.error("[@modular-react/journeys] onError (registration) threw", e);
777
- }
778
- }
779
- function F(e, t, n) {
780
- let r = e.definition.resumes;
781
- if (!r) return;
782
- let i = r[t.moduleId];
783
- if (!i) return;
784
- let a = i[t.entry];
785
- if (a) return a[n];
786
- }
787
- function I(e) {
788
- let t = [], n = e.parent, r = new Set([e.id]);
789
- for (; n && !r.has(n.instanceId);) {
790
- let e = o.get(n.instanceId);
791
- if (!e) break;
792
- r.add(e.id), t.unshift(e), n = e.parent;
793
- }
794
- return t;
795
- }
796
- function L(e) {
797
- let t = e.options?.maxResumeBouncesPerStep;
798
- return typeof t == "number" && Number.isFinite(t) && t > 0 ? t : M;
799
- }
800
- function R(e, t, n) {
801
- let r = j, i = !1, o = (e) => {
802
- let t = e?.options?.maxCallStackDepth;
803
- typeof t == "number" && Number.isFinite(t) && t > 0 && (r = i ? Math.min(r, t) : t, i = !0);
804
- };
805
- for (let t of e) o(a.get(t.journeyId));
806
- return o(t), o(n), r;
807
- }
808
- function z(e, t, n, i) {
809
- let s = e.step;
810
- if (!s) return B(e, t, { abort: {
811
- reason: "invoke-without-step",
812
- exit: i
813
- } }, { kind: "invoke" }), !1;
814
- if (!n || typeof n != "object") return B(e, t, { abort: {
815
- reason: "invoke-missing-spec",
816
- exit: i
817
- } }, { kind: "invoke" }), !1;
818
- let l = n.handle?.id;
819
- if (!l || !a.has(l)) return r && console.error(`[@modular-react/journeys] Invoke from "${e.journeyId}.${s.moduleId}.${s.entry}" references unknown child journey id "${l}".`), P(t, e, /* @__PURE__ */ Error(`Unknown child journey "${l}"`), s, "invoke"), B(e, t, { abort: {
820
- reason: "invoke-unknown-journey",
821
- journeyId: l,
822
- exit: i
823
- } }, { kind: "invoke" }), !1;
824
- if (typeof n.resume != "string" || n.resume.length === 0) return r && console.error(`[@modular-react/journeys] Invoke from "${e.journeyId}.${s.moduleId}.${s.entry}" is missing a resume name.`), P(t, e, /* @__PURE__ */ Error("Invoke missing resume name"), s, "invoke"), B(e, t, { abort: {
825
- reason: "invoke-missing-resume",
826
- exit: i
827
- } }, { kind: "invoke" }), !1;
828
- if (!F(t, s, n.resume)) return r && console.error(`[@modular-react/journeys] Invoke from "${e.journeyId}.${s.moduleId}.${s.entry}" names resume "${n.resume}" but no such handler is declared on def.resumes[${s.moduleId}][${s.entry}].`), P(t, e, /* @__PURE__ */ Error(`Resume "${n.resume}" not declared on ${s.moduleId}.${s.entry}`), s, "invoke"), B(e, t, { abort: {
829
- reason: "invoke-unknown-resume",
830
- resume: n.resume,
831
- exit: i
832
- } }, { kind: "invoke" }), !1;
833
- let d = a.get(l), f = t.definition.invokes;
834
- if (Array.isArray(f)) {
835
- let n = !1;
836
- for (let e of f) if (e?.id === l) {
837
- n = !0;
838
- break;
839
- }
840
- if (!n) return r && console.error(`[@modular-react/journeys] Invoke from "${e.journeyId}.${s.moduleId}.${s.entry}" dispatched handle "${l}" which is not in the parent's declared invokes[]. Add the handle to the parent journey's \`invokes\` array, or remove the declaration to opt out of static checking.`), P(t, e, /* @__PURE__ */ Error(`Child journey "${l}" is not in "${e.journeyId}".invokes[]`), s, "invoke"), B(e, t, { abort: {
841
- reason: "invoke-undeclared-child",
842
- parentJourneyId: e.journeyId,
843
- childJourneyId: l,
844
- exit: i
845
- } }, { kind: "invoke" }), !1;
846
- }
847
- let p = I(e);
848
- {
849
- let n = p.map((e) => e.journeyId).concat(e.journeyId), a = n.indexOf(l);
850
- if (a >= 0) {
851
- let o = [...n.slice(a), l], c = o.map((e) => `"${e}"`).join(" → ");
852
- return r && console.error(`[@modular-react/journeys] Invoke would re-enter journey "${l}" already on the active chain: ${c}. Aborting parent "${e.id}".`), P(t, e, /* @__PURE__ */ Error(`Invoke cycle on chain: ${c}`), s, "invoke"), B(e, t, { abort: {
853
- reason: "invoke-cycle",
854
- childJourneyId: l,
855
- chain: o,
856
- exit: i
857
- } }, { kind: "invoke" }), !1;
858
- }
859
- }
860
- let m = R(p, t, d), h = p.length + 2;
861
- if (h > m) {
862
- let n = p.map((e) => e.journeyId).concat(e.journeyId, l), a = n.map((e) => `"${e}"`).join(" → ");
863
- return r && console.error(`[@modular-react/journeys] Invoke would exceed maxCallStackDepth (${m}) — chain: ${a}. Aborting parent "${e.id}".`), P(t, e, /* @__PURE__ */ Error(`Invoke would exceed depth cap ${m} on chain ${a}`), s, "invoke"), B(e, t, { abort: {
864
- reason: "invoke-stack-overflow",
865
- depth: h,
866
- cap: m,
867
- chain: n,
868
- exit: i
869
- } }, { kind: "invoke" }), !1;
870
- }
871
- let g;
872
- try {
873
- g = $.start(n.handle, n.input);
874
- } catch (n) {
875
- return r && console.error("[@modular-react/journeys] runtime.start during invoke threw", n), P(t, e, n, s, "invoke"), B(e, t, { abort: {
876
- reason: "invoke-start-threw",
877
- error: n,
878
- exit: i
879
- } }, { kind: "invoke" }), !1;
880
- }
881
- let _ = o.get(g);
882
- if (!_) return B(e, t, { abort: {
883
- reason: "invoke-start-no-record",
884
- exit: i
885
- } }, { kind: "invoke" }), !1;
886
- if (_.parent && _.parent.instanceId !== e.id) return r && console.error(`[@modular-react/journeys] Invoke target "${_.id}" is already linked to parent "${_.parent.instanceId}". Aborting parent "${e.id}".`), P(t, e, /* @__PURE__ */ Error(`Child instance "${_.id}" is already linked to parent "${_.parent.instanceId}"`), s, "invoke"), B(e, t, { abort: {
887
- reason: "invoke-child-already-linked",
888
- childInstanceId: _.id,
889
- existingParentId: _.parent.instanceId,
890
- exit: i
891
- } }, { kind: "invoke" }), !1;
892
- e.activeChildId = g, _.parent = {
893
- instanceId: e.id,
894
- resumeName: n.resume
895
- }, c.set(g, e.id);
896
- let v = d?.options?.persistence;
897
- return v && b(_, v), u(_), _;
898
- }
899
- function B(e, t, n, r) {
900
- H(e, t, n, null, r);
901
- }
902
- function V(e) {
903
- if (e.status !== "completed" && e.status !== "aborted") return;
904
- let t = e.parent;
905
- if (!t) return;
906
- let n = t.instanceId, i = o.get(n);
907
- if (c.delete(e.id), e.parent = null, u(e), !i || i.activeChildId !== e.id) return;
908
- if (i.activeChildId = null, i.status !== "active") {
909
- u(i);
910
- return;
911
- }
912
- let s = a.get(i.journeyId), l = i.step;
913
- if (!s || !l) {
914
- u(i);
915
- return;
916
- }
917
- let d = F(s, l, t.resumeName);
918
- if (!d) {
919
- r && console.warn(`[@modular-react/journeys] Resume "${t.resumeName}" no longer declared on ${l.moduleId}.${l.entry} — aborting parent ${i.id}.`), P(s, i, /* @__PURE__ */ Error(`Resume "${t.resumeName}" missing`), l, "resume"), H(i, s, { abort: {
920
- reason: "resume-missing",
921
- resume: t.resumeName,
922
- childJourneyId: e.journeyId
923
- } }, null);
924
- return;
925
- }
926
- let f = e.status === "completed" ? {
927
- status: "completed",
928
- payload: e.terminalPayload
929
- } : {
930
- status: "aborted",
931
- reason: e.terminalPayload
932
- }, p;
933
- try {
934
- p = d({
935
- state: i.state,
936
- input: l.input,
937
- outcome: f
938
- });
939
- } catch (e) {
940
- r && console.error("[@modular-react/journeys] resume handler threw", e), P(s, i, e, l, "resume"), H(i, s, { abort: {
941
- reason: "resume-threw",
942
- resume: t.resumeName,
943
- error: e
944
- } }, null);
945
- return;
946
- }
947
- if (p && typeof p.then == "function") {
948
- r && console.error(`[@modular-react/journeys] Resume handler "${t.resumeName}" on ${l.moduleId}.${l.entry} returned a Promise. Resumes must be synchronous and pure.`), H(i, s, { abort: {
949
- reason: "resume-returned-promise",
950
- resume: t.resumeName
951
- } }, null, {
952
- kind: "resume",
953
- outcome: f,
954
- resume: t.resumeName
955
- });
956
- return;
957
- }
958
- H(i, s, p, null, {
959
- kind: "resume",
960
- outcome: f,
961
- resume: t.resumeName
962
- });
963
- }
964
- function H(e, t, n, a, o) {
965
- let s = e.step, c = e.state;
966
- if ("state" in n && (e.state = n.state), ("next" in n || "complete" in n || "abort" in n) && (e.resumeBouncesAtStep = null), "next" in n) {
967
- let l = h(n.next);
968
- if (r && Object.keys(i).length > 0) {
969
- let e = i[l.moduleId];
970
- e ? e.entryPoints?.[l.entry] || console.warn(`[@modular-react/journeys] Transition on "${s?.moduleId}.${s?.entry}" returned next.entry="${l.moduleId}.${l.entry}" which is not a declared entry on that module.`) : console.warn(`[@modular-react/journeys] Transition on "${s?.moduleId}.${s?.entry}" returned next.module="${l.moduleId}" which is not in the runtime's module map — the outlet will render a "no entry" error.`);
971
- }
972
- s && (e.history.push(s), U(l) === "rollback" ? (e.rollbackSnapshots.push(v(c)), e.hasRollbackSnapshot = !0) : e.rollbackSnapshots.push(void 0)), e.step = l, e.status = "active", e.stepToken += 1, e.updatedAt = d(), e.cachedCallbacks = null, y(e, t), O(t, e, s, l, a, o);
973
- } else if ("complete" in n) s && (e.history.push(s), e.rollbackSnapshots.push(void 0)), e.step = null, e.status = "completed", e.terminalPayload = n.complete, e.stepToken += 1, e.updatedAt = d(), e.cachedCallbacks = null, y(e, t), O(t, e, s, null, a, o), k(t, e, n.complete);
974
- else if ("abort" in n) s && (e.history.push(s), e.rollbackSnapshots.push(void 0)), e.step = null, e.status = "aborted", e.terminalPayload = n.abort, e.stepToken += 1, e.updatedAt = d(), e.cachedCallbacks = null, y(e, t), O(t, e, s, null, a, o), A(t, e, n.abort);
975
- else if ("invoke" in n) {
976
- if (o?.kind === "resume") {
977
- let n = L(t), i = e.resumeBouncesAtStep, a = i && i.stepToken === e.stepToken ? i.count + 1 : 1;
978
- if (a > n) {
979
- r && console.error(`[@modular-react/journeys] Resume on "${e.journeyId}.${s?.moduleId}.${s?.entry}" would bounce ${a} times in a row at the same step (cap ${n}). Aborting "${e.id}" to break the loop.`), P(t, e, /* @__PURE__ */ Error(`Resume bounce limit exceeded (${n}) at ${s?.moduleId}.${s?.entry}`), s, "resume"), H(e, t, { abort: {
980
- reason: "resume-bounce-limit",
981
- cap: n,
982
- count: a,
983
- resume: o.resume
984
- } }, null, {
985
- kind: "resume",
986
- outcome: o.outcome,
987
- resume: o.resume
988
- });
989
- return;
990
- }
991
- e.resumeBouncesAtStep = {
992
- stepToken: e.stepToken,
993
- count: a
994
- };
995
- }
996
- let i = z(e, t, n.invoke, a);
997
- if (!i) return;
998
- e.updatedAt = d(), O(t, e, s, s, a, {
999
- kind: "invoke",
1000
- child: {
1001
- instanceId: i.id,
1002
- journeyId: i.journeyId
1003
- }
1004
- });
1005
- }
1006
- let l = t.options?.persistence;
1007
- l && (e.status === "active" ? b(e, l) : S(e, l)), u(e), V(e);
1008
- }
1009
- function U(e) {
1010
- return g(e);
1011
- }
1012
- function W(e, t, n, i, a) {
1013
- if (e.status !== "active") {
1014
- r && console.warn(`[@modular-react/journeys] Exit("${i}") dropped on instance ${e.id} — status=${e.status}. (This is the expected no-op when an exit fires before the initial async load settles; await the load or subscribe for status changes before dispatching.)`);
1015
- return;
1016
- }
1017
- if (e.activeChildId) {
1018
- r && console.warn(`[@modular-react/journeys] Exit("${i}") dropped on instance ${e.id} — a child journey is in flight (activeChildId=${e.activeChildId}). The parent advances when the child resumes.`);
1019
- return;
1020
- }
1021
- if (e.stepToken !== n) {
1022
- r && console.warn(`[@modular-react/journeys] Stale exit("${i}") dropped on instance ${e.id}`);
1023
- return;
1024
- }
1025
- let o = e.step;
1026
- if (!o) return;
1027
- let s = t.definition.transitions?.[o.moduleId]?.[o.entry]?.[i];
1028
- if (typeof s != "function") {
1029
- r && console.warn(`[@modular-react/journeys] No transition for exit("${i}") on ${o.moduleId}.${o.entry} — ignoring.`);
1030
- return;
1031
- }
1032
- let c;
1033
- try {
1034
- c = s({
1035
- state: e.state,
1036
- input: o.input,
1037
- output: a
1038
- });
1039
- } catch (n) {
1040
- r && console.error("[@modular-react/journeys] transition handler threw", n), P(t, e, n, o), H(e, t, { abort: {
1041
- reason: "transition-error",
1042
- exit: i,
1043
- error: n
1044
- } }, i);
1045
- return;
1046
- }
1047
- if (c && typeof c.then == "function") {
1048
- r && console.error(`[@modular-react/journeys] Transition handler for ${o.moduleId}.${o.entry}."${i}" returned a Promise. Transitions must be synchronous and pure — put async work inside a loading entry point on a module.`), H(e, t, { abort: {
1049
- reason: "transition-returned-promise",
1050
- exit: i
1051
- } }, i);
1052
- return;
1053
- }
1054
- H(e, t, c, i);
1055
- }
1056
- function G(e, t, n) {
1057
- if (e.status !== "active" || e.activeChildId || e.stepToken !== n || e.history.length === 0) return;
1058
- let r = e.step;
1059
- if (!r || !_(t.definition, r)) return;
1060
- let i = e.history.pop(), a = e.rollbackSnapshots.pop();
1061
- g(r) === "rollback" && a !== void 0 && (e.state = a), e.hasRollbackSnapshot = e.rollbackSnapshots.some((e) => e !== void 0), e.step = i, e.resumeBouncesAtStep = null, e.stepToken += 1, e.updatedAt = d(), e.cachedCallbacks = null, O(t, e, r, i, null);
1062
- let o = t.options?.persistence;
1063
- o && b(e, o), u(e);
1064
- }
1065
- function ee(e, t) {
1066
- if (e.cachedCallbacks && e.cachedCallbacks.stepToken === e.stepToken) return e.cachedCallbacks;
1067
- let n = e.stepToken, r = (r, i) => {
1068
- W(e, t, n, r, i);
1069
- }, a = g(e.step);
1070
- return a === !1 && e.step && _(t.definition, e.step) && !i[e.step.moduleId] && (a = "preserve-state"), e.cachedCallbacks = {
1071
- stepToken: n,
1072
- exit: r,
1073
- goBack: a !== !1 && _(t.definition, e.step) && e.history.length > 0 ? () => {
1074
- G(e, t, n);
1075
- } : void 0
1076
- }, e.cachedCallbacks;
1077
- }
1078
- function te(e) {
1079
- if (e.cachedSnapshot && e.cachedSnapshot.revision === e.revision) return e.cachedSnapshot.instance;
1080
- let t = [...e.history], n = {
1081
- id: e.id,
1082
- journeyId: e.journeyId,
1083
- status: e.status,
1084
- step: e.step,
1085
- history: t,
1086
- state: e.state,
1087
- terminalPayload: e.status === "completed" || e.status === "aborted" ? e.terminalPayload : void 0,
1088
- startedAt: e.startedAt,
1089
- updatedAt: e.updatedAt,
1090
- activeChildId: e.activeChildId,
1091
- parent: e.parent ? { ...e.parent } : null,
1092
- serialize: () => D(e)
1093
- };
1094
- return e.cachedSnapshot = {
1095
- revision: e.revision,
1096
- instance: n
1097
- }, n;
1098
- }
1099
- function K(e, t, n, r) {
1100
- let i = d();
1101
- return {
1102
- id: t,
1103
- journeyId: e.definition.id,
1104
- status: "loading",
1105
- step: null,
1106
- history: [],
1107
- rollbackSnapshots: [],
1108
- hasRollbackSnapshot: !1,
1109
- state: r,
1110
- terminalPayload: void 0,
1111
- startedAt: i,
1112
- updatedAt: i,
1113
- stepToken: 0,
1114
- persistenceKey: n,
1115
- terminalFired: !1,
1116
- retryCount: 0,
1117
- listeners: /* @__PURE__ */ new Set(),
1118
- pendingSave: null,
1119
- saveInFlight: !1,
1120
- pendingRemove: !1,
1121
- revision: 0,
1122
- cachedSnapshot: null,
1123
- cachedCallbacks: null,
1124
- parent: null,
1125
- activeChildId: null,
1126
- resumeBouncesAtStep: null
1127
- };
1128
- }
1129
- function q(e, t, n) {
1130
- let r = e.definition, i = n ?? K(e, f(), null, r.initialState(t));
1131
- n ? (i.state = r.initialState(t), i.history = [], i.rollbackSnapshots = [], i.hasRollbackSnapshot = !1) : o.set(i.id, i);
1132
- let a = h(r.start(i.state, t));
1133
- i.step = a, i.status = "active", i.stepToken += 1, i.terminalFired = !1, i.terminalPayload = void 0, i.retryCount = 0, i.updatedAt = d(), i.cachedCallbacks = null, O(e, i, null, a, null);
1134
- let s = e.options?.persistence;
1135
- return s && b(i, s), u(i), i.id;
1136
- }
1137
- function J(e, t) {
1138
- let n = t.history.length;
1139
- if (t.rollbackSnapshots && t.rollbackSnapshots.length !== n) throw new C(`Blob for journey "${e.journeyId}" has rollbackSnapshots.length=${t.rollbackSnapshots.length} but history.length=${n}. Fix the persisted blob (pad rollbackSnapshots with null for non-rollback entries) or provide onHydrate to migrate.`);
1140
- e.state = t.state, e.step = t.step, e.history = [...t.history], t.rollbackSnapshots ? (e.rollbackSnapshots = t.rollbackSnapshots.map((e) => e === null ? void 0 : e), e.hasRollbackSnapshot = e.rollbackSnapshots.some((e) => e !== void 0)) : (e.rollbackSnapshots = Array.from({ length: n }, () => void 0), e.hasRollbackSnapshot = !1), e.status = t.status, e.terminalPayload = t.terminalPayload, e.startedAt = t.startedAt, e.updatedAt = t.updatedAt, e.stepToken += 1, e.terminalFired = t.status !== "active", e.cachedCallbacks = null, t.parentLink ? e.parent = {
1141
- instanceId: t.parentLink.parentInstanceId,
1142
- resumeName: t.parentLink.resumeName
1143
- } : e.parent = null, t.pendingInvoke ? e.activeChildId = t.pendingInvoke.childInstanceId : e.activeChildId = null, t.resumeBouncesAtStep && Number.isFinite(t.resumeBouncesAtStep.count) ? e.resumeBouncesAtStep = {
1144
- stepToken: e.stepToken,
1145
- count: Math.max(0, Math.floor(t.resumeBouncesAtStep.count))
1146
- } : e.resumeBouncesAtStep = null;
1147
- }
1148
- function Y() {
1149
- c.clear();
1150
- for (let e of o.values()) {
1151
- if (!e.parent) continue;
1152
- let t = o.get(e.parent.instanceId);
1153
- t && t.activeChildId === e.id && c.set(e.id, t.id);
1154
- }
1155
- }
1156
- function X(e, t) {
1157
- let n = t.pendingInvoke;
1158
- if (!n || o.has(n.childInstanceId) || !n.childPersistenceKey) return;
1159
- let i = a.get(n.childJourneyId);
1160
- if (!i) return;
1161
- let c = i.options?.persistence;
1162
- if (!c) return;
1163
- let d = l(n.childJourneyId, n.childPersistenceKey);
1164
- if (s.has(d)) return;
1165
- let f = Z(i, c, n.childPersistenceKey), p = (t) => {
1166
- if (!t || t.status !== "active" || o.has(n.childInstanceId) || s.has(d) || e.activeChildId !== n.childInstanceId) return;
1167
- let a = Q(i, t);
1168
- if (!a.ok) return;
1169
- let c = K(i, n.childInstanceId, n.childPersistenceKey, a.blob.state);
1170
- o.set(n.childInstanceId, c), s.set(d, n.childInstanceId);
1171
- try {
1172
- J(c, a.blob);
1173
- } catch (e) {
1174
- r && console.error("[@modular-react/journeys] auto-rehydrate child failed", e), o.delete(n.childInstanceId), s.delete(d);
1175
- return;
1176
- }
1177
- X(c, a.blob), Y(), u(c), u(e);
1178
- };
1179
- if (f && typeof f.then == "function") {
1180
- f.then((e) => p(e), (e) => {
1181
- r && console.error("[@modular-react/journeys] auto-rehydrate child persistence.load rejected", e);
1182
- });
1183
- return;
1184
- }
1185
- p(f);
1186
- }
1187
- function Z(e, t, n) {
1188
- let i;
1189
- try {
1190
- i = t.load(n);
1191
- } catch (e) {
1192
- return r && console.error("[@modular-react/journeys] persistence.load threw", e), null;
1193
- }
1194
- return i && i.then, i;
1195
- }
1196
- function Q(e, t) {
1197
- let n = t, i = !1;
1198
- if (e.definition.onHydrate) {
1199
- i = !0;
1200
- try {
1201
- n = e.definition.onHydrate(n);
1202
- } catch (e) {
1203
- return r && console.error("[@modular-react/journeys] onHydrate (definition) threw", e), {
1204
- ok: !1,
1205
- reason: "on-hydrate-threw",
1206
- cause: e
1207
- };
1208
- }
1209
- }
1210
- let a = e.options?.onHydrate;
1211
- if (a) {
1212
- i = !0;
1213
- try {
1214
- n = a(n);
1215
- } catch (e) {
1216
- return r && console.error("[@modular-react/journeys] onHydrate (registration) threw", e), {
1217
- ok: !1,
1218
- reason: "on-hydrate-threw",
1219
- cause: e
1220
- };
1221
- }
1222
- }
1223
- return i ? {
1224
- ok: !0,
1225
- blob: n
1226
- } : t.version === e.definition.version ? {
1227
- ok: !0,
1228
- blob: t
1229
- } : {
1230
- ok: !1,
1231
- reason: "version-mismatch"
1232
- };
1233
- }
1234
- let $ = {
1235
- start(e, ...t) {
1236
- let n = t.length > 0 ? t[0] : void 0, i = m(typeof e == "string" ? e : e.id), a = i.options?.persistence;
1237
- if (a) {
1238
- let e = a.keyFor({
1239
- journeyId: i.definition.id,
1240
- input: n
1241
- }), t = l(i.definition.id, e), c = s.get(t), d = c ? o.get(c) : null;
1242
- if (d && (d.status === "active" || d.status === "loading")) return d.id;
1243
- let p = i.definition, m = Z(i, a, e);
1244
- if (m && typeof m.then == "function") {
1245
- let c = f(), l = K(i, c, e, p.initialState(n));
1246
- return o.set(c, l), s.set(t, c), u(l), m.then((t) => {
1247
- if (l.status !== "loading") return;
1248
- if (!t || t.status !== "active") {
1249
- t && T(a, e), q(i, n, l);
1250
- return;
1251
- }
1252
- let o = Q(i, t);
1253
- if (!o.ok) {
1254
- T(a, e), q(i, n, l);
1255
- return;
1256
- }
1257
- try {
1258
- J(l, o.blob);
1259
- } catch (t) {
1260
- r && console.error("[@modular-react/journeys] hydrate after async load failed", t), T(a, e), q(i, n, l);
1261
- return;
1262
- }
1263
- X(l, o.blob), Y(), u(l);
1264
- }, (e) => {
1265
- r && console.error("[@modular-react/journeys] persistence.load rejected", e), l.status === "loading" && q(i, n, l);
1266
- }), c;
1267
- }
1268
- let h = m;
1269
- if (h && h.status === "active") {
1270
- let c = Q(i, h);
1271
- if (c.ok) {
1272
- let l = c.blob.instanceId && !o.has(c.blob.instanceId) ? c.blob.instanceId : f(), d = K(i, l, e, p.initialState(n));
1273
- o.set(l, d), s.set(t, l);
1274
- try {
1275
- J(d, c.blob);
1276
- } catch (c) {
1277
- r && console.error("[@modular-react/journeys] hydrate during start failed", c), o.delete(l), s.delete(t), T(a, e);
1278
- let u = f(), d = K(i, u, e, p.initialState(n));
1279
- return o.set(u, d), s.set(t, u), q(i, n, d);
1280
- }
1281
- return X(d, c.blob), Y(), u(d), l;
1282
- }
1283
- T(a, e);
1284
- } else h && T(a, e);
1285
- let g = f(), _ = K(i, g, e, p.initialState(n));
1286
- return o.set(g, _), s.set(t, g), q(i, n, _);
1287
- }
1288
- return q(i, n);
1289
- },
1290
- hydrate(e, t) {
1291
- let n = m(e), r = Q(n, t);
1292
- if (!r.ok) throw r.reason === "on-hydrate-threw" ? new C(`onHydrate threw while migrating blob for "${e}" (blob=${t.version} def=${n.definition.version}).`, { cause: r.cause }) : new C(`Hydrate version mismatch for "${e}": blob=${t.version} def=${n.definition.version}. Provide onHydrate to migrate.`);
1293
- let i = r.blob.instanceId || f();
1294
- if (o.has(i)) throw new C(`Cannot hydrate journey "${e}" with instance id "${i}" — an instance with the same id is already in memory. Call forget(id) first if you intend to replace it.`);
1295
- let a = K(n, i, null, r.blob.state);
1296
- o.set(i, a);
1297
- try {
1298
- J(a, r.blob);
1299
- } catch (e) {
1300
- throw o.delete(i), e;
1301
- }
1302
- return X(a, r.blob), Y(), u(a), i;
1303
- },
1304
- getInstance(e) {
1305
- let t = o.get(e);
1306
- return t ? te(t) : null;
1307
- },
1308
- listInstances() {
1309
- return [...o.keys()];
1310
- },
1311
- listDefinitions() {
1312
- return [...a.values()].map(p);
1313
- },
1314
- isRegistered(e) {
1315
- return a.has(e);
1316
- },
1317
- subscribe(e, t) {
1318
- let n = o.get(e);
1319
- return n ? (n.listeners.add(t), () => {
1320
- n.listeners.delete(t);
1321
- }) : () => {};
1322
- },
1323
- end(e, t) {
1324
- let n = o.get(e);
1325
- if (!n || n.status === "completed" || n.status === "aborted") return;
1326
- let i = a.get(n.journeyId);
1327
- if (!i) return;
1328
- if (n.activeChildId) {
1329
- let e = n.activeChildId;
1330
- n.activeChildId = null;
1331
- let r = o.get(e);
1332
- r && r.parent && r.parent.instanceId === n.id && (r.parent = null), c.delete(e), $.end(e, {
1333
- reason: "parent-ended",
1334
- parentId: n.id,
1335
- cause: t
1336
- });
1337
- }
1338
- if (n.status === "loading") {
1339
- H(n, i, { abort: t ?? "abandoned" }, null);
1340
- return;
1341
- }
1342
- let s = { abort: t ?? "abandoned" }, l = i.options?.onAbandon ?? i.definition.onAbandon;
1343
- if (l) try {
1344
- s = l({
1345
- journeyId: n.journeyId,
1346
- instanceId: n.id,
1347
- step: n.step,
1348
- state: n.state,
1349
- reason: t ?? "abandoned"
1350
- });
1351
- } catch (e) {
1352
- r && console.error("[@modular-react/journeys] onAbandon threw", e), P(i, n, e, n.step, "abandon"), s = { abort: {
1353
- reason: t ?? "abandoned",
1354
- cause: "onAbandon-threw",
1355
- error: e
1356
- } };
1357
- }
1358
- H(n, i, s, null);
1359
- },
1360
- forget(e) {
1361
- let t = o.get(e);
1362
- if (t && !(t.status !== "completed" && t.status !== "aborted")) {
1363
- if (t.persistenceKey && s.delete(l(t.journeyId, t.persistenceKey)), c.delete(e), t.activeChildId) {
1364
- let e = o.get(t.activeChildId);
1365
- e && e.parent && e.parent.instanceId === t.id && (e.parent = null, u(e)), c.delete(t.activeChildId), t.activeChildId = null;
1366
- }
1367
- t.listeners.clear(), o.delete(e);
1368
- }
1369
- },
1370
- forgetTerminal() {
1371
- let e = 0;
1372
- for (let [t, n] of o) if (n.status === "completed" || n.status === "aborted") {
1373
- if (n.persistenceKey && s.delete(l(n.journeyId, n.persistenceKey)), c.delete(t), n.activeChildId) {
1374
- let e = o.get(n.activeChildId);
1375
- e && e.parent && e.parent.instanceId === n.id && (e.parent = null, u(e)), c.delete(n.activeChildId);
1376
- }
1377
- n.listeners.clear(), o.delete(t), e += 1;
1378
- }
1379
- return e;
1380
- }
1381
- };
1382
- function ne(e, t, n) {
1383
- let r = o.get(e);
1384
- if (!r) return;
1385
- let i = a.get(r.journeyId);
1386
- i && P(i, r, t, n, "step");
1387
- }
1388
- let re = {
1389
- __bindStepCallbacks: ee,
1390
- __getRecord: (e) => o.get(e),
1391
- __getRegistered: (e) => a.get(e),
1392
- __moduleMap: i,
1393
- __debug: r,
1394
- __fireComponentError: ne,
1395
- __synthesizeCompletion: (e, t) => {
1396
- let n = o.get(e);
1397
- if (!n) return;
1398
- let r = a.get(n.journeyId);
1399
- r && H(n, r, { complete: t }, null);
1400
- },
1401
- __synthesizeAbort: (e, t) => {
1402
- let n = o.get(e);
1403
- if (!n) return;
1404
- let r = a.get(n.journeyId);
1405
- r && H(n, r, { abort: t }, null);
1406
- },
1407
- __consumeRetry: (e, t) => {
1408
- let n = o.get(e);
1409
- return !n || n.retryCount >= t ? !1 : (n.retryCount += 1, !0);
1410
- }
1411
- };
1412
- return N.set($, re), $;
1413
- }
1414
- function F(e) {
1415
- let t = N.get(e);
1416
- if (!t) throw Error("[@modular-react/journeys] getInternals() called on a runtime that was not produced by createJourneyRuntime().");
1417
- return t;
1418
- }
1419
- //#endregion
1420
- export { w as a, E as c, o as d, S as i, n as l, F as n, T as o, C as r, A as s, P as t, c as u };
1421
-
1422
- //# sourceMappingURL=runtime-BUVl0_Ad.js.map