-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: encapsulate internal members (#1)
- Encapsulate members inherited from `ComponentStore`
- Loading branch information
Showing
5 changed files
with
251 additions
and
155 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
packages/router-component-store/src/lib/global-router-store/global-router-component-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Data, Params, Router } from '@angular/router'; | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
import { map, Observable } from 'rxjs'; | ||
|
||
import { | ||
MinimalActivatedRouteSnapshot, | ||
MinimalRouterStateSerializer, | ||
MinimalRouterStateSnapshot, | ||
} from '../@ngrx/router-store/minimal_serializer'; | ||
import { RouterComponentStore } from '../router-component-store'; | ||
|
||
interface GlobalRouterState { | ||
readonly routerState: MinimalRouterStateSnapshot; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class GlobalRouterComponentStore | ||
extends ComponentStore<GlobalRouterState> | ||
implements RouterComponentStore | ||
{ | ||
#routerState$: Observable<MinimalRouterStateSnapshot> = this.select( | ||
(state) => state.routerState | ||
); | ||
#rootRoute$: Observable<MinimalActivatedRouteSnapshot> = this.select( | ||
this.#routerState$, | ||
(routerState) => routerState.root | ||
); | ||
|
||
readonly currentRoute$: Observable<MinimalActivatedRouteSnapshot> = | ||
this.select(this.#rootRoute$, (route) => { | ||
while (route.firstChild) { | ||
route = route.firstChild; | ||
} | ||
|
||
return route; | ||
}); | ||
readonly fragment$: Observable<string | null> = this.select( | ||
this.#rootRoute$, | ||
(route) => route.fragment | ||
); | ||
readonly queryParams$: Observable<Params> = this.select( | ||
this.#rootRoute$, | ||
(route) => route.queryParams | ||
); | ||
readonly routeData$: Observable<Data> = this.select( | ||
this.currentRoute$, | ||
(route) => route.data | ||
); | ||
readonly routeParams$: Observable<Params> = this.select( | ||
this.currentRoute$, | ||
(route) => route.params | ||
); | ||
readonly url$: Observable<string> = this.select( | ||
this.#routerState$, | ||
(routerState) => routerState.url | ||
); | ||
|
||
constructor(router: Router, serializer: MinimalRouterStateSerializer) { | ||
super({ | ||
routerState: serializer.serialize(router.routerState.snapshot), | ||
}); | ||
|
||
this.#updateRouterState( | ||
router.events.pipe( | ||
map(() => serializer.serialize(router.routerState.snapshot)) | ||
) | ||
); | ||
} | ||
|
||
selectQueryParam<TValue>(param: string): Observable<TValue> { | ||
return this.select(this.queryParams$, (params) => params[param]); | ||
} | ||
|
||
selectRouteParam<TValue>(param: string): Observable<TValue> { | ||
return this.select(this.routeParams$, (params) => params[param]); | ||
} | ||
|
||
#updateRouterState = this.updater<MinimalRouterStateSnapshot>( | ||
(state, routerState): GlobalRouterState => ({ | ||
...state, | ||
routerState, | ||
}) | ||
); | ||
} |
99 changes: 28 additions & 71 deletions
99
packages/router-component-store/src/lib/global-router-store/global-router-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,44 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Data, Params, Router } from '@angular/router'; | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
import { map, Observable } from 'rxjs'; | ||
import { Data, Params } from '@angular/router'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { | ||
MinimalActivatedRouteSnapshot, | ||
MinimalRouterStateSerializer, | ||
MinimalRouterStateSnapshot, | ||
} from '../@ngrx/router-store/minimal_serializer'; | ||
import { MinimalActivatedRouteSnapshot } from '../@ngrx/router-store/minimal_serializer'; | ||
import { RouterComponentStore } from '../router-component-store'; | ||
|
||
interface GlobalRouterStoreState { | ||
readonly routerState: MinimalRouterStateSnapshot; | ||
} | ||
import { GlobalRouterComponentStore } from './global-router-component-store'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class GlobalRouterStore | ||
extends ComponentStore<GlobalRouterStoreState> | ||
implements RouterComponentStore | ||
{ | ||
#routerState$: Observable<MinimalRouterStateSnapshot> = this.select( | ||
(state) => state.routerState | ||
); | ||
#rootRoute$: Observable<MinimalActivatedRouteSnapshot> = this.select( | ||
this.#routerState$, | ||
(routerState) => routerState.root | ||
); | ||
|
||
readonly currentRoute$: Observable<MinimalActivatedRouteSnapshot> = | ||
this.select(this.#rootRoute$, (route) => { | ||
while (route.firstChild) { | ||
route = route.firstChild; | ||
} | ||
export class GlobalRouterStore implements RouterComponentStore { | ||
#store: GlobalRouterComponentStore; | ||
|
||
return route; | ||
}); | ||
readonly fragment$: Observable<string | null> = this.select( | ||
this.#rootRoute$, | ||
(route) => route.fragment | ||
); | ||
readonly queryParams$: Observable<Params> = this.select( | ||
this.#rootRoute$, | ||
(route) => route.queryParams | ||
); | ||
readonly routeData$: Observable<Data> = this.select( | ||
this.currentRoute$, | ||
(route) => route.data | ||
); | ||
readonly routeParams$: Observable<Params> = this.select( | ||
this.currentRoute$, | ||
(route) => route.params | ||
); | ||
readonly url$: Observable<string> = this.select( | ||
this.#routerState$, | ||
(routerState) => routerState.url | ||
); | ||
|
||
constructor(router: Router, serializer: MinimalRouterStateSerializer) { | ||
super({ | ||
routerState: serializer.serialize(router.routerState.snapshot), | ||
}); | ||
get currentRoute$(): Observable<MinimalActivatedRouteSnapshot> { | ||
return this.#store.currentRoute$; | ||
} | ||
get fragment$(): Observable<string | null> { | ||
return this.#store.fragment$; | ||
} | ||
get queryParams$(): Observable<Params> { | ||
return this.#store.queryParams$; | ||
} | ||
get routeData$(): Observable<Data> { | ||
return this.#store.routeData$; | ||
} | ||
get routeParams$(): Observable<Params> { | ||
return this.#store.routeParams$; | ||
} | ||
get url$(): Observable<string> { | ||
return this.#store.url$; | ||
} | ||
|
||
this.#updateRouterState( | ||
router.events.pipe( | ||
map(() => serializer.serialize(router.routerState.snapshot)) | ||
) | ||
); | ||
constructor(store: GlobalRouterComponentStore) { | ||
this.#store = store; | ||
} | ||
|
||
selectQueryParam<TValue>(param: string): Observable<TValue> { | ||
return this.select(this.queryParams$, (params) => params[param]); | ||
return this.#store.selectQueryParam(param); | ||
} | ||
|
||
selectRouteParam<TValue>(param: string): Observable<TValue> { | ||
return this.select(this.routeParams$, (params) => params[param]); | ||
return this.#store.selectRouteParam(param); | ||
} | ||
|
||
#updateRouterState = this.updater<MinimalRouterStateSnapshot>( | ||
(state, routerState): GlobalRouterStoreState => ({ | ||
...state, | ||
routerState, | ||
}) | ||
); | ||
} |
99 changes: 99 additions & 0 deletions
99
packages/router-component-store/src/lib/local-router-store/local-router-component-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Injectable, Injector } from '@angular/core'; | ||
import { ActivatedRoute, Data, Params, Router } from '@angular/router'; | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
import { map, Observable } from 'rxjs'; | ||
|
||
import { | ||
MinimalActivatedRouteSnapshot, | ||
MinimalRouterStateSerializer, | ||
MinimalRouterStateSnapshot, | ||
} from '../@ngrx/router-store/minimal_serializer'; | ||
import { RouterComponentStore } from '../router-component-store'; | ||
|
||
interface LocalRouterState { | ||
readonly routerState: MinimalRouterStateSnapshot; | ||
} | ||
|
||
export function createLocalRouterStore( | ||
injector: Injector | ||
): LocalRouterComponentStore { | ||
return Injector.create({ | ||
name: 'LocalRouterStoreInjector', | ||
parent: injector, | ||
providers: [ | ||
{ | ||
provide: LocalRouterComponentStore, | ||
useClass: LocalRouterComponentStore, | ||
}, | ||
], | ||
}).get(LocalRouterComponentStore); | ||
} | ||
|
||
@Injectable() | ||
export class LocalRouterComponentStore | ||
extends ComponentStore<LocalRouterState> | ||
implements RouterComponentStore | ||
{ | ||
#routerState$: Observable<MinimalRouterStateSnapshot> = this.select( | ||
(state) => state.routerState | ||
); | ||
#rootRoute$: Observable<MinimalActivatedRouteSnapshot> = this.select( | ||
this.#routerState$, | ||
(routerState) => routerState.root | ||
); | ||
|
||
currentRoute$: Observable<MinimalActivatedRouteSnapshot> = this.select( | ||
this.#rootRoute$, | ||
(route) => { | ||
while (route.firstChild) { | ||
route = route.firstChild; | ||
} | ||
|
||
return route; | ||
} | ||
); | ||
fragment$: Observable<string | null>; | ||
queryParams$: Observable<Params>; | ||
routeData$: Observable<Data>; | ||
routeParams$: Observable<Params>; | ||
url$: Observable<string> = this.select( | ||
this.#routerState$, | ||
(routerState) => routerState.url | ||
); | ||
|
||
constructor( | ||
route: ActivatedRoute, | ||
router: Router, | ||
serializer: MinimalRouterStateSerializer | ||
) { | ||
super({ | ||
routerState: serializer.serialize(router.routerState.snapshot), | ||
}); | ||
|
||
this.fragment$ = route.fragment; | ||
this.queryParams$ = route.queryParams; | ||
this.routeData$ = route.data; | ||
this.routeParams$ = route.params; | ||
|
||
this.#updateRouterState( | ||
router.events.pipe( | ||
map(() => serializer.serialize(router.routerState.snapshot)) | ||
) | ||
); | ||
} | ||
|
||
selectQueryParam<TValue>(param: string): Observable<TValue> { | ||
return this.select(this.queryParams$, (params) => params[param]); | ||
} | ||
|
||
selectRouteParam<TValue>(param: string): Observable<TValue> { | ||
return this.select(this.routeParams$, (params) => params[param]); | ||
} | ||
|
||
#updateRouterState = this.updater<MinimalRouterStateSnapshot>( | ||
(state, routerState): LocalRouterState => ({ | ||
...state, | ||
routerState, | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.