Skip to content

Commit 84805a3

Browse files
committed
refactor: refactor #maxCompletedNavigationId$
1 parent 70fccc3 commit 84805a3

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

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

+26-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ type RouterNavigationHistory = Record<number, RouterNavigationSequence>;
2323
type RouterNavigationSequence = RouterRequestSequence | RouterNavigatedSequence;
2424
type RouterRequestSequence = readonly [NavigationStart];
2525

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+
2636
/**
2737
* Provide and initialize the `RouterHistoryStore`.
2838
*
@@ -62,10 +72,10 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
6272
);
6373

6474
/**
65-
* The navigation ID of the most recent completed navigation.
75+
* The navigation ID of the most recent router navigated sequence.
6676
*/
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)),
6979
(history) =>
7080
Number(
7181
// This callback is only triggered when at least one navigation has
@@ -79,11 +89,11 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
7989
/**
8090
* The most recent completed navigation.
8191
*/
82-
#latestCompletedNavigation$ = this.select(
83-
this.#maxCompletedNavigationId$,
92+
#latestRouterNavigatedSequence$ = this.select(
93+
this.#maxRouterNavigatedSequenceId$,
8494
this.#history$,
85-
(maxCompletedNavigationId, history) =>
86-
history[maxCompletedNavigationId] as RouterNavigatedSequence,
95+
(maxRouterNavigatedSequenceId, history) =>
96+
history[maxRouterNavigatedSequenceId] as RouterNavigatedSequence,
8797
{
8898
debounce: true,
8999
}
@@ -93,7 +103,7 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
93103
* The current URL.
94104
*/
95105
currentUrl$: Observable<string> = this.select(
96-
this.#latestCompletedNavigation$,
106+
this.#latestRouterNavigatedSequence$,
97107
([, end]) => end.urlAfterRedirects
98108
);
99109
/**
@@ -104,7 +114,7 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
104114
*/
105115
previousUrl$: Observable<string | undefined> = this.select(
106116
this.#history$,
107-
this.#maxCompletedNavigationId$,
117+
this.#maxRouterNavigatedSequenceId$,
108118
(history, maxCompletedNavigationId) => {
109119
if (maxCompletedNavigationId === 1) {
110120
return undefined;
@@ -193,6 +203,13 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
193203

194204
return navigation as RouterNavigatedSequence;
195205
}
206+
207+
#selectHasRouterNavigated(history: RouterNavigationHistory): boolean {
208+
const firstNavigationId = 1;
209+
const firstNavigation = history[firstNavigationId] ?? [];
210+
211+
return isRouterNavigatedSequence(firstNavigation);
212+
}
196213
}
197214

198215
/**

0 commit comments

Comments
 (0)