@real-router/core 0.54.6 → 0.55.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/dist/cjs/Router-C7eE1kIK.js +6 -0
- package/dist/cjs/Router-C7eE1kIK.js.map +1 -0
- package/dist/cjs/{Router-CJihdrWA.d.ts → Router-Dg-zk8AS.d.ts} +1 -1
- package/dist/cjs/{Router-CJihdrWA.d.ts.map → Router-Dg-zk8AS.d.ts.map} +1 -1
- package/dist/cjs/{RouterError-Bm9YnZ6e.d.ts → RouterError-WhCzIWuc.d.ts} +1 -1
- package/dist/cjs/{RouterError-Bm9YnZ6e.d.ts.map → RouterError-WhCzIWuc.d.ts.map} +1 -1
- package/dist/cjs/api.d.ts +2 -2
- package/dist/cjs/api.d.ts.map +1 -1
- package/dist/cjs/api.js +1 -1
- package/dist/cjs/api.js.map +1 -1
- package/dist/cjs/{cloneRouter-zhA3NNoI.js → cloneRouter-C9Rth_8U.js} +2 -2
- package/dist/cjs/{cloneRouter-zhA3NNoI.js.map → cloneRouter-C9Rth_8U.js.map} +1 -1
- package/dist/cjs/{index-EwbhzRQw.d.ts → index-C-i6vx5Y.d.ts} +1 -1
- package/dist/cjs/{index-EwbhzRQw.d.ts.map → index-C-i6vx5Y.d.ts.map} +1 -1
- package/dist/cjs/{index-8oPDJBQc.d.ts → index-K1U_fqfJ.d.ts} +2 -2
- package/dist/cjs/{index-8oPDJBQc.d.ts.map → index-K1U_fqfJ.d.ts.map} +1 -1
- package/dist/cjs/index.d.ts +3 -3
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/{internals-CM6oaz9n.js → internals-CWMOL1B8.js} +2 -2
- package/dist/cjs/{internals-CM6oaz9n.js.map → internals-CWMOL1B8.js.map} +1 -1
- package/dist/cjs/utils.d.ts +1 -1
- package/dist/cjs/utils.js +1 -1
- package/dist/cjs/validation.d.ts +4 -4
- package/dist/cjs/validation.js +1 -1
- package/dist/esm/{Router-BmhiDQUJ.d.mts → Router-Dg-zk8AS.d.mts} +1 -1
- package/dist/esm/{Router-BmhiDQUJ.d.mts.map → Router-Dg-zk8AS.d.mts.map} +1 -1
- package/dist/esm/Router-DiZbYMLx.mjs +6 -0
- package/dist/esm/Router-DiZbYMLx.mjs.map +1 -0
- package/dist/esm/{RouterError-hhfSVGtY.d.mts → RouterError-WhCzIWuc.d.mts} +1 -1
- package/dist/esm/{RouterError-hhfSVGtY.d.mts.map → RouterError-WhCzIWuc.d.mts.map} +1 -1
- package/dist/esm/api.d.mts +2 -2
- package/dist/esm/api.d.mts.map +1 -1
- package/dist/esm/api.mjs +1 -1
- package/dist/esm/api.mjs.map +1 -1
- package/dist/esm/cloneRouter-BYNiwchg.mjs +2 -0
- package/dist/esm/{cloneRouter-U8NeEoPX.mjs.map → cloneRouter-BYNiwchg.mjs.map} +1 -1
- package/dist/esm/{index-DNjaY7KH.d.mts → index-C-i6vx5Y.d.mts} +1 -1
- package/dist/esm/{index-DNjaY7KH.d.mts.map → index-C-i6vx5Y.d.mts.map} +1 -1
- package/dist/esm/{index-r_JTvSBH.d.mts → index-DKzxav48.d.mts} +2 -2
- package/dist/esm/{index-r_JTvSBH.d.mts.map → index-DKzxav48.d.mts.map} +1 -1
- package/dist/esm/index.d.mts +3 -3
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/{internals-C59msvHY.mjs → internals-DT4mneSz.mjs} +1 -1
- package/dist/esm/{internals-C59msvHY.mjs.map → internals-DT4mneSz.mjs.map} +1 -1
- package/dist/esm/utils.d.mts +1 -1
- package/dist/esm/utils.mjs +1 -1
- package/dist/esm/validation.d.mts +4 -4
- package/dist/esm/validation.mjs +1 -1
- package/package.json +3 -3
- package/src/api/getRoutesApi.ts +47 -88
- package/src/namespaces/NavigationNamespace/types.ts +1 -1
- package/src/namespaces/RoutesNamespace/routesStore.ts +272 -52
- package/dist/cjs/Router-DnIAA87f.js +0 -6
- package/dist/cjs/Router-DnIAA87f.js.map +0 -1
- package/dist/esm/Router-pwd8YBWr.mjs +0 -6
- package/dist/esm/Router-pwd8YBWr.mjs.map +0 -1
- package/dist/esm/cloneRouter-U8NeEoPX.mjs +0 -2
package/src/api/getRoutesApi.ts
CHANGED
|
@@ -6,16 +6,17 @@ import { createInterceptable, getInternals } from "../internals";
|
|
|
6
6
|
import {
|
|
7
7
|
clearConfigEntries,
|
|
8
8
|
removeFromDefinitions,
|
|
9
|
-
sanitizeRoute,
|
|
10
9
|
} from "../namespaces/RoutesNamespace/helpers";
|
|
11
10
|
import {
|
|
12
11
|
validateClearRoutes,
|
|
13
12
|
validateRemoveRoute,
|
|
14
13
|
} from "../namespaces/RoutesNamespace/routeGuards";
|
|
15
14
|
import {
|
|
16
|
-
|
|
15
|
+
adoptRouteArtifacts,
|
|
16
|
+
assertAddable,
|
|
17
|
+
buildAddArtifacts,
|
|
18
|
+
buildReplaceArtifacts,
|
|
17
19
|
refreshForwardMap,
|
|
18
|
-
registerAllRouteHandlers,
|
|
19
20
|
} from "../namespaces/RoutesNamespace/routesStore";
|
|
20
21
|
|
|
21
22
|
import type { RoutesApi } from "./types";
|
|
@@ -36,32 +37,6 @@ import type { RouteDefinition, RouteTree } from "route-tree";
|
|
|
36
37
|
// Helpers
|
|
37
38
|
// ============================================================================
|
|
38
39
|
|
|
39
|
-
/**
|
|
40
|
-
* Recursively finds a route definition by its full dotted name.
|
|
41
|
-
*/
|
|
42
|
-
function findDefinition(
|
|
43
|
-
definitions: RouteDefinition[],
|
|
44
|
-
fullName: string,
|
|
45
|
-
parentPrefix = "",
|
|
46
|
-
): RouteDefinition | undefined {
|
|
47
|
-
for (const def of definitions) {
|
|
48
|
-
const currentFullName = parentPrefix
|
|
49
|
-
? `${parentPrefix}.${def.name}`
|
|
50
|
-
: def.name;
|
|
51
|
-
|
|
52
|
-
if (currentFullName === fullName) {
|
|
53
|
-
return def;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (def.children && fullName.startsWith(`${currentFullName}.`)) {
|
|
57
|
-
return findDefinition(def.children, fullName, currentFullName);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/* v8 ignore next -- @preserve: defensive return, callers validate route exists before calling */
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
40
|
/**
|
|
66
41
|
* Clears all config entries and lifecycle handlers for a removed route
|
|
67
42
|
* (and all its descendants).
|
|
@@ -116,20 +91,36 @@ function updateForwardTo<
|
|
|
116
91
|
name: string,
|
|
117
92
|
forwardTo: string | ForwardToCallback<Dependencies> | null,
|
|
118
93
|
config: RouteConfig,
|
|
119
|
-
refreshForwardMapFn: (config: RouteConfig) => Record<string, string>,
|
|
120
94
|
): Record<string, string> {
|
|
95
|
+
// Prepare-then-commit (issue #698): apply the change to CLONES of the forward
|
|
96
|
+
// maps, resolve the chain (a cycle throws here), and only then swap the clones
|
|
97
|
+
// in — so a rejected update never leaves config.forwardMap poisoned.
|
|
98
|
+
const forwardMap = Object.assign(
|
|
99
|
+
Object.create(null) as RouteConfig["forwardMap"],
|
|
100
|
+
config.forwardMap,
|
|
101
|
+
);
|
|
102
|
+
const forwardFnMap = Object.assign(
|
|
103
|
+
Object.create(null) as RouteConfig["forwardFnMap"],
|
|
104
|
+
config.forwardFnMap,
|
|
105
|
+
);
|
|
106
|
+
|
|
121
107
|
if (forwardTo === null) {
|
|
122
|
-
delete
|
|
123
|
-
delete
|
|
108
|
+
delete forwardMap[name];
|
|
109
|
+
delete forwardFnMap[name];
|
|
124
110
|
} else if (typeof forwardTo === "string") {
|
|
125
|
-
delete
|
|
126
|
-
|
|
111
|
+
delete forwardFnMap[name];
|
|
112
|
+
forwardMap[name] = forwardTo;
|
|
127
113
|
} else {
|
|
128
|
-
delete
|
|
129
|
-
|
|
114
|
+
delete forwardMap[name];
|
|
115
|
+
forwardFnMap[name] = forwardTo;
|
|
130
116
|
}
|
|
131
117
|
|
|
132
|
-
|
|
118
|
+
const resolved = refreshForwardMap({ ...config, forwardMap });
|
|
119
|
+
|
|
120
|
+
config.forwardMap = forwardMap;
|
|
121
|
+
config.forwardFnMap = forwardFnMap;
|
|
122
|
+
|
|
123
|
+
return resolved;
|
|
133
124
|
}
|
|
134
125
|
|
|
135
126
|
/**
|
|
@@ -212,37 +203,19 @@ function addRoutes<
|
|
|
212
203
|
routes: Route<Dependencies>[],
|
|
213
204
|
parentName?: string,
|
|
214
205
|
): void {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
for (const route of routes) {
|
|
222
|
-
parentDef.children.push(sanitizeRoute(route));
|
|
223
|
-
}
|
|
224
|
-
} else {
|
|
225
|
-
for (const route of routes) {
|
|
226
|
-
store.definitions.push(sanitizeRoute(route));
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
registerAllRouteHandlers(
|
|
231
|
-
routes,
|
|
232
|
-
store.config,
|
|
233
|
-
store.routeCustomFields,
|
|
234
|
-
store.pendingCanActivate,
|
|
235
|
-
store.pendingCanDeactivate,
|
|
236
|
-
store.depsStore,
|
|
237
|
-
parentName ?? "",
|
|
238
|
-
);
|
|
239
|
-
|
|
240
|
-
store.treeOperations.commitTreeChanges(store);
|
|
206
|
+
// Prepare-then-commit (issue #698): reject the silent-corruption cases
|
|
207
|
+
// up front (dup name vs existing, missing parent), build the merged tree /
|
|
208
|
+
// config into locals (async/circular forwardTo + invalid constraint throw
|
|
209
|
+
// here), then swap atomically. A rejected add leaves the store untouched.
|
|
210
|
+
assertAddable(store, routes, parentName);
|
|
211
|
+
adoptRouteArtifacts(store, buildAddArtifacts(store, routes, parentName));
|
|
241
212
|
}
|
|
242
213
|
|
|
243
214
|
/**
|
|
244
|
-
* Atomically replaces all routes with a new set.
|
|
245
|
-
*
|
|
215
|
+
* Atomically replaces all routes with a new set (HMR / code-splitting).
|
|
216
|
+
* Prepare-then-commit (issue #698): the new set is fully built into locals
|
|
217
|
+
* first — a circular/async forwardTo or invalid path throws here, leaving the
|
|
218
|
+
* existing tree intact — then committed.
|
|
246
219
|
*/
|
|
247
220
|
function replaceRoutes<
|
|
248
221
|
Dependencies extends DefaultDependencies = DefaultDependencies,
|
|
@@ -253,32 +226,19 @@ function replaceRoutes<
|
|
|
253
226
|
currentPath: string | undefined,
|
|
254
227
|
previousTransition: TransitionMeta | undefined,
|
|
255
228
|
): void {
|
|
256
|
-
//
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
// Step 3: Clear definition lifecycle handlers (preserve external guards)
|
|
260
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed set after wiring
|
|
261
|
-
store.lifecycleNamespace!.clearDefinitionGuards();
|
|
262
|
-
|
|
263
|
-
// Step 4: Register new routes
|
|
264
|
-
for (const route of routes) {
|
|
265
|
-
store.definitions.push(sanitizeRoute(route));
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
registerAllRouteHandlers(
|
|
229
|
+
// Build the whole new set BEFORE touching the store.
|
|
230
|
+
const artifacts = buildReplaceArtifacts(
|
|
269
231
|
routes,
|
|
270
|
-
store.
|
|
271
|
-
store.
|
|
272
|
-
store.pendingCanActivate,
|
|
273
|
-
store.pendingCanDeactivate,
|
|
274
|
-
store.depsStore,
|
|
275
|
-
"",
|
|
232
|
+
store.rootPath,
|
|
233
|
+
store.matcherOptions,
|
|
276
234
|
);
|
|
277
235
|
|
|
278
|
-
//
|
|
279
|
-
|
|
236
|
+
// Clear definition lifecycle handlers (preserve external guards), then swap.
|
|
237
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed set after wiring
|
|
238
|
+
store.lifecycleNamespace!.clearDefinitionGuards();
|
|
239
|
+
adoptRouteArtifacts(store, artifacts);
|
|
280
240
|
|
|
281
|
-
//
|
|
241
|
+
// Revalidate state (preserve transition from previous state)
|
|
282
242
|
if (currentPath !== undefined) {
|
|
283
243
|
const revalidated = ctx.matchPath(currentPath, ctx.getOptions());
|
|
284
244
|
|
|
@@ -341,7 +301,6 @@ function updateRouteConfig<
|
|
|
341
301
|
name,
|
|
342
302
|
updates.forwardTo,
|
|
343
303
|
store.config,
|
|
344
|
-
(config) => refreshForwardMap(config),
|
|
345
304
|
);
|
|
346
305
|
}
|
|
347
306
|
|
|
@@ -23,7 +23,7 @@ export interface NavigationContext {
|
|
|
23
23
|
*
|
|
24
24
|
* These are function references from other namespaces/facade,
|
|
25
25
|
* avoiding the need to pass the entire Router object.
|
|
26
|
-
|
|
26
|
+
*/
|
|
27
27
|
export interface NavigationDependencies {
|
|
28
28
|
/** Get router options */
|
|
29
29
|
getOptions: () => Options;
|
|
@@ -132,6 +132,32 @@ export function refreshForwardMap(config: RouteConfig): Record<string, string> {
|
|
|
132
132
|
// Route handler registration
|
|
133
133
|
// =============================================================================
|
|
134
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Throws if `forwardTo` is an async function (native or transpiled). Async
|
|
137
|
+
* forwardTo callbacks break the synchronous matchPath/buildPath contract.
|
|
138
|
+
* Runs inside `registerForwardTo`, which the prepare-phase build invokes via
|
|
139
|
+
* `registerAllRouteHandlers` — so the check fires before any store mutation.
|
|
140
|
+
*/
|
|
141
|
+
function assertForwardToNotAsync(forwardTo: unknown, fullName: string): void {
|
|
142
|
+
if (typeof forwardTo !== "function") {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const isNativeAsync =
|
|
147
|
+
(forwardTo as { constructor: { name: string } }).constructor.name ===
|
|
148
|
+
"AsyncFunction";
|
|
149
|
+
const isTranspiledAsync = (forwardTo as { toString: () => string })
|
|
150
|
+
.toString()
|
|
151
|
+
.includes("__awaiter");
|
|
152
|
+
|
|
153
|
+
if (isNativeAsync || isTranspiledAsync) {
|
|
154
|
+
throw new TypeError(
|
|
155
|
+
`forwardTo callback cannot be async for route "${fullName}". ` +
|
|
156
|
+
`Async functions break matchPath/buildPath.`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
135
161
|
function registerForwardTo<Dependencies extends DefaultDependencies>(
|
|
136
162
|
route: Route<Dependencies>,
|
|
137
163
|
fullName: string,
|
|
@@ -163,19 +189,7 @@ function registerForwardTo<Dependencies extends DefaultDependencies>(
|
|
|
163
189
|
);
|
|
164
190
|
}
|
|
165
191
|
|
|
166
|
-
|
|
167
|
-
const isNativeAsync =
|
|
168
|
-
(route.forwardTo as { constructor: { name: string } }).constructor
|
|
169
|
-
.name === "AsyncFunction";
|
|
170
|
-
const isTranspiledAsync = route.forwardTo.toString().includes("__awaiter");
|
|
171
|
-
|
|
172
|
-
if (isNativeAsync || isTranspiledAsync) {
|
|
173
|
-
throw new TypeError(
|
|
174
|
-
`forwardTo callback cannot be async for route "${fullName}". ` +
|
|
175
|
-
`Async functions break matchPath/buildPath.`,
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
192
|
+
assertForwardToNotAsync(route.forwardTo, fullName);
|
|
179
193
|
|
|
180
194
|
// forwardTo is guaranteed to exist at this point
|
|
181
195
|
if (typeof route.forwardTo === "string") {
|
|
@@ -193,7 +207,6 @@ function registerSingleRouteHandlers<Dependencies extends DefaultDependencies>(
|
|
|
193
207
|
routeCustomFields: Record<string, Record<string, unknown>>,
|
|
194
208
|
pendingCanActivate: Map<string, GuardFnFactory<Dependencies>>,
|
|
195
209
|
pendingCanDeactivate: Map<string, GuardFnFactory<Dependencies>>,
|
|
196
|
-
depsStore: RoutesDependencies<Dependencies> | undefined,
|
|
197
210
|
): void {
|
|
198
211
|
const standardKeys = new Set([
|
|
199
212
|
"name",
|
|
@@ -214,20 +227,15 @@ function registerSingleRouteHandlers<Dependencies extends DefaultDependencies>(
|
|
|
214
227
|
routeCustomFields[fullName] = customFields;
|
|
215
228
|
}
|
|
216
229
|
|
|
230
|
+
// Guards are collected here and registered into the lifecycle later — by
|
|
231
|
+
// `adoptRouteArtifacts` (add/replace) or `setDependencies` (initial routes) —
|
|
232
|
+
// so the build stays a pure, side-effect-free preparation step.
|
|
217
233
|
if (route.canActivate) {
|
|
218
|
-
|
|
219
|
-
depsStore.addActivateGuard(fullName, route.canActivate);
|
|
220
|
-
} else {
|
|
221
|
-
pendingCanActivate.set(fullName, route.canActivate);
|
|
222
|
-
}
|
|
234
|
+
pendingCanActivate.set(fullName, route.canActivate);
|
|
223
235
|
}
|
|
224
236
|
|
|
225
237
|
if (route.canDeactivate) {
|
|
226
|
-
|
|
227
|
-
depsStore.addDeactivateGuard(fullName, route.canDeactivate);
|
|
228
|
-
} else {
|
|
229
|
-
pendingCanDeactivate.set(fullName, route.canDeactivate);
|
|
230
|
-
}
|
|
238
|
+
pendingCanDeactivate.set(fullName, route.canDeactivate);
|
|
231
239
|
}
|
|
232
240
|
|
|
233
241
|
if (route.forwardTo) {
|
|
@@ -249,15 +257,12 @@ function registerSingleRouteHandlers<Dependencies extends DefaultDependencies>(
|
|
|
249
257
|
}
|
|
250
258
|
}
|
|
251
259
|
|
|
252
|
-
|
|
253
|
-
Dependencies extends DefaultDependencies,
|
|
254
|
-
>(
|
|
260
|
+
function registerAllRouteHandlers<Dependencies extends DefaultDependencies>(
|
|
255
261
|
routes: readonly Route<Dependencies>[],
|
|
256
262
|
config: RouteConfig,
|
|
257
263
|
routeCustomFields: Record<string, Record<string, unknown>>,
|
|
258
264
|
pendingCanActivate: Map<string, GuardFnFactory<Dependencies>>,
|
|
259
265
|
pendingCanDeactivate: Map<string, GuardFnFactory<Dependencies>>,
|
|
260
|
-
depsStore: RoutesDependencies<Dependencies> | undefined,
|
|
261
266
|
parentName = "",
|
|
262
267
|
): void {
|
|
263
268
|
for (const route of routes) {
|
|
@@ -270,7 +275,6 @@ export function registerAllRouteHandlers<
|
|
|
270
275
|
routeCustomFields,
|
|
271
276
|
pendingCanActivate,
|
|
272
277
|
pendingCanDeactivate,
|
|
273
|
-
depsStore,
|
|
274
278
|
);
|
|
275
279
|
|
|
276
280
|
if (route.children) {
|
|
@@ -280,7 +284,6 @@ export function registerAllRouteHandlers<
|
|
|
280
284
|
routeCustomFields,
|
|
281
285
|
pendingCanActivate,
|
|
282
286
|
pendingCanDeactivate,
|
|
283
|
-
depsStore,
|
|
284
287
|
fullName,
|
|
285
288
|
);
|
|
286
289
|
}
|
|
@@ -288,55 +291,272 @@ export function registerAllRouteHandlers<
|
|
|
288
291
|
}
|
|
289
292
|
|
|
290
293
|
// =============================================================================
|
|
291
|
-
//
|
|
294
|
+
// Prepare-then-commit (issue #698)
|
|
295
|
+
//
|
|
296
|
+
// add()/replace() build the complete new store state into LOCAL structures, and
|
|
297
|
+
// only swap it into the store once every core-level error has surfaced from the
|
|
298
|
+
// build itself (async/circular forwardTo throw in registerAllRouteHandlers /
|
|
299
|
+
// refreshForwardMap; invalid path constraint throws in rebuildTree). The store
|
|
300
|
+
// is mutated only by `adoptRouteArtifacts`, which cannot throw — so a rejected
|
|
301
|
+
// build leaves the existing routes untouched. The two silent-corruption cases
|
|
302
|
+
// route-tree never throws on (duplicate name vs an existing route, missing
|
|
303
|
+
// parent) are caught up front by `assertAddable`.
|
|
292
304
|
// =============================================================================
|
|
293
305
|
|
|
294
|
-
|
|
306
|
+
/**
|
|
307
|
+
* The fully-built, ready-to-swap result of preparing a route mutation. Holds
|
|
308
|
+
* everything `adoptRouteArtifacts` assigns into the store.
|
|
309
|
+
*/
|
|
310
|
+
interface RouteArtifacts<
|
|
295
311
|
Dependencies extends DefaultDependencies = DefaultDependencies,
|
|
296
|
-
>
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
const pendingCanActivate = new Map<string, GuardFnFactory<Dependencies>>();
|
|
307
|
-
const pendingCanDeactivate = new Map<string, GuardFnFactory<Dependencies>>();
|
|
312
|
+
> {
|
|
313
|
+
readonly definitions: RouteDefinition[];
|
|
314
|
+
readonly config: RouteConfig;
|
|
315
|
+
readonly routeCustomFields: Record<string, Record<string, unknown>>;
|
|
316
|
+
readonly pendingCanActivate: Map<string, GuardFnFactory<Dependencies>>;
|
|
317
|
+
readonly pendingCanDeactivate: Map<string, GuardFnFactory<Dependencies>>;
|
|
318
|
+
readonly tree: RouteTree;
|
|
319
|
+
readonly matcher: Matcher;
|
|
320
|
+
readonly resolvedForwardMap: Record<string, string>;
|
|
321
|
+
}
|
|
308
322
|
|
|
323
|
+
/** Null-proto shallow clone of a RouteConfig (preserves the 5 maps' contents). */
|
|
324
|
+
function cloneConfig(config: RouteConfig): RouteConfig {
|
|
325
|
+
const clone = createEmptyConfig();
|
|
326
|
+
|
|
327
|
+
Object.assign(clone.decoders, config.decoders);
|
|
328
|
+
Object.assign(clone.encoders, config.encoders);
|
|
329
|
+
Object.assign(clone.defaultParams, config.defaultParams);
|
|
330
|
+
Object.assign(clone.forwardMap, config.forwardMap);
|
|
331
|
+
Object.assign(clone.forwardFnMap, config.forwardFnMap);
|
|
332
|
+
|
|
333
|
+
return clone;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Returns a new definitions array with `added` inserted, without mutating the
|
|
338
|
+
* input. For a top-level add the existing definitions are shallow-copied and
|
|
339
|
+
* `added` appended. For a parented add the spine down to the parent is cloned
|
|
340
|
+
* (siblings/other branches are shared by reference) and `added` appended to the
|
|
341
|
+
* parent's children. Caller guarantees the parent path exists (see assertAddable).
|
|
342
|
+
*/
|
|
343
|
+
function insertAddedDefinitions(
|
|
344
|
+
definitions: readonly RouteDefinition[],
|
|
345
|
+
added: RouteDefinition[],
|
|
346
|
+
parentSegments: readonly string[],
|
|
347
|
+
): RouteDefinition[] {
|
|
348
|
+
if (parentSegments.length === 0) {
|
|
349
|
+
return [...definitions, ...added];
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const [head, ...rest] = parentSegments;
|
|
353
|
+
|
|
354
|
+
return definitions.map((def) => {
|
|
355
|
+
if (def.name !== head) {
|
|
356
|
+
return def;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const children = def.children ?? [];
|
|
360
|
+
|
|
361
|
+
return {
|
|
362
|
+
...def,
|
|
363
|
+
children:
|
|
364
|
+
rest.length === 0
|
|
365
|
+
? [...children, ...added]
|
|
366
|
+
: insertAddedDefinitions(children, added, rest),
|
|
367
|
+
};
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** Depth-first walk yielding each route's full dotted name (no side effects). */
|
|
372
|
+
function walkRouteNames<Dependencies extends DefaultDependencies>(
|
|
373
|
+
routes: readonly Route<Dependencies>[],
|
|
374
|
+
parentName: string,
|
|
375
|
+
onName: (fullName: string) => void,
|
|
376
|
+
): void {
|
|
309
377
|
for (const route of routes) {
|
|
310
|
-
|
|
378
|
+
const fullName = parentName ? `${parentName}.${route.name}` : route.name;
|
|
379
|
+
|
|
380
|
+
onName(fullName);
|
|
381
|
+
|
|
382
|
+
if (route.children) {
|
|
383
|
+
walkRouteNames(route.children, fullName, onName);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Up-front guard for `add` against the two corruptions route-tree stays silent
|
|
390
|
+
* on: a missing `parent`, and a name that collides with an EXISTING route
|
|
391
|
+
* (which would otherwise be silently overwritten). Throws before any build.
|
|
392
|
+
*/
|
|
393
|
+
export function assertAddable<Dependencies extends DefaultDependencies>(
|
|
394
|
+
store: RoutesStore<Dependencies>,
|
|
395
|
+
routes: readonly Route<Dependencies>[],
|
|
396
|
+
parentName: string | undefined,
|
|
397
|
+
): void {
|
|
398
|
+
if (parentName !== undefined && !store.matcher.hasRoute(parentName)) {
|
|
399
|
+
throw new Error(
|
|
400
|
+
`[router.addRoute] Parent route "${parentName}" does not exist`,
|
|
401
|
+
);
|
|
311
402
|
}
|
|
312
403
|
|
|
313
|
-
|
|
404
|
+
walkRouteNames(routes, parentName ?? "", (fullName) => {
|
|
405
|
+
if (store.matcher.hasRoute(fullName)) {
|
|
406
|
+
throw new Error(`[router.addRoute] Route "${fullName}" already exists`);
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Builds RouteArtifacts from a final definitions array and the routes whose
|
|
413
|
+
* handlers (config + guards) populate `config`/`routeCustomFields`. Guards are
|
|
414
|
+
* collected into the returned pending maps (depsStore is intentionally omitted
|
|
415
|
+
* so nothing compiles or touches the lifecycle here). THROWS on async/circular
|
|
416
|
+
* forwardTo and invalid path constraint — before the caller mutates the store.
|
|
417
|
+
*/
|
|
418
|
+
function buildArtifacts<Dependencies extends DefaultDependencies>(
|
|
419
|
+
definitions: RouteDefinition[],
|
|
420
|
+
routesForHandlers: readonly Route<Dependencies>[],
|
|
421
|
+
config: RouteConfig,
|
|
422
|
+
routeCustomFields: Record<string, Record<string, unknown>>,
|
|
423
|
+
handlerParentName: string,
|
|
424
|
+
rootPath: string,
|
|
425
|
+
matcherOptions: CreateMatcherOptions | undefined,
|
|
426
|
+
): RouteArtifacts<Dependencies> {
|
|
427
|
+
const pendingCanActivate = new Map<string, GuardFnFactory<Dependencies>>();
|
|
428
|
+
const pendingCanDeactivate = new Map<string, GuardFnFactory<Dependencies>>();
|
|
314
429
|
|
|
315
430
|
registerAllRouteHandlers(
|
|
316
|
-
|
|
431
|
+
routesForHandlers,
|
|
317
432
|
config,
|
|
318
433
|
routeCustomFields,
|
|
319
434
|
pendingCanActivate,
|
|
320
435
|
pendingCanDeactivate,
|
|
321
|
-
|
|
322
|
-
"",
|
|
436
|
+
handlerParentName,
|
|
323
437
|
);
|
|
324
438
|
|
|
325
439
|
const resolvedForwardMap = refreshForwardMap(config);
|
|
440
|
+
const { tree, matcher } = rebuildTree(definitions, rootPath, matcherOptions);
|
|
326
441
|
|
|
327
442
|
return {
|
|
328
443
|
definitions,
|
|
329
444
|
config,
|
|
445
|
+
routeCustomFields,
|
|
446
|
+
pendingCanActivate,
|
|
447
|
+
pendingCanDeactivate,
|
|
330
448
|
tree,
|
|
331
449
|
matcher,
|
|
332
450
|
resolvedForwardMap,
|
|
333
|
-
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Builds the merged artifacts for an incremental `add` (existing ∪ new). */
|
|
455
|
+
export function buildAddArtifacts<Dependencies extends DefaultDependencies>(
|
|
456
|
+
store: RoutesStore<Dependencies>,
|
|
457
|
+
routes: readonly Route<Dependencies>[],
|
|
458
|
+
parentName: string | undefined,
|
|
459
|
+
): RouteArtifacts<Dependencies> {
|
|
460
|
+
const definitions = insertAddedDefinitions(
|
|
461
|
+
store.definitions,
|
|
462
|
+
routes.map((route) => sanitizeRoute(route)),
|
|
463
|
+
parentName === undefined ? [] : parentName.split("."),
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
return buildArtifacts(
|
|
467
|
+
definitions,
|
|
468
|
+
routes,
|
|
469
|
+
cloneConfig(store.config),
|
|
470
|
+
Object.assign(
|
|
471
|
+
Object.create(null) as Record<string, Record<string, unknown>>,
|
|
472
|
+
store.routeCustomFields,
|
|
473
|
+
),
|
|
474
|
+
parentName ?? "",
|
|
475
|
+
store.rootPath,
|
|
476
|
+
store.matcherOptions,
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** Builds the fresh artifacts for a full `replace` (standalone new set). */
|
|
481
|
+
export function buildReplaceArtifacts<Dependencies extends DefaultDependencies>(
|
|
482
|
+
routes: readonly Route<Dependencies>[],
|
|
483
|
+
rootPath: string,
|
|
484
|
+
matcherOptions: CreateMatcherOptions | undefined,
|
|
485
|
+
): RouteArtifacts<Dependencies> {
|
|
486
|
+
return buildArtifacts(
|
|
487
|
+
routes.map((route) => sanitizeRoute(route)),
|
|
488
|
+
routes,
|
|
489
|
+
createEmptyConfig(),
|
|
490
|
+
Object.create(null) as Record<string, Record<string, unknown>>,
|
|
491
|
+
"",
|
|
492
|
+
rootPath,
|
|
493
|
+
matcherOptions,
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Commits prepared artifacts into the store in place. Pure assignment — never
|
|
499
|
+
* throws — so it is the single atomic swap point of the prepare-then-commit
|
|
500
|
+
* pipeline. Guard registration is deferred to here (the build collected guards
|
|
501
|
+
* without compiling); `depsStore` is always set on a wired router, which is the
|
|
502
|
+
* only path that reaches `add`/`replace`.
|
|
503
|
+
*/
|
|
504
|
+
export function adoptRouteArtifacts<Dependencies extends DefaultDependencies>(
|
|
505
|
+
store: RoutesStore<Dependencies>,
|
|
506
|
+
artifacts: RouteArtifacts<Dependencies>,
|
|
507
|
+
): void {
|
|
508
|
+
store.definitions.length = 0;
|
|
509
|
+
|
|
510
|
+
for (const def of artifacts.definitions) {
|
|
511
|
+
store.definitions.push(def);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
Object.assign(store.config, artifacts.config);
|
|
515
|
+
store.routeCustomFields = artifacts.routeCustomFields;
|
|
516
|
+
store.tree = artifacts.tree;
|
|
517
|
+
store.matcher = artifacts.matcher;
|
|
518
|
+
store.resolvedForwardMap = artifacts.resolvedForwardMap;
|
|
519
|
+
|
|
520
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- depsStore is set once the router is wired; add/replace only run on a wired router (constructor-time registration uses createRoutesStore)
|
|
521
|
+
const deps = store.depsStore!;
|
|
522
|
+
|
|
523
|
+
for (const [name, handler] of artifacts.pendingCanActivate) {
|
|
524
|
+
deps.addActivateGuard(name, handler);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
for (const [name, handler] of artifacts.pendingCanDeactivate) {
|
|
528
|
+
deps.addDeactivateGuard(name, handler);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// =============================================================================
|
|
533
|
+
// Factory
|
|
534
|
+
// =============================================================================
|
|
535
|
+
|
|
536
|
+
export function createRoutesStore<
|
|
537
|
+
Dependencies extends DefaultDependencies = DefaultDependencies,
|
|
538
|
+
>(
|
|
539
|
+
routes: Route<Dependencies>[],
|
|
540
|
+
matcherOptions?: CreateMatcherOptions,
|
|
541
|
+
): RoutesStore<Dependencies> {
|
|
542
|
+
// Initial routes are a standalone set at rootPath "" — same build the
|
|
543
|
+
// prepare-then-commit `replace` path uses. Guards land in the pending maps
|
|
544
|
+
// (depsStore is wired later via setDependencies, which flushes them).
|
|
545
|
+
const artifacts = buildReplaceArtifacts(routes, "", matcherOptions);
|
|
546
|
+
|
|
547
|
+
return {
|
|
548
|
+
definitions: artifacts.definitions,
|
|
549
|
+
config: artifacts.config,
|
|
550
|
+
tree: artifacts.tree,
|
|
551
|
+
matcher: artifacts.matcher,
|
|
552
|
+
resolvedForwardMap: artifacts.resolvedForwardMap,
|
|
553
|
+
routeCustomFields: artifacts.routeCustomFields,
|
|
334
554
|
rootPath: "",
|
|
335
555
|
matcherOptions,
|
|
336
556
|
depsStore: undefined,
|
|
337
557
|
lifecycleNamespace: undefined,
|
|
338
|
-
pendingCanActivate,
|
|
339
|
-
pendingCanDeactivate,
|
|
558
|
+
pendingCanActivate: artifacts.pendingCanActivate,
|
|
559
|
+
pendingCanDeactivate: artifacts.pendingCanDeactivate,
|
|
340
560
|
treeOperations: {
|
|
341
561
|
commitTreeChanges,
|
|
342
562
|
resetStore,
|