Skip to content

Commit

Permalink
ngrx#1262 ngrx#1010 Add custom serializer to config object
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Aug 21, 2018
1 parent f3903ff commit 77763d3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
29 changes: 23 additions & 6 deletions modules/router-store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,10 @@ describe('integration spec', () => {
});
});

it('should support a custom RouterStateSnapshot serializer ', (done: any) => {
function shouldSupportCustomSerializer(
serializerThroughConfig: boolean,
done: Function
) {
interface SerializedState {
url: string;
params: any;
Expand Down Expand Up @@ -422,11 +425,17 @@ describe('integration spec', () => {
}
}

const providers = [
{ provide: RouterStateSerializer, useClass: CustomSerializer },
];

createTestModule({ reducers: { routerReducer, reducer }, providers });
if (serializerThroughConfig) {
createTestModule({
reducers: { routerReducer, reducer },
config: { serializer: CustomSerializer },
});
} else {
const providers = [
{ provide: RouterStateSerializer, useClass: CustomSerializer },
];
createTestModule({ reducers: { routerReducer, reducer }, providers });
}

const router = TestBed.get(Router);
const log = logOfRouterAndActionsAndStore();
Expand Down Expand Up @@ -464,6 +473,14 @@ describe('integration spec', () => {
log.splice(0);
done();
});
}

it('should support a custom RouterStateSnapshot serializer via provider', (done: any) => {
shouldSupportCustomSerializer(false, done);
});

it('should support a custom RouterStateSnapshot serializer via config', (done: any) => {
shouldSupportCustomSerializer(true, done);
});

it('should support event during an async canActivate guard', (done: any) => {
Expand Down
43 changes: 23 additions & 20 deletions modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
DefaultRouterStateSerializer,
RouterStateSerializer,
SerializedRouterStateSnapshot,
RouterState,
} from './serializer';

/**
Expand Down Expand Up @@ -146,14 +147,6 @@ export type RouterAction<
| RouterErrorAction<T, V>
| RouterNavigatedAction;

/**
* Simple router state.
* All custom router states should have at least this property.
*/
export type RouterState = {
url: string;
};

export type RouterReducerState<
T extends RouterState = SerializedRouterStateSnapshot
> = {
Expand Down Expand Up @@ -182,6 +175,7 @@ export function routerReducer<

export interface StoreRouterConfig {
stateKey?: string;
serializer?: new () => RouterStateSerializer;
}

export const _ROUTER_CONFIG = new InjectionToken(
Expand All @@ -192,7 +186,7 @@ export const ROUTER_CONFIG = new InjectionToken(
);
export const DEFAULT_ROUTER_FEATURENAME = 'routerReducer';

export function _createDefaultRouterConfig(
export function _createRouterConfig(
config: StoreRouterConfig | StoreRouterConfigFunction
): StoreRouterConfig {
let _config: StoreRouterConfig;
Expand All @@ -205,10 +199,19 @@ export function _createDefaultRouterConfig(

return {
stateKey: DEFAULT_ROUTER_FEATURENAME,
serializer: DefaultRouterStateSerializer,
..._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 {
Expand Down Expand Up @@ -261,16 +264,23 @@ enum RouterTrigger {
*/
@NgModule({
providers: [
{ provide: RouterStateSerializer, useClass: DefaultRouterStateSerializer },
{
provide: _ROUTER_CONFIG,
useValue: { stateKey: DEFAULT_ROUTER_FEATURENAME },
useValue: {
stateKey: DEFAULT_ROUTER_FEATURENAME,
serializer: DefaultRouterStateSerializer,
},
},
{
provide: ROUTER_CONFIG,
useFactory: _createDefaultRouterConfig,
useFactory: _createRouterConfig,
deps: [_ROUTER_CONFIG],
},
{
provide: RouterStateSerializer,
deps: [ROUTER_CONFIG],
useFactory: _createSerializer,
},
],
})
export class StoreRouterConnectingModule {
Expand All @@ -282,14 +292,7 @@ export class StoreRouterConnectingModule {
): ModuleWithProviders {
return {
ngModule: StoreRouterConnectingModule,
providers: [
{ provide: _ROUTER_CONFIG, useValue: config },
{
provide: ROUTER_CONFIG,
useFactory: _createDefaultRouterConfig,
deps: [_ROUTER_CONFIG],
},
],
providers: [{ provide: _ROUTER_CONFIG, useValue: config }],
};
}

Expand Down
12 changes: 10 additions & 2 deletions modules/router-store/src/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

export abstract class RouterStateSerializer<T> {
/**
* Simple router state.
* All custom router states / state serializers should have at least this property.
*/
export interface RouterState {
url: string;
}

export abstract class RouterStateSerializer<T extends RouterState = RouterState> {
abstract serialize(routerState: RouterStateSnapshot): T;
}

export interface SerializedRouterStateSnapshot {
export interface SerializedRouterStateSnapshot extends RouterState {
root: ActivatedRouteSnapshot;
url: string;
}
Expand Down

0 comments on commit 77763d3

Please sign in to comment.