-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6c419f8
Added toEqualObservable jasmine matcher for effect testing
phillipzada 90901e3
Merge pull request #1 from ngrx/master
phillipzada dec974c
#378 Default selectId function for EntityAdapter
phillipzada 3219623
#405 Build fix
phillipzada 5249ac5
#405 selectId as optional
phillipzada 86caaee
Merge remote-tracking branch 'upstream/master'
phillipzada 4583010
#410 Creation of StoreRouterConnectingModule.forRoot with configurati…
phillipzada 4183e01
Fixed injection issues, added function type handling
phillipzada cbbe801
Readd Inject annonation
phillipzada 735f0f5
Adding provides to NgModule meta of StoreRouterConnectingModule
phillipzada 64a8b35
Merge branch 'master' into master
phillipzada 98c73b5
Merge branch 'master' of https://github.com/phillipzada/platform
phillipzada a08b6a9
Update NgModule providers for StoreRouterConnectingModule
phillipzada dae762f
Fix broken integration tests
phillipzada 3e5ff01
Merge branch 'master' into master
phillipzada 8061277
Remove yarn.lock
phillipzada d04b3f6
Readd yarn.lock
phillipzada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,33 @@ export function routerReducer<T = RouterStateSnapshot>( | |
} | ||
} | ||
|
||
export type StoreRouterConfig = { | ||
stateKey?: string; | ||
}; | ||
|
||
export const _ROUTER_CONFIG = new InjectionToken( | ||
'@ngrx/router-store Internal Configuration' | ||
); | ||
export const ROUTER_CONFIG = new InjectionToken( | ||
'@ngrx/router-store Configuration' | ||
); | ||
export const DEFAULT_ROUTER_FEATURENAME = 'routerReducer'; | ||
|
||
export function _createDefaultRouterConfig(config: any): StoreRouterConfig { | ||
let _config = {}; | ||
|
||
if (typeof config === 'function') { | ||
_config = config(); | ||
} | ||
|
||
return { | ||
stateKey: DEFAULT_ROUTER_FEATURENAME, | ||
..._config, | ||
}; | ||
} | ||
|
||
export type StoreRouterConfigFunction = () => StoreRouterConfig; | ||
|
||
/** | ||
* Connects RouterModule with StoreModule. | ||
* | ||
|
@@ -152,21 +184,54 @@ export function routerReducer<T = RouterStateSnapshot>( | |
@NgModule({ | ||
providers: [ | ||
{ provide: RouterStateSerializer, useClass: DefaultRouterStateSerializer }, | ||
{ | ||
provide: _ROUTER_CONFIG, | ||
useValue: { stateKey: DEFAULT_ROUTER_FEATURENAME }, | ||
}, | ||
{ | ||
provide: ROUTER_CONFIG, | ||
useFactory: _createDefaultRouterConfig, | ||
deps: [_ROUTER_CONFIG], | ||
}, | ||
], | ||
}) | ||
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 | StoreRouterConfigFunction | ||
): ModuleWithProviders; | ||
static forRoot( | ||
config: StoreRouterConfig | StoreRouterConfigFunction = {} | ||
): ModuleWithProviders { | ||
return { | ||
ngModule: StoreRouterConnectingModule, | ||
providers: [ | ||
{ provide: _ROUTER_CONFIG, useValue: config }, | ||
{ | ||
provide: ROUTER_CONFIG, | ||
useFactory: _createDefaultRouterConfig, | ||
deps: [_ROUTER_CONFIG], | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
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; | ||
|
||
constructor( | ||
private store: Store<any>, | ||
private router: Router, | ||
private serializer: RouterStateSerializer<RouterStateSnapshot> | ||
private serializer: RouterStateSerializer<RouterStateSnapshot>, | ||
@Inject(ROUTER_CONFIG) private config: StoreRouterConfig | ||
) { | ||
this.stateKey = this.config.stateKey as string; | ||
|
||
this.setUpBeforePreactivationHook(); | ||
this.setUpStoreStateListener(); | ||
this.setUpStateRollbackEvents(); | ||
|
@@ -187,28 +252,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); | ||
} | ||
} | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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