@@ -23,6 +23,16 @@ type RouterNavigationHistory = Record<number, RouterNavigationSequence>;
23
23
type RouterNavigationSequence = RouterRequestSequence | RouterNavigatedSequence ;
24
24
type RouterRequestSequence = readonly [ NavigationStart ] ;
25
25
26
+ function isRouterNavigatedSequence (
27
+ sequence : RouterNavigationSequence
28
+ ) : sequence is RouterNavigatedSequence {
29
+ return (
30
+ sequence . length === 2 &&
31
+ sequence [ 0 ] instanceof NavigationStart &&
32
+ sequence [ 1 ] instanceof NavigationEnd
33
+ ) ;
34
+ }
35
+
26
36
/**
27
37
* Provide and initialize the `RouterHistoryStore`.
28
38
*
@@ -62,10 +72,10 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
62
72
) ;
63
73
64
74
/**
65
- * The navigation ID of the most recent completed navigation .
75
+ * The navigation ID of the most recent router navigated sequence .
66
76
*/
67
- #maxCompletedNavigationId $ = this . select (
68
- this . #history$. pipe ( filter ( ( history ) => ( history [ 1 ] ?? [ ] ) . length > 1 ) ) ,
77
+ #maxRouterNavigatedSequenceId $ = this . select (
78
+ this . #history$. pipe ( filter ( this . #selectHasRouterNavigated ) ) ,
69
79
( history ) =>
70
80
Number (
71
81
// This callback is only triggered when at least one navigation has
@@ -79,11 +89,11 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
79
89
/**
80
90
* The most recent completed navigation.
81
91
*/
82
- #latestCompletedNavigation $ = this . select (
83
- this . #maxCompletedNavigationId $,
92
+ #latestRouterNavigatedSequence $ = this . select (
93
+ this . #maxRouterNavigatedSequenceId $,
84
94
this . #history$,
85
- ( maxCompletedNavigationId , history ) =>
86
- history [ maxCompletedNavigationId ] as RouterNavigatedSequence ,
95
+ ( maxRouterNavigatedSequenceId , history ) =>
96
+ history [ maxRouterNavigatedSequenceId ] as RouterNavigatedSequence ,
87
97
{
88
98
debounce : true ,
89
99
}
@@ -93,7 +103,7 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
93
103
* The current URL.
94
104
*/
95
105
currentUrl$ : Observable < string > = this . select (
96
- this . #latestCompletedNavigation $,
106
+ this . #latestRouterNavigatedSequence $,
97
107
( [ , end ] ) => end . urlAfterRedirects
98
108
) ;
99
109
/**
@@ -104,7 +114,7 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
104
114
*/
105
115
previousUrl$ : Observable < string | undefined > = this . select (
106
116
this . #history$,
107
- this . #maxCompletedNavigationId $,
117
+ this . #maxRouterNavigatedSequenceId $,
108
118
( history , maxCompletedNavigationId ) => {
109
119
if ( maxCompletedNavigationId === 1 ) {
110
120
return undefined ;
@@ -193,6 +203,13 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
193
203
194
204
return navigation as RouterNavigatedSequence ;
195
205
}
206
+
207
+ #selectHasRouterNavigated( history : RouterNavigationHistory ) : boolean {
208
+ const firstNavigationId = 1 ;
209
+ const firstNavigation = history [ firstNavigationId ] ?? [ ] ;
210
+
211
+ return isRouterNavigatedSequence ( firstNavigation ) ;
212
+ }
196
213
}
197
214
198
215
/**
0 commit comments