Skip to content

Commit 521cde1

Browse files
committed
fix: handle NavigationCancel and NavigationError
1 parent 9c694e4 commit 521cde1

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

packages/router-component-store/src/lib/router-history-store/router-history.store.ts

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import {
55
Injectable,
66
Provider,
77
} from '@angular/core';
8-
import { NavigationEnd, NavigationStart, Router } from '@angular/router';
8+
import {
9+
NavigationCancel,
10+
NavigationEnd,
11+
NavigationError,
12+
NavigationStart,
13+
Router,
14+
} from '@angular/router';
915
import { ComponentStore, provideComponentStore } from '@ngrx/component-store';
1016
import { filter, map, Observable, switchMap, take } from 'rxjs';
1117
import { filterRouterEvents } from '../filter-router-event.operator';
@@ -26,6 +32,16 @@ interface RouterHistoryState {
2632

2733
type RouterNavigatedSequence = readonly [NavigationStart, NavigationEnd];
2834
type RouterNavigatedHistory = Readonly<Record<number, RouterNavigatedSequence>>;
35+
type RouterSequence = readonly [
36+
NavigationStart,
37+
NavigationEnd | NavigationCancel | NavigationError
38+
];
39+
40+
function isRouterNavigatedSequence(
41+
sequence: RouterSequence
42+
): sequence is RouterNavigatedSequence {
43+
return sequence[1] instanceof NavigationEnd;
44+
}
2945

3046
/**
3147
* Provide and initialize the `RouterHistoryStore`.
@@ -55,34 +71,39 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
5571
(maxNavigatedId): maxNavigatedId is number => maxNavigatedId !== undefined
5672
)
5773
);
74+
75+
/**
76+
* All router events.
77+
*/
78+
#routerEvents = this.select(this.#router.events, (events) => events);
5879
/**
59-
* All `NavigationEnd` events.
80+
* All router events concluding a navigation.
6081
*/
61-
#navigationEnd$: Observable<NavigationEnd> = this.#router.events.pipe(
62-
filterRouterEvents(NavigationEnd)
82+
#navigationResult$: Observable<
83+
NavigationEnd | NavigationCancel | NavigationError
84+
> = this.#routerEvents.pipe(
85+
filterRouterEvents(NavigationEnd, NavigationCancel, NavigationError)
6386
);
6487
/**
65-
* All `NavigationStart` events.
88+
* All router sequences.
6689
*/
67-
#navigationStart$: Observable<NavigationStart> = this.#router.events.pipe(
68-
filterRouterEvents(NavigationStart)
90+
#routerSequence$: Observable<RouterSequence> = this.#routerEvents.pipe(
91+
filterRouterEvents(NavigationStart),
92+
switchMap((navigationStart) =>
93+
this.#navigationResult$.pipe(
94+
filter(
95+
(navigationResult) => navigationResult.id === navigationStart.id
96+
),
97+
take(1),
98+
map((navigationResult) => [navigationStart, navigationResult] as const)
99+
)
100+
)
69101
);
70102
/**
71103
* All router navigated sequences, that is `NavigationStart` followed by `NavigationEnd`.
72104
*/
73105
#routerNavigated$: Observable<RouterNavigatedSequence> =
74-
this.#navigationStart$.pipe(
75-
switchMap((navigationStart) =>
76-
this.#navigationEnd$.pipe(
77-
filter((navigationEnd) => navigationEnd.id === navigationStart.id),
78-
take(1),
79-
map(
80-
(navigationEnd) =>
81-
[navigationStart, navigationEnd] as RouterNavigatedSequence
82-
)
83-
)
84-
)
85-
);
106+
this.#routerSequence$.pipe(filter(isRouterNavigatedSequence));
86107

87108
/**
88109
* The most recent completed navigation.

0 commit comments

Comments
 (0)