@vielzeug/wayfinder 3.0.0 → 26.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/_dev.cjs +1 -1
  2. package/dist/_dev.cjs.map +1 -1
  3. package/dist/_dev.js +1 -6
  4. package/dist/_dev.js.map +1 -1
  5. package/dist/_navigation.cjs +1 -1
  6. package/dist/_navigation.cjs.map +1 -1
  7. package/dist/_navigation.js +1 -20
  8. package/dist/_navigation.js.map +1 -1
  9. package/dist/compile.cjs +1 -1
  10. package/dist/compile.cjs.map +1 -1
  11. package/dist/compile.js +1 -46
  12. package/dist/compile.js.map +1 -1
  13. package/dist/context.cjs +1 -1
  14. package/dist/context.cjs.map +1 -1
  15. package/dist/context.js +1 -59
  16. package/dist/context.js.map +1 -1
  17. package/dist/errors.cjs +1 -1
  18. package/dist/errors.cjs.map +1 -1
  19. package/dist/errors.d.ts +4 -1
  20. package/dist/errors.d.ts.map +1 -1
  21. package/dist/errors.js +1 -17
  22. package/dist/errors.js.map +1 -1
  23. package/dist/guards.cjs +1 -1
  24. package/dist/guards.cjs.map +1 -1
  25. package/dist/guards.js +1 -9
  26. package/dist/guards.js.map +1 -1
  27. package/dist/history.cjs +1 -1
  28. package/dist/history.cjs.map +1 -1
  29. package/dist/history.d.ts +11 -0
  30. package/dist/history.d.ts.map +1 -1
  31. package/dist/history.js +1 -67
  32. package/dist/history.js.map +1 -1
  33. package/dist/index.cjs +1 -1
  34. package/dist/index.d.ts +6 -2
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +1 -5
  37. package/dist/middleware.cjs +1 -1
  38. package/dist/middleware.cjs.map +1 -1
  39. package/dist/middleware.js +1 -9
  40. package/dist/middleware.js.map +1 -1
  41. package/dist/path.cjs +1 -1
  42. package/dist/path.cjs.map +1 -1
  43. package/dist/path.js +1 -132
  44. package/dist/path.js.map +1 -1
  45. package/dist/phase-mirror.cjs +2 -0
  46. package/dist/phase-mirror.cjs.map +1 -0
  47. package/dist/phase-mirror.d.ts +51 -0
  48. package/dist/phase-mirror.d.ts.map +1 -0
  49. package/dist/phase-mirror.js +2 -0
  50. package/dist/phase-mirror.js.map +1 -0
  51. package/dist/route-signals.cjs +2 -0
  52. package/dist/route-signals.cjs.map +1 -0
  53. package/dist/route-signals.d.ts +23 -0
  54. package/dist/route-signals.d.ts.map +1 -0
  55. package/dist/route-signals.js +2 -0
  56. package/dist/route-signals.js.map +1 -0
  57. package/dist/router.cjs +1 -1
  58. package/dist/router.cjs.map +1 -1
  59. package/dist/router.d.ts +7 -1
  60. package/dist/router.d.ts.map +1 -1
  61. package/dist/router.js +1 -475
  62. package/dist/router.js.map +1 -1
  63. package/dist/types.d.ts +7 -0
  64. package/dist/types.d.ts.map +1 -1
  65. package/dist/wayfinder.cjs +1 -1
  66. package/dist/wayfinder.cjs.map +1 -1
  67. package/dist/wayfinder.iife.js +1 -1
  68. package/dist/wayfinder.iife.js.map +1 -1
  69. package/dist/wayfinder.js +1 -1
  70. package/dist/wayfinder.js.map +1 -1
  71. package/package.json +8 -5
package/dist/router.js CHANGED
@@ -1,476 +1,2 @@
1
- import { WayfinderDisposedError as e, WayfinderRedirectLoopError as t, WayfinderRouteError as n } from "./errors.js";
2
- import { createBrowserHistory as r } from "./history.js";
3
- import { createNavigationCoordinator as i } from "./_navigation.js";
4
- import { buildUrl as a, joinPaths as o, matchRouteFor as s, matchesPrefix as c, normalizePath as l, parseQuery as u, readLocation as d, stripBase as f } from "./path.js";
5
- import { compileRoutes as p } from "./compile.js";
6
- import { buildMatchBranch as m, createRouteContext as h, createRouteState as g, executeMiddlewarePipeline as _, reportError as v } from "./context.js";
7
- import { runLeaveBlockers as y } from "./guards.js";
8
- //#region src/router.ts
9
- function b(e, t) {
10
- let r = t.get(e);
11
- if (r) return r;
12
- let i = [...t.keys()].join(", ");
13
- throw new n(i ? `Unknown route name: ${e}. Available routes: ${i}` : `Unknown route name: ${e}`);
14
- }
15
- function x(e, t, n = "/") {
16
- if ("path" in e) return e.path;
17
- let r = b(e.name, t), i = a(n, r.path, e.params, e.query);
18
- return e.hash ? `${i}#${e.hash}` : i;
19
- }
20
- var S = Symbol("wayfinder.routerError");
21
- function C(e, t) {
22
- return {
23
- [S]: !0,
24
- context: t,
25
- error: e
26
- };
27
- }
28
- function w(e) {
29
- return typeof e == "object" && !!e && S in e;
30
- }
31
- function T(e) {
32
- throw w(e) ? e.error : e;
33
- }
34
- var E = class {
35
- #e;
36
- #t;
37
- #n;
38
- #r;
39
- #i;
40
- #a;
41
- #o;
42
- #s;
43
- #c;
44
- #l = /* @__PURE__ */ new Set();
45
- #u = i();
46
- #d;
47
- #f = !1;
48
- #p = new AbortController();
49
- #m = "/";
50
- #h = /* @__PURE__ */ new Set();
51
- #g = /* @__PURE__ */ new Map();
52
- #_ = /* @__PURE__ */ new Map();
53
- #v;
54
- #y;
55
- ready;
56
- constructor(e) {
57
- let t = p(e);
58
- if (this.#e = l(e.base ?? "/"), this.#t = e.middleware ?? [], this.#n = e.coerceSearch, this.#r = e.history ?? r(), this.#o = e.onError, this.#s = e.scroll, this.#c = e.viewTransition ?? !1, this.#i = t.records, this.#a = t.routesByName, this.#d = g({
59
- location: {
60
- hash: "",
61
- historyState: null,
62
- pathname: "/",
63
- query: {}
64
- },
65
- matches: [],
66
- status: "idle"
67
- }), e.notFound) {
68
- let t = e.notFound, n = {
69
- dataFn: t.data,
70
- name: "__notFound__"
71
- };
72
- this.#v = {
73
- branchDefs: [n],
74
- leaf: n,
75
- matcher: {
76
- paramNames: [],
77
- pattern: /(?:)/,
78
- prefixPattern: /(?:)/
79
- },
80
- ownMiddleware: t.middleware ?? [],
81
- path: "/*"
82
- };
83
- } else this.#v = null;
84
- this.#y = this.#x();
85
- let { hash: n, pathname: i, search: a } = this.#r.location;
86
- this.#m = `${i}${a}${n}`;
87
- let o = this.#u.begin(), s = this.#A(o, d(this.#e, this.#r), (e, t) => {
88
- if (!o.isCurrent()) return;
89
- let n = this.#F(e);
90
- t && this.#r.replace(n, e.historyState), this.#m = n;
91
- }, void 0, 0, !1).then(() => void 0);
92
- this.ready = s.catch(T), this.ready.catch(() => {}), this.#w(s, { source: "initial-navigation" }), this.beforeLeave = this.beforeLeave.bind(this), this.createViewRegistry = this.createViewRegistry.bind(this), this.dispose = this.dispose.bind(this), this.getSnapshot = this.getSnapshot.bind(this), this.isActive = this.isActive.bind(this), this.load = this.load.bind(this), this.match = this.match.bind(this), this.navigate = this.navigate.bind(this), this.preload = this.preload.bind(this), this.subscribe = this.subscribe.bind(this), this.url = this.url.bind(this), this.waitFor = this.waitFor.bind(this);
93
- }
94
- getSnapshot() {
95
- return this.#d;
96
- }
97
- createViewRegistry(e, t) {
98
- return { resolve: (n) => {
99
- let r = n.matches.at(-1)?.name;
100
- return r === "__notFound__" ? t?.notFound : r ? e[r] : void 0;
101
- } };
102
- }
103
- subscribe(e) {
104
- return this.#b(), this.#h.add(e), () => {
105
- this.#h.delete(e);
106
- };
107
- }
108
- navigate(e, t) {
109
- let n = x(typeof e == "string" ? { path: e } : e, this.#a);
110
- return this.#L(n, t);
111
- }
112
- url(e, t, n) {
113
- let r = b(e, this.#a);
114
- return a(this.#e, r.path, t, n);
115
- }
116
- isActive(e, t = {}) {
117
- let n = b(e, this.#a), r = this.#d.location.pathname;
118
- return t.exact ?? !1 ? s(r, [n]).record != null : c(r, n);
119
- }
120
- match(e) {
121
- let t = f(l(e), this.#e), { params: n, record: r } = s(t, this.#i);
122
- if (!r || r.redirect) return null;
123
- let i = m(r.branchDefs, n, t, r.branchDefs.map(() => void 0));
124
- return i.length ? i : null;
125
- }
126
- async load(e, t) {
127
- let n = await this.#D(e);
128
- if (n.type !== "matched") return null;
129
- let { location: r, params: i, record: a, resolvedQuery: o } = n, s = a.branchDefs, c = s.some((e) => e.dataFn != null), l = s.map(() => void 0), u, d = "idle";
130
- if (c) {
131
- let e = t?.signal ?? new AbortController().signal, n = m(s, i, r.pathname, l), a = h(r, o, i, n, () => Promise.resolve());
132
- try {
133
- l = await this.#E(s, a, e);
134
- } catch (e) {
135
- u = e, d = "error";
136
- }
137
- }
138
- return g({
139
- error: u,
140
- location: r,
141
- matches: m(s, i, r.pathname, l),
142
- status: d
143
- });
144
- }
145
- async preload(e) {
146
- this.#b();
147
- let t = this.#N(x(e, this.#a, this.#e)), n = this.#_.get(t);
148
- if (n) return n;
149
- let r = this.load(t, { signal: this.#p.signal }).then((e) => {
150
- if (this.#f) throw this.#p.signal.reason;
151
- if (e?.status === "error") throw e.error;
152
- if (e) {
153
- let t = this.#F(e.location);
154
- this.#g.size >= 20 && this.#g.delete(this.#g.keys().next().value), this.#g.set(t, e.matches.map((e) => e.data));
155
- }
156
- return e;
157
- }).catch((e) => {
158
- throw this.#o && this.#C(e, { source: "preload" }), e;
159
- }).finally(() => {
160
- this.#_.delete(t);
161
- });
162
- return this.#_.set(t, r), r;
163
- }
164
- waitFor(e) {
165
- return this.#b(), new Promise((t, n) => {
166
- let r = (t) => t.status === "idle" && t.matches.some((t) => t.name === e);
167
- if (this.#d.status === "error") {
168
- n(this.#d.error);
169
- return;
170
- }
171
- if (r(this.#d)) {
172
- t(this.#d);
173
- return;
174
- }
175
- let i = this.subscribe((e) => {
176
- r(e) ? (i(), t(e)) : e.status === "error" && (i(), n(e.error));
177
- });
178
- this.#p.signal.addEventListener("abort", () => {
179
- i(), n(this.#p.signal.reason);
180
- }, { once: !0 });
181
- });
182
- }
183
- beforeLeave(e, t) {
184
- this.#b();
185
- let n = {
186
- handler: e,
187
- routes: t?.routes
188
- };
189
- return this.#l.add(n), () => {
190
- this.#l.delete(n);
191
- };
192
- }
193
- get disposalSignal() {
194
- return this.#p.signal;
195
- }
196
- get disposed() {
197
- return this.#f;
198
- }
199
- dispose() {
200
- this.#f || (this.#f = !0, this.#l.clear(), this.#h.clear(), this.#g.clear(), this.#_.clear(), this.#u.invalidate(new e()), this.#y(), this.#p.abort(new e()));
201
- }
202
- [Symbol.dispose]() {
203
- this.dispose();
204
- }
205
- #b() {
206
- if (this.#f) throw new e();
207
- }
208
- #x() {
209
- return this.#r.onPopstate(() => {
210
- let { hash: e, pathname: t, search: n } = this.#r.location, r = `${t}${n}${e}`, i = this.#m;
211
- r !== i && this.#w(this.#S(r, i), { source: "history-listener" });
212
- });
213
- }
214
- async #S(e, t) {
215
- let n = this.#u.begin(), r = this.#d.matches.map((e) => e.name), i = this.#I(e), a = await y(this.#l, r, i);
216
- if (!n.isCurrent()) return;
217
- if (!a) {
218
- this.#r.replace(t, this.#d.location.historyState);
219
- return;
220
- }
221
- let o = await this.#A(n, d(this.#e, this.#r), (e, t) => {
222
- if (!n.isCurrent()) return;
223
- let r = this.#F(e);
224
- t && this.#r.replace(r, e.historyState), this.#m = r;
225
- }, void 0, 0, !1);
226
- n.isCurrent() && !o && this.#r.replace(t, this.#d.location.historyState);
227
- }
228
- #C(e, t) {
229
- v(e, t, this.#o);
230
- }
231
- #w(e, t) {
232
- e.catch((e) => {
233
- w(e) ? this.#C(e.error, e.context) : this.#C(e, t);
234
- });
235
- }
236
- #T() {
237
- this.#h.forEach((e) => {
238
- e(this.#d);
239
- });
240
- }
241
- async #E(e, t, n) {
242
- return Promise.all(e.map(async (e) => {
243
- if (!e.dataFn) return;
244
- let r = e.dataFn;
245
- try {
246
- return await r({
247
- ...t,
248
- signal: n
249
- });
250
- } catch (r) {
251
- if (e.onError) return e.onError(r, {
252
- ...t,
253
- signal: n
254
- });
255
- throw r;
256
- }
257
- }));
258
- }
259
- async #D(e) {
260
- let n = e;
261
- for (let e = 0; e < 5; e += 1) {
262
- let e = new URL(n, "http://localhost"), t = {
263
- hash: e.hash.replace(/^#/, ""),
264
- historyState: null,
265
- pathname: f(e.pathname, this.#e),
266
- query: u(e.search)
267
- }, r = await this.#O(t);
268
- if (r.type !== "redirect") return r;
269
- n = this.#N(r.redirectTo);
270
- }
271
- throw new t();
272
- }
273
- async #O(e) {
274
- let { params: t, record: n } = s(e.pathname, this.#i);
275
- if (!n) return {
276
- location: e,
277
- params: t,
278
- type: "unmatched"
279
- };
280
- if (n.redirect) return {
281
- location: e,
282
- params: t,
283
- redirectTo: x(n.redirect, this.#a, this.#e),
284
- type: "redirect"
285
- };
286
- let r = e.query, i = n.coerceSearch ?? this.#n;
287
- if (i) try {
288
- r = i(e.query);
289
- } catch (t) {
290
- this.#C(t, { source: "coerce-search" }), r = e.query;
291
- }
292
- return {
293
- branch: m(n.branchDefs, t, e.pathname, n.branchDefs.map(() => void 0)),
294
- location: e,
295
- params: t,
296
- record: n,
297
- resolvedQuery: r,
298
- type: "matched"
299
- };
300
- }
301
- async #k(e, t, n, r, i, a, o) {
302
- if (!a()) return;
303
- let s = e.branchDefs, c = s.some((e) => e.dataFn != null), l = s.map(() => void 0);
304
- if (c) {
305
- let e = this.#F(n), c = this.#_.get(e);
306
- if (c && (await c.catch(() => void 0), !a())) return;
307
- let u = this.#g.get(e);
308
- if (u) this.#g.delete(e), l = u;
309
- else {
310
- this.#d = g({
311
- location: n,
312
- matches: m(s, r, n.pathname, l),
313
- status: "loading"
314
- }), this.#T();
315
- try {
316
- l = await this.#E(s, t, i);
317
- } catch (e) {
318
- if (!a()) return;
319
- throw this.#d = g({
320
- error: e,
321
- location: n,
322
- matches: m(s, r, n.pathname, l),
323
- status: "error"
324
- }), o?.(e), e;
325
- }
326
- }
327
- }
328
- a() && (this.#d = g({
329
- location: n,
330
- matches: m(s, r, n.pathname, l),
331
- status: "idle"
332
- }));
333
- }
334
- async #A(e, n, r, i, a = 0, o = !1) {
335
- let s = this.#d, c = () => e.isCurrent() && !this.#f, l = await this.#O(n);
336
- if (!c()) return !1;
337
- if (l.type === "redirect") {
338
- if (a >= 5) throw new t();
339
- return this.#A(e, this.#P(l.redirectTo, n.historyState), r, i, a + 1, !0);
340
- }
341
- if (l.type === "unmatched") {
342
- if (this.#v) {
343
- let t = [this.#v.leaf], a = m(t, {}, n.pathname, [void 0]), l = !1, u, d = !1, f = !1, p = n.query;
344
- if (this.#n) try {
345
- p = this.#n(n.query);
346
- } catch (e) {
347
- this.#C(e, { source: "coerce-search" });
348
- }
349
- let g = async () => {
350
- if (!c()) return;
351
- let t = h(n, p, {}, a, (e, t) => this.navigate(e, t));
352
- f = await _(t, [...this.#t, ...this.#v.ownMiddleware], async () => {
353
- c() && (l = !0, r(n, o), c() && await this.#k(this.#v, t, n, {}, e.signal, c, (e) => {
354
- u = e, d = !0;
355
- }));
356
- }, (e) => w(e) || d && e === u ? e : C(e, {
357
- routeName: "__notFound__",
358
- source: "middleware"
359
- }));
360
- };
361
- try {
362
- await this.#j(g, i);
363
- } catch (e) {
364
- throw d && e === u ? C(e, {
365
- routeName: "__notFound__",
366
- source: "data-loader"
367
- }) : e;
368
- } finally {
369
- c() && l && (this.#T(), this.#M(this.#d, s));
370
- }
371
- return f && l;
372
- }
373
- return !c() || (r(l.location, o), !c()) ? !1 : (this.#d = g({
374
- location: l.location,
375
- matches: [],
376
- status: "idle"
377
- }), this.#T(), this.#M(this.#d, s), !0);
378
- }
379
- let { branch: u, location: d, params: f, record: p, resolvedQuery: v } = l, y = !1, b, x = !1, S = !1, T = async () => {
380
- if (!c()) return;
381
- let t = h(d, v, f, u, (e, t) => this.navigate(e, t));
382
- S = await _(t, [...this.#t, ...p.ownMiddleware], async () => {
383
- c() && (y = !0, r(d, o), c() && await this.#k(p, t, d, f, e.signal, c, (e) => {
384
- b = e, x = !0;
385
- }));
386
- }, (e) => w(e) || x && e === b ? e : C(e, {
387
- routeName: p.leaf.name,
388
- source: "middleware"
389
- }));
390
- };
391
- try {
392
- await this.#j(T, i);
393
- } catch (e) {
394
- throw x && e === b ? C(e, {
395
- routeName: p.leaf.name,
396
- source: "data-loader"
397
- }) : e;
398
- } finally {
399
- c() && y && (this.#T(), this.#M(this.#d, s));
400
- }
401
- return S && y;
402
- }
403
- async #j(e, t) {
404
- if ((t ?? this.#c) && typeof document < "u") {
405
- let t = document.startViewTransition;
406
- if (t) {
407
- await t.call(document, e).finished;
408
- return;
409
- }
410
- }
411
- await e();
412
- }
413
- #M(e, t) {
414
- if (!this.#s || typeof window > "u") return;
415
- let n = this.#s(e, t);
416
- if (n !== "preserve") {
417
- if (n === "top") {
418
- window.scrollTo(0, 0);
419
- return;
420
- }
421
- window.scrollTo(n.x, n.y);
422
- }
423
- }
424
- #N(e) {
425
- let t = new URL(e, "http://localhost"), n = f(t.pathname, this.#e);
426
- return `${o(this.#e, n)}${t.search}${t.hash}`;
427
- }
428
- #P(e, t) {
429
- let n = new URL(this.#N(e), "http://localhost");
430
- return {
431
- hash: n.hash.replace(/^#/, ""),
432
- historyState: t,
433
- pathname: f(n.pathname, this.#e),
434
- query: u(n.search)
435
- };
436
- }
437
- #F(e) {
438
- let t = new URLSearchParams();
439
- for (let [n, r] of Object.entries(e.query)) Array.isArray(r) ? r.forEach((e) => {
440
- t.append(n, e);
441
- }) : t.set(n, r);
442
- let n = t.toString(), r = e.hash ? `#${e.hash}` : "";
443
- return `${o(this.#e, e.pathname)}${n ? `?${n}` : ""}${r}`;
444
- }
445
- #I(e) {
446
- let t = new URL(e, "http://localhost"), n = f(t.pathname, this.#e), { params: r, record: i } = s(n, this.#i);
447
- return {
448
- name: i?.leaf.name,
449
- params: r,
450
- pathname: n,
451
- query: u(t.search)
452
- };
453
- }
454
- async #L(e, t = {}) {
455
- this.#b();
456
- let n = this.#N(e);
457
- if (!t.force && n === this.#m) return;
458
- let r = this.#u.begin(), i = this.#d.matches.map((e) => e.name), a = await y(this.#l, i, this.#I(n));
459
- if (r.isCurrent() && a) try {
460
- await this.#A(r, this.#P(n, t.state), (e, t) => {
461
- if (!r.isCurrent()) return;
462
- let n = this.#F(e);
463
- t ? this.#r.replace(n, e.historyState) : this.#r.push(n, e.historyState), this.#m = n;
464
- }, t.viewTransition, 0, t.replace ?? !1);
465
- } catch (e) {
466
- throw w(e) ? e.error : e;
467
- }
468
- }
469
- };
470
- function D(e) {
471
- return new E(e);
472
- }
473
- //#endregion
474
- export { D as createRouter };
475
-
1
+ import{WayfinderDisposedError as e,WayfinderRedirectLoopError as t,WayfinderRouteError as n}from"./errors.js";import{buildUrl as r,joinPaths as i,matchRouteFor as a,matchesPrefix as o,normalizePath as s,parseQuery as c,readLocation as l,stripBase as u}from"./path.js";import{createBrowserHistory as d}from"./history.js";import{createNavigationCoordinator as f}from"./_navigation.js";import{compileRoutes as p}from"./compile.js";import{buildMatchBranch as m,createRouteContext as h,createRouteState as g,executeMiddlewarePipeline as _,reportError as v}from"./context.js";import{runLeaveBlockers as y}from"./guards.js";function getRouteByName(e,t){let r=t.get(e);if(r)return r;let i=[...t.keys()].join(`, `);throw new n(i?`Unknown route name: ${e}. Available routes: ${i}`:`Unknown route name: ${e}`)}function resolveTarget(e,t,n=`/`){if(`path`in e)return e.path;let i=getRouteByName(e.name,t),a=r(n,i.path,e.params,e.query);return e.hash?`${a}#${e.hash}`:a}var b=Symbol(`wayfinder.routerError`);function carryError(e,t){return{[b]:!0,context:t,error:e}}function isRouterErrorCarrier(e){return typeof e==`object`&&!!e&&b in e}function unwrapCarrier(e){throw isRouterErrorCarrier(e)?e.error:e}var Router=class{#e;#t;#n;#r;#i;#a;#o;#s;#c;#l=new Set;#u=f();#d;#f=!1;#p=new AbortController;#m=`/`;#h=new Set;#g=new Map;#_=new Map;#v;#y;ready;constructor(e){let t=p(e);if(this.#e=s(e.base??`/`),this.#t=e.middleware??[],this.#n=e.coerceSearch,this.#r=e.history??d(),this.#o=e.onError,this.#s=e.scroll,this.#c=e.viewTransition??!1,this.#i=t.records,this.#a=t.routesByName,this.#d=g({location:{hash:``,historyState:null,pathname:`/`,query:{}},matches:[],status:`idle`}),e.notFound){let t=e.notFound,n={dataFn:t.data,name:`__notFound__`};this.#v={branchDefs:[n],leaf:n,matcher:{paramNames:[],pattern:/(?:)/,prefixPattern:/(?:)/},ownMiddleware:t.middleware??[],path:`/*`}}else this.#v=null;this.#y=this.#x();let{hash:n,pathname:r,search:i}=this.#r.location;this.#m=`${r}${i}${n}`;let a=this.#u.begin(),o=this.#A(a,l(this.#e,this.#r),(e,t)=>{if(!a.isCurrent())return;let n=this.#F(e);t&&this.#r.replace(n,e.historyState),this.#m=n},void 0,0,!1).then(()=>void 0);this.ready=o.catch(unwrapCarrier),this.ready.catch(()=>{}),this.#w(o,{source:`initial-navigation`}),this.beforeLeave=this.beforeLeave.bind(this),this.createViewRegistry=this.createViewRegistry.bind(this),this.dispose=this.dispose.bind(this),this.getSnapshot=this.getSnapshot.bind(this),this.isActive=this.isActive.bind(this),this.load=this.load.bind(this),this.match=this.match.bind(this),this.navigate=this.navigate.bind(this),this.preload=this.preload.bind(this),this.subscribe=this.subscribe.bind(this),this.url=this.url.bind(this),this.waitFor=this.waitFor.bind(this)}getSnapshot(){return this.#d}createViewRegistry(e,t){return{resolve:n=>{let r=n.matches.at(-1)?.name;return r===`__notFound__`?t?.notFound:r?e[r]:void 0}}}subscribe(e){return this.#b(),this.#h.add(e),()=>{this.#h.delete(e)}}navigate(e,t){let n=resolveTarget(typeof e==`string`?{path:e}:e,this.#a);return this.#L(n,t)}url(e,t,n){let i=getRouteByName(e,this.#a);return r(this.#e,i.path,t,n)}href(e,t,n){let i=getRouteByName(e,this.#a),a=r(this.#e,i.path,t,n);return this.#r.href?.(a)??a}isActive(e,t={}){let n=getRouteByName(e,this.#a),r=this.#d.location.pathname;return t.exact??!1?a(r,[n]).record!=null:o(r,n)}match(e){let t=u(s(e),this.#e),{params:n,record:r}=a(t,this.#i);if(!r||r.redirect)return null;let i=m(r.branchDefs,n,t,r.branchDefs.map(()=>void 0));return i.length?i:null}async load(e,t){let n=await this.#D(e);if(n.type!==`matched`)return null;let{location:r,params:i,record:a,resolvedQuery:o}=n,s=a.branchDefs,c=s.some(e=>e.dataFn!=null),l=s.map(()=>void 0),u,d=`idle`;if(c){let e=t?.signal??new AbortController().signal,n=m(s,i,r.pathname,l),a=h(r,o,i,n,()=>Promise.resolve());try{l=await this.#E(s,a,e)}catch(e){u=e,d=`error`}}return g({error:u,location:r,matches:m(s,i,r.pathname,l),status:d})}async preload(e){this.#b();let t=this.#N(resolveTarget(e,this.#a,this.#e)),n=this.#_.get(t);if(n)return n;let r=this.load(t,{signal:this.#p.signal}).then(e=>{if(this.#f)throw this.#p.signal.reason;if(e?.status===`error`)throw e.error;if(e){let t=this.#F(e.location);this.#g.size>=20&&this.#g.delete(this.#g.keys().next().value),this.#g.set(t,e.matches.map(e=>e.data))}return e}).catch(e=>{throw this.#o&&this.#C(e,{source:`preload`}),e}).finally(()=>{this.#_.delete(t)});return this.#_.set(t,r),r}waitFor(e){return this.#b(),new Promise((t,n)=>{let matchesName=t=>t.status===`idle`&&t.matches.some(t=>t.name===e);if(this.#d.status===`error`){n(this.#d.error);return}if(matchesName(this.#d)){t(this.#d);return}let r=this.subscribe(e=>{matchesName(e)?(r(),t(e)):e.status===`error`&&(r(),n(e.error))});this.#p.signal.addEventListener(`abort`,()=>{r(),n(this.#p.signal.reason)},{once:!0})})}beforeLeave(e,t){this.#b();let n={handler:e,routes:t?.routes};return this.#l.add(n),()=>{this.#l.delete(n)}}get disposalSignal(){return this.#p.signal}get disposed(){return this.#f}dispose(){this.#f||(this.#f=!0,this.#l.clear(),this.#h.clear(),this.#g.clear(),this.#_.clear(),this.#u.invalidate(new e),this.#y(),this.#p.abort(new e))}[Symbol.dispose](){this.dispose()}#b(){if(this.#f)throw new e}#x(){return this.#r.onPopstate(()=>{let{hash:e,pathname:t,search:n}=this.#r.location,r=`${t}${n}${e}`,i=this.#m;r!==i&&this.#w(this.#S(r,i),{source:`history-listener`})})}async#S(e,t){let n=this.#u.begin(),r=this.#d.matches.map(e=>e.name),i=this.#I(e),a=await y(this.#l,r,i);if(!n.isCurrent())return;if(!a){this.#r.replace(t,this.#d.location.historyState);return}let o=await this.#A(n,l(this.#e,this.#r),(e,t)=>{if(!n.isCurrent())return;let r=this.#F(e);t&&this.#r.replace(r,e.historyState),this.#m=r},void 0,0,!1);n.isCurrent()&&!o&&this.#r.replace(t,this.#d.location.historyState)}#C(e,t){v(e,t,this.#o)}#w(e,t){e.catch(e=>{isRouterErrorCarrier(e)?this.#C(e.error,e.context):this.#C(e,t)})}#T(){for(let e of[...this.#h])try{e(this.#d)}catch(e){queueMicrotask(()=>{throw e})}}async#E(e,t,n){return Promise.all(e.map(async e=>{if(!e.dataFn)return;let r=e.dataFn;try{return await r({...t,signal:n})}catch(r){if(e.onError)return e.onError(r,{...t,signal:n});throw r}}))}async#D(e){let n=e;for(let e=0;e<5;e+=1){let e=new URL(n,`http://localhost`),t={hash:e.hash.replace(/^#/,``),historyState:null,pathname:u(e.pathname,this.#e),query:c(e.search)},r=await this.#O(t);if(r.type!==`redirect`)return r;n=this.#N(r.redirectTo)}throw new t}async#O(e){let{params:t,record:n}=a(e.pathname,this.#i);if(!n)return{location:e,params:t,type:`unmatched`};if(n.redirect)return{location:e,params:t,redirectTo:resolveTarget(n.redirect,this.#a,this.#e),type:`redirect`};let r=e.query,i=n.coerceSearch??this.#n;if(i)try{r=i(e.query)}catch(t){this.#C(t,{source:`coerce-search`}),r=e.query}return{branch:m(n.branchDefs,t,e.pathname,n.branchDefs.map(()=>void 0)),location:e,params:t,record:n,resolvedQuery:r,type:`matched`}}async#k(e,t,n,r,i,a,o){if(!a())return;let s=e.branchDefs,c=s.some(e=>e.dataFn!=null),l=s.map(()=>void 0);if(c){let e=this.#F(n),c=this.#_.get(e);if(c&&(await c.catch(()=>void 0),!a()))return;let u=this.#g.get(e);if(u)this.#g.delete(e),l=u;else{this.#d=g({location:n,matches:m(s,r,n.pathname,l),status:`loading`}),this.#T();try{l=await this.#E(s,t,i)}catch(e){if(!a())return;throw this.#d=g({error:e,location:n,matches:m(s,r,n.pathname,l),status:`error`}),o?.(e),e}}}a()&&(this.#d=g({location:n,matches:m(s,r,n.pathname,l),status:`idle`}))}async#A(e,n,r,i,a=0,o=!1){let s=this.#d,isCurrent=()=>e.isCurrent()&&!this.#f,c=await this.#O(n);if(!isCurrent())return!1;if(c.type===`redirect`){if(a>=5)throw new t;return this.#A(e,this.#P(c.redirectTo,n.historyState),r,i,a+1,!0)}if(c.type===`unmatched`){if(this.#v){let t=[this.#v.leaf],a=m(t,{},n.pathname,[void 0]),c=!1,l,u=!1,d=!1,f=n.query,p=!1;if(this.#n)try{f=this.#n(n.query)}catch(e){this.#C(e,{source:`coerce-search`})}let run=async()=>{if(!isCurrent())return;let t=h(n,f,{},a,(e,t)=>this.navigate(e,t));d=await _(t,[...this.#t,...this.#v.ownMiddleware],async()=>{isCurrent()&&(c=!0,r(n,o),isCurrent()&&(await this.#k(this.#v,t,n,{},e.signal,isCurrent,e=>{l=e,u=!0}),isCurrent()&&(p=!0,this.#T(),this.#M(this.#d,s))))},e=>isRouterErrorCarrier(e)||u&&e===l?e:carryError(e,{routeName:`__notFound__`,source:`middleware`}))};try{await this.#j(run,i)}catch(e){throw u&&e===l?carryError(e,{routeName:`__notFound__`,source:`data-loader`}):e}finally{isCurrent()&&c&&!p&&(this.#T(),this.#M(this.#d,s))}return d&&c}return!isCurrent()||(r(c.location,o),!isCurrent())?!1:(this.#d=g({location:c.location,matches:[],status:`idle`}),this.#T(),this.#M(this.#d,s),!0)}let{branch:l,location:u,params:d,record:f,resolvedQuery:p}=c,v=!1,y,b=!1,x=!1,S=!1,run=async()=>{if(!isCurrent())return;let t=h(u,p,d,l,(e,t)=>this.navigate(e,t));x=await _(t,[...this.#t,...f.ownMiddleware],async()=>{isCurrent()&&(v=!0,r(u,o),isCurrent()&&(await this.#k(f,t,u,d,e.signal,isCurrent,e=>{y=e,b=!0}),isCurrent()&&(S=!0,this.#T(),this.#M(this.#d,s))))},e=>isRouterErrorCarrier(e)||b&&e===y?e:carryError(e,{routeName:f.leaf.name,source:`middleware`}))};try{await this.#j(run,i)}catch(e){throw b&&e===y?carryError(e,{routeName:f.leaf.name,source:`data-loader`}):e}finally{isCurrent()&&v&&!S&&(this.#T(),this.#M(this.#d,s))}return x&&v}async#j(e,t){if((t??this.#c)&&typeof document<`u`){let t=document.startViewTransition;if(t){await t.call(document,e).finished;return}}await e()}#M(e,t){if(!this.#s||typeof window>`u`)return;let n=this.#s(e,t);if(n!==`preserve`){if(n===`top`){window.scrollTo(0,0);return}window.scrollTo(n.x,n.y)}}#N(e){let t=new URL(e,`http://localhost`),n=u(t.pathname,this.#e);return`${i(this.#e,n)}${t.search}${t.hash}`}#P(e,t){let n=new URL(this.#N(e),`http://localhost`);return{hash:n.hash.replace(/^#/,``),historyState:t,pathname:u(n.pathname,this.#e),query:c(n.search)}}#F(e){let t=new URLSearchParams;for(let[n,r]of Object.entries(e.query))Array.isArray(r)?r.forEach(e=>{t.append(n,e)}):t.set(n,r);let n=t.toString(),r=e.hash?`#${e.hash}`:``;return`${i(this.#e,e.pathname)}${n?`?${n}`:``}${r}`}#I(e){let t=new URL(e,`http://localhost`),n=u(t.pathname,this.#e),{params:r,record:i}=a(n,this.#i);return{name:i?.leaf.name,params:r,pathname:n,query:c(t.search)}}async#L(e,t={}){this.#b();let n=this.#N(e);if(!t.force&&n===this.#m)return;let r=this.#u.begin(),i=this.#d.matches.map(e=>e.name),a=await y(this.#l,i,this.#I(n));if(r.isCurrent()&&a)try{await this.#A(r,this.#P(n,t.state),(e,t)=>{if(!r.isCurrent())return;let n=this.#F(e);t?this.#r.replace(n,e.historyState):this.#r.push(n,e.historyState),this.#m=n},t.viewTransition,0,t.replace??!1)}catch(e){throw isRouterErrorCarrier(e)?e.error:e}}};function createRouter(e){return new Router(e)}export{createRouter};
476
2
  //# sourceMappingURL=router.js.map