@oomfware/stacker 0.2.0 → 0.2.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.
- package/README.md +20 -0
- package/dist/index.d.mts +42 -6
- package/dist/index.mjs +109 -33
- package/dist/testing.d.mts +2 -2
- package/dist/testing.mjs +2 -1
- package/dist/{types-KOb6CZ7N.d.mts → types-Bo4ESWAT.d.mts} +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -206,6 +206,16 @@ const label = (target: RouteTarget<typeof routes>): string => {
|
|
|
206
206
|
because a target spells its params alongside `name`, a route cannot declare a param called `name`;
|
|
207
207
|
`defineRoutes` rejects it.
|
|
208
208
|
|
|
209
|
+
wrap route components in stacker's `lazy` to allow the router to preload them ahead of time.
|
|
210
|
+
|
|
211
|
+
```tsx
|
|
212
|
+
const Profile = lazy(() => import('./screens/profile.tsx'));
|
|
213
|
+
|
|
214
|
+
<Link onPointerEnter={() => void router.preload('Profile')} to="/profile/alice">
|
|
215
|
+
alice
|
|
216
|
+
</Link>;
|
|
217
|
+
```
|
|
218
|
+
|
|
209
219
|
### hooks
|
|
210
220
|
|
|
211
221
|
generate type-safe hooks bound to your route configuration using `createRouterHooks`:
|
|
@@ -315,6 +325,16 @@ new NavigationHistory({
|
|
|
315
325
|
});
|
|
316
326
|
```
|
|
317
327
|
|
|
328
|
+
### failed navigations
|
|
329
|
+
|
|
330
|
+
`NavigationHistory` sends navigation and render errors to `onError`. it defaults to `reportError`:
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
new NavigationHistory({
|
|
334
|
+
onError: (error) => captureException(error),
|
|
335
|
+
});
|
|
336
|
+
```
|
|
337
|
+
|
|
318
338
|
### metadata
|
|
319
339
|
|
|
320
340
|
declare custom route metadata using module augmentation:
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as HistoryLocation, c as HistoryUpdate, i as HistoryListener, n as HistoryAction, o as HistoryNavigateOptions, r as HistoryEntry, s as HistoryScrollBehavior, t as History } from "./types-
|
|
2
|
-
import { AnchorHTMLAttributes, ComponentType, EffectCallback, ReactElement, ReactNode } from "react";
|
|
1
|
+
import { a as HistoryLocation, c as HistoryUpdate, i as HistoryListener, n as HistoryAction, o as HistoryNavigateOptions, r as HistoryEntry, s as HistoryScrollBehavior, t as History } from "./types-Bo4ESWAT.mjs";
|
|
2
|
+
import { AnchorHTMLAttributes, ComponentType, EffectCallback, LazyExoticComponent, ReactElement, ReactNode } from "react";
|
|
3
3
|
//#region src/codec.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* a bidirectional codec for a single route parameter.
|
|
@@ -199,11 +199,11 @@ declare const route: <const Params extends CodecRecord = Empty, const Query exte
|
|
|
199
199
|
declare const layout: <const Children extends RouteChildren, const Params extends CodecRecord = Empty>(config: LayoutConfig<Children, Params>) => LayoutNode<Children, Params>;
|
|
200
200
|
/** a node resolved with an id and normalized parameters. */
|
|
201
201
|
interface ResolvedNode {
|
|
202
|
+
readonly kind: 'layout' | 'route';
|
|
202
203
|
/** unique dot-separated node path. */
|
|
203
204
|
readonly id: string;
|
|
204
205
|
/** key name within the parent record. */
|
|
205
206
|
readonly key: string;
|
|
206
|
-
readonly kind: 'layout' | 'route';
|
|
207
207
|
readonly node: RouteNode;
|
|
208
208
|
/** local parameter codecs. */
|
|
209
209
|
readonly params: CodecRecord;
|
|
@@ -285,9 +285,9 @@ type QueryPatchOf<R, K extends RouteName<R>> = LeafQueryPatch<LeavesMap<R>[K]>;
|
|
|
285
285
|
//#region src/match.d.ts
|
|
286
286
|
/** a node on a matched chain paired with its instance parameters. */
|
|
287
287
|
interface MatchedNode {
|
|
288
|
+
readonly node: ResolvedNode;
|
|
288
289
|
/** parameters used for instance keying. */
|
|
289
290
|
readonly params: Readonly<Record<string, unknown>>;
|
|
290
|
-
readonly node: ResolvedNode;
|
|
291
291
|
}
|
|
292
292
|
/** the result of matching a URL, containing the chain, leaf, name, and parameters. */
|
|
293
293
|
interface RouteMatch {
|
|
@@ -322,6 +322,12 @@ interface NavigationHistoryOptions {
|
|
|
322
322
|
* @returns true to leave the navigation to the browser
|
|
323
323
|
*/
|
|
324
324
|
readonly ignore?: (url: URL) => boolean;
|
|
325
|
+
/**
|
|
326
|
+
* handles navigation and render failures. defaults to `Window.reportError`.
|
|
327
|
+
*
|
|
328
|
+
* @param error failure reason
|
|
329
|
+
*/
|
|
330
|
+
readonly onError?: (error: unknown) => void;
|
|
325
331
|
/** window instance to bind to; defaults to global window. */
|
|
326
332
|
readonly window?: Window;
|
|
327
333
|
}
|
|
@@ -340,7 +346,7 @@ declare class NavigationHistory implements History {
|
|
|
340
346
|
entries(): readonly HistoryEntry[];
|
|
341
347
|
push(to: string, options?: HistoryNavigateOptions): void;
|
|
342
348
|
replace(to: string, options?: HistoryNavigateOptions): void;
|
|
343
|
-
traverseTo(key: string): void
|
|
349
|
+
traverseTo(key: string): Promise<void>;
|
|
344
350
|
go(delta: number): void;
|
|
345
351
|
back(): void;
|
|
346
352
|
forward(): void;
|
|
@@ -364,6 +370,26 @@ interface View {
|
|
|
364
370
|
readonly roots: readonly InstanceNode[];
|
|
365
371
|
}
|
|
366
372
|
//#endregion
|
|
373
|
+
//#region src/lazy.d.ts
|
|
374
|
+
/** a preloadable lazily-loaded component. */
|
|
375
|
+
type LazyComponent<T extends ComponentType<any>> = LazyExoticComponent<T> & {
|
|
376
|
+
/**
|
|
377
|
+
* start loading the component code.
|
|
378
|
+
*
|
|
379
|
+
* @returns promise that settles once loading finishes
|
|
380
|
+
*/
|
|
381
|
+
preload(): Promise<void>;
|
|
382
|
+
};
|
|
383
|
+
/**
|
|
384
|
+
* defines a lazily-loaded component that can be preloaded.
|
|
385
|
+
*
|
|
386
|
+
* @param init component module loader
|
|
387
|
+
* @returns lazy component
|
|
388
|
+
*/
|
|
389
|
+
declare const lazy: <T extends ComponentType<any>>(init: () => Promise<{
|
|
390
|
+
default: T;
|
|
391
|
+
}>) => LazyComponent<T>;
|
|
392
|
+
//#endregion
|
|
367
393
|
//#region src/router.d.ts
|
|
368
394
|
/** options for a navigation. */
|
|
369
395
|
interface NavigateOptions<R extends RouteRegistry<unknown>> extends HistoryNavigateOptions {
|
|
@@ -446,9 +472,19 @@ declare class Router<R extends RouteRegistry<unknown>> {
|
|
|
446
472
|
* @param options where to go, and how to get there
|
|
447
473
|
*/
|
|
448
474
|
navigate(options: NavigateOptions<R>): void;
|
|
475
|
+
/**
|
|
476
|
+
* loads the code a route needs, without navigating to it.
|
|
477
|
+
*
|
|
478
|
+
* @param name route name
|
|
479
|
+
* @returns promise that settles once loading finishes
|
|
480
|
+
* @throws when `name` is not in the registry
|
|
481
|
+
*/
|
|
482
|
+
preload(name: RouteName<R>): Promise<void>;
|
|
449
483
|
/**
|
|
450
484
|
* returns to the nearest existing history entry for a route, or pushes if none exists.
|
|
451
485
|
*
|
|
486
|
+
* pushes the destination if traversal to a matching entry fails.
|
|
487
|
+
*
|
|
452
488
|
* @param to route target, or a relative URL resolved against the active location
|
|
453
489
|
*/
|
|
454
490
|
popTo(to: RouteTarget<R> | string): void;
|
|
@@ -592,4 +628,4 @@ interface LinkProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'
|
|
|
592
628
|
*/
|
|
593
629
|
declare const Link: ({ children, onClick, replace, to, ...rest }: LinkProps) => ReactElement;
|
|
594
630
|
//#endregion
|
|
595
|
-
export { type BuildParamsOf, type Codec, type DefaultedCodec, type History, type HistoryAction, type HistoryEntry, type HistoryListener, type HistoryLocation, type HistoryNavigateOptions, type HistoryScrollBehavior, type HistoryUpdate, type Infer, type InstanceNode, type LayoutConfig, type LayoutNode, type LeafType, Link, type LinkProps, type MatchedNode, type MatchedTarget, type NavigateOptions, NavigationHistory, type NavigationHistoryOptions, type OptionalCodec, Outlet, type ParamsOf, type QueryPatchOf, type ReadonlyURLSearchParams, type ResolvedLeaf, type ResolvedNode, type RouteChildren, type RouteConfig, type RouteLeaf, type RouteMatch, type RouteMeta, type RouteName, type RouteNode, type RouteRegistry, type RouteTarget, Router, type RouterHooks, type RouterOptions, RouterView, type View, type WhenContext, boolean, createRouterHooks, defineRoutes, enumOf, integer, layout, nonEmpty, optional, resolveMeta, route, string, useFocusEffect, useIsFocused, useLocation, useParams, useRoute, useRouter, withDefault };
|
|
631
|
+
export { type BuildParamsOf, type Codec, type DefaultedCodec, type History, type HistoryAction, type HistoryEntry, type HistoryListener, type HistoryLocation, type HistoryNavigateOptions, type HistoryScrollBehavior, type HistoryUpdate, type Infer, type InstanceNode, type LayoutConfig, type LayoutNode, type LazyComponent, type LeafType, Link, type LinkProps, type MatchedNode, type MatchedTarget, type NavigateOptions, NavigationHistory, type NavigationHistoryOptions, type OptionalCodec, Outlet, type ParamsOf, type QueryPatchOf, type ReadonlyURLSearchParams, type ResolvedLeaf, type ResolvedNode, type RouteChildren, type RouteConfig, type RouteLeaf, type RouteMatch, type RouteMeta, type RouteName, type RouteNode, type RouteRegistry, type RouteTarget, Router, type RouterHooks, type RouterOptions, RouterView, type View, type WhenContext, boolean, createRouterHooks, defineRoutes, enumOf, integer, layout, lazy, nonEmpty, optional, resolveMeta, route, string, useFocusEffect, useIsFocused, useLocation, useParams, useRoute, useRouter, withDefault };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { a as parsePath, i as encodeSegment, n as decodeRemainder, o as resolvePath, r as encodeRemainder, t as createPath } from "./url-DerpiRjh.mjs";
|
|
2
|
+
import { Suspense, createContext, lazy as lazy$1, useContext, useEffect, useLayoutEffect, useMemo, useSyncExternalStore } from "react";
|
|
2
3
|
import { SimpleEventEmitter } from "@mary-ext/simple-event-emitter";
|
|
3
|
-
import { Suspense, createContext, useContext, useEffect, useLayoutEffect, useMemo, useSyncExternalStore } from "react";
|
|
4
4
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
5
5
|
//#region src/codec.ts
|
|
6
6
|
const OPTIONAL = Symbol("stacker.optional");
|
|
@@ -81,9 +81,9 @@ const nonEmpty = () => ({
|
|
|
81
81
|
* @returns the optional codec
|
|
82
82
|
*/
|
|
83
83
|
const optional = (codec) => ({
|
|
84
|
+
[OPTIONAL]: true,
|
|
84
85
|
decode: (raw) => codec.decode(raw),
|
|
85
|
-
encode: (value) => codec.encode(value)
|
|
86
|
-
[OPTIONAL]: true
|
|
86
|
+
encode: (value) => codec.encode(value)
|
|
87
87
|
});
|
|
88
88
|
/**
|
|
89
89
|
* wraps a codec with a default value.
|
|
@@ -93,9 +93,9 @@ const optional = (codec) => ({
|
|
|
93
93
|
* @returns the defaulted codec
|
|
94
94
|
*/
|
|
95
95
|
const withDefault = (codec, fallback) => ({
|
|
96
|
+
[DEFAULT]: fallback,
|
|
96
97
|
decode: (raw) => codec.decode(raw),
|
|
97
|
-
encode: (value) => codec.encode(value)
|
|
98
|
-
[DEFAULT]: fallback
|
|
98
|
+
encode: (value) => codec.encode(value)
|
|
99
99
|
});
|
|
100
100
|
const isOptional = (codec) => {
|
|
101
101
|
return OPTIONAL in codec;
|
|
@@ -182,9 +182,9 @@ const defineRoutes = (tree) => {
|
|
|
182
182
|
if (ids.has(id)) throw new Error(`stacker: duplicate node id '${id}'`);
|
|
183
183
|
const params = node.params ?? {};
|
|
184
184
|
const resolved = {
|
|
185
|
+
kind: node.kind,
|
|
185
186
|
id,
|
|
186
187
|
key,
|
|
187
|
-
kind: node.kind,
|
|
188
188
|
node,
|
|
189
189
|
params,
|
|
190
190
|
path: node.kind === "route" ? node.path : void 0
|
|
@@ -388,10 +388,7 @@ const toAction = (type) => {
|
|
|
388
388
|
default: return;
|
|
389
389
|
}
|
|
390
390
|
};
|
|
391
|
-
const
|
|
392
|
-
result.committed?.catch(() => {});
|
|
393
|
-
result.finished?.catch(() => {});
|
|
394
|
-
};
|
|
391
|
+
const isAbort = (error) => typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
|
|
395
392
|
/**
|
|
396
393
|
* history implementation backed by the web navigation API.
|
|
397
394
|
*
|
|
@@ -399,17 +396,23 @@ const ignore = (result) => {
|
|
|
399
396
|
* metadata and indices are managed natively by the browser.
|
|
400
397
|
*/
|
|
401
398
|
var NavigationHistory = class {
|
|
402
|
-
#win;
|
|
403
|
-
#nav;
|
|
404
399
|
#ignore;
|
|
405
400
|
#listeners = /* @__PURE__ */ new Set();
|
|
401
|
+
#nav;
|
|
402
|
+
#onError;
|
|
403
|
+
#win;
|
|
404
|
+
#disposed = false;
|
|
406
405
|
#location;
|
|
407
406
|
constructor(options = {}) {
|
|
408
407
|
this.#win = options.window ?? window;
|
|
409
408
|
this.#nav = this.#win.navigation;
|
|
410
409
|
this.#ignore = options.ignore;
|
|
410
|
+
this.#onError = options.onError ?? ((error) => {
|
|
411
|
+
this.#win.reportError(error);
|
|
412
|
+
});
|
|
411
413
|
this.#location = this.#read();
|
|
412
414
|
this.#nav.addEventListener("navigate", this.#onNavigate);
|
|
415
|
+
this.#nav.addEventListener("navigateerror", this.#onNavigateError);
|
|
413
416
|
this.#win.history.scrollRestoration = "manual";
|
|
414
417
|
}
|
|
415
418
|
get location() {
|
|
@@ -438,19 +441,19 @@ var NavigationHistory = class {
|
|
|
438
441
|
this.#navigate(to, "replace", options);
|
|
439
442
|
}
|
|
440
443
|
traverseTo(key) {
|
|
441
|
-
|
|
444
|
+
return this.#track(this.#nav.traverseTo(key));
|
|
442
445
|
}
|
|
443
446
|
go(delta) {
|
|
444
447
|
const entries = this.#nav.entries();
|
|
445
448
|
const target = entries[Math.max(0, Math.min(this.#location.index + delta, entries.length - 1))];
|
|
446
449
|
if (target === void 0 || target.key === this.#location.key) return;
|
|
447
|
-
this.traverseTo(target.key);
|
|
450
|
+
this.#report(this.traverseTo(target.key));
|
|
448
451
|
}
|
|
449
452
|
back() {
|
|
450
|
-
if (this.#nav.canGoBack)
|
|
453
|
+
if (this.#nav.canGoBack) this.#report(this.#track(this.#nav.back()));
|
|
451
454
|
}
|
|
452
455
|
forward() {
|
|
453
|
-
if (this.#nav.canGoForward)
|
|
456
|
+
if (this.#nav.canGoForward) this.#report(this.#track(this.#nav.forward()));
|
|
454
457
|
}
|
|
455
458
|
listen(listener) {
|
|
456
459
|
this.#listeners.add(listener);
|
|
@@ -459,9 +462,24 @@ var NavigationHistory = class {
|
|
|
459
462
|
};
|
|
460
463
|
}
|
|
461
464
|
dispose() {
|
|
465
|
+
this.#disposed = true;
|
|
462
466
|
this.#nav.removeEventListener("navigate", this.#onNavigate);
|
|
467
|
+
this.#nav.removeEventListener("navigateerror", this.#onNavigateError);
|
|
463
468
|
this.#listeners.clear();
|
|
464
469
|
}
|
|
470
|
+
#track(result) {
|
|
471
|
+
result.finished?.catch(() => {});
|
|
472
|
+
return result.committed?.then(() => void 0) ?? Promise.resolve();
|
|
473
|
+
}
|
|
474
|
+
#fail(error) {
|
|
475
|
+
if (this.#disposed || isAbort(error)) return;
|
|
476
|
+
this.#onError(error);
|
|
477
|
+
}
|
|
478
|
+
#report(committed) {
|
|
479
|
+
committed.catch((error) => {
|
|
480
|
+
this.#fail(error);
|
|
481
|
+
});
|
|
482
|
+
}
|
|
465
483
|
#navigate(to, history, options) {
|
|
466
484
|
const url = new URL(to, this.#win.location.href).href;
|
|
467
485
|
const envelope = {
|
|
@@ -469,11 +487,11 @@ var NavigationHistory = class {
|
|
|
469
487
|
info: options.info,
|
|
470
488
|
scroll: options.scroll ?? "auto"
|
|
471
489
|
};
|
|
472
|
-
|
|
490
|
+
this.#report(this.#track(this.#nav.navigate(url, {
|
|
473
491
|
history,
|
|
474
492
|
info: envelope,
|
|
475
493
|
state: options.state ?? null
|
|
476
|
-
}));
|
|
494
|
+
})));
|
|
477
495
|
}
|
|
478
496
|
#read() {
|
|
479
497
|
const entry = this.#nav.currentEntry;
|
|
@@ -513,6 +531,10 @@ var NavigationHistory = class {
|
|
|
513
531
|
}
|
|
514
532
|
});
|
|
515
533
|
};
|
|
534
|
+
#onNavigateError = (event) => {
|
|
535
|
+
const error = event.error;
|
|
536
|
+
this.#fail(error);
|
|
537
|
+
};
|
|
516
538
|
};
|
|
517
539
|
const toRelative = (url) => {
|
|
518
540
|
if (url === null) return null;
|
|
@@ -520,6 +542,45 @@ const toRelative = (url) => {
|
|
|
520
542
|
return `${pathname}${search}${hash}`;
|
|
521
543
|
};
|
|
522
544
|
//#endregion
|
|
545
|
+
//#region src/lazy.ts
|
|
546
|
+
const noop = () => {};
|
|
547
|
+
/**
|
|
548
|
+
* defines a lazily-loaded component that can be preloaded.
|
|
549
|
+
*
|
|
550
|
+
* @param init component module loader
|
|
551
|
+
* @returns lazy component
|
|
552
|
+
*/
|
|
553
|
+
const lazy = (init) => {
|
|
554
|
+
const component = lazy$1(init);
|
|
555
|
+
return Object.assign(component, { preload() {
|
|
556
|
+
try {
|
|
557
|
+
const internals = component;
|
|
558
|
+
internals._init(internals._payload);
|
|
559
|
+
} catch {}
|
|
560
|
+
return Promise.try(init).then(noop, noop);
|
|
561
|
+
} });
|
|
562
|
+
};
|
|
563
|
+
const isPreloadable = (component) => {
|
|
564
|
+
return typeof component.preload === "function";
|
|
565
|
+
};
|
|
566
|
+
/**
|
|
567
|
+
* loads every preloadable component in a list.
|
|
568
|
+
*
|
|
569
|
+
* @param components components to load
|
|
570
|
+
* @returns promise that settles once loading finishes
|
|
571
|
+
*/
|
|
572
|
+
const preloadAll = (chain) => {
|
|
573
|
+
const pending = [];
|
|
574
|
+
for (const node of chain) {
|
|
575
|
+
const component = node.node.component;
|
|
576
|
+
if (!isPreloadable(component)) continue;
|
|
577
|
+
try {
|
|
578
|
+
pending.push(component.preload().then(noop, noop));
|
|
579
|
+
} catch {}
|
|
580
|
+
}
|
|
581
|
+
return Promise.all(pending).then(noop);
|
|
582
|
+
};
|
|
583
|
+
//#endregion
|
|
523
584
|
//#region src/build.ts
|
|
524
585
|
const SEGMENT = /:([A-Za-z_]\w*)/g;
|
|
525
586
|
var Builder = class {
|
|
@@ -573,18 +634,14 @@ var Builder = class {
|
|
|
573
634
|
};
|
|
574
635
|
//#endregion
|
|
575
636
|
//#region src/cache.ts
|
|
576
|
-
/**
|
|
577
|
-
|
|
578
|
-
*
|
|
579
|
-
* retains the active entry plus backward entries using an LRU policy. forward entries are dropped.
|
|
580
|
-
*/
|
|
581
|
-
const computeCachedKeys = (entries, activeIndex, recency, policy) => {
|
|
637
|
+
/** selects the active entry and recent backward entries for caching. */
|
|
638
|
+
const computeCachedKeys = (entries, activeKey, recency, policy) => {
|
|
582
639
|
const cached = /* @__PURE__ */ new Set();
|
|
583
|
-
const active = entries.find((entry) => entry.
|
|
584
|
-
if (active === void 0) throw new Error(`stacker: no history entry
|
|
640
|
+
const active = entries.find((entry) => entry.key === activeKey);
|
|
641
|
+
if (active === void 0) throw new Error(`stacker: no history entry with the active key '${activeKey}'`);
|
|
585
642
|
cached.add(active.key);
|
|
586
643
|
const backward = /* @__PURE__ */ new Map();
|
|
587
|
-
for (const entry of entries) if (entry.index <
|
|
644
|
+
for (const entry of entries) if (entry.index < active.index) backward.set(entry.key, entry);
|
|
588
645
|
let kept = 0;
|
|
589
646
|
const take = (key) => {
|
|
590
647
|
if (kept >= policy.max || cached.has(key) || !backward.has(key)) return;
|
|
@@ -808,8 +865,22 @@ var Router = class {
|
|
|
808
865
|
else this.#history.push(url, rest);
|
|
809
866
|
}
|
|
810
867
|
/**
|
|
868
|
+
* loads the code a route needs, without navigating to it.
|
|
869
|
+
*
|
|
870
|
+
* @param name route name
|
|
871
|
+
* @returns promise that settles once loading finishes
|
|
872
|
+
* @throws when `name` is not in the registry
|
|
873
|
+
*/
|
|
874
|
+
preload(name) {
|
|
875
|
+
const leaf = this.#routes.leaves.get(name);
|
|
876
|
+
if (leaf === void 0) throw new Error(`stacker: unknown route '${name}'`);
|
|
877
|
+
return preloadAll(leaf.chain);
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
811
880
|
* returns to the nearest existing history entry for a route, or pushes if none exists.
|
|
812
881
|
*
|
|
882
|
+
* pushes the destination if traversal to a matching entry fails.
|
|
883
|
+
*
|
|
813
884
|
* @param to route target, or a relative URL resolved against the active location
|
|
814
885
|
*/
|
|
815
886
|
popTo(to) {
|
|
@@ -819,13 +890,17 @@ var Router = class {
|
|
|
819
890
|
if (wantedMatch !== void 0) {
|
|
820
891
|
const wanted = this.#canonical(wantedMatch);
|
|
821
892
|
const entries = this.#history.entries();
|
|
822
|
-
|
|
893
|
+
const from = this.#history.location.key;
|
|
894
|
+
const start = entries.findIndex((entry) => entry.key === from);
|
|
895
|
+
for (let i = start; i >= 0; i--) {
|
|
823
896
|
const entry = entries[i];
|
|
824
897
|
if (entry === void 0 || !entry.sameDocument || entry.url === null) break;
|
|
825
898
|
const parts = parsePath(entry.url);
|
|
826
899
|
const match = this.#matcher.match(parts.pathname, parts.search, parts.hash);
|
|
827
900
|
if (match !== void 0 && match.name === wantedMatch.name && this.#canonical(match) === wanted) {
|
|
828
|
-
this.#history.traverseTo(entry.key)
|
|
901
|
+
this.#history.traverseTo(entry.key).catch(() => {
|
|
902
|
+
if (this.#history.location.key === from) this.#history.push(url);
|
|
903
|
+
});
|
|
829
904
|
return;
|
|
830
905
|
}
|
|
831
906
|
}
|
|
@@ -940,6 +1015,7 @@ var Router = class {
|
|
|
940
1015
|
}
|
|
941
1016
|
#record(location, scrollPos) {
|
|
942
1017
|
const match = this.#match(location);
|
|
1018
|
+
preloadAll(match.leaf.chain);
|
|
943
1019
|
const entry = {
|
|
944
1020
|
index: location.index,
|
|
945
1021
|
key: location.key,
|
|
@@ -957,7 +1033,7 @@ var Router = class {
|
|
|
957
1033
|
const cachedKeys = computeCachedKeys([...this.#entries.values()].map((entry) => ({
|
|
958
1034
|
index: entry.index,
|
|
959
1035
|
key: entry.key
|
|
960
|
-
})), active.
|
|
1036
|
+
})), active.key, this.#recency, { max: this.#max });
|
|
961
1037
|
const cached = [];
|
|
962
1038
|
for (const entry of this.#entries.values()) if (cachedKeys.has(entry.key)) cached.push({
|
|
963
1039
|
key: entry.key,
|
|
@@ -999,17 +1075,17 @@ const toTarget = (match) => ({
|
|
|
999
1075
|
});
|
|
1000
1076
|
const makeNotFound = (component) => {
|
|
1001
1077
|
const leafNode = {
|
|
1002
|
-
component,
|
|
1003
1078
|
kind: "route",
|
|
1079
|
+
component,
|
|
1004
1080
|
params: {},
|
|
1005
1081
|
path: "*",
|
|
1006
1082
|
query: {},
|
|
1007
1083
|
type: "page"
|
|
1008
1084
|
};
|
|
1009
1085
|
const node = {
|
|
1086
|
+
kind: "route",
|
|
1010
1087
|
id: " notFound",
|
|
1011
1088
|
key: " notFound",
|
|
1012
|
-
kind: "route",
|
|
1013
1089
|
node: leafNode,
|
|
1014
1090
|
params: {},
|
|
1015
1091
|
path: "*"
|
|
@@ -1244,4 +1320,4 @@ const Link = ({ children, onClick, replace, to, ...rest }) => {
|
|
|
1244
1320
|
});
|
|
1245
1321
|
};
|
|
1246
1322
|
//#endregion
|
|
1247
|
-
export { Link, NavigationHistory, Outlet, Router, RouterView, boolean, createRouterHooks, defineRoutes, enumOf, integer, layout, nonEmpty, optional, resolveMeta, route, string, useFocusEffect, useIsFocused, useLocation, useParams, useRoute, useRouter, withDefault };
|
|
1323
|
+
export { Link, NavigationHistory, Outlet, Router, RouterView, boolean, createRouterHooks, defineRoutes, enumOf, integer, layout, lazy, nonEmpty, optional, resolveMeta, route, string, useFocusEffect, useIsFocused, useLocation, useParams, useRoute, useRouter, withDefault };
|
package/dist/testing.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as HistoryLocation, i as HistoryListener, o as HistoryNavigateOptions, r as HistoryEntry, t as History } from "./types-
|
|
1
|
+
import { a as HistoryLocation, i as HistoryListener, o as HistoryNavigateOptions, r as HistoryEntry, t as History } from "./types-Bo4ESWAT.mjs";
|
|
2
2
|
//#region src/history/memory.d.ts
|
|
3
3
|
/** options for the memory history constructor. */
|
|
4
4
|
interface MemoryHistoryOptions {
|
|
@@ -22,7 +22,7 @@ declare class MemoryHistory implements History {
|
|
|
22
22
|
entries(): readonly HistoryEntry[];
|
|
23
23
|
push(to: string, options?: HistoryNavigateOptions): void;
|
|
24
24
|
replace(to: string, options?: HistoryNavigateOptions): void;
|
|
25
|
-
traverseTo(key: string): void
|
|
25
|
+
traverseTo(key: string): Promise<void>;
|
|
26
26
|
go(delta: number): void;
|
|
27
27
|
back(): void;
|
|
28
28
|
forward(): void;
|
package/dist/testing.mjs
CHANGED
|
@@ -67,8 +67,9 @@ var MemoryHistory = class {
|
|
|
67
67
|
}
|
|
68
68
|
traverseTo(key) {
|
|
69
69
|
const index = this.#entries.findIndex((entry) => entry.key === key);
|
|
70
|
-
if (index === -1)
|
|
70
|
+
if (index === -1) return Promise.reject(/* @__PURE__ */ new Error(`stacker: no history entry with key '${key}'`));
|
|
71
71
|
this.go(index - this.#index);
|
|
72
|
+
return Promise.resolve();
|
|
72
73
|
}
|
|
73
74
|
go(delta) {
|
|
74
75
|
const next = Math.max(0, Math.min(this.#index + delta, this.#entries.length - 1));
|
|
@@ -124,8 +124,9 @@ interface History {
|
|
|
124
124
|
* navigates to an existing entry by key.
|
|
125
125
|
*
|
|
126
126
|
* @param key target entry key
|
|
127
|
+
* @returns promise that resolves on commit and rejects if traversal fails
|
|
127
128
|
*/
|
|
128
|
-
traverseTo(key: string): void
|
|
129
|
+
traverseTo(key: string): Promise<void>;
|
|
129
130
|
}
|
|
130
131
|
//#endregion
|
|
131
132
|
export { HistoryLocation as a, HistoryUpdate as c, HistoryListener as i, HistoryAction as n, HistoryNavigateOptions as o, HistoryEntry as r, HistoryScrollBehavior as s, History as t };
|