Skip to content
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 17 commits into from
Oct 23, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed injection issues, added function type handling
phillipzada committed Oct 10, 2017
commit 4183e01f0cab5b944d83804d8b5525eacf7e2813
4 changes: 4 additions & 0 deletions modules/router-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,10 @@ export {
RouterCancelPayload,
RouterNavigationPayload,
StoreRouterConnectingModule,
StoreRouterConfig,
StoreRouterConfigFunction,
ROUTER_CONFIG,
DEFAULT_ROUTER_FEATURENAME,
} from './router_store_module';

export {
47 changes: 39 additions & 8 deletions modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
@@ -112,10 +112,30 @@ export function routerReducer<T = RouterStateSnapshot>(
}
}

export interface StoreRouterConfig {
export type StoreRouterConfig = {
stateKey?: string;
};

export const _ROUTER_CONFIG = new InjectionToken(
'@ngrx/router Internal Configuration'
);
export const ROUTER_CONFIG = new InjectionToken('@ngrx/router 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 const _ROUTER_CONFIG = new InjectionToken('@ngrx/router Configuration');

export type StoreRouterConfigFunction = () => StoreRouterConfig;

/**
* Connects RouterModule with StoreModule.
@@ -165,11 +185,22 @@ export const _ROUTER_CONFIG = new InjectionToken('@ngrx/router Configuration');
],
})
export class StoreRouterConnectingModule {
Copy link
Member

Choose a reason for hiding this comment

The 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 {
static forRoot(
config?: StoreRouterConfig | StoreRouterConfigFunction
): ModuleWithProviders;
static forRoot(
config: StoreRouterConfig | StoreRouterConfigFunction = {}
): ModuleWithProviders {
return {
ngModule: StoreRouterConnectingModule,
providers: [{ provide: _ROUTER_CONFIG, useValue: config }],
providers: [
{ provide: _ROUTER_CONFIG, useValue: config },
{
provide: ROUTER_CONFIG,
useFactory: _createDefaultRouterConfig,
deps: [_ROUTER_CONFIG],
},
],
};
}

@@ -180,15 +211,15 @@ export class StoreRouterConnectingModule {
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';
private stateKey: string;

constructor(
private store: Store<any>,
private router: Router,
private serializer: RouterStateSerializer<RouterStateSnapshot>,
@Inject(_ROUTER_CONFIG) private config: StoreRouterConfig
private config: StoreRouterConfig
) {
this.stateKey = (config && config.stateKey) || this.stateKey;
this.stateKey = this.config.stateKey as string;

this.setUpBeforePreactivationHook();
this.setUpStoreStateListener();