Skip to content

Commit 4b4f938

Browse files
committed
feat!: use local route for local router store
1 parent 1412405 commit 4b4f938

2 files changed

Lines changed: 27 additions & 27 deletions

File tree

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { inject, Injectable, Type } from '@angular/core';
22
import {
33
ActivatedRoute,
4+
createUrlTreeFromSnapshot,
45
Data,
56
Event as RouterEvent,
67
NavigationCancel,
@@ -9,6 +10,7 @@ import {
910
NavigationStart,
1011
Params,
1112
Router,
13+
RouterStateSnapshot,
1214
RoutesRecognized,
1315
} from '@angular/router';
1416
import { ComponentStore } from '@ngrx/component-store';
@@ -35,21 +37,12 @@ export class LocalRouterStore
3537
#routerState$: Observable<MinimalRouterStateSnapshot> = this.select(
3638
(state) => state.routerState
3739
);
38-
#rootRoute$: Observable<MinimalActivatedRouteSnapshot> = this.select(
40+
#localRoute: Observable<MinimalActivatedRouteSnapshot> = this.select(
3941
this.#routerState$,
4042
(routerState) => routerState.root
4143
);
4244

43-
currentRoute$: Observable<MinimalActivatedRouteSnapshot> = this.select(
44-
this.#rootRoute$,
45-
(route) => {
46-
while (route.firstChild) {
47-
route = route.firstChild;
48-
}
49-
50-
return route;
51-
}
52-
);
45+
currentRoute$: Observable<MinimalActivatedRouteSnapshot> = this.#localRoute;
5346
fragment$: Observable<string | null>;
5447
queryParams$: Observable<Params>;
5548
routeData$: Observable<Data>;
@@ -70,9 +63,7 @@ export class LocalRouterStore
7063
title: this.title$,
7164
} = this.#route);
7265
this.setState({
73-
routerState: this.#serializer.serialize(
74-
this.#router.routerState.snapshot
75-
),
66+
routerState: this.#serializeRouterState(this.#route),
7667
});
7768

7869
this.#updateRouterState(
@@ -83,7 +74,8 @@ export class LocalRouterStore
8374
NavigationCancel,
8475
NavigationError
8576
).pipe(
86-
map(() => this.#serializer.serialize(this.#router.routerState.snapshot))
77+
map(() => this.#route),
78+
map((route) => this.#serializeRouterState(route))
8779
)
8880
);
8981
}
@@ -114,4 +106,22 @@ export class LocalRouterStore
114106
filterRouterEvents(...acceptedEventTypes)
115107
) as Observable<InstanceType<TAcceptedRouterEvents[number]>>;
116108
}
109+
110+
#createRouterStateSnapshot(route: ActivatedRoute): RouterStateSnapshot {
111+
return {
112+
root: route.snapshot,
113+
url: this.#router.serializeUrl(
114+
createUrlTreeFromSnapshot(
115+
route.snapshot,
116+
[],
117+
route.snapshot.queryParams,
118+
route.snapshot.fragment
119+
)
120+
),
121+
};
122+
}
123+
124+
#serializeRouterState(route: ActivatedRoute): MinimalRouterStateSnapshot {
125+
return this.#serializer.serialize(this.#createRouterStateSnapshot(route));
126+
}
117127
}

packages/router-component-store/src/lib/local-router-store/nested-current-route.spec.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,7 @@ describe(`${LocalRouterStore.name} nested current route`, () => {
8383

8484
await expect(
8585
firstValueFrom(routerStore.currentRoute$)
86-
).resolves.toEqual(
87-
navigateTo === '/parent'
88-
? expectedRoutes.parent
89-
: navigateTo === '/parent/child'
90-
? expectedRoutes.child
91-
: expectedRoutes.grandchild
92-
);
86+
).resolves.toEqual(expectedRoutes.parent);
9387
const {
9488
children,
9589
data,
@@ -130,11 +124,7 @@ describe(`${LocalRouterStore.name} nested current route`, () => {
130124

131125
await expect(
132126
firstValueFrom(routerStore.currentRoute$)
133-
).resolves.toEqual(
134-
navigateTo === '/parent/child'
135-
? expectedRoutes.child
136-
: expectedRoutes.grandchild
137-
);
127+
).resolves.toEqual(expectedRoutes.child);
138128
const {
139129
children,
140130
data,

0 commit comments

Comments
 (0)