@oomfware/stacker 0.2.2 → 0.2.4

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/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as parsePath, i as encodeSegment, n as decodeRemainder, o as resolvePath, r as encodeRemainder, t as createPath } from "./url-DerpiRjh.mjs";
1
+ import { a as parsePath, i as encodeSegment, n as decodeRemainder, o as resolvePath, r as encodeRemainder, t as createPath } from "./url-ghfWE9j6.mjs";
2
2
  import { Suspense, createContext, lazy as lazy$1, useContext, useEffect, useLayoutEffect, useMemo, useSyncExternalStore } from "react";
3
3
  import { SimpleEventEmitter } from "@mary-ext/simple-event-emitter";
4
4
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
@@ -185,9 +185,9 @@ const defineRoutes = (tree) => {
185
185
  kind: node.kind,
186
186
  id,
187
187
  key,
188
- node,
188
+ path: node.kind === "route" ? node.path : void 0,
189
189
  params,
190
- path: node.kind === "route" ? node.path : void 0
190
+ node
191
191
  };
192
192
  ids.add(id);
193
193
  const chain = [...parentChain, resolved];
@@ -210,12 +210,12 @@ const defineRoutes = (tree) => {
210
210
  for (const layoutParam of Object.keys(ancestor.params)) if (!ownPathParams.includes(layoutParam)) throw new Error(`stacker: layout '${ancestor.id}' param '${layoutParam}' is not present in route '${key}' path '${node.path}'`);
211
211
  }
212
212
  leaves.set(key, {
213
- chain,
214
- leaf: node,
215
213
  name: key,
216
- params,
217
214
  path: node.path,
218
- query
215
+ params,
216
+ query,
217
+ leaf: node,
218
+ chain
219
219
  });
220
220
  }
221
221
  };
@@ -259,16 +259,16 @@ const compile = (path) => {
259
259
  if (splat !== void 0) {
260
260
  paramNames.push(splat);
261
261
  return {
262
- hasSplat: true,
262
+ regex: new RegExp(`^${pattern}(?:/(.*))?$`),
263
263
  paramNames,
264
- regex: new RegExp(`^${pattern}(?:/(.*))?$`)
264
+ hasSplat: true
265
265
  };
266
266
  }
267
267
  const suffix = pattern.endsWith("/") ? "" : "/?";
268
268
  return {
269
- hasSplat: false,
269
+ regex: new RegExp(`^${pattern}${suffix}$`),
270
270
  paramNames,
271
- regex: new RegExp(`^${pattern}${suffix}$`)
271
+ hasSplat: false
272
272
  };
273
273
  };
274
274
  /** matches URLs in declaration order against a compiled route registry. */
@@ -276,22 +276,22 @@ var Matcher = class {
276
276
  #compiled;
277
277
  constructor(registry) {
278
278
  this.#compiled = [...registry.leaves.values()].map((leaf) => {
279
- const { hasSplat, paramNames, regex } = compile(leaf.path);
279
+ const { regex, paramNames, hasSplat } = compile(leaf.path);
280
280
  return {
281
- hasSplat,
282
281
  leaf,
282
+ regex,
283
283
  paramNames,
284
- regex
284
+ hasSplat
285
285
  };
286
286
  });
287
287
  }
288
288
  match(pathname, search = "", hash = "") {
289
289
  const rawSearch = new URLSearchParams(search);
290
290
  const ctx = {
291
- hash,
292
291
  pathname,
293
- rawSearch,
294
- search
292
+ search,
293
+ hash,
294
+ rawSearch
295
295
  };
296
296
  for (const compiled of this.#compiled) {
297
297
  const { leaf, regex } = compiled;
@@ -315,14 +315,14 @@ var Matcher = class {
315
315
  };
316
316
  });
317
317
  return {
318
- chain,
319
- leaf,
320
318
  name: leaf.name,
321
- params: chain.at(-1)?.params ?? {}
319
+ params: chain.at(-1)?.params ?? {},
320
+ leaf,
321
+ chain
322
322
  };
323
323
  }
324
324
  }
325
- #rawValues({ hasSplat, paramNames }, execed) {
325
+ #rawValues({ paramNames, hasSplat }, execed) {
326
326
  const splatIndex = hasSplat ? paramNames.length - 1 : -1;
327
327
  const raw = {};
328
328
  for (const [i, name] of paramNames.entries()) {
@@ -389,6 +389,7 @@ const toAction = (type) => {
389
389
  }
390
390
  };
391
391
  const isAbort = (error) => typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
392
+ const isWebKit = (userAgent) => /AppleWebKit\//.test(userAgent) && !/(?:Chrome|Chromium)\//.test(userAgent);
392
393
  /**
393
394
  * history implementation backed by the web navigation API.
394
395
  *
@@ -403,14 +404,17 @@ var NavigationHistory = class {
403
404
  #win;
404
405
  #disposed = false;
405
406
  #location;
407
+ #interceptTraversals;
406
408
  constructor(options = {}) {
407
409
  this.#win = options.window ?? window;
408
410
  this.#nav = this.#win.navigation;
409
411
  this.#ignore = options.ignore;
412
+ this.#interceptTraversals = !isWebKit(this.#win.navigator.userAgent);
410
413
  this.#onError = options.onError ?? ((error) => {
411
414
  this.#win.reportError(error);
412
415
  });
413
416
  this.#location = this.#read();
417
+ this.#nav.addEventListener("currententrychange", this.#onCurrentEntryChange);
414
418
  this.#nav.addEventListener("navigate", this.#onNavigate);
415
419
  this.#nav.addEventListener("navigateerror", this.#onNavigateError);
416
420
  this.#win.history.scrollRestoration = "manual";
@@ -426,12 +430,12 @@ var NavigationHistory = class {
426
430
  }
427
431
  entries() {
428
432
  return this.#nav.entries().map((entry) => ({
433
+ key: entry.key,
429
434
  id: entry.id,
430
435
  index: entry.index,
431
- key: entry.key,
436
+ url: toRelative(entry.url),
432
437
  sameDocument: entry.sameDocument,
433
- state: entry.getState(),
434
- url: toRelative(entry.url)
438
+ state: entry.getState()
435
439
  }));
436
440
  }
437
441
  push(to, options = {}) {
@@ -440,6 +444,9 @@ var NavigationHistory = class {
440
444
  replace(to, options = {}) {
441
445
  this.#navigate(to, "replace", options);
442
446
  }
447
+ updateState(state) {
448
+ this.#nav.updateCurrentEntry({ state });
449
+ }
443
450
  traverseTo(key) {
444
451
  return this.#track(this.#nav.traverseTo(key));
445
452
  }
@@ -463,6 +470,7 @@ var NavigationHistory = class {
463
470
  }
464
471
  dispose() {
465
472
  this.#disposed = true;
473
+ this.#nav.removeEventListener("currententrychange", this.#onCurrentEntryChange);
466
474
  this.#nav.removeEventListener("navigate", this.#onNavigate);
467
475
  this.#nav.removeEventListener("navigateerror", this.#onNavigateError);
468
476
  this.#listeners.clear();
@@ -475,8 +483,11 @@ var NavigationHistory = class {
475
483
  if (this.#disposed || isAbort(error)) return;
476
484
  this.#onError(error);
477
485
  }
478
- #report(committed) {
479
- committed.catch((error) => {
486
+ async #notify(update) {
487
+ await Promise.all([...this.#listeners].map(async (listener) => listener(update)));
488
+ }
489
+ #report(work) {
490
+ work.catch((error) => {
480
491
  this.#fail(error);
481
492
  });
482
493
  }
@@ -484,33 +495,65 @@ var NavigationHistory = class {
484
495
  const url = new URL(to, this.#win.location.href).href;
485
496
  const envelope = {
486
497
  [ENVELOPE]: true,
487
- info: options.info,
488
- scroll: options.scroll ?? "auto"
498
+ scroll: options.scroll ?? "auto",
499
+ info: options.info
489
500
  };
490
501
  this.#report(this.#track(this.#nav.navigate(url, {
491
- history,
502
+ state: options.state ?? null,
492
503
  info: envelope,
493
- state: options.state ?? null
504
+ history
494
505
  })));
495
506
  }
496
507
  #read() {
497
508
  const entry = this.#nav.currentEntry;
498
509
  if (entry === null) throw new Error("stacker: the navigation API has no current entry (an opaque origin?)");
499
- const { hash, pathname, search } = new URL(entry.url ?? this.#win.location.href);
510
+ const { pathname, search, hash } = new URL(entry.url ?? this.#win.location.href);
500
511
  return {
501
- hash,
512
+ key: entry.key,
502
513
  id: entry.id,
503
514
  index: entry.index,
504
- key: entry.key,
505
515
  pathname,
506
516
  search,
517
+ hash,
507
518
  state: entry.getState()
508
519
  };
509
520
  }
521
+ #onCurrentEntryChange = (event) => {
522
+ if (event.navigationType === "traverse" && !this.#interceptTraversals) {
523
+ this.#report(this.#traverseUnintercepted());
524
+ return;
525
+ }
526
+ if (event.navigationType !== null) return;
527
+ this.#location = this.#read();
528
+ this.#report(this.#notify({
529
+ action: "update",
530
+ location: this.#location,
531
+ scroll: "preserve",
532
+ info: void 0
533
+ }));
534
+ };
535
+ async #traverseUnintercepted() {
536
+ const doc = this.#win.document;
537
+ const focused = doc.activeElement;
538
+ this.#location = this.#read();
539
+ await this.#notify({
540
+ action: "traverse",
541
+ location: this.#location,
542
+ scroll: "auto",
543
+ info: void 0
544
+ });
545
+ if (doc.activeElement !== focused) return;
546
+ const body = doc.body;
547
+ const hadTabIndex = body.hasAttribute("tabindex");
548
+ if (!hadTabIndex) body.tabIndex = -1;
549
+ body.focus({ preventScroll: true });
550
+ if (!hadTabIndex) body.removeAttribute("tabindex");
551
+ }
510
552
  #onNavigate = (event) => {
511
553
  const action = toAction(event.navigationType);
512
554
  if (action === void 0 || !event.canIntercept) return;
513
555
  if (event.downloadRequest !== null || event.formData !== null) return;
556
+ if (action === "traverse" && !this.#interceptTraversals) return;
514
557
  if (action !== "traverse" && this.#ignore?.(new URL(event.destination.url))) return;
515
558
  const envelope = isEnvelope(event.info) ? event.info : void 0;
516
559
  const info = envelope === void 0 ? event.info : envelope.info;
@@ -521,13 +564,12 @@ var NavigationHistory = class {
521
564
  scroll: "manual",
522
565
  handler: async () => {
523
566
  this.#location = this.#read();
524
- const update = {
567
+ await this.#notify({
525
568
  action,
526
- info,
527
569
  location: this.#location,
528
- scroll
529
- };
530
- await Promise.all([...this.#listeners].map(async (listener) => listener(update)));
570
+ scroll,
571
+ info
572
+ });
531
573
  }
532
574
  });
533
575
  };
@@ -538,7 +580,7 @@ var NavigationHistory = class {
538
580
  };
539
581
  const toRelative = (url) => {
540
582
  if (url === null) return null;
541
- const { hash, pathname, search } = new URL(url);
583
+ const { pathname, search, hash } = new URL(url);
542
584
  return `${pathname}${search}${hash}`;
543
585
  };
544
586
  //#endregion
@@ -626,9 +668,9 @@ var Builder = class {
626
668
  }
627
669
  const query = search.toString();
628
670
  return createPath({
629
- hash: "",
630
671
  pathname,
631
- search: query ? `?${query}` : ""
672
+ search: query ? `?${query}` : "",
673
+ hash: ""
632
674
  });
633
675
  }
634
676
  };
@@ -698,10 +740,10 @@ const computeView = (cached, pinned, activeKey) => {
698
740
  let inst = byKey.get(key);
699
741
  if (inst === void 0) {
700
742
  inst = {
701
- children: [],
702
743
  instanceKey: key,
703
744
  node: matched.node,
704
- params: matched.params
745
+ params: matched.params,
746
+ children: []
705
747
  };
706
748
  byKey.set(key, inst);
707
749
  pool.push(inst);
@@ -714,8 +756,8 @@ const computeView = (cached, pinned, activeKey) => {
714
756
  const active = cached.find((entry) => entry.key === activeKey);
715
757
  if (active === void 0) throw new Error(`stacker: active entry '${activeKey}' is not in the cached set`);
716
758
  return {
717
- activePath: active.match.chain.map((matched) => instanceKey(matched, active.key)),
718
- roots
759
+ roots,
760
+ activePath: active.match.chain.map((matched) => instanceKey(matched, active.key))
719
761
  };
720
762
  };
721
763
  //#endregion
@@ -766,6 +808,10 @@ var Router = class {
766
808
  this.#record(location, TOP);
767
809
  this.#view = this.#recompute();
768
810
  this.#unlisten = this.#history.listen(async ({ action, location: next, scroll }) => {
811
+ if (action === "update") {
812
+ this.#updates.emit();
813
+ return;
814
+ }
769
815
  const win = this.#win;
770
816
  const restoring = scroll === "auto" && win !== null;
771
817
  if (restoring) {
@@ -849,7 +895,7 @@ var Router = class {
849
895
  * @returns the target it resolves to, or undefined when no route matches
850
896
  */
851
897
  match(to) {
852
- const { hash, pathname, search } = resolvePath(to, this.#history.location);
898
+ const { pathname, search, hash } = resolvePath(to, this.#history.location);
853
899
  const match = this.#matcher.match(pathname, search, hash);
854
900
  return match === void 0 ? void 0 : toTarget(match);
855
901
  }
@@ -859,7 +905,7 @@ var Router = class {
859
905
  * @param options where to go, and how to get there
860
906
  */
861
907
  navigate(options) {
862
- const { replace = false, to, ...rest } = options;
908
+ const { to, replace = false, ...rest } = options;
863
909
  const url = typeof to === "string" ? to : this.#builder.build(to);
864
910
  if (replace) this.#history.replace(url, rest);
865
911
  else this.#history.push(url, rest);
@@ -885,7 +931,7 @@ var Router = class {
885
931
  */
886
932
  popTo(to) {
887
933
  const url = typeof to === "string" ? to : this.#builder.build(to);
888
- const { hash, pathname, search } = resolvePath(url, this.#history.location);
934
+ const { pathname, search, hash } = resolvePath(url, this.#history.location);
889
935
  const wantedMatch = this.#matcher.match(pathname, search, hash);
890
936
  if (wantedMatch !== void 0) {
891
937
  const wanted = this.#canonical(wantedMatch);
@@ -932,15 +978,28 @@ var Router = class {
932
978
  }
933
979
  const query = search.toString();
934
980
  const to = createPath({
935
- hash: location.hash,
936
981
  pathname: location.pathname,
937
- search: query ? `?${query}` : ""
982
+ search: query ? `?${query}` : "",
983
+ hash: location.hash
938
984
  });
939
985
  this.#history.replace(to, {
940
- scroll: "preserve",
941
- state: location.state
986
+ state: location.state,
987
+ scroll: "preserve"
942
988
  });
943
989
  }
990
+ /**
991
+ * rewrites the active history entry's state, without navigating.
992
+ *
993
+ * the entry keeps its URL and slot, so the screen stays mounted and the viewport does not move. the whole
994
+ * state is replaced, so build the next value from `router.location.state` to keep the parts you still want.
995
+ * readers of `useLocation` re-render with it.
996
+ *
997
+ * @param state state data to store on the entry
998
+ * @throws when the state cannot be structured-cloned, leaving the entry as it was
999
+ */
1000
+ updateState(state) {
1001
+ this.#history.updateState(state);
1002
+ }
944
1003
  /** navigates back one entry. */
945
1004
  back() {
946
1005
  this.#history.back();
@@ -1017,11 +1076,11 @@ var Router = class {
1017
1076
  const match = this.#match(location);
1018
1077
  preloadAll(match.leaf.chain);
1019
1078
  const entry = {
1020
- index: location.index,
1021
1079
  key: location.key,
1080
+ index: location.index,
1022
1081
  match,
1023
- scrollPos,
1024
- target: toTarget(match)
1082
+ target: toTarget(match),
1083
+ scrollPos
1025
1084
  };
1026
1085
  this.#entries.set(location.key, entry);
1027
1086
  this.#recency = [entry.key, ...this.#recency.filter((key) => key !== entry.key)];
@@ -1031,8 +1090,8 @@ var Router = class {
1031
1090
  #recompute() {
1032
1091
  const active = this.#active();
1033
1092
  const cachedKeys = computeCachedKeys([...this.#entries.values()].map((entry) => ({
1034
- index: entry.index,
1035
- key: entry.key
1093
+ key: entry.key,
1094
+ index: entry.index
1036
1095
  })), active.key, this.#recency, { max: this.#max });
1037
1096
  const cached = [];
1038
1097
  for (const entry of this.#entries.values()) if (cachedKeys.has(entry.key)) cached.push({
@@ -1049,7 +1108,7 @@ var Router = class {
1049
1108
  if (leaf === void 0) throw new Error(`stacker: pinned route '${name}' is not in the registry`);
1050
1109
  if ((leaf.leaf.type ?? "page") !== "singleton") throw new Error(`stacker: pinned route '${name}' must be a singleton`);
1051
1110
  if (Object.keys(leaf.params).length > 0) throw new Error(`stacker: pinned route '${name}' must have no path params`);
1052
- const { hash, pathname, search } = parsePath(leaf.path);
1111
+ const { pathname, search, hash } = parsePath(leaf.path);
1053
1112
  const match = this.#matcher.match(pathname, search, hash);
1054
1113
  if (match === void 0) throw new Error(`stacker: pinned route '${name}' (path '${leaf.path}') did not match`);
1055
1114
  return {
@@ -1059,13 +1118,13 @@ var Router = class {
1059
1118
  }
1060
1119
  #notFoundMatch() {
1061
1120
  return {
1121
+ name: this.#notFound.leaf.name,
1122
+ params: {},
1123
+ leaf: this.#notFound.leaf,
1062
1124
  chain: [{
1063
1125
  node: this.#notFound.node,
1064
1126
  params: {}
1065
- }],
1066
- leaf: this.#notFound.leaf,
1067
- name: this.#notFound.leaf.name,
1068
- params: {}
1127
+ }]
1069
1128
  };
1070
1129
  }
1071
1130
  };
@@ -1076,28 +1135,28 @@ const toTarget = (match) => ({
1076
1135
  const makeNotFound = (component) => {
1077
1136
  const leafNode = {
1078
1137
  kind: "route",
1079
- component,
1080
- params: {},
1081
1138
  path: "*",
1139
+ params: {},
1082
1140
  query: {},
1083
- type: "page"
1141
+ type: "page",
1142
+ component
1084
1143
  };
1085
1144
  const node = {
1086
1145
  kind: "route",
1087
1146
  id: " notFound",
1088
1147
  key: " notFound",
1089
- node: leafNode,
1148
+ path: "*",
1090
1149
  params: {},
1091
- path: "*"
1150
+ node: leafNode
1092
1151
  };
1093
1152
  return {
1094
1153
  leaf: {
1095
- chain: [node],
1096
- leaf: leafNode,
1097
1154
  name: " notFound",
1098
- params: {},
1099
1155
  path: "*",
1100
- query: {}
1156
+ params: {},
1157
+ query: {},
1158
+ leaf: leafNode,
1159
+ chain: [node]
1101
1160
  },
1102
1161
  node
1103
1162
  };
@@ -1147,11 +1206,11 @@ const ScrollRestorer = ({ router, view }) => {
1147
1206
  const Pool = ({ nodes }) => {
1148
1207
  const activePath = useContext(ActiveChainContext);
1149
1208
  return /* @__PURE__ */ jsx(Fragment, { children: nodes.map((node) => /* @__PURE__ */ jsx(Branch, {
1150
- focused: activePath.includes(node.instanceKey),
1151
- node
1209
+ node,
1210
+ focused: activePath.includes(node.instanceKey)
1152
1211
  }, node.instanceKey)) });
1153
1212
  };
1154
- const Branch = ({ focused, node }) => {
1213
+ const Branch = ({ node, focused }) => {
1155
1214
  const resolved = node.node;
1156
1215
  const Component = resolved.node.component;
1157
1216
  const router = useContext(RouterContext);
@@ -1162,8 +1221,8 @@ const Branch = ({ focused, node }) => {
1162
1221
  style: { display: focused ? "contents" : "none" },
1163
1222
  children: /* @__PURE__ */ jsx(CurrentNodeContext, {
1164
1223
  value: {
1165
- focused,
1166
- node
1224
+ node,
1225
+ focused
1167
1226
  },
1168
1227
  children: /* @__PURE__ */ jsx(Suspense, {
1169
1228
  fallback: fallback ?? null,
@@ -1267,6 +1326,11 @@ const createRouterHooks = (routes) => {
1267
1326
  return router;
1268
1327
  };
1269
1328
  return {
1329
+ useRouter: useTypedRouter,
1330
+ useTarget() {
1331
+ const router = useTypedRouter();
1332
+ return useSyncExternalStore(router.subscribe, () => router.target);
1333
+ },
1270
1334
  useParams(name) {
1271
1335
  const router = useTypedRouter();
1272
1336
  const current = useContext(CurrentNodeContext);
@@ -1276,11 +1340,6 @@ const createRouterHooks = (routes) => {
1276
1340
  if (current === null) throw new Error("stacker: useParams must be used within a route component");
1277
1341
  if (current.node.node.kind !== "route" || current.node.node.key !== name) throw new Error(`stacker: useParams('${name}') called under ${current.node.node.kind} '${current.node.node.key}'`);
1278
1342
  return [current.node.params, replace];
1279
- },
1280
- useRouter: useTypedRouter,
1281
- useTarget() {
1282
- const router = useTypedRouter();
1283
- return useSyncExternalStore(router.subscribe, () => router.target);
1284
1343
  }
1285
1344
  };
1286
1345
  };
@@ -1302,7 +1361,7 @@ const isRoutable = (to) => {
1302
1361
  * @param props link properties
1303
1362
  * @returns rendered anchor element
1304
1363
  */
1305
- const Link = ({ children, onClick, replace, to, ...rest }) => {
1364
+ const Link = ({ to, replace, children, onClick, ...rest }) => {
1306
1365
  const router = useRouter();
1307
1366
  return /* @__PURE__ */ jsx("a", {
1308
1367
  href: to,
@@ -1311,8 +1370,8 @@ const Link = ({ children, onClick, replace, to, ...rest }) => {
1311
1370
  if (event.defaultPrevented || isModifiedClick(event) || rest.download !== void 0 || rest.target !== void 0 && rest.target !== "" && rest.target !== "_self" || !isRoutable(to)) return;
1312
1371
  event.preventDefault();
1313
1372
  router.navigate({
1314
- replace,
1315
- to
1373
+ to,
1374
+ replace
1316
1375
  });
1317
1376
  },
1318
1377
  ...rest,
@@ -1,11 +1,11 @@
1
- import { a as HistoryLocation, i as HistoryListener, o as HistoryNavigateOptions, r as HistoryEntry, t as History } from "./types-Bo4ESWAT.mjs";
1
+ import { a as HistoryLocation, i as HistoryListener, o as HistoryNavigateOptions, r as HistoryEntry, t as History } from "./types--iOZjfC1.mjs";
2
2
  //#region src/history/memory.d.ts
3
3
  /** options for the memory history constructor. */
4
4
  interface MemoryHistoryOptions {
5
- /** initially active entry index. */
6
- readonly index?: number;
7
5
  /** initial entry relative URLs. */
8
6
  readonly initialEntries?: readonly string[];
7
+ /** initially active entry index. */
8
+ readonly index?: number;
9
9
  }
10
10
  /**
11
11
  * in-memory history manager for testing.
@@ -22,6 +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
+ updateState(state: unknown): void;
25
26
  traverseTo(key: string): Promise<void>;
26
27
  go(delta: number): void;
27
28
  back(): void;
package/dist/testing.mjs CHANGED
@@ -1,10 +1,10 @@
1
- import { o as resolvePath, t as createPath } from "./url-DerpiRjh.mjs";
1
+ import { o as resolvePath, t as createPath } from "./url-ghfWE9j6.mjs";
2
2
  import { SimpleEventEmitter } from "@mary-ext/simple-event-emitter";
3
3
  //#region src/history/memory.ts
4
4
  const ROOT = {
5
- hash: "",
6
5
  pathname: "/",
7
- search: ""
6
+ search: "",
7
+ hash: ""
8
8
  };
9
9
  /**
10
10
  * in-memory history manager for testing.
@@ -34,12 +34,12 @@ var MemoryHistory = class {
34
34
  }
35
35
  entries() {
36
36
  return this.#entries.map((location) => ({
37
+ key: location.key,
37
38
  id: location.id,
38
39
  index: location.index,
39
- key: location.key,
40
+ url: createPath(location),
40
41
  sameDocument: true,
41
- state: location.state,
42
- url: createPath(location)
42
+ state: location.state
43
43
  }));
44
44
  }
45
45
  push(to, options = {}) {
@@ -49,9 +49,9 @@ var MemoryHistory = class {
49
49
  this.#entries = [...this.#entries.slice(0, this.#index), location];
50
50
  this.#emit({
51
51
  action: "push",
52
- info: options.info,
53
52
  location,
54
- scroll: options.scroll ?? "auto"
53
+ scroll: options.scroll ?? "auto",
54
+ info: options.info
55
55
  });
56
56
  }
57
57
  replace(to, options = {}) {
@@ -60,9 +60,22 @@ var MemoryHistory = class {
60
60
  this.#entries = this.#entries.with(this.#index, location);
61
61
  this.#emit({
62
62
  action: "replace",
63
- info: options.info,
64
63
  location,
65
- scroll: options.scroll ?? "auto"
64
+ scroll: options.scroll ?? "auto",
65
+ info: options.info
66
+ });
67
+ }
68
+ updateState(state) {
69
+ const location = {
70
+ ...this.#current(),
71
+ state: structuredClone(state)
72
+ };
73
+ this.#entries = this.#entries.with(this.#index, location);
74
+ this.#emit({
75
+ action: "update",
76
+ location,
77
+ scroll: "preserve",
78
+ info: void 0
66
79
  });
67
80
  }
68
81
  traverseTo(key) {
@@ -77,9 +90,9 @@ var MemoryHistory = class {
77
90
  this.#index = next;
78
91
  this.#emit({
79
92
  action: "traverse",
80
- info: void 0,
81
93
  location: this.#current(),
82
- scroll: "auto"
94
+ scroll: "auto",
95
+ info: void 0
83
96
  });
84
97
  }
85
98
  back() {
@@ -101,15 +114,15 @@ var MemoryHistory = class {
101
114
  return `k${this.#keySeq++}`;
102
115
  }
103
116
  #make(to, base, id, key, index, state) {
104
- const { hash, pathname, search } = resolvePath(to, base);
117
+ const { pathname, search, hash } = resolvePath(to, base);
105
118
  return {
106
- hash,
119
+ key,
107
120
  id,
108
121
  index,
109
- key,
110
122
  pathname,
111
123
  search,
112
- state
124
+ hash,
125
+ state: structuredClone(state)
113
126
  };
114
127
  }
115
128
  #current() {