Skip to content

Commit

Permalink
fix(RouterStore): change the default serializer to work around cycles…
Browse files Browse the repository at this point in the history
… in RouterStateSnapshot
  • Loading branch information
vsavkin authored and brandonroberts committed Mar 30, 2018
1 parent a0f45ff commit 7917a27
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions modules/router-store/src/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
import { InjectionToken } from '@angular/core';
import { RouterStateSnapshot } from '@angular/router';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

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

export interface SerializedRouterStateSnapshot {
root: ActivatedRouteSnapshot;
url: string;
}

export class DefaultRouterStateSerializer
implements RouterStateSerializer<RouterStateSnapshot> {
serialize(routerState: RouterStateSnapshot) {
return routerState;
implements RouterStateSerializer<SerializedRouterStateSnapshot> {
serialize(routerState: RouterStateSnapshot): SerializedRouterStateSnapshot {
return {
root: this.serializeRoute(routerState.root),
url: routerState.url,
};
}

private serializeRoute(
route: ActivatedRouteSnapshot
): ActivatedRouteSnapshot {
const children = route.children.map(c => this.serializeRoute(c));
return {
params: route.params,
paramMap: route.paramMap,
data: route.data,
url: route.url,
outlet: route.outlet,
routeConfig: {
component: route.routeConfig ? route.routeConfig.component : undefined,
},
queryParams: route.queryParams,
queryParamMap: route.queryParamMap,
fragment: route.fragment,
component: (route.routeConfig
? route.routeConfig.component
: undefined) as any,
root: undefined as any,
parent: undefined as any,
firstChild: children[0],
pathFromRoot: undefined as any,
children,
};
}
}

0 comments on commit 7917a27

Please sign in to comment.