@real-router/core 0.36.0 → 0.36.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/api.js +1 -1
- package/dist/cjs/api.js.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/metafile-cjs.json +1 -1
- package/dist/esm/api.mjs +1 -1
- package/dist/esm/api.mjs.map +1 -1
- package/dist/esm/chunk-PKKD6URG.mjs +1 -0
- package/dist/esm/chunk-PKKD6URG.mjs.map +1 -0
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/metafile-esm.json +1 -1
- package/package.json +4 -4
- package/src/Router.ts +33 -12
- package/src/api/getPluginApi.ts +10 -4
- package/src/constants.ts +2 -0
- package/src/fsm/routerFSM.ts +2 -21
- package/src/internals.ts +21 -2
- package/src/namespaces/EventBusNamespace/EventBusNamespace.ts +50 -39
- package/src/namespaces/NavigationNamespace/NavigationNamespace.ts +221 -153
- package/src/namespaces/NavigationNamespace/constants.ts +55 -0
- package/src/namespaces/NavigationNamespace/transition/completeTransition.ts +100 -0
- package/src/namespaces/NavigationNamespace/transition/errorHandling.ts +34 -0
- package/src/namespaces/NavigationNamespace/transition/guardPhase.ts +214 -0
- package/src/namespaces/NavigationNamespace/types.ts +14 -30
- package/src/namespaces/RouteLifecycleNamespace/RouteLifecycleNamespace.ts +6 -1
- package/src/namespaces/RoutesNamespace/RoutesNamespace.ts +36 -35
- package/src/namespaces/RoutesNamespace/forwardToValidation.ts +2 -5
- package/src/namespaces/RoutesNamespace/types.ts +1 -2
- package/src/namespaces/StateNamespace/StateNamespace.ts +13 -17
- package/src/transitionPath.ts +68 -39
- package/src/wiring/RouterWiringBuilder.ts +8 -11
- package/dist/esm/chunk-AP4ME3HM.mjs +0 -1
- package/dist/esm/chunk-AP4ME3HM.mjs.map +0 -1
- package/src/namespaces/NavigationNamespace/transition/executeLifecycleGuards.ts +0 -52
- package/src/namespaces/NavigationNamespace/transition/index.ts +0 -93
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
// packages/core/src/namespaces/NavigationNamespace/NavigationNamespace.ts
|
|
2
|
-
|
|
3
1
|
import { logger } from "@real-router/logger";
|
|
4
2
|
|
|
5
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
CACHED_NOT_STARTED_REJECTION,
|
|
5
|
+
CACHED_ROUTE_NOT_FOUND_ERROR,
|
|
6
|
+
CACHED_ROUTE_NOT_FOUND_REJECTION,
|
|
7
|
+
CACHED_SAME_STATES_ERROR,
|
|
8
|
+
CACHED_SAME_STATES_REJECTION,
|
|
9
|
+
} from "./constants";
|
|
10
|
+
import { completeTransition } from "./transition/completeTransition";
|
|
11
|
+
import { routeTransitionError } from "./transition/errorHandling";
|
|
12
|
+
import { executeGuardPipeline } from "./transition/guardPhase";
|
|
6
13
|
import {
|
|
7
14
|
validateNavigateArgs,
|
|
8
15
|
validateNavigateToDefaultArgs,
|
|
@@ -10,9 +17,9 @@ import {
|
|
|
10
17
|
} from "./validators";
|
|
11
18
|
import { errorCodes, constants } from "../../constants";
|
|
12
19
|
import { RouterError } from "../../RouterError";
|
|
13
|
-
import { nameToIDs } from "../../transitionPath";
|
|
20
|
+
import { getTransitionPath, nameToIDs } from "../../transitionPath";
|
|
14
21
|
|
|
15
|
-
import type {
|
|
22
|
+
import type { NavigationContext, NavigationDependencies } from "./types";
|
|
16
23
|
import type {
|
|
17
24
|
NavigationOptions,
|
|
18
25
|
Params,
|
|
@@ -36,65 +43,35 @@ function forceReplaceFromUnknown(
|
|
|
36
43
|
: opts;
|
|
37
44
|
}
|
|
38
45
|
|
|
39
|
-
function
|
|
40
|
-
signal: _,
|
|
41
|
-
...rest
|
|
42
|
-
}: NavigationOptions): NavigationOptions {
|
|
43
|
-
return rest;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function routeTransitionError(
|
|
47
|
-
deps: NavigationDependencies,
|
|
48
|
-
error: unknown,
|
|
49
|
-
toState: State,
|
|
50
|
-
fromState: State | undefined,
|
|
51
|
-
): void {
|
|
52
|
-
const routerError = error as RouterError;
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
routerError.code === errorCodes.TRANSITION_CANCELLED ||
|
|
56
|
-
routerError.code === errorCodes.ROUTE_NOT_FOUND
|
|
57
|
-
) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
deps.sendTransitionFail(toState, fromState, routerError);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function buildSuccessState(
|
|
65
|
-
finalState: State,
|
|
66
|
-
transitionOutput: TransitionOutput["meta"],
|
|
46
|
+
function isSameNavigation(
|
|
67
47
|
fromState: State | undefined,
|
|
68
48
|
opts: NavigationOptions,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
Object.freeze(transitionMeta.segments.deactivated);
|
|
80
|
-
Object.freeze(transitionMeta.segments.activated);
|
|
81
|
-
Object.freeze(transitionMeta.segments);
|
|
82
|
-
Object.freeze(transitionMeta);
|
|
83
|
-
|
|
84
|
-
return {
|
|
85
|
-
...finalState,
|
|
86
|
-
transition: transitionMeta,
|
|
87
|
-
};
|
|
49
|
+
toState: State,
|
|
50
|
+
areStatesEqual: (a: State, b: State, ignoreQuery: boolean) => boolean,
|
|
51
|
+
): boolean {
|
|
52
|
+
return (
|
|
53
|
+
!!fromState &&
|
|
54
|
+
!opts.reload &&
|
|
55
|
+
!opts.force &&
|
|
56
|
+
areStatesEqual(fromState, toState, false)
|
|
57
|
+
);
|
|
88
58
|
}
|
|
89
59
|
|
|
90
60
|
/**
|
|
91
61
|
* Independent namespace for managing navigation.
|
|
92
62
|
*
|
|
93
63
|
* Handles navigate(), navigateToDefault(), navigateToNotFound(), and transition state.
|
|
64
|
+
*
|
|
65
|
+
* Performance: navigate() uses optimistic sync execution — guards run synchronously
|
|
66
|
+
* until one returns a Promise, then switches to async. This eliminates Promise/AbortController
|
|
67
|
+
* overhead for the common case (no guards or sync guards).
|
|
94
68
|
*/
|
|
95
69
|
export class NavigationNamespace {
|
|
70
|
+
lastSyncResolved = false;
|
|
71
|
+
lastSyncRejected = false;
|
|
96
72
|
#deps!: NavigationDependencies;
|
|
97
73
|
#currentController: AbortController | null = null;
|
|
74
|
+
#navigationId = 0;
|
|
98
75
|
|
|
99
76
|
// =========================================================================
|
|
100
77
|
// Static validation methods (called by facade before instance methods)
|
|
@@ -128,154 +105,176 @@ export class NavigationNamespace {
|
|
|
128
105
|
// Instance methods
|
|
129
106
|
// =========================================================================
|
|
130
107
|
|
|
131
|
-
|
|
132
|
-
* Navigates to a route by name.
|
|
133
|
-
* Arguments should be pre-parsed and validated by facade.
|
|
134
|
-
*/
|
|
135
|
-
async navigate(
|
|
108
|
+
navigate(
|
|
136
109
|
name: string,
|
|
137
110
|
params: Params,
|
|
138
111
|
opts: NavigationOptions,
|
|
139
112
|
): Promise<State> {
|
|
140
|
-
|
|
141
|
-
throw new RouterError(errorCodes.ROUTER_NOT_STARTED);
|
|
142
|
-
}
|
|
143
|
-
|
|
113
|
+
this.lastSyncResolved = false;
|
|
144
114
|
const deps = this.#deps;
|
|
145
115
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if (!
|
|
149
|
-
|
|
116
|
+
// Fast-path sync rejections: cached error + cached Promise.reject
|
|
117
|
+
// No allocations, no throw/catch overhead, facade skips .catch() suppression
|
|
118
|
+
if (!deps.canNavigate()) {
|
|
119
|
+
this.lastSyncRejected = true;
|
|
150
120
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
throw err;
|
|
121
|
+
return CACHED_NOT_STARTED_REJECTION;
|
|
154
122
|
}
|
|
155
123
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
route.params,
|
|
161
|
-
deps.buildPath(route.name, route.params),
|
|
162
|
-
{
|
|
163
|
-
params: route.meta,
|
|
164
|
-
},
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
const fromState = deps.getState();
|
|
124
|
+
let toState: State | undefined;
|
|
125
|
+
let fromState: State | undefined;
|
|
126
|
+
let transitionStarted = false;
|
|
127
|
+
let controller: AbortController | null = null;
|
|
168
128
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (
|
|
172
|
-
fromState &&
|
|
173
|
-
!opts.reload &&
|
|
174
|
-
!opts.force &&
|
|
175
|
-
deps.areStatesEqual(fromState, toState, false)
|
|
176
|
-
) {
|
|
177
|
-
const err = new RouterError(errorCodes.SAME_STATES);
|
|
129
|
+
try {
|
|
130
|
+
toState = deps.buildNavigateState(name, params);
|
|
178
131
|
|
|
179
|
-
|
|
132
|
+
if (!toState) {
|
|
133
|
+
deps.emitTransitionError(
|
|
134
|
+
undefined,
|
|
135
|
+
deps.getState(),
|
|
136
|
+
CACHED_ROUTE_NOT_FOUND_ERROR,
|
|
137
|
+
);
|
|
138
|
+
this.lastSyncRejected = true;
|
|
180
139
|
|
|
181
|
-
|
|
182
|
-
|
|
140
|
+
return CACHED_ROUTE_NOT_FOUND_REJECTION;
|
|
141
|
+
}
|
|
183
142
|
|
|
184
|
-
|
|
143
|
+
fromState = deps.getState();
|
|
144
|
+
opts = forceReplaceFromUnknown(opts, fromState);
|
|
185
145
|
|
|
186
|
-
|
|
146
|
+
if (isSameNavigation(fromState, opts, toState, deps.areStatesEqual)) {
|
|
147
|
+
deps.emitTransitionError(toState, fromState, CACHED_SAME_STATES_ERROR);
|
|
148
|
+
this.lastSyncRejected = true;
|
|
187
149
|
|
|
188
|
-
|
|
150
|
+
return CACHED_SAME_STATES_REJECTION;
|
|
151
|
+
}
|
|
189
152
|
|
|
190
|
-
|
|
191
|
-
if (opts.signal.aborted) {
|
|
192
|
-
this.#currentController = null;
|
|
153
|
+
this.#abortPreviousNavigation();
|
|
193
154
|
|
|
155
|
+
if (opts.signal?.aborted) {
|
|
194
156
|
throw new RouterError(errorCodes.TRANSITION_CANCELLED, {
|
|
195
157
|
reason: opts.signal.reason,
|
|
196
158
|
});
|
|
197
159
|
}
|
|
198
160
|
|
|
199
|
-
|
|
200
|
-
"abort",
|
|
201
|
-
() => {
|
|
202
|
-
controller.abort(opts.signal?.reason);
|
|
203
|
-
},
|
|
204
|
-
{ once: true, signal: controller.signal },
|
|
205
|
-
);
|
|
206
|
-
}
|
|
161
|
+
const myId = ++this.#navigationId;
|
|
207
162
|
|
|
208
|
-
|
|
163
|
+
deps.startTransition(toState, fromState);
|
|
164
|
+
transitionStarted = true;
|
|
209
165
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
166
|
+
// Reentrant navigate from TRANSITION_START listener superseded this navigation
|
|
167
|
+
if (this.#navigationId !== myId) {
|
|
168
|
+
throw new RouterError(errorCodes.TRANSITION_CANCELLED);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const [canDeactivateFunctions, canActivateFunctions] =
|
|
172
|
+
deps.getLifecycleFunctions();
|
|
173
|
+
const isUnknownRoute = toState.name === constants.UNKNOWN_ROUTE;
|
|
174
|
+
|
|
175
|
+
const { toDeactivate, toActivate, intersection } = getTransitionPath(
|
|
213
176
|
toState,
|
|
214
177
|
fromState,
|
|
215
|
-
opts,
|
|
216
|
-
controller.signal,
|
|
178
|
+
opts.reload,
|
|
217
179
|
);
|
|
218
180
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
181
|
+
const shouldDeactivate =
|
|
182
|
+
fromState && !opts.forceDeactivate && toDeactivate.length > 0;
|
|
183
|
+
const shouldActivate = !isUnknownRoute && toActivate.length > 0;
|
|
184
|
+
const hasGuards =
|
|
185
|
+
canDeactivateFunctions.size > 0 || canActivateFunctions.size > 0;
|
|
186
|
+
|
|
187
|
+
if (hasGuards) {
|
|
188
|
+
controller = new AbortController();
|
|
189
|
+
this.#currentController = controller;
|
|
190
|
+
|
|
191
|
+
const signal = controller.signal;
|
|
192
|
+
const isCurrentNav = () =>
|
|
193
|
+
this.#navigationId === myId && deps.isActive();
|
|
194
|
+
|
|
195
|
+
const guardCompletion = executeGuardPipeline(
|
|
196
|
+
canDeactivateFunctions,
|
|
197
|
+
canActivateFunctions,
|
|
198
|
+
toDeactivate,
|
|
199
|
+
toActivate,
|
|
200
|
+
!!shouldDeactivate,
|
|
201
|
+
shouldActivate,
|
|
202
|
+
toState,
|
|
226
203
|
fromState,
|
|
227
|
-
|
|
204
|
+
signal,
|
|
205
|
+
isCurrentNav,
|
|
228
206
|
);
|
|
229
207
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
208
|
+
if (guardCompletion !== undefined) {
|
|
209
|
+
return this.#finishAsyncNavigation(
|
|
210
|
+
guardCompletion,
|
|
211
|
+
{
|
|
212
|
+
toState,
|
|
213
|
+
fromState,
|
|
214
|
+
opts,
|
|
215
|
+
toDeactivate,
|
|
216
|
+
toActivate,
|
|
217
|
+
intersection,
|
|
218
|
+
canDeactivateFunctions,
|
|
219
|
+
},
|
|
220
|
+
controller,
|
|
221
|
+
myId,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (!isCurrentNav()) {
|
|
226
|
+
throw new RouterError(errorCodes.TRANSITION_CANCELLED);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
this.#cleanupController(controller);
|
|
230
|
+
}
|
|
242
231
|
|
|
243
|
-
|
|
232
|
+
this.lastSyncResolved = true;
|
|
244
233
|
|
|
245
|
-
|
|
246
|
-
|
|
234
|
+
return Promise.resolve(
|
|
235
|
+
completeTransition(deps, {
|
|
236
|
+
toState,
|
|
237
|
+
fromState,
|
|
238
|
+
opts,
|
|
239
|
+
toDeactivate,
|
|
240
|
+
toActivate,
|
|
241
|
+
intersection,
|
|
242
|
+
canDeactivateFunctions,
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
247
245
|
} catch (error) {
|
|
248
|
-
|
|
246
|
+
this.#handleNavigateError(
|
|
247
|
+
error,
|
|
248
|
+
controller,
|
|
249
|
+
transitionStarted,
|
|
250
|
+
toState,
|
|
251
|
+
fromState,
|
|
252
|
+
);
|
|
249
253
|
|
|
250
|
-
|
|
251
|
-
} finally {
|
|
252
|
-
controller.abort();
|
|
253
|
-
if (this.#currentController === controller) {
|
|
254
|
-
this.#currentController = null;
|
|
255
|
-
}
|
|
254
|
+
return Promise.reject(error as Error);
|
|
256
255
|
}
|
|
257
256
|
}
|
|
258
257
|
|
|
259
|
-
|
|
260
|
-
* Navigates to the default route if configured.
|
|
261
|
-
* Arguments should be pre-parsed and validated by facade.
|
|
262
|
-
*/
|
|
263
|
-
async navigateToDefault(opts: NavigationOptions): Promise<State> {
|
|
258
|
+
navigateToDefault(opts: NavigationOptions): Promise<State> {
|
|
264
259
|
const deps = this.#deps;
|
|
265
260
|
const options = deps.getOptions();
|
|
266
261
|
|
|
267
262
|
if (!options.defaultRoute) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
263
|
+
return Promise.reject(
|
|
264
|
+
new RouterError(errorCodes.ROUTE_NOT_FOUND, {
|
|
265
|
+
routeName: "defaultRoute not configured",
|
|
266
|
+
}),
|
|
267
|
+
);
|
|
271
268
|
}
|
|
272
269
|
|
|
273
270
|
const { route, params } = deps.resolveDefault();
|
|
274
271
|
|
|
275
272
|
if (!route) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
273
|
+
return Promise.reject(
|
|
274
|
+
new RouterError(errorCodes.ROUTE_NOT_FOUND, {
|
|
275
|
+
routeName: "defaultRoute resolved to empty",
|
|
276
|
+
}),
|
|
277
|
+
);
|
|
279
278
|
}
|
|
280
279
|
|
|
281
280
|
return this.navigate(route, params, opts);
|
|
@@ -330,6 +329,75 @@ export class NavigationNamespace {
|
|
|
330
329
|
this.#currentController = null;
|
|
331
330
|
}
|
|
332
331
|
|
|
332
|
+
async #finishAsyncNavigation(
|
|
333
|
+
guardCompletion: Promise<void>,
|
|
334
|
+
nav: NavigationContext,
|
|
335
|
+
controller: AbortController,
|
|
336
|
+
myId: number,
|
|
337
|
+
): Promise<State> {
|
|
338
|
+
const deps = this.#deps;
|
|
339
|
+
const isActive = () =>
|
|
340
|
+
this.#navigationId === myId &&
|
|
341
|
+
!controller.signal.aborted &&
|
|
342
|
+
deps.isActive();
|
|
343
|
+
|
|
344
|
+
try {
|
|
345
|
+
if (nav.opts.signal) {
|
|
346
|
+
if (nav.opts.signal.aborted) {
|
|
347
|
+
throw new RouterError(errorCodes.TRANSITION_CANCELLED, {
|
|
348
|
+
reason: nav.opts.signal.reason,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
nav.opts.signal.addEventListener(
|
|
353
|
+
"abort",
|
|
354
|
+
() => {
|
|
355
|
+
controller.abort(nav.opts.signal?.reason);
|
|
356
|
+
},
|
|
357
|
+
{ once: true, signal: controller.signal },
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
await guardCompletion;
|
|
362
|
+
|
|
363
|
+
if (!isActive()) {
|
|
364
|
+
throw new RouterError(errorCodes.TRANSITION_CANCELLED);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return completeTransition(deps, nav);
|
|
368
|
+
} catch (error) {
|
|
369
|
+
routeTransitionError(deps, error, nav.toState, nav.fromState);
|
|
370
|
+
|
|
371
|
+
throw error;
|
|
372
|
+
} finally {
|
|
373
|
+
this.#cleanupController(controller);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
#handleNavigateError(
|
|
378
|
+
error: unknown,
|
|
379
|
+
controller: AbortController | null,
|
|
380
|
+
transitionStarted: boolean,
|
|
381
|
+
toState: State | undefined,
|
|
382
|
+
fromState: State | undefined,
|
|
383
|
+
): void {
|
|
384
|
+
if (controller) {
|
|
385
|
+
this.#cleanupController(controller);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (transitionStarted && toState) {
|
|
389
|
+
routeTransitionError(this.#deps, error, toState, fromState);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
#cleanupController(controller: AbortController): void {
|
|
394
|
+
controller.abort();
|
|
395
|
+
|
|
396
|
+
if (this.#currentController === controller) {
|
|
397
|
+
this.#currentController = null;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
333
401
|
#abortPreviousNavigation(): void {
|
|
334
402
|
if (this.#deps.isTransitioning()) {
|
|
335
403
|
logger.warn(
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// packages/core/src/namespaces/NavigationNamespace/constants.ts
|
|
2
|
+
|
|
3
|
+
import { errorCodes } from "../../constants";
|
|
4
|
+
import { RouterError } from "../../RouterError";
|
|
5
|
+
|
|
6
|
+
import type { State } from "@real-router/types";
|
|
7
|
+
|
|
8
|
+
// =============================================================================
|
|
9
|
+
// Cached Errors & Rejected Promises (Performance Optimization)
|
|
10
|
+
// =============================================================================
|
|
11
|
+
// Pre-create error instances and rejected promises for sync error paths
|
|
12
|
+
// in navigate(). Eliminates per-call allocations:
|
|
13
|
+
// - new RouterError() — object + stack trace capture (~500ns-2μs)
|
|
14
|
+
// - Promise.reject() — promise allocation
|
|
15
|
+
// - .catch(handler) — derived promise from suppression
|
|
16
|
+
//
|
|
17
|
+
// Trade-off: All error instances share the same stack trace (points here).
|
|
18
|
+
// This is acceptable because:
|
|
19
|
+
// 1. These errors indicate expected conditions, not internal bugs
|
|
20
|
+
// 2. Error code and message are sufficient for debugging
|
|
21
|
+
// 3. The facade skips .catch() suppression for cached promises (zero alloc)
|
|
22
|
+
// =============================================================================
|
|
23
|
+
|
|
24
|
+
export const CACHED_NOT_STARTED_ERROR = new RouterError(
|
|
25
|
+
errorCodes.ROUTER_NOT_STARTED,
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export const CACHED_ROUTE_NOT_FOUND_ERROR = new RouterError(
|
|
29
|
+
errorCodes.ROUTE_NOT_FOUND,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export const CACHED_SAME_STATES_ERROR = new RouterError(errorCodes.SAME_STATES);
|
|
33
|
+
|
|
34
|
+
// Pre-suppressed rejected promises — .catch() at module load prevents
|
|
35
|
+
// unhandled rejection warnings. The facade skips additional .catch() calls
|
|
36
|
+
// via the lastSyncRejected flag (zero derived-promise allocation).
|
|
37
|
+
export const CACHED_NOT_STARTED_REJECTION: Promise<State> = Promise.reject(
|
|
38
|
+
CACHED_NOT_STARTED_ERROR,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
export const CACHED_ROUTE_NOT_FOUND_REJECTION: Promise<State> = Promise.reject(
|
|
42
|
+
CACHED_ROUTE_NOT_FOUND_ERROR,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
export const CACHED_SAME_STATES_REJECTION: Promise<State> = Promise.reject(
|
|
46
|
+
CACHED_SAME_STATES_ERROR,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
// Suppress once at module load — prevents unhandled rejection events.
|
|
50
|
+
// Subsequent .catch() / await by user code still works correctly:
|
|
51
|
+
// a rejected promise stays rejected forever, each .catch() creates
|
|
52
|
+
// its own derived promise and fires its handler.
|
|
53
|
+
CACHED_NOT_STARTED_REJECTION.catch(() => {});
|
|
54
|
+
CACHED_ROUTE_NOT_FOUND_REJECTION.catch(() => {});
|
|
55
|
+
CACHED_SAME_STATES_REJECTION.catch(() => {});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { errorCodes, constants } from "../../../constants";
|
|
2
|
+
import { freezeStateInPlace } from "../../../helpers";
|
|
3
|
+
import { RouterError } from "../../../RouterError";
|
|
4
|
+
|
|
5
|
+
import type { NavigationDependencies, NavigationContext } from "../types";
|
|
6
|
+
import type {
|
|
7
|
+
NavigationOptions,
|
|
8
|
+
State,
|
|
9
|
+
TransitionMeta,
|
|
10
|
+
} from "@real-router/types";
|
|
11
|
+
|
|
12
|
+
type MutableTransitionMeta = {
|
|
13
|
+
-readonly [K in keyof TransitionMeta]: TransitionMeta[K];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function buildTransitionMeta(
|
|
17
|
+
fromState: State | undefined,
|
|
18
|
+
opts: NavigationOptions,
|
|
19
|
+
toDeactivate: string[],
|
|
20
|
+
toActivate: string[],
|
|
21
|
+
intersection: string,
|
|
22
|
+
): TransitionMeta {
|
|
23
|
+
const meta: MutableTransitionMeta = {
|
|
24
|
+
phase: "activating",
|
|
25
|
+
reason: "success",
|
|
26
|
+
segments: {
|
|
27
|
+
deactivated: toDeactivate,
|
|
28
|
+
activated: toActivate,
|
|
29
|
+
intersection,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
if (fromState?.name !== undefined) {
|
|
34
|
+
meta.from = fromState.name;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (opts.reload !== undefined) {
|
|
38
|
+
meta.reload = opts.reload;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (opts.redirected !== undefined) {
|
|
42
|
+
meta.redirected = opts.redirected;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return meta;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function stripSignal({
|
|
49
|
+
signal: _,
|
|
50
|
+
...rest
|
|
51
|
+
}: NavigationOptions): NavigationOptions {
|
|
52
|
+
return rest;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function completeTransition(
|
|
56
|
+
deps: NavigationDependencies,
|
|
57
|
+
nav: NavigationContext,
|
|
58
|
+
): State {
|
|
59
|
+
const { toState, fromState, opts, toDeactivate, toActivate, intersection } =
|
|
60
|
+
nav;
|
|
61
|
+
|
|
62
|
+
if (
|
|
63
|
+
toState.name !== constants.UNKNOWN_ROUTE &&
|
|
64
|
+
!deps.hasRoute(toState.name)
|
|
65
|
+
) {
|
|
66
|
+
const err = new RouterError(errorCodes.ROUTE_NOT_FOUND, {
|
|
67
|
+
routeName: toState.name,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
deps.sendTransitionFail(toState, fromState, err);
|
|
71
|
+
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (fromState) {
|
|
76
|
+
for (const name of toDeactivate) {
|
|
77
|
+
if (!toActivate.includes(name) && nav.canDeactivateFunctions.has(name)) {
|
|
78
|
+
deps.clearCanDeactivate(name);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
(toState as { transition: TransitionMeta }).transition = buildTransitionMeta(
|
|
84
|
+
fromState,
|
|
85
|
+
opts,
|
|
86
|
+
toDeactivate,
|
|
87
|
+
toActivate,
|
|
88
|
+
intersection,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const finalState = freezeStateInPlace(toState);
|
|
92
|
+
|
|
93
|
+
deps.setState(finalState);
|
|
94
|
+
|
|
95
|
+
const transitionOpts = opts.signal === undefined ? opts : stripSignal(opts);
|
|
96
|
+
|
|
97
|
+
deps.sendTransitionDone(finalState, fromState, transitionOpts);
|
|
98
|
+
|
|
99
|
+
return finalState;
|
|
100
|
+
}
|
|
@@ -1,7 +1,41 @@
|
|
|
1
1
|
// packages/core/src/namespaces/NavigationNamespace/transition/errorHandling.ts
|
|
2
2
|
|
|
3
|
+
import { errorCodes } from "../../../constants";
|
|
3
4
|
import { RouterError } from "../../../RouterError";
|
|
4
5
|
|
|
6
|
+
import type { NavigationDependencies } from "../types";
|
|
7
|
+
import type { State } from "@real-router/types";
|
|
8
|
+
|
|
9
|
+
export function routeTransitionError(
|
|
10
|
+
deps: NavigationDependencies,
|
|
11
|
+
error: unknown,
|
|
12
|
+
toState: State,
|
|
13
|
+
fromState: State | undefined,
|
|
14
|
+
): void {
|
|
15
|
+
const routerError = error as RouterError;
|
|
16
|
+
|
|
17
|
+
if (
|
|
18
|
+
routerError.code === errorCodes.TRANSITION_CANCELLED ||
|
|
19
|
+
routerError.code === errorCodes.ROUTE_NOT_FOUND
|
|
20
|
+
) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
deps.sendTransitionFail(toState, fromState, routerError);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function handleGuardError(
|
|
28
|
+
error: unknown,
|
|
29
|
+
errorCode: string,
|
|
30
|
+
segment: string,
|
|
31
|
+
): never {
|
|
32
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
33
|
+
throw new RouterError(errorCodes.TRANSITION_CANCELLED);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
rethrowAsRouterError(error, errorCode, segment);
|
|
37
|
+
}
|
|
38
|
+
|
|
5
39
|
/**
|
|
6
40
|
* Error metadata structure for transition errors.
|
|
7
41
|
* Contains information extracted from caught exceptions.
|