Skip to content

Commit 88c624d

Browse files
fix(params): Clone all properties of a Node. Introduce applyRawParams()
1 parent f5fd578 commit 88c624d

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

src/common/common.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,10 @@ export function find(collection, callback) {
286286
return result;
287287
}
288288

289+
/** Given an object, returns a new object, where each property is transformed by the callback function */
290+
export let mapObj: <T,U>(collection: { [key: string]: T }, callback: Mapper<T,U>) => { [key: string]: U } = map;
289291
/** Given an array, returns a new array, where each element is transformed by the callback function */
290292
export function map<T, U>(collection: T[], callback: Mapper<T, U>): U[];
291-
/** Given an object, returns a new object, where each property is transformed by the callback function */
292293
export function map<T, U>(collection: { [key: string]: T }, callback: Mapper<T, U>): { [key: string]: U }
293294
/** Maps an array or object properties using a callback function */
294295
export function map(collection: any, callback: any): any {

src/path/node.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @module path */ /** for typedoc */
2-
import {extend, applyPairs, map, find, allTrueR, values} from "../common/common";
2+
import {extend, applyPairs, map, find, allTrueR, values, mapObj} from "../common/common";
33
import {prop, propEq} from "../common/hof";
44
import {State} from "../state/module";
55
import {RawParams} from "../params/interface";
@@ -9,22 +9,38 @@ import {ViewConfig} from "../view/interface";
99
import {Resolvables} from "../resolve/interface";
1010

1111
export class Node {
12+
public state: State;
1213
public paramSchema: Param[];
1314
public paramValues: { [key: string]: any };
1415
public resolves: Resolvables;
1516
public views: ViewConfig[];
1617
public resolveContext: ResolveContext;
1718
public resolveInjector: ResolveInjector;
1819

19-
// Possibly extract this logic into an intermediary object that maps states to nodes
20-
constructor(public state: State, params: RawParams = {}, resolvables: Resolvables = {}) {
21-
// Object.freeze(extend(this, { ... }))
22-
this.paramSchema = state.parameters({ inherit: false });
20+
constructor(state: Node);
21+
constructor(state: State);
22+
constructor(state) {
23+
if (state instanceof Node) {
24+
let node: Node = state;
25+
this.state = node.state;
26+
this.paramSchema = node.paramSchema.slice();
27+
this.paramValues = extend({}, node.paramValues);
28+
this.resolves = extend({}, node.resolves);
29+
this.views = node.views && node.views.slice();
30+
this.resolveContext = node.resolveContext;
31+
this.resolveInjector = node.resolveInjector;
32+
} else {
33+
this.state = state;
34+
this.paramSchema = state.parameters({ inherit: false });
35+
this.paramValues = {};
36+
this.resolves = mapObj(state.resolve, (fn: Function, name: string) => new Resolvable(name, fn));
37+
}
38+
}
2339

40+
applyRawParams(params: RawParams): Node {
2441
const getParamVal = (paramDef: Param) => [ paramDef.id, paramDef.value(params[paramDef.id]) ];
2542
this.paramValues = this.paramSchema.reduce((memo, pDef) => applyPairs(memo, getParamVal(pDef)), {});
26-
27-
this.resolves = extend(map(state.resolve, (fn: Function, name: string) => new Resolvable(name, fn)), resolvables);
43+
return this;
2844
}
2945

3046
parameter(name: string): Param {
@@ -36,8 +52,8 @@ export class Node {
3652
return this.state === node.state && keys.map(paramValsEq).reduce(allTrueR, true);
3753
}
3854

39-
static clone(node: Node, update: any = {}) {
40-
return new Node(node.state, (update.paramValues || node.paramValues), (update.resolves || node.resolves));
55+
static clone(node: Node) {
56+
return new Node(node);
4157
}
4258

4359
/**

src/path/pathFactory.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class PathFactory {
2828
/** Given a fromPath: Node[] and a TargetState, builds a toPath: Node[] */
2929
static buildToPath(fromPath: Node[], targetState: TargetState): Node[] {
3030
let toParams = targetState.params();
31-
let toPath: Node[] = targetState.$state().path.map(state => new Node(state, toParams));
31+
let toPath: Node[] = targetState.$state().path.map(state => new Node(state).applyRawParams(toParams));
3232

3333
if (targetState.options().inherit) toPath = PathFactory.inheritParams(fromPath, toPath, Object.keys(toParams));
3434
return toPath;
@@ -70,7 +70,7 @@ export class PathFactory {
7070
let fromParamVals = nodeParamVals(_fromPath, toNode.state) || {};
7171
// extend toParamVals with any fromParamVals, then override any of those those with incomingParamVals
7272
let ownParamVals: RawParams = extend(toParamVals, fromParamVals, incomingParamVals);
73-
return new Node(toNode.state, ownParamVals);
73+
return new Node(toNode.state).applyRawParams(ownParamVals);
7474
});
7575

7676
// The param keys specified by the incoming toParams
@@ -111,7 +111,9 @@ export class PathFactory {
111111

112112
/** Given a retained node, return a new node which uses the to node's param values */
113113
function applyToParams(retainedNode: Node, idx: number): Node {
114-
return Node.clone(retainedNode, { paramValues: toPath[idx].paramValues });
114+
let cloned = Node.clone(retainedNode);
115+
cloned.paramValues = toPath[idx].paramValues;
116+
return cloned;
115117
}
116118

117119
let from: Node[], retained: Node[], exiting: Node[], entering: Node[], to: Node[];

src/state/stateService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export class StateService {
284284

285285
let ref: TargetState = this.target(to, toParams, options);
286286
let latestTreeChanges: TreeChanges = treeChangesQueue.peekTail();
287-
const rootPath = () => PathFactory.bindTransNodesToPath([new Node(this.stateRegistry.root(), {})]);
287+
const rootPath = () => PathFactory.bindTransNodesToPath([new Node(this.stateRegistry.root())]);
288288
let currentPath: Node[] = latestTreeChanges ? latestTreeChanges.to : rootPath();
289289

290290
if (!ref.exists())

0 commit comments

Comments
 (0)