@modular-react/journeys 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1669 -0
- package/dist/index.d.ts +754 -0
- package/dist/index.js +303 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime-DyU_PmaC.js +599 -0
- package/dist/runtime-DyU_PmaC.js.map +1 -0
- package/dist/testing.d.ts +121 -0
- package/dist/testing.js +102 -0
- package/dist/testing.js.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
//#region src/validation.ts
|
|
2
|
+
var e = class extends Error {
|
|
3
|
+
issues;
|
|
4
|
+
constructor(e) {
|
|
5
|
+
super(`[@modular-react/journeys] Invalid journey registration:\n - ${e.join("\n - ")}`), this.name = "JourneyValidationError", this.issues = e;
|
|
6
|
+
}
|
|
7
|
+
}, t = class extends Error {
|
|
8
|
+
constructor(e, t) {
|
|
9
|
+
super(`[@modular-react/journeys] ${e}`, t), this.name = "JourneyHydrationError";
|
|
10
|
+
}
|
|
11
|
+
}, n = class extends Error {
|
|
12
|
+
journeyId;
|
|
13
|
+
constructor(e, t) {
|
|
14
|
+
super(`[@modular-react/journeys] Unknown journey id "${e}". Registered: ${t.join(", ") || "(none)"}`), this.name = "UnknownJourneyError", this.journeyId = e;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
function r(t, n) {
|
|
18
|
+
let r = [], i = /* @__PURE__ */ new Map();
|
|
19
|
+
for (let e of n) i.set(e.id, e);
|
|
20
|
+
for (let e of n) e.exitPoints && Object.prototype.hasOwnProperty.call(e.exitPoints, "allowBack") && r.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").`);
|
|
21
|
+
let a = /* @__PURE__ */ new Set();
|
|
22
|
+
for (let e of t) {
|
|
23
|
+
let t = e.definition;
|
|
24
|
+
a.has(t.id) && r.push(`journey "${t.id}" is registered more than once`), a.add(t.id);
|
|
25
|
+
let n = t.transitions ?? {};
|
|
26
|
+
for (let [e, a] of Object.entries(n)) {
|
|
27
|
+
let n = i.get(e);
|
|
28
|
+
if (!n) {
|
|
29
|
+
r.push(`journey "${t.id}" references unknown module id "${e}" in transitions`);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (!a || typeof a != "object") {
|
|
33
|
+
r.push(`journey "${t.id}" has malformed transitions for module "${e}" (expected an object)`);
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
for (let [i, o] of Object.entries(a)) {
|
|
37
|
+
let a = n.entryPoints?.[i];
|
|
38
|
+
if (!a) {
|
|
39
|
+
r.push(`journey "${t.id}" references unknown entry "${e}.${i}"`);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (!o || typeof o != "object") {
|
|
43
|
+
r.push(`journey "${t.id}" has malformed transitions for entry "${e}.${i}" (expected an object)`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
let s = o;
|
|
47
|
+
for (let a of Object.keys(s)) a !== "allowBack" && (!n.exitPoints || !(a in n.exitPoints)) && r.push(`journey "${t.id}" references unknown exit "${e}.${i}.${a}"`);
|
|
48
|
+
if (s.allowBack === !0) {
|
|
49
|
+
let n = a.allowBack;
|
|
50
|
+
n !== "preserve-state" && n !== "rollback" && r.push(`journey "${t.id}" sets allowBack on "${e}.${i}" but the module entry does not declare allowBack`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (r.length > 0) throw new e(r);
|
|
56
|
+
}
|
|
57
|
+
function i(e) {
|
|
58
|
+
let t = [];
|
|
59
|
+
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;
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/runtime.ts
|
|
63
|
+
var a = /* @__PURE__ */ new WeakMap();
|
|
64
|
+
function o(e, r = {}) {
|
|
65
|
+
let i = r.debug ?? s(), o = r.modules ?? {}, c = /* @__PURE__ */ new Map();
|
|
66
|
+
for (let t of e) c.set(t.definition.id, t);
|
|
67
|
+
let l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map();
|
|
68
|
+
function d(e, t) {
|
|
69
|
+
return `${e}::${t}`;
|
|
70
|
+
}
|
|
71
|
+
function f(e) {
|
|
72
|
+
e.revision += 1, e.cachedSnapshot = null;
|
|
73
|
+
for (let t of e.listeners) try {
|
|
74
|
+
t();
|
|
75
|
+
} catch (e) {
|
|
76
|
+
i && console.error("[@modular-react/journeys] listener threw", e);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function p() {
|
|
80
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
81
|
+
}
|
|
82
|
+
function m() {
|
|
83
|
+
try {
|
|
84
|
+
let e = globalThis.crypto;
|
|
85
|
+
if (e?.randomUUID) return `ji_${e.randomUUID()}`;
|
|
86
|
+
} catch {}
|
|
87
|
+
let e = Math.random().toString(36).slice(2, 10);
|
|
88
|
+
return `ji_${Date.now().toString(36)}_${e}`;
|
|
89
|
+
}
|
|
90
|
+
function h(e) {
|
|
91
|
+
return {
|
|
92
|
+
id: e.definition.id,
|
|
93
|
+
version: e.definition.version,
|
|
94
|
+
meta: e.definition.meta
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function g(e) {
|
|
98
|
+
let t = c.get(e);
|
|
99
|
+
if (!t) throw new n(e, [...c.keys()]);
|
|
100
|
+
return t;
|
|
101
|
+
}
|
|
102
|
+
function _(e) {
|
|
103
|
+
return {
|
|
104
|
+
moduleId: e.module,
|
|
105
|
+
entry: e.entry,
|
|
106
|
+
input: e.input
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function v(e) {
|
|
110
|
+
if (!e) return !1;
|
|
111
|
+
let t = o[e.moduleId]?.entryPoints?.[e.entry]?.allowBack;
|
|
112
|
+
return t === "rollback" || t === "preserve-state" ? t : !1;
|
|
113
|
+
}
|
|
114
|
+
function y(e, t) {
|
|
115
|
+
return t ? e.transitions?.[t.moduleId]?.[t.entry]?.allowBack === !0 : !1;
|
|
116
|
+
}
|
|
117
|
+
function b(e) {
|
|
118
|
+
if (typeof e != "object" || !e) return e;
|
|
119
|
+
let t = Array.isArray(e) ? [...e] : { ...e };
|
|
120
|
+
if (i) try {
|
|
121
|
+
Object.freeze(t);
|
|
122
|
+
} catch {}
|
|
123
|
+
return t;
|
|
124
|
+
}
|
|
125
|
+
function x(e, t) {
|
|
126
|
+
let n = t.options?.maxHistory;
|
|
127
|
+
if (!(n === void 0 || n <= 0)) {
|
|
128
|
+
for (; e.history.length > n;) e.history.shift(), e.rollbackSnapshots.shift();
|
|
129
|
+
e.hasRollbackSnapshot = e.rollbackSnapshots.some((e) => e !== void 0);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function S(e, t) {
|
|
133
|
+
let n = D(e);
|
|
134
|
+
if (e.saveInFlight) {
|
|
135
|
+
e.pendingSave = n;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
C(e, t, n);
|
|
139
|
+
}
|
|
140
|
+
async function C(e, t, n) {
|
|
141
|
+
e.saveInFlight = !0;
|
|
142
|
+
try {
|
|
143
|
+
if (!e.persistenceKey) return;
|
|
144
|
+
await t.save(e.persistenceKey, n);
|
|
145
|
+
} catch (t) {
|
|
146
|
+
i && console.error(`[@modular-react/journeys] Failed to persist "${e.journeyId}" instance ${e.id}`, t);
|
|
147
|
+
} finally {
|
|
148
|
+
if (e.saveInFlight = !1, e.pendingRemove) e.pendingRemove = !1, e.pendingSave = null, e.persistenceKey && E(t, e.persistenceKey);
|
|
149
|
+
else if (e.pendingSave) {
|
|
150
|
+
let n = e.pendingSave;
|
|
151
|
+
e.pendingSave = null, C(e, t, n);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function w(e, t) {
|
|
156
|
+
if (!e.persistenceKey) return;
|
|
157
|
+
e.pendingSave = null;
|
|
158
|
+
let n = e.persistenceKey;
|
|
159
|
+
if (u.delete(d(e.journeyId, n)), e.saveInFlight) {
|
|
160
|
+
e.pendingRemove = !0;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
E(t, n);
|
|
164
|
+
}
|
|
165
|
+
function T(e, t) {
|
|
166
|
+
E(e, t);
|
|
167
|
+
}
|
|
168
|
+
function E(e, t) {
|
|
169
|
+
try {
|
|
170
|
+
let n = e.remove(t);
|
|
171
|
+
n && typeof n.catch == "function" && n.catch((e) => {
|
|
172
|
+
i && console.error("[@modular-react/journeys] persistence.remove rejected", e);
|
|
173
|
+
});
|
|
174
|
+
} catch (e) {
|
|
175
|
+
i && console.error("[@modular-react/journeys] persistence.remove threw", e);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function D(e) {
|
|
179
|
+
return {
|
|
180
|
+
definitionId: e.journeyId,
|
|
181
|
+
version: c.get(e.journeyId).definition.version,
|
|
182
|
+
instanceId: e.id,
|
|
183
|
+
status: e.status === "loading" ? "active" : e.status,
|
|
184
|
+
step: e.step,
|
|
185
|
+
history: [...e.history],
|
|
186
|
+
rollbackSnapshots: e.hasRollbackSnapshot ? e.rollbackSnapshots.map((e) => e === void 0 ? null : e) : void 0,
|
|
187
|
+
terminalPayload: e.status === "completed" || e.status === "aborted" ? e.terminalPayload : void 0,
|
|
188
|
+
state: e.state,
|
|
189
|
+
startedAt: e.startedAt,
|
|
190
|
+
updatedAt: e.updatedAt
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function O(e, t, n, r, a) {
|
|
194
|
+
let o = {
|
|
195
|
+
journeyId: t.journeyId,
|
|
196
|
+
instanceId: t.id,
|
|
197
|
+
from: n,
|
|
198
|
+
to: r,
|
|
199
|
+
exit: a,
|
|
200
|
+
state: t.state,
|
|
201
|
+
history: [...t.history]
|
|
202
|
+
};
|
|
203
|
+
try {
|
|
204
|
+
e.definition.onTransition?.(o);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
i && console.error("[@modular-react/journeys] onTransition (definition) threw", e);
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
e.options?.onTransition?.(o);
|
|
210
|
+
} catch (e) {
|
|
211
|
+
i && console.error("[@modular-react/journeys] onTransition (registration) threw", e);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function k(e, t, n) {
|
|
215
|
+
if (t.terminalFired) return;
|
|
216
|
+
t.terminalFired = !0;
|
|
217
|
+
let r = {
|
|
218
|
+
journeyId: t.journeyId,
|
|
219
|
+
instanceId: t.id,
|
|
220
|
+
state: t.state,
|
|
221
|
+
history: t.history
|
|
222
|
+
};
|
|
223
|
+
try {
|
|
224
|
+
e.definition.onComplete?.(r, n);
|
|
225
|
+
} catch (e) {
|
|
226
|
+
i && console.error("[@modular-react/journeys] onComplete (definition) threw", e);
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
e.options?.onComplete?.(r, n);
|
|
230
|
+
} catch (e) {
|
|
231
|
+
i && console.error("[@modular-react/journeys] onComplete (registration) threw", e);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function A(e, t, n) {
|
|
235
|
+
if (t.terminalFired) return;
|
|
236
|
+
t.terminalFired = !0;
|
|
237
|
+
let r = {
|
|
238
|
+
journeyId: t.journeyId,
|
|
239
|
+
instanceId: t.id,
|
|
240
|
+
state: t.state,
|
|
241
|
+
history: t.history
|
|
242
|
+
};
|
|
243
|
+
try {
|
|
244
|
+
e.definition.onAbort?.(r, n);
|
|
245
|
+
} catch (e) {
|
|
246
|
+
i && console.error("[@modular-react/journeys] onAbort (definition) threw", e);
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
e.options?.onAbort?.(r, n);
|
|
250
|
+
} catch (e) {
|
|
251
|
+
i && console.error("[@modular-react/journeys] onAbort (registration) threw", e);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function j(e, t, n, r) {
|
|
255
|
+
try {
|
|
256
|
+
e.options?.onError?.(n, { step: r });
|
|
257
|
+
} catch (e) {
|
|
258
|
+
i && console.error("[@modular-react/journeys] onError (registration) threw", e);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function M(e, t, n, r) {
|
|
262
|
+
let a = e.step, s = e.state;
|
|
263
|
+
if ("state" in n && (e.state = n.state), "next" in n) {
|
|
264
|
+
let c = _(n.next);
|
|
265
|
+
if (i && Object.keys(o).length > 0) {
|
|
266
|
+
let e = o[c.moduleId];
|
|
267
|
+
e ? e.entryPoints?.[c.entry] || console.warn(`[@modular-react/journeys] Transition on "${a?.moduleId}.${a?.entry}" returned next.entry="${c.moduleId}.${c.entry}" which is not a declared entry on that module.`) : console.warn(`[@modular-react/journeys] Transition on "${a?.moduleId}.${a?.entry}" returned next.module="${c.moduleId}" which is not in the runtime's module map — the outlet will render a "no entry" error.`);
|
|
268
|
+
}
|
|
269
|
+
a && (e.history.push(a), N(c) === "rollback" ? (e.rollbackSnapshots.push(b(s)), e.hasRollbackSnapshot = !0) : e.rollbackSnapshots.push(void 0)), e.step = c, e.status = "active", e.stepToken += 1, e.updatedAt = p(), e.cachedCallbacks = null, x(e, t), O(t, e, a, c, r);
|
|
270
|
+
} else "complete" in n ? (a && (e.history.push(a), e.rollbackSnapshots.push(void 0)), e.step = null, e.status = "completed", e.terminalPayload = n.complete, e.stepToken += 1, e.updatedAt = p(), e.cachedCallbacks = null, x(e, t), O(t, e, a, null, r), k(t, e, n.complete)) : "abort" in n && (a && (e.history.push(a), e.rollbackSnapshots.push(void 0)), e.step = null, e.status = "aborted", e.terminalPayload = n.abort, e.stepToken += 1, e.updatedAt = p(), e.cachedCallbacks = null, x(e, t), O(t, e, a, null, r), A(t, e, n.abort));
|
|
271
|
+
let c = t.options?.persistence;
|
|
272
|
+
c && (e.status === "active" ? S(e, c) : w(e, c)), f(e);
|
|
273
|
+
}
|
|
274
|
+
function N(e) {
|
|
275
|
+
return v(e);
|
|
276
|
+
}
|
|
277
|
+
function P(e, t, n, r, a) {
|
|
278
|
+
if (e.status !== "active") {
|
|
279
|
+
i && console.warn(`[@modular-react/journeys] Exit("${r}") 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.)`);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (e.stepToken !== n) {
|
|
283
|
+
i && console.warn(`[@modular-react/journeys] Stale exit("${r}") dropped on instance ${e.id}`);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
let o = e.step;
|
|
287
|
+
if (!o) return;
|
|
288
|
+
let s = t.definition.transitions?.[o.moduleId]?.[o.entry]?.[r];
|
|
289
|
+
if (typeof s != "function") {
|
|
290
|
+
i && console.warn(`[@modular-react/journeys] No transition for exit("${r}") on ${o.moduleId}.${o.entry} — ignoring.`);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
let c;
|
|
294
|
+
try {
|
|
295
|
+
c = s({
|
|
296
|
+
state: e.state,
|
|
297
|
+
input: o.input,
|
|
298
|
+
output: a
|
|
299
|
+
});
|
|
300
|
+
} catch (n) {
|
|
301
|
+
i && console.error("[@modular-react/journeys] transition handler threw", n), j(t, e, n, o), M(e, t, { abort: {
|
|
302
|
+
reason: "transition-error",
|
|
303
|
+
exit: r,
|
|
304
|
+
error: n
|
|
305
|
+
} }, r);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (c && typeof c.then == "function") {
|
|
309
|
+
i && console.error(`[@modular-react/journeys] Transition handler for ${o.moduleId}.${o.entry}."${r}" returned a Promise. Transitions must be synchronous and pure — put async work inside a loading entry point on a module.`), M(e, t, { abort: {
|
|
310
|
+
reason: "transition-returned-promise",
|
|
311
|
+
exit: r
|
|
312
|
+
} }, r);
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
M(e, t, c, r);
|
|
316
|
+
}
|
|
317
|
+
function F(e, t, n) {
|
|
318
|
+
if (e.status !== "active" || e.stepToken !== n || e.history.length === 0) return;
|
|
319
|
+
let r = e.step;
|
|
320
|
+
if (!r || !y(t.definition, r)) return;
|
|
321
|
+
let i = e.history.pop(), a = e.rollbackSnapshots.pop();
|
|
322
|
+
v(r) === "rollback" && a !== void 0 && (e.state = a), e.hasRollbackSnapshot = e.rollbackSnapshots.some((e) => e !== void 0), e.step = i, e.stepToken += 1, e.updatedAt = p(), e.cachedCallbacks = null, O(t, e, r, i, null);
|
|
323
|
+
let o = t.options?.persistence;
|
|
324
|
+
o && S(e, o), f(e);
|
|
325
|
+
}
|
|
326
|
+
function I(e, t) {
|
|
327
|
+
if (e.cachedCallbacks && e.cachedCallbacks.stepToken === e.stepToken) return e.cachedCallbacks;
|
|
328
|
+
let n = e.stepToken, r = (r, i) => {
|
|
329
|
+
P(e, t, n, r, i);
|
|
330
|
+
}, i = v(e.step);
|
|
331
|
+
return i === !1 && e.step && y(t.definition, e.step) && !o[e.step.moduleId] && (i = "preserve-state"), e.cachedCallbacks = {
|
|
332
|
+
stepToken: n,
|
|
333
|
+
exit: r,
|
|
334
|
+
goBack: i !== !1 && y(t.definition, e.step) && e.history.length > 0 ? () => {
|
|
335
|
+
F(e, t, n);
|
|
336
|
+
} : void 0
|
|
337
|
+
}, e.cachedCallbacks;
|
|
338
|
+
}
|
|
339
|
+
function L(e) {
|
|
340
|
+
if (e.cachedSnapshot && e.cachedSnapshot.revision === e.revision) return e.cachedSnapshot.instance;
|
|
341
|
+
let t = [...e.history], n = {
|
|
342
|
+
id: e.id,
|
|
343
|
+
journeyId: e.journeyId,
|
|
344
|
+
status: e.status,
|
|
345
|
+
step: e.step,
|
|
346
|
+
history: t,
|
|
347
|
+
state: e.state,
|
|
348
|
+
terminalPayload: e.status === "completed" || e.status === "aborted" ? e.terminalPayload : void 0,
|
|
349
|
+
startedAt: e.startedAt,
|
|
350
|
+
updatedAt: e.updatedAt,
|
|
351
|
+
serialize: () => D(e)
|
|
352
|
+
};
|
|
353
|
+
return e.cachedSnapshot = {
|
|
354
|
+
revision: e.revision,
|
|
355
|
+
instance: n
|
|
356
|
+
}, n;
|
|
357
|
+
}
|
|
358
|
+
function R(e, t, n, r) {
|
|
359
|
+
let i = p();
|
|
360
|
+
return {
|
|
361
|
+
id: t,
|
|
362
|
+
journeyId: e.definition.id,
|
|
363
|
+
status: "loading",
|
|
364
|
+
step: null,
|
|
365
|
+
history: [],
|
|
366
|
+
rollbackSnapshots: [],
|
|
367
|
+
hasRollbackSnapshot: !1,
|
|
368
|
+
state: r,
|
|
369
|
+
terminalPayload: void 0,
|
|
370
|
+
startedAt: i,
|
|
371
|
+
updatedAt: i,
|
|
372
|
+
stepToken: 0,
|
|
373
|
+
persistenceKey: n,
|
|
374
|
+
terminalFired: !1,
|
|
375
|
+
retryCount: 0,
|
|
376
|
+
listeners: /* @__PURE__ */ new Set(),
|
|
377
|
+
pendingSave: null,
|
|
378
|
+
saveInFlight: !1,
|
|
379
|
+
pendingRemove: !1,
|
|
380
|
+
revision: 0,
|
|
381
|
+
cachedSnapshot: null,
|
|
382
|
+
cachedCallbacks: null
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
function z(e, t, n) {
|
|
386
|
+
let r = e.definition, i = n ?? R(e, m(), null, r.initialState(t));
|
|
387
|
+
n ? (i.state = r.initialState(t), i.history = [], i.rollbackSnapshots = [], i.hasRollbackSnapshot = !1) : l.set(i.id, i);
|
|
388
|
+
let a = _(r.start(i.state, t));
|
|
389
|
+
i.step = a, i.status = "active", i.stepToken += 1, i.terminalFired = !1, i.terminalPayload = void 0, i.retryCount = 0, i.updatedAt = p(), i.cachedCallbacks = null, O(e, i, null, a, null);
|
|
390
|
+
let o = e.options?.persistence;
|
|
391
|
+
return o && S(i, o), f(i), i.id;
|
|
392
|
+
}
|
|
393
|
+
function B(e, n) {
|
|
394
|
+
let r = n.history.length;
|
|
395
|
+
if (n.rollbackSnapshots && n.rollbackSnapshots.length !== r) throw new t(`Blob for journey "${e.journeyId}" has rollbackSnapshots.length=${n.rollbackSnapshots.length} but history.length=${r}. Fix the persisted blob (pad rollbackSnapshots with null for non-rollback entries) or provide onHydrate to migrate.`);
|
|
396
|
+
e.state = n.state, e.step = n.step, e.history = [...n.history], n.rollbackSnapshots ? (e.rollbackSnapshots = n.rollbackSnapshots.map((e) => e === null ? void 0 : e), e.hasRollbackSnapshot = e.rollbackSnapshots.some((e) => e !== void 0)) : (e.rollbackSnapshots = Array.from({ length: r }, () => void 0), e.hasRollbackSnapshot = !1), e.status = n.status, e.terminalPayload = n.terminalPayload, e.startedAt = n.startedAt, e.updatedAt = n.updatedAt, e.stepToken += 1, e.terminalFired = n.status !== "active", e.cachedCallbacks = null;
|
|
397
|
+
}
|
|
398
|
+
function V(e, t, n) {
|
|
399
|
+
let r;
|
|
400
|
+
try {
|
|
401
|
+
r = t.load(n);
|
|
402
|
+
} catch (e) {
|
|
403
|
+
return i && console.error("[@modular-react/journeys] persistence.load threw", e), null;
|
|
404
|
+
}
|
|
405
|
+
return r && r.then, r;
|
|
406
|
+
}
|
|
407
|
+
function H(e, t) {
|
|
408
|
+
let n = t, r = !1;
|
|
409
|
+
if (e.definition.onHydrate) {
|
|
410
|
+
r = !0;
|
|
411
|
+
try {
|
|
412
|
+
n = e.definition.onHydrate(n);
|
|
413
|
+
} catch (e) {
|
|
414
|
+
return i && console.error("[@modular-react/journeys] onHydrate (definition) threw", e), {
|
|
415
|
+
ok: !1,
|
|
416
|
+
reason: "on-hydrate-threw",
|
|
417
|
+
cause: e
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
let a = e.options?.onHydrate;
|
|
422
|
+
if (a) {
|
|
423
|
+
r = !0;
|
|
424
|
+
try {
|
|
425
|
+
n = a(n);
|
|
426
|
+
} catch (e) {
|
|
427
|
+
return i && console.error("[@modular-react/journeys] onHydrate (registration) threw", e), {
|
|
428
|
+
ok: !1,
|
|
429
|
+
reason: "on-hydrate-threw",
|
|
430
|
+
cause: e
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return r ? {
|
|
435
|
+
ok: !0,
|
|
436
|
+
blob: n
|
|
437
|
+
} : t.version === e.definition.version ? {
|
|
438
|
+
ok: !0,
|
|
439
|
+
blob: t
|
|
440
|
+
} : {
|
|
441
|
+
ok: !1,
|
|
442
|
+
reason: "version-mismatch"
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
let U = {
|
|
446
|
+
start(e, ...t) {
|
|
447
|
+
let n = t.length > 0 ? t[0] : void 0, r = g(typeof e == "string" ? e : e.id), a = r.options?.persistence;
|
|
448
|
+
if (a) {
|
|
449
|
+
let e = a.keyFor({
|
|
450
|
+
journeyId: r.definition.id,
|
|
451
|
+
input: n
|
|
452
|
+
}), t = d(r.definition.id, e), o = u.get(t), s = o ? l.get(o) : null;
|
|
453
|
+
if (s && (s.status === "active" || s.status === "loading")) return s.id;
|
|
454
|
+
let c = r.definition, p = V(r, a, e);
|
|
455
|
+
if (p && typeof p.then == "function") {
|
|
456
|
+
let o = m(), s = R(r, o, e, c.initialState(n));
|
|
457
|
+
return l.set(o, s), u.set(t, o), f(s), p.then((t) => {
|
|
458
|
+
if (s.status !== "loading") return;
|
|
459
|
+
if (!t || t.status !== "active") {
|
|
460
|
+
t && T(a, e), z(r, n, s);
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
let o = H(r, t);
|
|
464
|
+
if (!o.ok) {
|
|
465
|
+
T(a, e), z(r, n, s);
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
try {
|
|
469
|
+
B(s, o.blob);
|
|
470
|
+
} catch (t) {
|
|
471
|
+
i && console.error("[@modular-react/journeys] hydrate after async load failed", t), T(a, e), z(r, n, s);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
f(s);
|
|
475
|
+
}, (e) => {
|
|
476
|
+
i && console.error("[@modular-react/journeys] persistence.load rejected", e), s.status === "loading" && z(r, n, s);
|
|
477
|
+
}), o;
|
|
478
|
+
}
|
|
479
|
+
let h = p;
|
|
480
|
+
if (h && h.status === "active") {
|
|
481
|
+
let o = H(r, h);
|
|
482
|
+
if (o.ok) {
|
|
483
|
+
let s = o.blob.instanceId && !l.has(o.blob.instanceId) ? o.blob.instanceId : m(), d = R(r, s, e, c.initialState(n));
|
|
484
|
+
l.set(s, d), u.set(t, s);
|
|
485
|
+
try {
|
|
486
|
+
B(d, o.blob);
|
|
487
|
+
} catch (o) {
|
|
488
|
+
i && console.error("[@modular-react/journeys] hydrate during start failed", o), l.delete(s), u.delete(t), T(a, e);
|
|
489
|
+
let d = m(), f = R(r, d, e, c.initialState(n));
|
|
490
|
+
return l.set(d, f), u.set(t, d), z(r, n, f);
|
|
491
|
+
}
|
|
492
|
+
return f(d), s;
|
|
493
|
+
}
|
|
494
|
+
T(a, e);
|
|
495
|
+
} else h && T(a, e);
|
|
496
|
+
let g = m(), _ = R(r, g, e, c.initialState(n));
|
|
497
|
+
return l.set(g, _), u.set(t, g), z(r, n, _);
|
|
498
|
+
}
|
|
499
|
+
return z(r, n);
|
|
500
|
+
},
|
|
501
|
+
hydrate(e, n) {
|
|
502
|
+
let r = g(e), i = H(r, n);
|
|
503
|
+
if (!i.ok) throw i.reason === "on-hydrate-threw" ? new t(`onHydrate threw while migrating blob for "${e}" (blob=${n.version} def=${r.definition.version}).`, { cause: i.cause }) : new t(`Hydrate version mismatch for "${e}": blob=${n.version} def=${r.definition.version}. Provide onHydrate to migrate.`);
|
|
504
|
+
let a = i.blob.instanceId || m();
|
|
505
|
+
if (l.has(a)) throw new t(`Cannot hydrate journey "${e}" with instance id "${a}" — an instance with the same id is already in memory. Call forget(id) first if you intend to replace it.`);
|
|
506
|
+
let o = R(r, a, null, i.blob.state);
|
|
507
|
+
l.set(a, o);
|
|
508
|
+
try {
|
|
509
|
+
B(o, i.blob);
|
|
510
|
+
} catch (e) {
|
|
511
|
+
throw l.delete(a), e;
|
|
512
|
+
}
|
|
513
|
+
return f(o), a;
|
|
514
|
+
},
|
|
515
|
+
getInstance(e) {
|
|
516
|
+
let t = l.get(e);
|
|
517
|
+
return t ? L(t) : null;
|
|
518
|
+
},
|
|
519
|
+
listInstances() {
|
|
520
|
+
return [...l.keys()];
|
|
521
|
+
},
|
|
522
|
+
listDefinitions() {
|
|
523
|
+
return [...c.values()].map(h);
|
|
524
|
+
},
|
|
525
|
+
isRegistered(e) {
|
|
526
|
+
return c.has(e);
|
|
527
|
+
},
|
|
528
|
+
subscribe(e, t) {
|
|
529
|
+
let n = l.get(e);
|
|
530
|
+
return n ? (n.listeners.add(t), () => {
|
|
531
|
+
n.listeners.delete(t);
|
|
532
|
+
}) : () => {};
|
|
533
|
+
},
|
|
534
|
+
end(e, t) {
|
|
535
|
+
let n = l.get(e);
|
|
536
|
+
if (!n || n.status === "completed" || n.status === "aborted") return;
|
|
537
|
+
let r = c.get(n.journeyId);
|
|
538
|
+
if (!r) return;
|
|
539
|
+
if (n.status === "loading") {
|
|
540
|
+
M(n, r, { abort: { reason: t ?? "abandoned" } }, null);
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
let a = { abort: { reason: t ?? "abandoned" } }, o = r.options?.onAbandon ?? r.definition.onAbandon;
|
|
544
|
+
if (o) try {
|
|
545
|
+
a = o({
|
|
546
|
+
journeyId: n.journeyId,
|
|
547
|
+
instanceId: n.id,
|
|
548
|
+
step: n.step,
|
|
549
|
+
state: n.state,
|
|
550
|
+
reason: t ?? "abandoned"
|
|
551
|
+
});
|
|
552
|
+
} catch (e) {
|
|
553
|
+
i && console.error("[@modular-react/journeys] onAbandon threw", e), j(r, n, e, n.step), a = { abort: {
|
|
554
|
+
reason: t ?? "abandoned",
|
|
555
|
+
cause: "onAbandon-threw",
|
|
556
|
+
error: e
|
|
557
|
+
} };
|
|
558
|
+
}
|
|
559
|
+
M(n, r, a, null);
|
|
560
|
+
},
|
|
561
|
+
forget(e) {
|
|
562
|
+
let t = l.get(e);
|
|
563
|
+
t && (t.status !== "completed" && t.status !== "aborted" || (t.persistenceKey && u.delete(d(t.journeyId, t.persistenceKey)), t.listeners.clear(), l.delete(e)));
|
|
564
|
+
},
|
|
565
|
+
forgetTerminal() {
|
|
566
|
+
let e = 0;
|
|
567
|
+
for (let [t, n] of l) (n.status === "completed" || n.status === "aborted") && (n.persistenceKey && u.delete(d(n.journeyId, n.persistenceKey)), n.listeners.clear(), l.delete(t), e += 1);
|
|
568
|
+
return e;
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
function W(e, t, n) {
|
|
572
|
+
let r = l.get(e);
|
|
573
|
+
if (!r) return;
|
|
574
|
+
let i = c.get(r.journeyId);
|
|
575
|
+
i && j(i, r, t, n);
|
|
576
|
+
}
|
|
577
|
+
let G = {
|
|
578
|
+
__bindStepCallbacks: I,
|
|
579
|
+
__getRecord: (e) => l.get(e),
|
|
580
|
+
__getRegistered: (e) => c.get(e),
|
|
581
|
+
__moduleMap: o,
|
|
582
|
+
__debug: i,
|
|
583
|
+
__fireComponentError: W
|
|
584
|
+
};
|
|
585
|
+
return a.set(U, G), U;
|
|
586
|
+
}
|
|
587
|
+
function s() {
|
|
588
|
+
let e = globalThis;
|
|
589
|
+
return !!e.process && e.process.env?.NODE_ENV !== "production";
|
|
590
|
+
}
|
|
591
|
+
function c(e) {
|
|
592
|
+
let t = a.get(e);
|
|
593
|
+
if (!t) throw Error("[@modular-react/journeys] getInternals() called on a runtime that was not produced by createJourneyRuntime().");
|
|
594
|
+
return t;
|
|
595
|
+
}
|
|
596
|
+
//#endregion
|
|
597
|
+
export { n as a, e as i, c as n, r as o, t as r, i as s, o as t };
|
|
598
|
+
|
|
599
|
+
//# sourceMappingURL=runtime-DyU_PmaC.js.map
|