@furystack/shades 13.2.2 → 15.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.
- package/CHANGELOG.md +157 -0
- package/esm/component-factory.spec.d.ts.map +1 -1
- package/esm/component-factory.spec.js +2 -2
- package/esm/component-factory.spec.js.map +1 -1
- package/esm/components/define-nested-routes.d.ts +27 -0
- package/esm/components/define-nested-routes.d.ts.map +1 -0
- package/esm/components/define-nested-routes.js +26 -0
- package/esm/components/define-nested-routes.js.map +1 -0
- package/esm/components/define-nested-routes.spec.d.ts +2 -0
- package/esm/components/define-nested-routes.spec.d.ts.map +1 -0
- package/esm/components/define-nested-routes.spec.js +81 -0
- package/esm/components/define-nested-routes.spec.js.map +1 -0
- package/esm/components/index.d.ts +3 -3
- package/esm/components/index.d.ts.map +1 -1
- package/esm/components/index.js +3 -3
- package/esm/components/index.js.map +1 -1
- package/esm/components/nested-hooks.d.ts +62 -0
- package/esm/components/nested-hooks.d.ts.map +1 -0
- package/esm/components/nested-hooks.js +73 -0
- package/esm/components/nested-hooks.js.map +1 -0
- package/esm/components/nested-hooks.spec.d.ts +2 -0
- package/esm/components/nested-hooks.spec.d.ts.map +1 -0
- package/esm/components/nested-hooks.spec.js +211 -0
- package/esm/components/nested-hooks.spec.js.map +1 -0
- package/esm/components/nested-navigate.d.ts +52 -21
- package/esm/components/nested-navigate.d.ts.map +1 -1
- package/esm/components/nested-navigate.js +32 -17
- package/esm/components/nested-navigate.js.map +1 -1
- package/esm/components/nested-navigate.spec.js +109 -19
- package/esm/components/nested-navigate.spec.js.map +1 -1
- package/esm/components/nested-replace.d.ts +36 -0
- package/esm/components/nested-replace.d.ts.map +1 -0
- package/esm/components/nested-replace.js +38 -0
- package/esm/components/nested-replace.js.map +1 -0
- package/esm/components/nested-replace.spec.d.ts +2 -0
- package/esm/components/nested-replace.spec.d.ts.map +1 -0
- package/esm/components/nested-replace.spec.js +59 -0
- package/esm/components/nested-replace.spec.js.map +1 -0
- package/esm/components/nested-route-link.d.ts +36 -26
- package/esm/components/nested-route-link.d.ts.map +1 -1
- package/esm/components/nested-route-link.js +18 -20
- package/esm/components/nested-route-link.js.map +1 -1
- package/esm/components/nested-route-link.spec.js +67 -17
- package/esm/components/nested-route-link.spec.js.map +1 -1
- package/esm/components/nested-route-types.d.ts +79 -2
- package/esm/components/nested-route-types.d.ts.map +1 -1
- package/esm/components/nested-router.d.ts +77 -7
- package/esm/components/nested-router.d.ts.map +1 -1
- package/esm/components/nested-router.js +139 -13
- package/esm/components/nested-router.js.map +1 -1
- package/esm/components/nested-router.spec.js +283 -52
- package/esm/components/nested-router.spec.js.map +1 -1
- package/esm/services/location-service.d.ts +39 -42
- package/esm/services/location-service.d.ts.map +1 -1
- package/esm/services/location-service.js +115 -134
- package/esm/services/location-service.js.map +1 -1
- package/esm/services/location-service.spec.js +59 -24
- package/esm/services/location-service.spec.js.map +1 -1
- package/esm/services/route-match-service.d.ts +3 -2
- package/esm/services/route-match-service.d.ts.map +1 -1
- package/esm/services/route-match-service.js +13 -22
- package/esm/services/route-match-service.js.map +1 -1
- package/esm/services/route-match-service.spec.js +42 -49
- package/esm/services/route-match-service.spec.js.map +1 -1
- package/esm/services/route-meta-utils.d.ts +28 -6
- package/esm/services/route-meta-utils.d.ts.map +1 -1
- package/esm/services/route-meta-utils.js +18 -4
- package/esm/services/route-meta-utils.js.map +1 -1
- package/esm/services/route-meta-utils.spec.js +60 -5
- package/esm/services/route-meta-utils.spec.js.map +1 -1
- package/esm/services/screen-service.d.ts +13 -72
- package/esm/services/screen-service.d.ts.map +1 -1
- package/esm/services/screen-service.js +45 -137
- package/esm/services/screen-service.js.map +1 -1
- package/esm/services/screen-service.spec.js +19 -18
- package/esm/services/screen-service.spec.js.map +1 -1
- package/esm/services/spatial-navigation-service.d.ts +17 -39
- package/esm/services/spatial-navigation-service.d.ts.map +1 -1
- package/esm/services/spatial-navigation-service.js +280 -313
- package/esm/services/spatial-navigation-service.js.map +1 -1
- package/esm/services/spatial-navigation-service.spec.js +163 -158
- package/esm/services/spatial-navigation-service.spec.js.map +1 -1
- package/esm/shade.d.ts +1 -1
- package/esm/shade.d.ts.map +1 -1
- package/esm/shade.js +10 -7
- package/esm/shade.js.map +1 -1
- package/esm/vnode.d.ts +1 -1
- package/esm/vnode.js +1 -1
- package/package.json +5 -4
- package/src/component-factory.spec.tsx +3 -2
- package/src/components/define-nested-routes.spec.ts +91 -0
- package/src/components/define-nested-routes.ts +27 -0
- package/src/components/index.ts +3 -3
- package/src/components/nested-hooks.spec.ts +254 -0
- package/src/components/nested-hooks.ts +109 -0
- package/src/components/nested-navigate.spec.ts +136 -32
- package/src/components/nested-navigate.ts +75 -26
- package/src/components/nested-replace.spec.ts +89 -0
- package/src/components/nested-replace.ts +46 -0
- package/src/components/nested-route-link.spec.tsx +90 -22
- package/src/components/nested-route-link.tsx +54 -29
- package/src/components/nested-route-types.ts +82 -2
- package/src/components/nested-router.spec.tsx +334 -51
- package/src/components/nested-router.tsx +184 -24
- package/src/services/location-service.spec.ts +68 -25
- package/src/services/location-service.tsx +156 -122
- package/src/services/route-match-service.spec.ts +41 -28
- package/src/services/route-match-service.ts +17 -8
- package/src/services/route-meta-utils.spec.ts +78 -10
- package/src/services/route-meta-utils.ts +44 -15
- package/src/services/screen-service.spec.ts +19 -18
- package/src/services/screen-service.ts +65 -110
- package/src/services/spatial-navigation-service.spec.ts +165 -160
- package/src/services/spatial-navigation-service.ts +299 -287
- package/src/shade.ts +11 -8
- package/src/vnode.ts +1 -1
- package/esm/components/link-to-route.d.ts +0 -10
- package/esm/components/link-to-route.d.ts.map +0 -1
- package/esm/components/link-to-route.js +0 -16
- package/esm/components/link-to-route.js.map +0 -1
- package/esm/components/link-to-route.spec.d.ts +0 -2
- package/esm/components/link-to-route.spec.d.ts.map +0 -1
- package/esm/components/link-to-route.spec.js +0 -30
- package/esm/components/link-to-route.spec.js.map +0 -1
- package/esm/components/route-link.d.ts +0 -10
- package/esm/components/route-link.d.ts.map +0 -1
- package/esm/components/route-link.js +0 -25
- package/esm/components/route-link.js.map +0 -1
- package/esm/components/route-link.spec.d.ts +0 -2
- package/esm/components/route-link.spec.d.ts.map +0 -1
- package/esm/components/route-link.spec.js +0 -34
- package/esm/components/route-link.spec.js.map +0 -1
- package/esm/components/router.d.ts +0 -44
- package/esm/components/router.d.ts.map +0 -1
- package/esm/components/router.js +0 -66
- package/esm/components/router.js.map +0 -1
- package/esm/components/router.spec.d.ts +0 -2
- package/esm/components/router.spec.d.ts.map +0 -1
- package/esm/components/router.spec.js +0 -161
- package/esm/components/router.spec.js.map +0 -1
- package/src/components/link-to-route.spec.tsx +0 -45
- package/src/components/link-to-route.tsx +0 -26
- package/src/components/route-link.spec.tsx +0 -44
- package/src/components/route-link.tsx +0 -29
- package/src/components/router.spec.tsx +0 -217
- package/src/components/router.tsx +0 -103
|
@@ -9,6 +9,7 @@ import { createComponent, setRenderMode } from '../shade-component.js'
|
|
|
9
9
|
import { Shade } from '../shade.js'
|
|
10
10
|
import type { ViewTransitionConfig } from '../view-transition.js'
|
|
11
11
|
import { maybeViewTransition } from '../view-transition.js'
|
|
12
|
+
import type { HashLiterals, QueryValidator } from './nested-route-types.js'
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Options passed to a dynamic title resolver function.
|
|
@@ -47,13 +48,27 @@ export interface NestedRouteMeta<TMatchResult = unknown> {
|
|
|
47
48
|
* A single route entry in a NestedRouter configuration.
|
|
48
49
|
* Unlike flat `Route`, the URL is the Record key (not a field), and the
|
|
49
50
|
* `component` receives an `outlet` for rendering matched child content.
|
|
51
|
+
*
|
|
52
|
+
* Routes may additionally declare:
|
|
53
|
+
* - `query`: a validator that parses the deserialized query string into a
|
|
54
|
+
* typed shape; `component` receives the parsed value (or `null` when
|
|
55
|
+
* validation fails). The route still matches on path alone — an invalid
|
|
56
|
+
* query never prevents navigation.
|
|
57
|
+
* - `hash`: a readonly tuple of allowed URL hash literals; `component`
|
|
58
|
+
* receives the current hash when it matches one of the listed literals,
|
|
59
|
+
* or `undefined` otherwise.
|
|
60
|
+
*
|
|
50
61
|
* @typeParam TMatchResult - The type of matched URL parameters
|
|
62
|
+
* @typeParam TQuery - The typed query shape parsed from the URL search string (defaults to `never`)
|
|
63
|
+
* @typeParam THash - The readonly tuple of allowed hash literals (defaults to `never`)
|
|
51
64
|
*/
|
|
52
|
-
export type NestedRoute<TMatchResult = unknown> = {
|
|
65
|
+
export type NestedRoute<TMatchResult = unknown, TQuery = any, THash extends HashLiterals = readonly any[]> = {
|
|
53
66
|
meta?: NestedRouteMeta<TMatchResult>
|
|
54
67
|
component: (options: {
|
|
55
68
|
currentUrl: string
|
|
56
69
|
match: MatchResult<TMatchResult extends object ? TMatchResult : object>
|
|
70
|
+
query: TQuery | null
|
|
71
|
+
hash: THash[number] | undefined
|
|
57
72
|
outlet?: JSX.Element
|
|
58
73
|
}) => JSX.Element
|
|
59
74
|
routingOptions?: MatchOptions
|
|
@@ -70,8 +85,21 @@ export type NestedRoute<TMatchResult = unknown> = {
|
|
|
70
85
|
* View Transition API when `viewTransition` is enabled.
|
|
71
86
|
*/
|
|
72
87
|
onLeave?: (options: RenderOptions<unknown> & { element: JSX.Element }) => Promise<void>
|
|
73
|
-
children?: Record<string, NestedRoute<any>>
|
|
88
|
+
children?: Record<string, NestedRoute<any, any, any>>
|
|
74
89
|
viewTransition?: boolean | ViewTransitionConfig
|
|
90
|
+
/**
|
|
91
|
+
* Optional validator that narrows the deserialized URL query string into a
|
|
92
|
+
* typed shape. Return `null` when the URL's query does not satisfy the route's
|
|
93
|
+
* contract — the route still matches on path, but `component` receives `null`.
|
|
94
|
+
*/
|
|
95
|
+
query?: QueryValidator<TQuery>
|
|
96
|
+
/**
|
|
97
|
+
* Optional readonly tuple of URL hash literals the route understands. Declare
|
|
98
|
+
* with `as const` to preserve literal types, e.g. `hash: ['tab1', 'tab2'] as const`.
|
|
99
|
+
* The router forwards the current hash to `component` only when it matches one
|
|
100
|
+
* of the listed literals; otherwise `component.hash` is `undefined`.
|
|
101
|
+
*/
|
|
102
|
+
hash?: THash
|
|
75
103
|
}
|
|
76
104
|
|
|
77
105
|
/**
|
|
@@ -79,17 +107,21 @@ export type NestedRoute<TMatchResult = unknown> = {
|
|
|
79
107
|
* Routes are defined as a Record where keys are URL patterns.
|
|
80
108
|
*/
|
|
81
109
|
export type NestedRouterProps = {
|
|
82
|
-
routes: Record<string, NestedRoute<any>>
|
|
110
|
+
routes: Record<string, NestedRoute<any, any, any>>
|
|
83
111
|
notFound?: JSX.Element
|
|
84
112
|
viewTransition?: boolean | ViewTransitionConfig
|
|
85
113
|
}
|
|
86
114
|
|
|
87
115
|
/**
|
|
88
|
-
* A single entry in a match chain, pairing a matched route with its match
|
|
116
|
+
* A single entry in a match chain, pairing a matched route with its match
|
|
117
|
+
* result and the typed `query` / `hash` values derived from the URL for that
|
|
118
|
+
* route's declared schema.
|
|
89
119
|
*/
|
|
90
120
|
export type MatchChainEntry = {
|
|
91
|
-
route: NestedRoute<unknown>
|
|
121
|
+
route: NestedRoute<unknown, any, any>
|
|
92
122
|
match: MatchResult<object>
|
|
123
|
+
query: unknown
|
|
124
|
+
hash: string | undefined
|
|
93
125
|
}
|
|
94
126
|
|
|
95
127
|
/**
|
|
@@ -112,12 +144,15 @@ export type NestedRouterState = {
|
|
|
112
144
|
*
|
|
113
145
|
* For leaf routes (no children), only exact matching is used.
|
|
114
146
|
*
|
|
147
|
+
* The returned entries contain placeholder `query: null` / `hash: undefined`
|
|
148
|
+
* values; callers are expected to populate them via {@link enrichMatchChain}.
|
|
149
|
+
*
|
|
115
150
|
* @param routes - The route definitions to match against
|
|
116
151
|
* @param currentUrl - The URL path to match
|
|
117
152
|
* @returns An array of matched chain entries from outermost to innermost, or null if no match
|
|
118
153
|
*/
|
|
119
154
|
export const buildMatchChain = (
|
|
120
|
-
routes: Record<string, NestedRoute<any>>,
|
|
155
|
+
routes: Record<string, NestedRoute<any, any, any>>,
|
|
121
156
|
currentUrl: string,
|
|
122
157
|
): MatchChainEntry[] | null => {
|
|
123
158
|
for (const [pattern, route] of Object.entries(routes)) {
|
|
@@ -139,20 +174,20 @@ export const buildMatchChain = (
|
|
|
139
174
|
|
|
140
175
|
const childChain = buildMatchChain(route.children, remainingUrl)
|
|
141
176
|
if (childChain) {
|
|
142
|
-
return [{ route, match: prefixResult }, ...childChain]
|
|
177
|
+
return [{ route, match: prefixResult, query: null, hash: undefined }, ...childChain]
|
|
143
178
|
}
|
|
144
179
|
}
|
|
145
180
|
|
|
146
181
|
const exactMatchFn = match(pattern, route.routingOptions)
|
|
147
182
|
const exactResult = exactMatchFn(currentUrl)
|
|
148
183
|
if (exactResult) {
|
|
149
|
-
return [{ route, match: exactResult }]
|
|
184
|
+
return [{ route, match: exactResult, query: null, hash: undefined }]
|
|
150
185
|
}
|
|
151
186
|
} else {
|
|
152
187
|
const matchFn = match(pattern, route.routingOptions)
|
|
153
188
|
const matchResult = matchFn(currentUrl)
|
|
154
189
|
if (matchResult) {
|
|
155
|
-
return [{ route, match: matchResult }]
|
|
190
|
+
return [{ route, match: matchResult, query: null, hash: undefined }]
|
|
156
191
|
}
|
|
157
192
|
}
|
|
158
193
|
}
|
|
@@ -161,7 +196,44 @@ export const buildMatchChain = (
|
|
|
161
196
|
}
|
|
162
197
|
|
|
163
198
|
/**
|
|
164
|
-
*
|
|
199
|
+
* Populates each chain entry's `query` and `hash` fields by running the route's
|
|
200
|
+
* declared validator against the URL's deserialized query string, and matching
|
|
201
|
+
* the current URL hash against the route's declared literal tuple.
|
|
202
|
+
*
|
|
203
|
+
* Entries whose route declares neither `query` nor `hash` are returned with
|
|
204
|
+
* `query: null` / `hash: undefined`.
|
|
205
|
+
*
|
|
206
|
+
* When no entry in the chain declares either `query` or `hash`, the input
|
|
207
|
+
* array is returned unchanged to avoid a per-navigation allocation on the
|
|
208
|
+
* common path-only case.
|
|
209
|
+
*
|
|
210
|
+
* @param chain - The chain produced by {@link buildMatchChain}
|
|
211
|
+
* @param deserializedSearch - The deserialized URL query string
|
|
212
|
+
* @param currentHash - The current URL hash (without the leading `#`)
|
|
213
|
+
*/
|
|
214
|
+
export const enrichMatchChain = (
|
|
215
|
+
chain: MatchChainEntry[],
|
|
216
|
+
deserializedSearch: Record<string, unknown>,
|
|
217
|
+
currentHash: string,
|
|
218
|
+
): MatchChainEntry[] => {
|
|
219
|
+
const hasAnyDeclaration = chain.some((entry) => entry.route.query || entry.route.hash)
|
|
220
|
+
if (!hasAnyDeclaration) return chain
|
|
221
|
+
|
|
222
|
+
return chain.map((entry) => {
|
|
223
|
+
const validator = entry.route.query as QueryValidator<unknown> | undefined
|
|
224
|
+
const query: unknown = validator ? validator(deserializedSearch) : null
|
|
225
|
+
const declaredHash = entry.route.hash as readonly string[] | undefined
|
|
226
|
+
const hash = declaredHash?.includes(currentHash) ? currentHash : undefined
|
|
227
|
+
return { ...entry, query, hash }
|
|
228
|
+
})
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Finds the first index where two match chains diverge, considering route
|
|
233
|
+
* identity and matched path parameters only. Used to scope lifecycle hooks
|
|
234
|
+
* (`onLeave` / `onVisit`) so that a query string or hash change does not
|
|
235
|
+
* fire spurious mount / unmount callbacks.
|
|
236
|
+
*
|
|
165
237
|
* Returns the length of the shorter chain if one is a prefix of the other.
|
|
166
238
|
*/
|
|
167
239
|
export const findDivergenceIndex = (oldChain: MatchChainEntry[], newChain: MatchChainEntry[]): number => {
|
|
@@ -177,6 +249,56 @@ export const findDivergenceIndex = (oldChain: MatchChainEntry[], newChain: Match
|
|
|
177
249
|
return minLength
|
|
178
250
|
}
|
|
179
251
|
|
|
252
|
+
/**
|
|
253
|
+
* Shallow structural equality for query values. Handles the shapes produced by
|
|
254
|
+
* route-declared query validators: primitives, plain objects (by own enumerable
|
|
255
|
+
* string keys, recursively shallow-compared), and arrays (by index).
|
|
256
|
+
*
|
|
257
|
+
* Nested objects / arrays are compared shallowly one level deep, then fall back
|
|
258
|
+
* to `Object.is` — sufficient for the typed-query shapes the router surfaces,
|
|
259
|
+
* and order-independent unlike `JSON.stringify`.
|
|
260
|
+
*/
|
|
261
|
+
const isShallowEqual = (a: unknown, b: unknown): boolean => {
|
|
262
|
+
if (Object.is(a, b)) return true
|
|
263
|
+
if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object') return false
|
|
264
|
+
|
|
265
|
+
if (Array.isArray(a)) {
|
|
266
|
+
if (!Array.isArray(b) || a.length !== b.length) return false
|
|
267
|
+
for (let i = 0; i < a.length; i++) {
|
|
268
|
+
if (!Object.is(a[i], b[i])) return false
|
|
269
|
+
}
|
|
270
|
+
return true
|
|
271
|
+
}
|
|
272
|
+
if (Array.isArray(b)) return false
|
|
273
|
+
|
|
274
|
+
const aKeys = Object.keys(a as Record<string, unknown>)
|
|
275
|
+
const bKeys = Object.keys(b as Record<string, unknown>)
|
|
276
|
+
if (aKeys.length !== bKeys.length) return false
|
|
277
|
+
for (const key of aKeys) {
|
|
278
|
+
if (!Object.prototype.hasOwnProperty.call(b, key)) return false
|
|
279
|
+
if (!Object.is((a as Record<string, unknown>)[key], (b as Record<string, unknown>)[key])) return false
|
|
280
|
+
}
|
|
281
|
+
return true
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Returns true when any chain entry differs in its `query` value or `hash`
|
|
286
|
+
* segment, ignoring path-level fields (route identity and params). Used to
|
|
287
|
+
* force a re-render when the URL's query string or hash changes without the
|
|
288
|
+
* matched route chain itself changing.
|
|
289
|
+
*
|
|
290
|
+
* Query values are compared with a key-order-independent shallow equality —
|
|
291
|
+
* sufficient for the typed shapes a route's `query` validator surfaces.
|
|
292
|
+
*/
|
|
293
|
+
export const hasQueryOrHashChanged = (oldChain: MatchChainEntry[], newChain: MatchChainEntry[]): boolean => {
|
|
294
|
+
const minLength = Math.min(oldChain.length, newChain.length)
|
|
295
|
+
for (let i = 0; i < minLength; i++) {
|
|
296
|
+
if (oldChain[i].hash !== newChain[i].hash) return true
|
|
297
|
+
if (!isShallowEqual(oldChain[i].query, newChain[i].query)) return true
|
|
298
|
+
}
|
|
299
|
+
return false
|
|
300
|
+
}
|
|
301
|
+
|
|
180
302
|
/**
|
|
181
303
|
* The result of rendering a match chain, containing both the fully composed
|
|
182
304
|
* JSX tree and per-entry elements for scoped lifecycle animations.
|
|
@@ -207,6 +329,8 @@ export const renderMatchChain = (chain: MatchChainEntry[], currentUrl: string):
|
|
|
207
329
|
outlet = entry.route.component({
|
|
208
330
|
currentUrl,
|
|
209
331
|
match: entry.match,
|
|
332
|
+
query: entry.query,
|
|
333
|
+
hash: entry.hash,
|
|
210
334
|
outlet,
|
|
211
335
|
})
|
|
212
336
|
chainElements[i] = outlet
|
|
@@ -246,6 +370,10 @@ export const resolveViewTransition = (
|
|
|
246
370
|
* Routes are defined as a Record where keys are URL patterns (following the
|
|
247
371
|
* RestApi pattern). The matching algorithm builds a chain from outermost to
|
|
248
372
|
* innermost route, then renders inside-out so each parent wraps its child.
|
|
373
|
+
*
|
|
374
|
+
* The router subscribes to path, query string and hash changes; path-level
|
|
375
|
+
* changes drive `onLeave` / `onVisit` lifecycle hooks while query / hash
|
|
376
|
+
* changes re-render the chain without firing lifecycle callbacks.
|
|
249
377
|
*/
|
|
250
378
|
export const NestedRouter = Shade<NestedRouterProps>({
|
|
251
379
|
customElementName: 'shade-nested-router',
|
|
@@ -258,19 +386,26 @@ export const NestedRouter = Shade<NestedRouterProps>({
|
|
|
258
386
|
chainElements: [],
|
|
259
387
|
})
|
|
260
388
|
|
|
389
|
+
const locationService = injector.get(LocationService)
|
|
390
|
+
|
|
261
391
|
const updateUrl = async (currentUrl: string) => {
|
|
262
392
|
const [lastState] = useState<NestedRouterState>('routerState', state)
|
|
263
393
|
const { matchChain: lastChain, chainElements: lastChainElements } = lastState
|
|
264
394
|
try {
|
|
265
|
-
const
|
|
395
|
+
const rawChain = buildMatchChain(options.props.routes, currentUrl)
|
|
396
|
+
|
|
397
|
+
if (rawChain) {
|
|
398
|
+
const deserializedSearch = locationService.onDeserializedLocationSearchChanged.getValue()
|
|
399
|
+
const currentHash = locationService.onLocationHashChanged.getValue()
|
|
400
|
+
const newChain = enrichMatchChain(rawChain, deserializedSearch, currentHash)
|
|
266
401
|
|
|
267
|
-
if (newChain) {
|
|
268
402
|
const lastChainEntries = lastChain ?? []
|
|
269
403
|
const divergeIndex = findDivergenceIndex(lastChainEntries, newChain)
|
|
270
|
-
const
|
|
404
|
+
const hasPathChanged =
|
|
271
405
|
divergeIndex < lastChainEntries.length ||
|
|
272
406
|
divergeIndex < newChain.length ||
|
|
273
407
|
lastChainEntries.length !== newChain.length
|
|
408
|
+
const hasChanged = hasPathChanged || hasQueryOrHashChanged(lastChainEntries, newChain)
|
|
274
409
|
|
|
275
410
|
if (hasChanged) {
|
|
276
411
|
const version = ++versionRef.current
|
|
@@ -291,7 +426,7 @@ export const NestedRouter = Shade<NestedRouterProps>({
|
|
|
291
426
|
|
|
292
427
|
const applyUpdate = () => {
|
|
293
428
|
setState({ matchChain: newChain, jsx: newResult.jsx, chainElements: newResult.chainElements })
|
|
294
|
-
injector.
|
|
429
|
+
injector.get(RouteMatchService).currentMatchChain.setValue(newChain)
|
|
295
430
|
}
|
|
296
431
|
|
|
297
432
|
const vtConfig = resolveViewTransition(options.props.viewTransition, newChain)
|
|
@@ -316,7 +451,7 @@ export const NestedRouter = Shade<NestedRouterProps>({
|
|
|
316
451
|
jsx: options.props.notFound || <div />,
|
|
317
452
|
chainElements: [],
|
|
318
453
|
})
|
|
319
|
-
injector.
|
|
454
|
+
injector.get(RouteMatchService).currentMatchChain.setValue([])
|
|
320
455
|
}
|
|
321
456
|
|
|
322
457
|
await maybeViewTransition(options.props.viewTransition, applyNotFound)
|
|
@@ -328,15 +463,40 @@ export const NestedRouter = Shade<NestedRouterProps>({
|
|
|
328
463
|
}
|
|
329
464
|
}
|
|
330
465
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
466
|
+
/**
|
|
467
|
+
* A single `LocationService.navigate` call synchronously fires three
|
|
468
|
+
* observables (path, search, hash). Without coalescing, each would kick
|
|
469
|
+
* off its own `updateUrl` and rely on `versionRef` to cancel the two
|
|
470
|
+
* that lose the race.
|
|
471
|
+
*
|
|
472
|
+
* We dedupe by the composed URL key: each observer fires after the
|
|
473
|
+
* browser's location has already been updated, so the key read at fire
|
|
474
|
+
* time is the final target URL. The first observer in the burst kicks
|
|
475
|
+
* off `updateUrl`; the remaining two see the same key and short-circuit.
|
|
476
|
+
*/
|
|
477
|
+
const getUrlKey = () =>
|
|
478
|
+
`${locationService.onLocationPathChanged.getValue()}?${locationService.onLocationSearchChanged.getValue()}#${locationService.onLocationHashChanged.getValue()}`
|
|
479
|
+
|
|
480
|
+
const [lastKeyRef] = useState('navLastKey', { current: getUrlKey() })
|
|
481
|
+
const scheduleUpdate = () => {
|
|
482
|
+
const key = getUrlKey()
|
|
483
|
+
if (key === lastKeyRef.current) return
|
|
484
|
+
lastKeyRef.current = key
|
|
485
|
+
void updateUrl(locationService.onLocationPathChanged.getValue())
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const [locationPath] = useObservable('locationPathChanged', locationService.onLocationPathChanged, {
|
|
489
|
+
onChange: scheduleUpdate,
|
|
490
|
+
})
|
|
491
|
+
|
|
492
|
+
useObservable('locationSearchChanged', locationService.onDeserializedLocationSearchChanged, {
|
|
493
|
+
onChange: scheduleUpdate,
|
|
494
|
+
})
|
|
495
|
+
|
|
496
|
+
useObservable('locationHashChanged', locationService.onLocationHashChanged, {
|
|
497
|
+
onChange: scheduleUpdate,
|
|
498
|
+
})
|
|
499
|
+
|
|
340
500
|
void updateUrl(locationPath)
|
|
341
501
|
return state.jsx
|
|
342
502
|
},
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createInjector } from '@furystack/inject'
|
|
2
2
|
import { deserializeQueryString, serializeToQueryString, serializeValue } from '@furystack/rest'
|
|
3
3
|
import { usingAsync } from '@furystack/utils'
|
|
4
4
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
@@ -13,16 +13,17 @@ describe('LocationService', () => {
|
|
|
13
13
|
})
|
|
14
14
|
|
|
15
15
|
it('Shuld be constructed', async () => {
|
|
16
|
-
await usingAsync(
|
|
17
|
-
const s = i.
|
|
18
|
-
expect(s).
|
|
16
|
+
await usingAsync(createInjector(), async (i) => {
|
|
17
|
+
const s = i.get(LocationService)
|
|
18
|
+
expect(s).toBeDefined()
|
|
19
|
+
expect(typeof s.navigate).toBe('function')
|
|
19
20
|
})
|
|
20
21
|
})
|
|
21
22
|
|
|
22
23
|
it('Shuld update state on events', async () => {
|
|
23
|
-
await usingAsync(
|
|
24
|
+
await usingAsync(createInjector(), async (i) => {
|
|
24
25
|
const onLocaionChanged = vi.fn()
|
|
25
|
-
const s = i.
|
|
26
|
+
const s = i.get(LocationService)
|
|
26
27
|
s.onLocationPathChanged.subscribe(onLocaionChanged)
|
|
27
28
|
expect(onLocaionChanged).toBeCalledTimes(0)
|
|
28
29
|
history.pushState(null, '', '/loc1')
|
|
@@ -39,9 +40,9 @@ describe('LocationService', () => {
|
|
|
39
40
|
})
|
|
40
41
|
|
|
41
42
|
it('Should update location path when navigate is called', async () => {
|
|
42
|
-
await usingAsync(
|
|
43
|
+
await usingAsync(createInjector(), async (i) => {
|
|
43
44
|
const onLocationChanged = vi.fn()
|
|
44
|
-
const s = i.
|
|
45
|
+
const s = i.get(LocationService)
|
|
45
46
|
s.onLocationPathChanged.subscribe(onLocationChanged)
|
|
46
47
|
s.navigate('/dashboard')
|
|
47
48
|
expect(s.onLocationPathChanged.getValue()).toBe('/dashboard')
|
|
@@ -49,10 +50,49 @@ describe('LocationService', () => {
|
|
|
49
50
|
})
|
|
50
51
|
})
|
|
51
52
|
|
|
53
|
+
describe('replace', () => {
|
|
54
|
+
it('Should update the observable path without pushing a new history entry', async () => {
|
|
55
|
+
await usingAsync(createInjector(), async (i) => {
|
|
56
|
+
const s = i.get(LocationService)
|
|
57
|
+
const lengthBefore = history.length
|
|
58
|
+
s.replace('/replaced')
|
|
59
|
+
expect(s.onLocationPathChanged.getValue()).toBe('/replaced')
|
|
60
|
+
expect(history.length).toBe(lengthBefore)
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('Should call history.replaceState rather than pushState', async () => {
|
|
65
|
+
await usingAsync(createInjector(), async (i) => {
|
|
66
|
+
const s = i.get(LocationService)
|
|
67
|
+
const pushSpy = vi.spyOn(history, 'pushState')
|
|
68
|
+
const replaceSpy = vi.spyOn(history, 'replaceState')
|
|
69
|
+
|
|
70
|
+
s.replace('/replaced-2')
|
|
71
|
+
|
|
72
|
+
expect(replaceSpy).toHaveBeenCalledTimes(1)
|
|
73
|
+
expect(replaceSpy).toHaveBeenCalledWith(null, '', '/replaced-2')
|
|
74
|
+
expect(pushSpy).not.toHaveBeenCalled()
|
|
75
|
+
|
|
76
|
+
pushSpy.mockRestore()
|
|
77
|
+
replaceSpy.mockRestore()
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('Should notify path subscribers after replace', async () => {
|
|
82
|
+
await usingAsync(createInjector(), async (i) => {
|
|
83
|
+
const s = i.get(LocationService)
|
|
84
|
+
const onLocationChanged = vi.fn()
|
|
85
|
+
s.onLocationPathChanged.subscribe(onLocationChanged)
|
|
86
|
+
s.replace('/notify')
|
|
87
|
+
expect(onLocationChanged).toHaveBeenCalledWith('/notify')
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
|
|
52
92
|
describe('useSearchParam', () => {
|
|
53
93
|
it('Should create observables lazily', async () => {
|
|
54
|
-
await usingAsync(
|
|
55
|
-
const service = i.
|
|
94
|
+
await usingAsync(createInjector(), async (i) => {
|
|
95
|
+
const service = i.get(LocationService)
|
|
56
96
|
const observables = service.searchParamObservables
|
|
57
97
|
|
|
58
98
|
const testSearchParam = service.useSearchParam('test', null)
|
|
@@ -71,16 +111,16 @@ describe('LocationService', () => {
|
|
|
71
111
|
})
|
|
72
112
|
|
|
73
113
|
it('Should return the default value, if not present in the query string', async () => {
|
|
74
|
-
await usingAsync(
|
|
75
|
-
const service = i.
|
|
114
|
+
await usingAsync(createInjector(), async (i) => {
|
|
115
|
+
const service = i.get(LocationService)
|
|
76
116
|
const testSearchParam = service.useSearchParam('test', { value: 'foo' })
|
|
77
117
|
expect(testSearchParam.getValue()).toEqual({ value: 'foo' })
|
|
78
118
|
})
|
|
79
119
|
})
|
|
80
120
|
|
|
81
121
|
it('Should return the value from the query string', async () => {
|
|
82
|
-
await usingAsync(
|
|
83
|
-
const service = i.
|
|
122
|
+
await usingAsync(createInjector(), async (i) => {
|
|
123
|
+
const service = i.get(LocationService)
|
|
84
124
|
history.pushState(null, '', `/loc1?test=${serializeValue(1)}`)
|
|
85
125
|
const testSearchParam = service.useSearchParam('test', 123)
|
|
86
126
|
expect(testSearchParam.getValue()).toBe(1)
|
|
@@ -88,8 +128,8 @@ describe('LocationService', () => {
|
|
|
88
128
|
})
|
|
89
129
|
|
|
90
130
|
it('should update the observable value on push / replace states', async () => {
|
|
91
|
-
await usingAsync(
|
|
92
|
-
const service = i.
|
|
131
|
+
await usingAsync(createInjector(), async (i) => {
|
|
132
|
+
const service = i.get(LocationService)
|
|
93
133
|
history.pushState(null, '', `/loc1?test=${serializeValue(1)}`)
|
|
94
134
|
const testSearchParam = service.useSearchParam('test', 234)
|
|
95
135
|
expect(testSearchParam.getValue()).toBe(1)
|
|
@@ -99,8 +139,8 @@ describe('LocationService', () => {
|
|
|
99
139
|
})
|
|
100
140
|
|
|
101
141
|
it('Should update the URL based on search value change', async () => {
|
|
102
|
-
await usingAsync(
|
|
103
|
-
const service = i.
|
|
142
|
+
await usingAsync(createInjector(), async (i) => {
|
|
143
|
+
const service = i.get(LocationService)
|
|
104
144
|
history.pushState(null, '', `/loc1?test=${serializeValue('2')}`)
|
|
105
145
|
const testSearchParam = service.useSearchParam('test', '')
|
|
106
146
|
testSearchParam.setValue('2')
|
|
@@ -108,25 +148,28 @@ describe('LocationService', () => {
|
|
|
108
148
|
})
|
|
109
149
|
})
|
|
110
150
|
|
|
111
|
-
it('Should throw
|
|
112
|
-
await usingAsync(
|
|
151
|
+
it('Should throw when called after LocationService has been resolved', async () => {
|
|
152
|
+
await usingAsync(createInjector(), async (i) => {
|
|
113
153
|
const customSerializer = vi.fn((value: any) => serializeToQueryString(value))
|
|
114
154
|
const customDeserializer = vi.fn((value: string) => deserializeQueryString(value))
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
155
|
+
|
|
156
|
+
// Eagerly resolve once so the service patches history / adds listeners.
|
|
157
|
+
i.get(LocationService)
|
|
158
|
+
|
|
159
|
+
expect(() => useCustomSearchStateSerializer(i, customSerializer, customDeserializer)).toThrow(
|
|
160
|
+
/before LocationService is resolved/,
|
|
118
161
|
)
|
|
119
162
|
})
|
|
120
163
|
})
|
|
121
164
|
|
|
122
165
|
it('Should use custom serializer and deserializer', async () => {
|
|
123
|
-
await usingAsync(
|
|
166
|
+
await usingAsync(createInjector(), async (i) => {
|
|
124
167
|
const customSerializer = vi.fn((value: any) => serializeToQueryString(value))
|
|
125
168
|
const customDeserializer = vi.fn((value: string) => deserializeQueryString(value))
|
|
126
169
|
|
|
127
170
|
useCustomSearchStateSerializer(i, customSerializer, customDeserializer)
|
|
128
171
|
|
|
129
|
-
const locationService = i.
|
|
172
|
+
const locationService = i.get(LocationService)
|
|
130
173
|
const testSearchParam = locationService.useSearchParam('test', { value: 'foo' })
|
|
131
174
|
|
|
132
175
|
testSearchParam.setValue({ value: 'bar' })
|