|
1 | | -import { computed, inject, Injectable, Signal, Type } from '@angular/core'; |
| 1 | +import { InjectionToken, Signal, Type, computed, inject } from '@angular/core'; |
2 | 2 | import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop'; |
3 | 3 | import { |
4 | 4 | ActivatedRoute, |
5 | | - createUrlTreeFromSnapshot, |
6 | | - Event as RouterEvent, |
7 | 5 | NavigationCancel, |
8 | 6 | NavigationEnd, |
9 | 7 | NavigationError, |
10 | 8 | NavigationStart, |
11 | 9 | Router, |
| 10 | + Event as RouterEvent, |
12 | 11 | RouterStateSnapshot, |
13 | 12 | RoutesRecognized, |
| 13 | + createUrlTreeFromSnapshot, |
14 | 14 | } from '@angular/router'; |
15 | | -import { patchState, signalStore, withState } from '@ngrx/signals'; |
16 | | -import { map, Observable } from 'rxjs'; |
17 | | -import { MinimalActivatedRouteSnapshot } from '../@ngrx/router-store/minimal-activated-route-state-snapshot'; |
| 15 | +import { |
| 16 | + patchState, |
| 17 | + signalStore, |
| 18 | + withComputed, |
| 19 | + withHooks, |
| 20 | + withMethods, |
| 21 | + withState, |
| 22 | +} from '@ngrx/signals'; |
| 23 | +import { Observable, map } from 'rxjs'; |
18 | 24 | import { MinimalRouterStateSnapshot } from '../@ngrx/router-store/minimal-router-state-snapshot'; |
19 | 25 | import { MinimalRouterStateSerializer } from '../@ngrx/router-store/minimal_serializer'; |
20 | 26 | import { filterRouterEvents } from '../filter-router-event.operator'; |
| 27 | +import { RouterSignalStore } from '../router-signal-store'; |
| 28 | +import { MinimalActivatedRouteSnapshot } from '@ngrx/router-store'; |
21 | 29 | import { InternalStrictQueryParams } from '../internal-strict-query-params'; |
22 | 30 | import { InternalStrictRouteData } from '../internal-strict-route-data'; |
23 | 31 | import { InternalStrictRouteParams } from '../internal-strict-route-params'; |
24 | | -import { RouterSignalStore } from '../router-signal-store'; |
25 | 32 |
|
26 | | -interface LocalRouterState { |
27 | | - readonly routerState: MinimalRouterStateSnapshot; |
| 33 | +interface LocalRouterSignalState { |
| 34 | + readonly _routerState: MinimalRouterStateSnapshot; |
28 | 35 | } |
29 | 36 |
|
30 | | -const LocalRouterSignalStoreBase = signalStore( |
31 | | - withState<LocalRouterState>({ |
32 | | - routerState: {} as MinimalRouterStateSnapshot, |
33 | | - }) |
34 | | -); |
35 | | - |
36 | | -@Injectable() |
37 | | -export class LocalRouterSignalStore |
38 | | - extends LocalRouterSignalStoreBase |
39 | | - implements RouterSignalStore |
40 | | -{ |
41 | | - #route = inject(ActivatedRoute); |
42 | | - #router = inject(Router); |
43 | | - #serializer = inject(MinimalRouterStateSerializer); |
44 | | - |
45 | | - #localRoute: Signal<MinimalActivatedRouteSnapshot> = computed( |
46 | | - () => this.routerState().root |
47 | | - ); |
48 | | - |
49 | | - currentRoute: Signal<MinimalActivatedRouteSnapshot> = this.#localRoute; |
50 | | - |
51 | | - fragment: Signal<string | null> = toSignal(this.#route.fragment, { |
52 | | - initialValue: null, |
53 | | - }); |
54 | | - queryParams: Signal<InternalStrictQueryParams> = toSignal( |
55 | | - this.#route.queryParams, |
56 | | - { initialValue: {} } |
57 | | - ); |
58 | | - routeData: Signal<InternalStrictRouteData> = toSignal(this.#route.data, { |
59 | | - initialValue: {}, |
60 | | - }); |
61 | | - routeParams: Signal<InternalStrictRouteParams> = toSignal( |
62 | | - this.#route.params, |
63 | | - { initialValue: {} } |
64 | | - ); |
65 | | - title: Signal<string | undefined> = computed(() => this.currentRoute().title); |
66 | | - |
67 | | - url: Signal<string> = computed(() => this.routerState().url); |
| 37 | +function createInitialLocalRouterSignalState(): LocalRouterSignalState { |
| 38 | + const route = inject(ActivatedRoute); |
| 39 | + const router = inject(Router); |
| 40 | + const serializer = inject(MinimalRouterStateSerializer); |
68 | 41 |
|
69 | | - constructor() { |
70 | | - super(); |
71 | | - |
72 | | - // Initialize state with current router snapshot |
73 | | - patchState(this, { |
74 | | - routerState: this.#serializeRouterState(this.#route), |
75 | | - }); |
| 42 | + return { |
| 43 | + _routerState: serializeRouterState(route, router, serializer), |
| 44 | + }; |
| 45 | +} |
76 | 46 |
|
77 | | - // Subscribe to router events and update state |
78 | | - this.selectRouterEvents( |
79 | | - NavigationStart, |
80 | | - RoutesRecognized, |
81 | | - NavigationEnd, |
82 | | - NavigationCancel, |
83 | | - NavigationError |
84 | | - ) |
85 | | - .pipe( |
86 | | - map(() => this.#route), |
87 | | - map((route) => this.#serializeRouterState(route)), |
88 | | - takeUntilDestroyed() |
| 47 | +function createRouterStateSnapshot( |
| 48 | + route: ActivatedRoute, |
| 49 | + router: Router |
| 50 | +): RouterStateSnapshot { |
| 51 | + return { |
| 52 | + root: route.snapshot, |
| 53 | + url: router.serializeUrl( |
| 54 | + createUrlTreeFromSnapshot( |
| 55 | + route.snapshot, |
| 56 | + [], |
| 57 | + route.snapshot.queryParams, |
| 58 | + route.snapshot.fragment |
89 | 59 | ) |
90 | | - .subscribe((routerState) => { |
91 | | - patchState(this, { routerState }); |
92 | | - }); |
93 | | - } |
94 | | - |
95 | | - selectQueryParam( |
96 | | - param: string |
97 | | - ): Signal<string | readonly string[] | undefined> { |
98 | | - return computed(() => this.queryParams()[param]); |
99 | | - } |
| 60 | + ), |
| 61 | + }; |
| 62 | +} |
100 | 63 |
|
101 | | - selectRouteDataParam(key: string): Signal<unknown> { |
102 | | - return computed(() => this.routeData()[key]); |
103 | | - } |
| 64 | +function serializeRouterState( |
| 65 | + route: ActivatedRoute, |
| 66 | + router: Router, |
| 67 | + serializer: MinimalRouterStateSerializer |
| 68 | +): MinimalRouterStateSnapshot { |
| 69 | + return serializer.serialize(createRouterStateSnapshot(route, router)); |
| 70 | +} |
104 | 71 |
|
105 | | - selectRouteParam(param: string): Signal<string | undefined> { |
106 | | - return computed(() => this.routeParams()[param]); |
107 | | - } |
| 72 | +const initialLocalRouterSignalStateToken = |
| 73 | + new InjectionToken<LocalRouterSignalState>( |
| 74 | + 'initialLocalRouterSignalStateToken', |
| 75 | + { |
| 76 | + factory: createInitialLocalRouterSignalState, |
| 77 | + } |
| 78 | + ); |
108 | 79 |
|
109 | | - selectRouterEvents<TAcceptedRouterEvents extends Type<RouterEvent>[]>( |
110 | | - ...acceptedEventTypes: [...TAcceptedRouterEvents] |
111 | | - ): Observable<InstanceType<TAcceptedRouterEvents[number]>> { |
112 | | - return this.#router.events.pipe( |
113 | | - filterRouterEvents(...acceptedEventTypes) |
114 | | - ) as Observable<InstanceType<TAcceptedRouterEvents[number]>>; |
115 | | - } |
| 80 | +export const LocalRouterSignalStore: Type<RouterSignalStore> = signalStore( |
| 81 | + withState<LocalRouterSignalState>(() => |
| 82 | + inject(initialLocalRouterSignalStateToken) |
| 83 | + ), |
| 84 | + withComputed(({ _routerState }) => { |
| 85 | + const route = inject(ActivatedRoute); |
116 | 86 |
|
117 | | - #createRouterStateSnapshot(route: ActivatedRoute): RouterStateSnapshot { |
118 | 87 | return { |
119 | | - root: route.snapshot, |
120 | | - url: this.#router.serializeUrl( |
121 | | - createUrlTreeFromSnapshot( |
122 | | - route.snapshot, |
123 | | - [], |
124 | | - route.snapshot.queryParams, |
125 | | - route.snapshot.fragment |
126 | | - ) |
| 88 | + currentRoute: computed( |
| 89 | + (): MinimalActivatedRouteSnapshot => _routerState().root |
127 | 90 | ), |
| 91 | + fragment: toSignal(route.fragment, { |
| 92 | + requireSync: true, |
| 93 | + }) satisfies Signal<string | null>, |
| 94 | + queryParams: toSignal(route.queryParams, { |
| 95 | + requireSync: true, |
| 96 | + }) satisfies Signal<InternalStrictQueryParams>, |
| 97 | + routeData: toSignal(route.data, { |
| 98 | + requireSync: true, |
| 99 | + }) satisfies Signal<InternalStrictRouteData>, |
| 100 | + routeParams: toSignal(route.params, { |
| 101 | + requireSync: true, |
| 102 | + }) satisfies Signal<InternalStrictRouteParams>, |
| 103 | + title: toSignal(route.title, { requireSync: true }) satisfies Signal< |
| 104 | + string | undefined |
| 105 | + >, |
| 106 | + url: computed((): string => _routerState().url), |
128 | 107 | }; |
129 | | - } |
130 | | - |
131 | | - #serializeRouterState(route: ActivatedRoute): MinimalRouterStateSnapshot { |
132 | | - return this.#serializer.serialize(this.#createRouterStateSnapshot(route)); |
133 | | - } |
134 | | -} |
| 108 | + }), |
| 109 | + withMethods( |
| 110 | + ( |
| 111 | + _store, |
| 112 | + router = inject(Router), |
| 113 | + serializer = inject(MinimalRouterStateSerializer) |
| 114 | + ) => ({ |
| 115 | + _serializeRouterState(route: ActivatedRoute): MinimalRouterStateSnapshot { |
| 116 | + return serializeRouterState(route, router, serializer); |
| 117 | + }, |
| 118 | + }) |
| 119 | + ), |
| 120 | + withMethods((store, router = inject(Router)) => ({ |
| 121 | + selectQueryParam( |
| 122 | + param: string |
| 123 | + ): Signal<string | readonly string[] | undefined> { |
| 124 | + return computed(() => store.queryParams()[param]); |
| 125 | + }, |
| 126 | + selectRouteDataParam(key: string): Signal<unknown> { |
| 127 | + return computed(() => store.routeData()[key]); |
| 128 | + }, |
| 129 | + selectRouteParam(param: string): Signal<string | undefined> { |
| 130 | + return computed(() => store.routeParams()[param]); |
| 131 | + }, |
| 132 | + selectRouterEvents<TAcceptedRouterEvents extends Type<RouterEvent>[]>( |
| 133 | + ...acceptedEventTypes: [...TAcceptedRouterEvents] |
| 134 | + ): Observable<InstanceType<TAcceptedRouterEvents[number]>> { |
| 135 | + return router.events.pipe( |
| 136 | + filterRouterEvents(...acceptedEventTypes) |
| 137 | + ) as Observable<InstanceType<TAcceptedRouterEvents[number]>>; |
| 138 | + }, |
| 139 | + })), |
| 140 | + withHooks({ |
| 141 | + onInit(store, route = inject(ActivatedRoute)): void { |
| 142 | + store |
| 143 | + .selectRouterEvents( |
| 144 | + NavigationStart, |
| 145 | + RoutesRecognized, |
| 146 | + NavigationEnd, |
| 147 | + NavigationCancel, |
| 148 | + NavigationError |
| 149 | + ) |
| 150 | + .pipe( |
| 151 | + map(() => route), |
| 152 | + map((route) => store._serializeRouterState(route)), |
| 153 | + takeUntilDestroyed() |
| 154 | + ) |
| 155 | + .subscribe((routerState) => |
| 156 | + patchState(store, { _routerState: routerState }) |
| 157 | + ); |
| 158 | + }, |
| 159 | + }) |
| 160 | +); |
0 commit comments