8000 refactor(router): Clean up the transition subject (#60357) · angular/angular@f030409 · GitHub
[go: up one dir, main page]

Skip to content

Commit f030409

Browse files
atscottpkozlowski-opensource
authored andcommitted
refactor(router): Clean up the transition subject (#60357)
This commit cleans up the transition subject a bit so it doesn't use a dummy initial value. It also avoids copying over data from the previous request when a new one is created. PR Close #60357
1 parent ca32602 commit f030409

File tree

2 files changed

+16
-43
lines changed

2 files changed

+16
-43
lines changed

packages/router/src/navigation_transition.ts

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export class NavigationTransitions {
372372
get hasRequestedNavigation() {
373373
return this.navigationId !== 0;
374374
}
375-
private transitions?: BehaviorSubject<NavigationTransition>;
375+
private transitions?: BehaviorSubject<NavigationTransition | null>;
376376
/**
377377
* Hook that enables you to pause navigation after the preactivation phase.
378378
* Used by `RouterModule`.
@@ -416,45 +416,21 @@ export class NavigationTransitions {
416416
>,
417417
) {
418418
const id = ++this.navigationId;
419-
this.transitions?.next({...this.transitions.value, ...request, id});
420-
}
421-
422-
setupNavigations(
423-
router: InternalRouterInterface,
424-
initialUrlTree: UrlTree,
425-
initialRouterState: RouterState,
426-
): Observable<NavigationTransition> {
427-
this.transitions = new BehaviorSubject<NavigationTransition>({
428-
id: 0,
429-
currentUrlTree: initialUrlTree,
430-
currentRawUrl: initialUrlTree,
431-
extractedUrl: this.urlHandlingStrategy.extract(initialUrlTree),
432-
urlAfterRedirects: this.urlHandlingStrategy.extract(initialUrlTree),
433-
rawUrl: initialUrlTree,
434-
extras: {},
435-
resolve: () => {},
436-
reject: () => {},
437-
promise: Promise.resolve(true),
438-
source: IMPERATIVE_NAVIGATION,
439-
restoredState: null,
440-
currentSnapshot: initialRouterState.snapshot,
419+
this.transitions?.next({
420+
...request,
421+
extractedUrl: this.urlHandlingStrategy.extract(request.rawUrl),
441422
targetSnapshot: null,
442-
currentRouterState: initialRouterState,
443423
targetRouterState: null,
444424
guards: {canActivateChecks: [], canDeactivateChecks: []},
445425
guardsResult: null,
426+
id,
446427
});
447-
return this.transitions.pipe(
448-
filter((t) => t.id !== 0),
428+
}
449429

450-
// Extract URL
451-
map(
452-
(t) =>
453-
({
454-
...t,
455-
extractedUrl: this.urlHandlingStrategy.extract(t.rawUrl),
456-
}) as NavigationTransition,
457-
),
430+
setupNavigations(router: InternalRouterInterface): Observable<NavigationTransition> {
431+
this.transitions = new BehaviorSubject<NavigationTransition | null>(null);
432+
return this.transitions.pipe(
433+
filter((t): t is NavigationTransition => t !== null),
458434

459435
// Using switchMap so we cancel executing navigations when a new one comes in
460436
switchMap((overallTransitionState) => {
@@ -522,7 +498,6 @@ export class NavigationTransitions {
522498
return of(t).pipe(
523499
// Fire NavigationStart event
524500
switchMap((t) => {
525-
const transition = this.transitions?.getValue();
526501
this.events.next(
527502
new NavigationStart(
528503
t.id,
@@ -531,7 +506,7 @@ export class NavigationTransitions {
531506
t.restoredState,
532507
),
533508
);
534-
if (transition !== this.transitions?.getValue()) {
509+
if (t.id !== this.navigationId) {
535510
return EMPTY;
536511
}
537512

packages/router/src/router.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,11 @@ export class Router {
176176
constructor() {
177177
this.resetConfig(this.config);
178178

179-
this.navigationTransitions
180-
.setupNavigations(this, this.currentUrlTree, this.routerState)
181-
.subscribe({
182-
error: (e) => {
183-
this.console.warn(ngDevMode ? `Unhandled Navigation Error: ${e}` : e);
184-
},
185-
});
179+
this.navigationTransitions.setupNavigations(this).subscribe({
180+
error: (e) => {
181+
this.console.warn(ngDevMode ? `Unhandled Navigation Error: ${e}` : e);
182+
},
183+
});
186184
this.subscribeToNavigationEvents();
187185
}
188186

0 commit comments

Comments
 (0)
0