Skip to content

Commit

Permalink
feat(router-store): Add support for serializers with injected values
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

StoreRouterConfigFunction is removed. It is no longer possible to pass a function returning a StoreRouterConfig to StoreRouterConnectingModule.forRoot

If you still need this, pass a provider like this:
{
      provide: ROUTER_CONFIG,
      useFactory: _createRouterConfig // you function
}
  • Loading branch information
dummdidumm authored and brandonroberts committed Aug 24, 2018
1 parent 293f960 commit 959cfac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
6 changes: 5 additions & 1 deletion modules/router-store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Provider } from '@angular/core';
import { Component, Provider, Injectable } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { NavigationEnd, Router, RouterStateSnapshot } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
Expand Down Expand Up @@ -416,7 +416,11 @@ describe('integration spec', () => {
: null;
};

@Injectable()
class CustomSerializer implements RouterStateSerializer<SerializedState> {
constructor(store: Store<any>) {
// Requiring store to test Serializer with injected arguments works.
}
serialize(routerState: RouterStateSnapshot): SerializedState {
const url = `${routerState.url}-custom`;
const params = { test: 1 };
Expand Down
1 change: 0 additions & 1 deletion modules/router-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export {
RouterNavigatedPayload,
StoreRouterConnectingModule,
StoreRouterConfig,
StoreRouterConfigFunction,
NavigationActionTiming,
ROUTER_CONFIG,
DEFAULT_ROUTER_FEATURENAME,
Expand Down
49 changes: 15 additions & 34 deletions modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export function routerReducer<

export interface StoreRouterConfig {
stateKey?: string;
serializer?: new () => RouterStateSerializer;
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
Expand All @@ -200,34 +200,16 @@ export const ROUTER_CONFIG = new InjectionToken(
export const DEFAULT_ROUTER_FEATURENAME = 'router';

export function _createRouterConfig(
config: StoreRouterConfig | StoreRouterConfigFunction
config: StoreRouterConfig
): StoreRouterConfig {
let _config: StoreRouterConfig;

if (typeof config === 'function') {
_config = config();
} else {
_config = config || {};
}

return {
stateKey: DEFAULT_ROUTER_FEATURENAME,
serializer: DefaultRouterStateSerializer,
navigationActionTiming: NavigationActionTiming.PreActivation,
..._config,
...config,
};
}

export function _createSerializer(
config: StoreRouterConfig
): RouterStateSerializer {
// This function gets handed a complete config-object from _createRouterConfig,
// so we know the serializer property exists
return new config.serializer!();
}

export type StoreRouterConfigFunction = () => StoreRouterConfig;

enum RouterTrigger {
NONE = 1,
ROUTER = 2,
Expand Down Expand Up @@ -280,10 +262,7 @@ enum RouterTrigger {
providers: [
{
provide: _ROUTER_CONFIG,
useValue: {
stateKey: DEFAULT_ROUTER_FEATURENAME,
serializer: DefaultRouterStateSerializer,
},
useValue: {},
},
{
provide: ROUTER_CONFIG,
Expand All @@ -292,21 +271,23 @@ enum RouterTrigger {
},
{
provide: RouterStateSerializer,
deps: [ROUTER_CONFIG],
useFactory: _createSerializer,
useClass: DefaultRouterStateSerializer,
},
],
})
export class StoreRouterConnectingModule {
static forRoot(
config?: StoreRouterConfig | StoreRouterConfigFunction
): ModuleWithProviders;
static forRoot(
config: StoreRouterConfig | StoreRouterConfigFunction = {}
): ModuleWithProviders {
static forRoot(config: StoreRouterConfig = {}): ModuleWithProviders {
return {
ngModule: StoreRouterConnectingModule,
providers: [{ provide: _ROUTER_CONFIG, useValue: config }],
providers: [
{ provide: _ROUTER_CONFIG, useValue: config },
{
provide: RouterStateSerializer,
useClass: config.serializer
? config.serializer
: DefaultRouterStateSerializer,
},
],
};
}

Expand Down

0 comments on commit 959cfac

Please sign in to comment.