-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replacing 'routerReducer' with a configurable option #417
Changes from 7 commits
6c419f8
90901e3
dec974c
3219623
5249ac5
86caaee
4583010
4183e01
cbbe801
735f0f5
64a8b35
98c73b5
a08b6a9
dae762f
3e5ff01
8061277
d04b3f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { | ||
NgModule, | ||
ModuleWithProviders, | ||
InjectionToken, | ||
Inject, | ||
} from '@angular/core'; | ||
import { | ||
NavigationCancel, | ||
NavigationError, | ||
|
@@ -107,6 +112,11 @@ export function routerReducer<T = RouterStateSnapshot>( | |
} | ||
} | ||
|
||
export interface StoreRouterConfig { | ||
stateKey?: string; | ||
} | ||
export const _ROUTER_CONFIG = new InjectionToken('@ngrx/router Configuration'); | ||
|
||
/** | ||
* Connects RouterModule with StoreModule. | ||
* | ||
|
@@ -155,18 +165,31 @@ export function routerReducer<T = RouterStateSnapshot>( | |
], | ||
}) | ||
export class StoreRouterConnectingModule { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be a breaking change for existing users unless you add default providers to to NgModule metadata |
||
static forRoot(config?: StoreRouterConfig): ModuleWithProviders; | ||
static forRoot(config: StoreRouterConfig = {}): ModuleWithProviders { | ||
return { | ||
ngModule: StoreRouterConnectingModule, | ||
providers: [{ provide: _ROUTER_CONFIG, useValue: config }], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work in AoT because the actual value for |
||
}; | ||
} | ||
|
||
private routerState: RouterStateSnapshot; | ||
private storeState: any; | ||
private lastRoutesRecognized: RoutesRecognized; | ||
|
||
private dispatchTriggeredByRouter: boolean = false; // used only in dev mode in combination with routerReducer | ||
private navigationTriggeredByDispatch: boolean = false; // used only in dev mode in combination with routerReducer | ||
|
||
private stateKey: string = 'routerReducer'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initialize this value in the factory function used to register the token |
||
|
||
constructor( | ||
private store: Store<any>, | ||
private router: Router, | ||
private serializer: RouterStateSerializer<RouterStateSnapshot> | ||
private serializer: RouterStateSerializer<RouterStateSnapshot>, | ||
@Inject(_ROUTER_CONFIG) private config: StoreRouterConfig | ||
) { | ||
this.stateKey = (config && config.stateKey) || this.stateKey; | ||
|
||
this.setUpBeforePreactivationHook(); | ||
this.setUpStoreStateListener(); | ||
this.setUpStateRollbackEvents(); | ||
|
@@ -187,28 +210,28 @@ export class StoreRouterConnectingModule { | |
this.store.subscribe(s => { | ||
this.storeState = s; | ||
}); | ||
this.store.select('routerReducer').subscribe(() => { | ||
this.store.select(this.stateKey).subscribe(() => { | ||
this.navigateIfNeeded(); | ||
}); | ||
} | ||
|
||
private shouldDispatchRouterNavigation(): boolean { | ||
if (!this.storeState['routerReducer']) return true; | ||
if (!this.storeState[this.stateKey]) return true; | ||
return !this.navigationTriggeredByDispatch; | ||
} | ||
|
||
private navigateIfNeeded(): void { | ||
if ( | ||
!this.storeState['routerReducer'] || | ||
!this.storeState['routerReducer'].state | ||
!this.storeState[this.stateKey] || | ||
!this.storeState[this.stateKey].state | ||
) { | ||
return; | ||
} | ||
if (this.dispatchTriggeredByRouter) return; | ||
|
||
if (this.router.url !== this.storeState['routerReducer'].state.url) { | ||
if (this.router.url !== this.storeState[this.stateKey].state.url) { | ||
this.navigationTriggeredByDispatch = true; | ||
this.router.navigateByUrl(this.storeState['routerReducer'].state.url); | ||
this.router.navigateByUrl(this.storeState[this.stateKey].state.url); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be added to the imports below from
../src/index