@multiplatform.one/router 9.16.1 → 10.0.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 (250) hide show
  1. package/dist/cjs/hooks/index.cjs +17 -19
  2. package/dist/cjs/hooks/index.native.cjs +31 -0
  3. package/dist/cjs/hooks/index.native.js +17 -18
  4. package/dist/cjs/hooks/index.native.js.map +1 -1
  5. package/dist/cjs/hooks/useUrlState.cjs +175 -180
  6. package/dist/cjs/hooks/useUrlState.native.cjs +215 -0
  7. package/dist/cjs/hooks/useUrlState.native.js +187 -196
  8. package/dist/cjs/hooks/useUrlState.native.js.map +1 -1
  9. package/dist/cjs/index.cjs +9 -11
  10. package/dist/cjs/index.native.cjs +22 -0
  11. package/dist/cjs/index.native.js +9 -10
  12. package/dist/cjs/index.native.js.map +1 -1
  13. package/dist/cjs/index.storybook.cjs +17 -19
  14. package/dist/cjs/index.storybook.native.cjs +34 -0
  15. package/dist/cjs/index.storybook.native.js +17 -18
  16. package/dist/cjs/index.storybook.native.js.map +1 -1
  17. package/dist/cjs/router/EmptyRouter.cjs +51 -58
  18. package/dist/cjs/router/EmptyRouter.native.cjs +98 -0
  19. package/dist/cjs/router/EmptyRouter.native.js +72 -78
  20. package/dist/cjs/router/EmptyRouter.native.js.map +1 -1
  21. package/dist/cjs/router/ReactRouter.cjs +72 -87
  22. package/dist/cjs/router/ReactRouter.native.cjs +119 -0
  23. package/dist/cjs/router/ReactRouter.native.js +95 -109
  24. package/dist/cjs/router/ReactRouter.native.js.map +1 -1
  25. package/dist/cjs/router/binding.cjs +18 -20
  26. package/dist/cjs/router/binding.native.cjs +35 -0
  27. package/dist/cjs/router/binding.native.js +18 -19
  28. package/dist/cjs/router/binding.native.js.map +1 -1
  29. package/dist/cjs/router/expoRouter.cjs +22 -26
  30. package/dist/cjs/router/expoRouter.native.cjs +45 -0
  31. package/dist/cjs/router/expoRouter.native.js +22 -25
  32. package/dist/cjs/router/expoRouter.native.js.map +1 -1
  33. package/dist/cjs/router/expoRouterBinding.cjs +35 -39
  34. package/dist/cjs/router/expoRouterBinding.native.cjs +61 -0
  35. package/dist/cjs/router/expoRouterBinding.native.js +43 -46
  36. package/dist/cjs/router/expoRouterBinding.native.js.map +1 -1
  37. package/dist/cjs/router/index.cjs +17 -19
  38. package/dist/cjs/router/index.native.cjs +51 -0
  39. package/dist/cjs/router/index.native.js +28 -29
  40. package/dist/cjs/router/index.native.js.map +1 -1
  41. package/dist/cjs/router/index.storybook-expo.cjs +9 -11
  42. package/dist/cjs/router/index.storybook-expo.native.cjs +20 -0
  43. package/dist/cjs/router/index.storybook-expo.native.js +9 -10
  44. package/dist/cjs/router/index.storybook-expo.native.js.map +1 -1
  45. package/dist/cjs/router/index.storybook.cjs +9 -11
  46. package/dist/cjs/router/index.storybook.native.cjs +20 -0
  47. package/dist/cjs/router/index.storybook.native.js +9 -10
  48. package/dist/cjs/router/index.storybook.native.js.map +1 -1
  49. package/dist/cjs/router/index.webext.cjs +9 -11
  50. package/dist/cjs/router/index.webext.native.cjs +20 -0
  51. package/dist/cjs/router/index.webext.native.js +9 -10
  52. package/dist/cjs/router/index.webext.native.js.map +1 -1
  53. package/dist/cjs/router/nativeRouter.cjs +33 -41
  54. package/dist/cjs/router/nativeRouter.native.cjs +72 -0
  55. package/dist/cjs/router/nativeRouter.native.js +42 -49
  56. package/dist/cjs/router/nativeRouter.native.js.map +1 -1
  57. package/dist/cjs/seam/DomNavigationHost.cjs +138 -150
  58. package/dist/cjs/seam/DomNavigationHost.native.cjs +233 -0
  59. package/dist/cjs/seam/DomNavigationHost.native.js +201 -207
  60. package/dist/cjs/seam/DomNavigationHost.native.js.map +1 -1
  61. package/dist/cjs/seam/index.cjs +31 -33
  62. package/dist/cjs/seam/index.native.cjs +47 -0
  63. package/dist/cjs/seam/index.native.js +31 -32
  64. package/dist/cjs/seam/index.native.js.map +1 -1
  65. package/dist/cjs/seam/navigator.cjs +309 -319
  66. package/dist/cjs/seam/navigator.native.cjs +525 -0
  67. package/dist/cjs/seam/navigator.native.js +490 -498
  68. package/dist/cjs/seam/navigator.native.js.map +1 -1
  69. package/dist/cjs/seam/oneAdapter.cjs +86 -93
  70. package/dist/cjs/seam/oneAdapter.native.cjs +133 -0
  71. package/dist/cjs/seam/oneAdapter.native.js +94 -104
  72. package/dist/cjs/seam/oneAdapter.native.js.map +1 -1
  73. package/dist/cjs/types.cjs +9 -11
  74. package/dist/cjs/types.native.cjs +18 -0
  75. package/dist/cjs/types.native.js +9 -10
  76. package/dist/cjs/types.native.js.map +1 -1
  77. package/dist/esm/hooks/index.mjs +3 -3
  78. package/dist/esm/hooks/index.native.js +3 -3
  79. package/dist/esm/hooks/useUrlState.mjs +160 -161
  80. package/dist/esm/hooks/useUrlState.mjs.map +1 -1
  81. package/dist/esm/hooks/useUrlState.native.js +171 -177
  82. package/dist/esm/hooks/useUrlState.native.js.map +1 -1
  83. package/dist/esm/index.mjs +5 -4
  84. package/dist/esm/index.native.js +5 -4
  85. package/dist/esm/index.storybook.mjs +7 -5
  86. package/dist/esm/index.storybook.native.js +7 -5
  87. package/dist/esm/router/EmptyRouter.mjs +35 -38
  88. package/dist/esm/router/EmptyRouter.mjs.map +1 -1
  89. package/dist/esm/router/EmptyRouter.native.js +57 -60
  90. package/dist/esm/router/EmptyRouter.native.js.map +1 -1
  91. package/dist/esm/router/ReactRouter.mjs +59 -70
  92. package/dist/esm/router/ReactRouter.mjs.map +1 -1
  93. package/dist/esm/router/ReactRouter.native.js +82 -93
  94. package/dist/esm/router/ReactRouter.native.js.map +1 -1
  95. package/dist/esm/router/binding.mjs +4 -3
  96. package/dist/esm/router/binding.mjs.map +1 -1
  97. package/dist/esm/router/binding.native.js +4 -3
  98. package/dist/esm/router/binding.native.js.map +1 -1
  99. package/dist/esm/router/expoRouter.mjs +7 -7
  100. package/dist/esm/router/expoRouter.mjs.map +1 -1
  101. package/dist/esm/router/expoRouter.native.js +7 -7
  102. package/dist/esm/router/expoRouter.native.js.map +1 -1
  103. package/dist/esm/router/expoRouterBinding.mjs +23 -21
  104. package/dist/esm/router/expoRouterBinding.mjs.map +1 -1
  105. package/dist/esm/router/expoRouterBinding.native.js +31 -29
  106. package/dist/esm/router/expoRouterBinding.native.js.map +1 -1
  107. package/dist/esm/router/index.mjs +2 -2
  108. package/dist/esm/router/index.native.js +13 -11
  109. package/dist/esm/router/index.native.js.map +1 -1
  110. package/dist/esm/router/index.storybook-expo.mjs +1 -2
  111. package/dist/esm/router/index.storybook-expo.native.js +1 -2
  112. package/dist/esm/router/index.storybook.mjs +1 -2
  113. package/dist/esm/router/index.storybook.native.js +1 -2
  114. package/dist/esm/router/index.webext.mjs +1 -2
  115. package/dist/esm/router/index.webext.native.js +1 -2
  116. package/dist/esm/router/nativeRouter.mjs +19 -23
  117. package/dist/esm/router/nativeRouter.mjs.map +1 -1
  118. package/dist/esm/router/nativeRouter.native.js +29 -33
  119. package/dist/esm/router/nativeRouter.native.js.map +1 -1
  120. package/dist/esm/seam/DomNavigationHost.mjs +125 -133
  121. package/dist/esm/seam/DomNavigationHost.mjs.map +1 -1
  122. package/dist/esm/seam/DomNavigationHost.native.js +189 -192
  123. package/dist/esm/seam/DomNavigationHost.native.js.map +1 -1
  124. package/dist/esm/seam/index.mjs +2 -2
  125. package/dist/esm/seam/index.native.js +2 -2
  126. package/dist/esm/seam/navigator.mjs +292 -298
  127. package/dist/esm/seam/navigator.mjs.map +1 -1
  128. package/dist/esm/seam/navigator.native.js +473 -478
  129. package/dist/esm/seam/navigator.native.js.map +1 -1
  130. package/dist/esm/seam/oneAdapter.mjs +61 -64
  131. package/dist/esm/seam/oneAdapter.mjs.map +1 -1
  132. package/dist/esm/seam/oneAdapter.native.js +70 -77
  133. package/dist/esm/seam/oneAdapter.native.js.map +1 -1
  134. package/dist/esm/types.mjs +0 -2
  135. package/dist/esm/types.native.js +0 -2
  136. package/dist/jsx/hooks/index.mjs +3 -3
  137. package/dist/jsx/hooks/index.native.cjs +31 -0
  138. package/dist/jsx/hooks/index.native.js +17 -18
  139. package/dist/jsx/hooks/index.native.js.map +1 -1
  140. package/dist/jsx/hooks/useUrlState.mjs +160 -161
  141. package/dist/jsx/hooks/useUrlState.mjs.map +1 -1
  142. package/dist/jsx/hooks/useUrlState.native.cjs +215 -0
  143. package/dist/jsx/hooks/useUrlState.native.js +187 -196
  144. package/dist/jsx/hooks/useUrlState.native.js.map +1 -1
  145. package/dist/jsx/index.js +5 -4
  146. package/dist/jsx/index.mjs +5 -4
  147. package/dist/jsx/index.native.cjs +22 -0
  148. package/dist/jsx/index.native.js +9 -10
  149. package/dist/jsx/index.native.js.map +1 -1
  150. package/dist/jsx/index.storybook.mjs +7 -5
  151. package/dist/jsx/index.storybook.native.cjs +34 -0
  152. package/dist/jsx/index.storybook.native.js +17 -18
  153. package/dist/jsx/index.storybook.native.js.map +1 -1
  154. package/dist/jsx/router/EmptyRouter.mjs +35 -38
  155. package/dist/jsx/router/EmptyRouter.mjs.map +1 -1
  156. package/dist/jsx/router/EmptyRouter.native.cjs +98 -0
  157. package/dist/jsx/router/EmptyRouter.native.js +72 -78
  158. package/dist/jsx/router/EmptyRouter.native.js.map +1 -1
  159. package/dist/jsx/router/ReactRouter.mjs +59 -70
  160. package/dist/jsx/router/ReactRouter.mjs.map +1 -1
  161. package/dist/jsx/router/ReactRouter.native.cjs +119 -0
  162. package/dist/jsx/router/ReactRouter.native.js +95 -109
  163. package/dist/jsx/router/ReactRouter.native.js.map +1 -1
  164. package/dist/jsx/router/binding.mjs +4 -3
  165. package/dist/jsx/router/binding.mjs.map +1 -1
  166. package/dist/jsx/router/binding.native.cjs +35 -0
  167. package/dist/jsx/router/binding.native.js +18 -19
  168. package/dist/jsx/router/binding.native.js.map +1 -1
  169. package/dist/jsx/router/expoRouter.mjs +7 -7
  170. package/dist/jsx/router/expoRouter.mjs.map +1 -1
  171. package/dist/jsx/router/expoRouter.native.cjs +45 -0
  172. package/dist/jsx/router/expoRouter.native.js +22 -25
  173. package/dist/jsx/router/expoRouter.native.js.map +1 -1
  174. package/dist/jsx/router/expoRouterBinding.mjs +23 -21
  175. package/dist/jsx/router/expoRouterBinding.mjs.map +1 -1
  176. package/dist/jsx/router/expoRouterBinding.native.cjs +61 -0
  177. package/dist/jsx/router/expoRouterBinding.native.js +43 -46
  178. package/dist/jsx/router/expoRouterBinding.native.js.map +1 -1
  179. package/dist/jsx/router/index.mjs +2 -2
  180. package/dist/jsx/router/index.native.cjs +51 -0
  181. package/dist/jsx/router/index.native.js +28 -29
  182. package/dist/jsx/router/index.native.js.map +1 -1
  183. package/dist/jsx/router/index.storybook-expo.mjs +1 -2
  184. package/dist/jsx/router/index.storybook-expo.native.cjs +20 -0
  185. package/dist/jsx/router/index.storybook-expo.native.js +9 -10
  186. package/dist/jsx/router/index.storybook-expo.native.js.map +1 -1
  187. package/dist/jsx/router/index.storybook.mjs +1 -2
  188. package/dist/jsx/router/index.storybook.native.cjs +20 -0
  189. package/dist/jsx/router/index.storybook.native.js +9 -10
  190. package/dist/jsx/router/index.storybook.native.js.map +1 -1
  191. package/dist/jsx/router/index.webext.mjs +1 -2
  192. package/dist/jsx/router/index.webext.native.cjs +20 -0
  193. package/dist/jsx/router/index.webext.native.js +9 -10
  194. package/dist/jsx/router/index.webext.native.js.map +1 -1
  195. package/dist/jsx/router/nativeRouter.mjs +19 -23
  196. package/dist/jsx/router/nativeRouter.mjs.map +1 -1
  197. package/dist/jsx/router/nativeRouter.native.cjs +72 -0
  198. package/dist/jsx/router/nativeRouter.native.js +42 -49
  199. package/dist/jsx/router/nativeRouter.native.js.map +1 -1
  200. package/dist/jsx/seam/DomNavigationHost.mjs +125 -133
  201. package/dist/jsx/seam/DomNavigationHost.mjs.map +1 -1
  202. package/dist/jsx/seam/DomNavigationHost.native.cjs +233 -0
  203. package/dist/jsx/seam/DomNavigationHost.native.js +201 -207
  204. package/dist/jsx/seam/DomNavigationHost.native.js.map +1 -1
  205. package/dist/jsx/seam/index.mjs +2 -2
  206. package/dist/jsx/seam/index.native.cjs +47 -0
  207. package/dist/jsx/seam/index.native.js +31 -32
  208. package/dist/jsx/seam/index.native.js.map +1 -1
  209. package/dist/jsx/seam/navigator.mjs +292 -298
  210. package/dist/jsx/seam/navigator.mjs.map +1 -1
  211. package/dist/jsx/seam/navigator.native.cjs +525 -0
  212. package/dist/jsx/seam/navigator.native.js +490 -498
  213. package/dist/jsx/seam/navigator.native.js.map +1 -1
  214. package/dist/jsx/seam/oneAdapter.mjs +61 -64
  215. package/dist/jsx/seam/oneAdapter.mjs.map +1 -1
  216. package/dist/jsx/seam/oneAdapter.native.cjs +133 -0
  217. package/dist/jsx/seam/oneAdapter.native.js +94 -104
  218. package/dist/jsx/seam/oneAdapter.native.js.map +1 -1
  219. package/dist/jsx/types.mjs +0 -2
  220. package/dist/jsx/types.native.cjs +18 -0
  221. package/dist/jsx/types.native.js +9 -10
  222. package/dist/jsx/types.native.js.map +1 -1
  223. package/package.json +14 -14
  224. package/dist/esm/hooks/index.mjs.map +0 -1
  225. package/dist/esm/hooks/index.native.js.map +0 -1
  226. package/dist/esm/index.mjs.map +0 -1
  227. package/dist/esm/index.native.js.map +0 -1
  228. package/dist/esm/index.storybook.mjs.map +0 -1
  229. package/dist/esm/index.storybook.native.js.map +0 -1
  230. package/dist/esm/router/index.mjs.map +0 -1
  231. package/dist/esm/router/index.storybook-expo.mjs.map +0 -1
  232. package/dist/esm/router/index.storybook-expo.native.js.map +0 -1
  233. package/dist/esm/router/index.storybook.mjs.map +0 -1
  234. package/dist/esm/router/index.storybook.native.js.map +0 -1
  235. package/dist/esm/router/index.webext.mjs.map +0 -1
  236. package/dist/esm/router/index.webext.native.js.map +0 -1
  237. package/dist/esm/seam/index.mjs.map +0 -1
  238. package/dist/esm/seam/index.native.js.map +0 -1
  239. package/dist/esm/types.mjs.map +0 -1
  240. package/dist/esm/types.native.js.map +0 -1
  241. package/dist/jsx/hooks/index.mjs.map +0 -1
  242. package/dist/jsx/index.js.map +0 -1
  243. package/dist/jsx/index.mjs.map +0 -1
  244. package/dist/jsx/index.storybook.mjs.map +0 -1
  245. package/dist/jsx/router/index.mjs.map +0 -1
  246. package/dist/jsx/router/index.storybook-expo.mjs.map +0 -1
  247. package/dist/jsx/router/index.storybook.mjs.map +0 -1
  248. package/dist/jsx/router/index.webext.mjs.map +0 -1
  249. package/dist/jsx/seam/index.mjs.map +0 -1
  250. package/dist/jsx/types.mjs.map +0 -1
@@ -1,314 +1,308 @@
1
1
  import { createContext, useContext, useSyncExternalStore } from "react";
2
+
2
3
  function compileRoute(def) {
3
- const segments = def.pattern.split("/").filter(Boolean).map(seg => {
4
- const m = seg.match(/^\[(\.{3})?(.+)\]$/);
5
- return m ? {
6
- param: m[2]
7
- } : {
8
- literal: seg
9
- };
10
- });
11
- return {
12
- ...def,
13
- segments
14
- };
4
+ const segments = def.pattern.split("/").filter(Boolean).map((seg) => {
5
+ const m = seg.match(/^\[(\.{3})?(.+)\]$/);
6
+ return m ? { param: m[2] } : { literal: seg };
7
+ });
8
+ return {
9
+ ...def,
10
+ segments
11
+ };
15
12
  }
16
13
  function parseQuery(query) {
17
- const params = {};
18
- if (!query) return params;
19
- for (const pair of query.split("&")) {
20
- if (!pair) continue;
21
- const eq = pair.indexOf("=");
22
- const key = decodeURIComponent(eq === -1 ? pair : pair.slice(0, eq));
23
- const value = eq === -1 ? "" : decodeURIComponent(pair.slice(eq + 1));
24
- const prev = params[key];
25
- if (prev === void 0) params[key] = value;else if (Array.isArray(prev)) prev.push(value);else params[key] = [prev, value];
26
- }
27
- return params;
14
+ const params = {};
15
+ if (!query) return params;
16
+ for (const pair of query.split("&")) {
17
+ if (!pair) continue;
18
+ const eq = pair.indexOf("=");
19
+ const key = decodeURIComponent(eq === -1 ? pair : pair.slice(0, eq));
20
+ const value = eq === -1 ? "" : decodeURIComponent(pair.slice(eq + 1));
21
+ const prev = params[key];
22
+ if (prev === void 0) params[key] = value;
23
+ else if (Array.isArray(prev)) prev.push(value);
24
+ else params[key] = [prev, value];
25
+ }
26
+ return params;
28
27
  }
29
28
  function hrefToString(href) {
30
- if (typeof href === "string") return href;
31
- let path = href.pathname;
32
- const leftover = [];
33
- for (const [key, raw] of Object.entries(href.params ?? {})) {
34
- const value = String(raw);
35
- const token = `[${key}]`;
36
- if (path.includes(token)) path = path.replace(token, encodeURIComponent(value));else leftover.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
37
- }
38
- return leftover.length ? `${path}?${leftover.join("&")}` : path;
29
+ if (typeof href === "string") return href;
30
+ let path = href.pathname;
31
+ const leftover = [];
32
+ for (const [key, raw] of Object.entries(href.params ?? {})) {
33
+ const value = String(raw);
34
+ const token = `[${key}]`;
35
+ if (path.includes(token)) path = path.replace(token, encodeURIComponent(value));
36
+ else leftover.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
37
+ }
38
+ return leftover.length ? `${path}?${leftover.join("&")}` : path;
39
39
  }
40
40
  const EMPTY_PARAMS = Object.freeze({});
41
- class StackNavigator {
42
- routes = [];
43
- host = null;
44
- listeners = /* @__PURE__ */new Set();
45
- /** Stack mirror rebuilt from the host on every pushed/popped/replaced
46
- * signal. */
47
- stackTags = [];
48
- paramsByTag = /* @__PURE__ */new Map();
49
- pathnameByTag = /* @__PURE__ */new Map();
50
- /** Tags whose page content has been navigated to at least once. The host
51
- * gates each page's React content on this (lazy mounting) — a tag enters
52
- * the set right before its first host push and never leaves (backgrounded
53
- * pages stay alive, same as One keeping stack screens mounted). */
54
- visitedTags = /* @__PURE__ */new Set();
55
- /** Route-loader results, per tag (the loader bridge's store). */
56
- loaderDataByTag = /* @__PURE__ */new Map();
57
- /** Monotonic navigation sequence — a loader that resolves after a NEWER
58
- * navigation started must not perform its (stale) host action. */
59
- navSeq = 0;
60
- /** Navigations that arrived while no host was attached — flushed FIFO on
61
- * attach() so deep links survive mount ordering and remount cycles. */
62
- pendingNavigations = [];
63
- generation = 0;
64
- snapshot = {
65
- pathname: "/",
66
- tag: "",
67
- depth: 0,
68
- canGoBack: false,
69
- generation: 0
70
- };
71
- get isConfigured() {
72
- return this.routes.length > 0;
73
- }
74
- /** Register the route manifest. The FIRST route is the root page. */
75
- configure(defs) {
76
- this.routes = defs.map(compileRoute);
77
- this.visitedTags = /* @__PURE__ */new Set();
78
- this.loaderDataByTag = /* @__PURE__ */new Map();
79
- this.pendingNavigations = [];
80
- const root = defs[0];
81
- if (root) {
82
- this.stackTags = [root.tag];
83
- this.visitedTags.add(root.tag);
84
- this.pathnameByTag.set(root.tag, root.pattern);
85
- this.rebuildSnapshot();
86
- }
87
- }
88
- /** Has this route's page been navigated to (⇒ its content may mount)? */
89
- isVisited(tag) {
90
- return tag !== null && this.visitedTags.has(tag);
91
- }
92
- /** The route's last loader result (stable identity until the loader runs
93
- * again). undefined when the route has no loader / hasn't resolved. */
94
- loaderSnapshot(tag) {
95
- if (!tag) return void 0;
96
- return this.loaderDataByTag.get(tag);
97
- }
98
- /** Seed a restored route's params/pathname BEFORE the first React render
99
- * (no host action, no stack mutation). The navigation host applies
100
- * `initialHref` from its attach EFFECT, which runs after the root page's
101
- * content already rendered once — a restored route's query params would
102
- * miss any mount-time state seeding (e.g. a list initializing filters
103
- * from useLocalSearchParams). Hosts call this while lazily initializing,
104
- * when no subscribers exist yet, so the rebuild notifies nobody. */
105
- seedInitialRoute(href) {
106
- const hit = this.match(href);
107
- if (!hit) return;
108
- this.setRouteState(hit.route.tag, hit.pathname, hit.params);
109
- }
110
- /** Resolve a route's loader WITHOUT navigating. For the initial page:
111
- * entries can `await stackNavigator.preload("/")` during top-level await
112
- * (where GJS reliably drains promise jobs) so even the root page's first
113
- * paint has loader data. */
114
- async preload(href) {
115
- const hit = this.match(href);
116
- if (!hit?.route.loader) return;
117
- try {
118
- const data = await hit.route.loader({
119
- path: hit.pathname,
120
- params: hit.params
121
- });
122
- this.loaderDataByTag.set(hit.route.tag, data);
123
- this.rebuildSnapshot();
124
- } catch (err) {
125
- console.error(`[stack nav] preload loader for ${hit.route.tag} failed: ${String(err)}`);
126
- }
127
- }
128
- /** Bind the live host stack (an Adw.NavigationView widget on GTK, a
129
- * DomStackAdapter on DOM targets). `initialHref` (state restore) applies
130
- * as a replace BEFORE queued navigations flush: a deep link that arrived
131
- * while unattached is fresher intent than a persisted route, so it lands
132
- * on top. */
133
- attach(host, options) {
134
- this.host = host;
135
- this.syncFromHost();
136
- const initialHref = options?.initialHref;
137
- if (initialHref !== void 0) this.replace(initialHref);
138
- for (const {
139
- href,
140
- mode
141
- } of this.pendingNavigations.splice(0)) {
142
- void this.navigate(href, mode);
143
- }
144
- }
145
- detach() {
146
- this.host = null;
147
- }
148
- subscribe = fn => {
149
- this.listeners.add(fn);
150
- return () => this.listeners.delete(fn);
151
- };
152
- getSnapshot = () => this.snapshot;
153
- paramsSnapshot(tag) {
154
- if (!tag) return EMPTY_PARAMS;
155
- return this.paramsByTag.get(tag) ?? EMPTY_PARAMS;
156
- }
157
- match(href) {
158
- const str = hrefToString(href);
159
- const [path = "", query] = str.split("?");
160
- const parts = path.split("/").filter(Boolean);
161
- for (const route of this.routes) {
162
- if (route.segments.length !== parts.length) continue;
163
- const params = parseQuery(query);
164
- let ok = true;
165
- for (let i = 0; i < parts.length; i++) {
166
- const seg = route.segments[i];
167
- const part = decodeURIComponent(parts[i]);
168
- if (seg.literal !== void 0) {
169
- if (seg.literal !== part) {
170
- ok = false;
171
- break;
172
- }
173
- } else if (seg.param !== void 0) {
174
- params[seg.param] = part;
175
- }
176
- }
177
- if (ok) return {
178
- route,
179
- pathname: path === "" ? "/" : `/${parts.join("/")}`,
180
- params
181
- };
182
- }
183
- return null;
184
- }
185
- push = href => {
186
- void this.navigate(href, "push");
187
- };
188
- replace = href => {
189
- void this.navigate(href, "replace");
190
- };
191
- /** Shared push/replace flow, incl. the loader bridge. When the target
192
- * route has no loader the whole body runs SYNCHRONOUSLY (an async
193
- * function only yields at its first await), preserving the original
194
- * timing. With a loader, the host action waits for the loader to resolve
195
- * — same contract as One on web, where client navigation completes only
196
- * after the route's loader data arrived — so the page's first paint has
197
- * its data (and the lazily-mounted content mounts with it). */
198
- async navigate(href, mode) {
199
- const hit = this.match(href);
200
- if (!hit) {
201
- console.log(`[stack nav] ${mode} ${hrefToString(href)} \u2014 no page in the route manifest, ignored`);
202
- return;
203
- }
204
- if (!this.host) {
205
- this.pendingNavigations.push({
206
- href,
207
- mode
208
- });
209
- console.log(`[stack nav] ${mode} ${hrefToString(href)} \u2014 queued until a stack host attaches`);
210
- return;
211
- }
212
- const {
213
- route,
214
- pathname,
215
- params
216
- } = hit;
217
- const seq = ++this.navSeq;
218
- if (route.loader) {
219
- try {
220
- const data = await route.loader({
221
- path: pathname,
222
- params
223
- });
224
- if (seq !== this.navSeq) return;
225
- this.loaderDataByTag.set(route.tag, data);
226
- } catch (err) {
227
- if (seq !== this.navSeq) return;
228
- console.error(`[stack nav] loader for ${route.tag} failed: ${String(err)}`);
229
- }
230
- }
231
- this.visitedTags.add(route.tag);
232
- this.setRouteState(route.tag, pathname, params);
233
- if (mode === "replace") {
234
- const top = this.stackTags.at(-1);
235
- if (top === route.tag) return;
236
- if (this.stackTags.length <= 1) {
237
- this.host.replace_with_tags([...this.stackTags, route.tag]);
238
- return;
239
- }
240
- const below = this.stackTags.slice(0, -1).filter(tag => tag !== route.tag);
241
- this.host.replace_with_tags([...below, route.tag]);
242
- return;
243
- }
244
- const idx = this.stackTags.indexOf(route.tag);
245
- if (idx === -1) {
246
- this.host.push_by_tag(route.tag);
247
- } else if (idx < this.stackTags.length - 1) {
248
- this.host.pop_to_tag(route.tag);
249
- }
250
- }
251
- back = () => {
252
- if (!this.host) {
253
- console.log("[stack nav] back \u2014 no stack host attached, ignored");
254
- return;
255
- }
256
- this.navSeq++;
257
- this.host.pop();
258
- };
259
- setParams = params => {
260
- const top = this.stackTags.at(-1);
261
- if (!top) return;
262
- const prev = this.paramsByTag.get(top) ?? {};
263
- this.paramsByTag.set(top, {
264
- ...prev,
265
- ...params
266
- });
267
- this.rebuildSnapshot();
268
- };
269
- /** Re-read the host navigation stack (called from pushed/popped/replaced
270
- * signal handlers and attach). The host is the source of truth. */
271
- syncFromHost = () => {
272
- if (!this.host) return;
273
- this.navSeq++;
274
- const model = this.host.get_navigation_stack();
275
- const tags = [];
276
- const count = model.get_n_items();
277
- for (let i = 0; i < count; i++) {
278
- const tag = model.get_item(i)?.get_tag();
279
- if (tag) tags.push(tag);
280
- }
281
- if (tags.length > 0) this.stackTags = tags;
282
- for (const tag of this.stackTags) this.visitedTags.add(tag);
283
- this.rebuildSnapshot();
284
- };
285
- setRouteState(tag, pathname, params) {
286
- this.paramsByTag.set(tag, params);
287
- this.pathnameByTag.set(tag, pathname);
288
- this.rebuildSnapshot();
289
- }
290
- rebuildSnapshot() {
291
- const tag = this.stackTags.at(-1) ?? "";
292
- const route = this.routes.find(r => r.tag === tag);
293
- this.generation++;
294
- this.snapshot = {
295
- pathname: this.pathnameByTag.get(tag) ?? route?.pattern ?? "/",
296
- tag,
297
- depth: this.stackTags.length,
298
- canGoBack: this.stackTags.length > 1,
299
- generation: this.generation
300
- };
301
- for (const fn of this.listeners) fn();
302
- }
303
- }
41
+ var StackNavigator = class {
42
+ routes = [];
43
+ host = null;
44
+ listeners = /* @__PURE__ */ new Set();
45
+ /** Stack mirror rebuilt from the host on every pushed/popped/replaced
46
+ * signal. */
47
+ stackTags = [];
48
+ paramsByTag = /* @__PURE__ */ new Map();
49
+ pathnameByTag = /* @__PURE__ */ new Map();
50
+ /** Tags whose page content has been navigated to at least once. The host
51
+ * gates each page's React content on this (lazy mounting) — a tag enters
52
+ * the set right before its first host push and never leaves (backgrounded
53
+ * pages stay alive, same as One keeping stack screens mounted). */
54
+ visitedTags = /* @__PURE__ */ new Set();
55
+ /** Route-loader results, per tag (the loader bridge's store). */
56
+ loaderDataByTag = /* @__PURE__ */ new Map();
57
+ /** Monotonic navigation sequence — a loader that resolves after a NEWER
58
+ * navigation started must not perform its (stale) host action. */
59
+ navSeq = 0;
60
+ /** Navigations that arrived while no host was attached — flushed FIFO on
61
+ * attach() so deep links survive mount ordering and remount cycles. */
62
+ pendingNavigations = [];
63
+ generation = 0;
64
+ snapshot = {
65
+ pathname: "/",
66
+ tag: "",
67
+ depth: 0,
68
+ canGoBack: false,
69
+ generation: 0
70
+ };
71
+ get isConfigured() {
72
+ return this.routes.length > 0;
73
+ }
74
+ /** Register the route manifest. The FIRST route is the root page. */
75
+ configure(defs) {
76
+ this.routes = defs.map(compileRoute);
77
+ this.visitedTags = /* @__PURE__ */ new Set();
78
+ this.loaderDataByTag = /* @__PURE__ */ new Map();
79
+ this.pendingNavigations = [];
80
+ const root = defs[0];
81
+ if (root) {
82
+ this.stackTags = [root.tag];
83
+ this.visitedTags.add(root.tag);
84
+ this.pathnameByTag.set(root.tag, root.pattern);
85
+ this.rebuildSnapshot();
86
+ }
87
+ }
88
+ /** Has this route's page been navigated to (⇒ its content may mount)? */
89
+ isVisited(tag) {
90
+ return tag !== null && this.visitedTags.has(tag);
91
+ }
92
+ /** The route's last loader result (stable identity until the loader runs
93
+ * again). undefined when the route has no loader / hasn't resolved. */
94
+ loaderSnapshot(tag) {
95
+ if (!tag) return void 0;
96
+ return this.loaderDataByTag.get(tag);
97
+ }
98
+ /** Seed a restored route's params/pathname BEFORE the first React render
99
+ * (no host action, no stack mutation). The navigation host applies
100
+ * `initialHref` from its attach EFFECT, which runs after the root page's
101
+ * content already rendered once — a restored route's query params would
102
+ * miss any mount-time state seeding (e.g. a list initializing filters
103
+ * from useLocalSearchParams). Hosts call this while lazily initializing,
104
+ * when no subscribers exist yet, so the rebuild notifies nobody. */
105
+ seedInitialRoute(href) {
106
+ const hit = this.match(href);
107
+ if (!hit) return;
108
+ this.setRouteState(hit.route.tag, hit.pathname, hit.params);
109
+ }
110
+ /** Resolve a route's loader WITHOUT navigating. For the initial page:
111
+ * entries can `await stackNavigator.preload("/")` during top-level await
112
+ * (where GJS reliably drains promise jobs) so even the root page's first
113
+ * paint has loader data. */
114
+ async preload(href) {
115
+ const hit = this.match(href);
116
+ if (!hit?.route.loader) return;
117
+ try {
118
+ const data = await hit.route.loader({
119
+ path: hit.pathname,
120
+ params: hit.params
121
+ });
122
+ this.loaderDataByTag.set(hit.route.tag, data);
123
+ this.rebuildSnapshot();
124
+ } catch (err) {
125
+ console.error(`[stack nav] preload loader for ${hit.route.tag} failed: ${String(err)}`);
126
+ }
127
+ }
128
+ /** Bind the live host stack (an Adw.NavigationView widget on GTK, a
129
+ * DomStackAdapter on DOM targets). `initialHref` (state restore) applies
130
+ * as a replace BEFORE queued navigations flush: a deep link that arrived
131
+ * while unattached is fresher intent than a persisted route, so it lands
132
+ * on top. */
133
+ attach(host, options) {
134
+ this.host = host;
135
+ this.syncFromHost();
136
+ const initialHref = options?.initialHref;
137
+ if (initialHref !== void 0) this.replace(initialHref);
138
+ for (const { href, mode } of this.pendingNavigations.splice(0)) {
139
+ void this.navigate(href, mode);
140
+ }
141
+ }
142
+ detach() {
143
+ this.host = null;
144
+ }
145
+ subscribe = (fn) => {
146
+ this.listeners.add(fn);
147
+ return () => this.listeners.delete(fn);
148
+ };
149
+ getSnapshot = () => this.snapshot;
150
+ paramsSnapshot(tag) {
151
+ if (!tag) return EMPTY_PARAMS;
152
+ return this.paramsByTag.get(tag) ?? EMPTY_PARAMS;
153
+ }
154
+ match(href) {
155
+ const str = hrefToString(href);
156
+ const [path = "", query] = str.split("?");
157
+ const parts = path.split("/").filter(Boolean);
158
+ for (const route of this.routes) {
159
+ if (route.segments.length !== parts.length) continue;
160
+ const params = parseQuery(query);
161
+ let ok = true;
162
+ for (let i = 0; i < parts.length; i++) {
163
+ const seg = route.segments[i];
164
+ const part = decodeURIComponent(parts[i]);
165
+ if (seg.literal !== void 0) {
166
+ if (seg.literal !== part) {
167
+ ok = false;
168
+ break;
169
+ }
170
+ } else if (seg.param !== void 0) {
171
+ params[seg.param] = part;
172
+ }
173
+ }
174
+ if (ok) return {
175
+ route,
176
+ pathname: path === "" ? "/" : `/${parts.join("/")}`,
177
+ params
178
+ };
179
+ }
180
+ return null;
181
+ }
182
+ push = (href) => {
183
+ void this.navigate(href, "push");
184
+ };
185
+ replace = (href) => {
186
+ void this.navigate(href, "replace");
187
+ };
188
+ /** Shared push/replace flow, incl. the loader bridge. When the target
189
+ * route has no loader the whole body runs SYNCHRONOUSLY (an async
190
+ * function only yields at its first await), preserving the original
191
+ * timing. With a loader, the host action waits for the loader to resolve
192
+ * — same contract as One on web, where client navigation completes only
193
+ * after the route's loader data arrived — so the page's first paint has
194
+ * its data (and the lazily-mounted content mounts with it). */
195
+ async navigate(href, mode) {
196
+ const hit = this.match(href);
197
+ if (!hit) {
198
+ console.log(`[stack nav] ${mode} ${hrefToString(href)} \u2014 no page in the route manifest, ignored`);
199
+ return;
200
+ }
201
+ if (!this.host) {
202
+ this.pendingNavigations.push({
203
+ href,
204
+ mode
205
+ });
206
+ console.log(`[stack nav] ${mode} ${hrefToString(href)} \u2014 queued until a stack host attaches`);
207
+ return;
208
+ }
209
+ const { route, pathname, params } = hit;
210
+ const seq = ++this.navSeq;
211
+ if (route.loader) {
212
+ try {
213
+ const data = await route.loader({
214
+ path: pathname,
215
+ params
216
+ });
217
+ if (seq !== this.navSeq) return;
218
+ this.loaderDataByTag.set(route.tag, data);
219
+ } catch (err) {
220
+ if (seq !== this.navSeq) return;
221
+ console.error(`[stack nav] loader for ${route.tag} failed: ${String(err)}`);
222
+ }
223
+ }
224
+ this.visitedTags.add(route.tag);
225
+ this.setRouteState(route.tag, pathname, params);
226
+ if (mode === "replace") {
227
+ const top = this.stackTags.at(-1);
228
+ if (top === route.tag) return;
229
+ if (this.stackTags.length <= 1) {
230
+ this.host.replace_with_tags([...this.stackTags, route.tag]);
231
+ return;
232
+ }
233
+ const below = this.stackTags.slice(0, -1).filter((tag) => tag !== route.tag);
234
+ this.host.replace_with_tags([...below, route.tag]);
235
+ return;
236
+ }
237
+ const idx = this.stackTags.indexOf(route.tag);
238
+ if (idx === -1) {
239
+ this.host.push_by_tag(route.tag);
240
+ } else if (idx < this.stackTags.length - 1) {
241
+ this.host.pop_to_tag(route.tag);
242
+ }
243
+ }
244
+ back = () => {
245
+ if (!this.host) {
246
+ console.log("[stack nav] back — no stack host attached, ignored");
247
+ return;
248
+ }
249
+ this.navSeq++;
250
+ this.host.pop();
251
+ };
252
+ setParams = (params) => {
253
+ const top = this.stackTags.at(-1);
254
+ if (!top) return;
255
+ const prev = this.paramsByTag.get(top) ?? {};
256
+ this.paramsByTag.set(top, {
257
+ ...prev,
258
+ ...params
259
+ });
260
+ this.rebuildSnapshot();
261
+ };
262
+ /** Re-read the host navigation stack (called from pushed/popped/replaced
263
+ * signal handlers and attach). The host is the source of truth. */
264
+ syncFromHost = () => {
265
+ if (!this.host) return;
266
+ this.navSeq++;
267
+ const model = this.host.get_navigation_stack();
268
+ const tags = [];
269
+ const count = model.get_n_items();
270
+ for (let i = 0; i < count; i++) {
271
+ const tag = model.get_item(i)?.get_tag();
272
+ if (tag) tags.push(tag);
273
+ }
274
+ if (tags.length > 0) this.stackTags = tags;
275
+ for (const tag of this.stackTags) this.visitedTags.add(tag);
276
+ this.rebuildSnapshot();
277
+ };
278
+ setRouteState(tag, pathname, params) {
279
+ this.paramsByTag.set(tag, params);
280
+ this.pathnameByTag.set(tag, pathname);
281
+ this.rebuildSnapshot();
282
+ }
283
+ rebuildSnapshot() {
284
+ const tag = this.stackTags.at(-1) ?? "";
285
+ const route = this.routes.find((r) => r.tag === tag);
286
+ this.generation++;
287
+ this.snapshot = {
288
+ pathname: this.pathnameByTag.get(tag) ?? route?.pattern ?? "/",
289
+ tag,
290
+ depth: this.stackTags.length,
291
+ canGoBack: this.stackTags.length > 1,
292
+ generation: this.generation
293
+ };
294
+ for (const fn of this.listeners) fn();
295
+ }
296
+ };
304
297
  const stackNavigator = new StackNavigator();
305
298
  const RouteTagContext = createContext(null);
306
299
  function useNavState() {
307
- return useSyncExternalStore(stackNavigator.subscribe, stackNavigator.getSnapshot);
300
+ return useSyncExternalStore(stackNavigator.subscribe, stackNavigator.getSnapshot);
308
301
  }
309
302
  function useRouteParams() {
310
- const tag = useContext(RouteTagContext);
311
- return useSyncExternalStore(stackNavigator.subscribe, () => stackNavigator.paramsSnapshot(tag));
303
+ const tag = useContext(RouteTagContext);
304
+ return useSyncExternalStore(stackNavigator.subscribe, () => stackNavigator.paramsSnapshot(tag));
312
305
  }
306
+
313
307
  export { RouteTagContext, StackNavigator, hrefToString, stackNavigator, useNavState, useRouteParams };
314
308
  //# sourceMappingURL=navigator.mjs.map