-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(router-store): add provideRouterStore function (#3532)
Closes #3528
- Loading branch information
1 parent
5639c1e
commit 511b7cf
Showing
9 changed files
with
423 additions
and
355 deletions.
There are no files selected for viewing
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
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
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
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,63 @@ | ||
import { ENVIRONMENT_INITIALIZER, inject, Provider } from '@angular/core'; | ||
import { | ||
_createRouterConfig, | ||
_ROUTER_CONFIG, | ||
ROUTER_CONFIG, | ||
RouterState, | ||
StoreRouterConfig, | ||
} from './router_store_config'; | ||
import { | ||
FullRouterStateSerializer, | ||
SerializedRouterStateSnapshot, | ||
} from './serializers/full_serializer'; | ||
import { MinimalRouterStateSerializer } from './serializers/minimal_serializer'; | ||
import { | ||
BaseRouterStoreState, | ||
RouterStateSerializer, | ||
} from './serializers/base'; | ||
import { StoreRouterConnectingService } from './store_router_connecting.service'; | ||
import { EnvironmentProviders } from '@ngrx/store'; | ||
|
||
/** | ||
* Connects the Angular Router to the Store. | ||
* | ||
* @usageNotes | ||
* | ||
* ```typescript | ||
* bootstrapApplication(AppComponent, { | ||
* providers: [ | ||
* provideRouterStore() | ||
* ] | ||
* }) | ||
* ``` | ||
*/ | ||
export function provideRouterStore< | ||
T extends BaseRouterStoreState = SerializedRouterStateSnapshot | ||
>(config: StoreRouterConfig<T> = {}): EnvironmentProviders { | ||
return { | ||
ɵproviders: [ | ||
{ provide: _ROUTER_CONFIG, useValue: config }, | ||
{ | ||
provide: ROUTER_CONFIG, | ||
useFactory: _createRouterConfig, | ||
deps: [_ROUTER_CONFIG], | ||
}, | ||
{ | ||
provide: RouterStateSerializer, | ||
useClass: config.serializer | ||
? config.serializer | ||
: config.routerState === RouterState.Full | ||
? FullRouterStateSerializer | ||
: MinimalRouterStateSerializer, | ||
}, | ||
{ | ||
provide: ENVIRONMENT_INITIALIZER, | ||
multi: true, | ||
useFactory() { | ||
return () => inject(StoreRouterConnectingService); | ||
}, | ||
}, | ||
StoreRouterConnectingService, | ||
], | ||
}; | ||
} |
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
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,67 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { Selector } from '@ngrx/store'; | ||
import { RouterReducerState } from './reducer'; | ||
import { | ||
BaseRouterStoreState, | ||
RouterStateSerializer, | ||
} from './serializers/base'; | ||
import { SerializedRouterStateSnapshot } from './serializers/full_serializer'; | ||
import { MinimalRouterStateSerializer } from './serializers/minimal_serializer'; | ||
|
||
export type StateKeyOrSelector< | ||
T extends BaseRouterStoreState = SerializedRouterStateSnapshot | ||
> = string | Selector<any, RouterReducerState<T>>; | ||
|
||
export enum NavigationActionTiming { | ||
PreActivation = 1, | ||
PostActivation = 2, | ||
} | ||
export const DEFAULT_ROUTER_FEATURENAME = 'router'; | ||
|
||
export const _ROUTER_CONFIG = new InjectionToken( | ||
'@ngrx/router-store Internal Configuration' | ||
); | ||
export const ROUTER_CONFIG = new InjectionToken( | ||
'@ngrx/router-store Configuration' | ||
); | ||
|
||
/** | ||
* Minimal = Serializes the router event with MinimalRouterStateSerializer | ||
* Full = Serializes the router event with FullRouterStateSerializer | ||
*/ | ||
export const enum RouterState { | ||
Full, | ||
Minimal, | ||
} | ||
|
||
export function _createRouterConfig( | ||
config: StoreRouterConfig | ||
): StoreRouterConfig { | ||
return { | ||
stateKey: DEFAULT_ROUTER_FEATURENAME, | ||
serializer: MinimalRouterStateSerializer, | ||
navigationActionTiming: NavigationActionTiming.PreActivation, | ||
...config, | ||
}; | ||
} | ||
|
||
export interface StoreRouterConfig< | ||
T extends BaseRouterStoreState = SerializedRouterStateSnapshot | ||
> { | ||
stateKey?: StateKeyOrSelector<T>; | ||
serializer?: new (...args: any[]) => RouterStateSerializer; | ||
/** | ||
* By default, ROUTER_NAVIGATION is dispatched before guards and resolvers run. | ||
* Therefore, the action could run too soon, for example | ||
* there may be a navigation cancel due to a guard saying the navigation is not allowed. | ||
* To run ROUTER_NAVIGATION after guards and resolvers, | ||
* set this property to NavigationActionTiming.PostActivation. | ||
*/ | ||
navigationActionTiming?: NavigationActionTiming; | ||
/** | ||
* Decides which router serializer should be used, if there is none provided, and the metadata on the dispatched @ngrx/router-store action payload. | ||
* Set to `Minimal` to use the `MinimalRouterStateSerializer` and to set a minimal router event with the navigation id and url as payload. | ||
* Set to `Full` to use the `FullRouterStateSerializer` and to set the angular router events as payload. | ||
*/ | ||
routerState?: RouterState; | ||
} |
Oops, something went wrong.