Skip to content

Commit

Permalink
fix(resolve): Don't re-resolve data when redirected to same state, bu…
Browse files Browse the repository at this point in the history
…t only dynamic params changed.

Closes #3033
  • Loading branch information
christopherthielen committed Sep 22, 2016
1 parent 3471c42 commit 98cd2d2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/path/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,23 @@ export class PathNode {
*
* The new path starts from root and contains any nodes that match the nodes in the second path.
* Nodes are compared using their state property and parameter values.
*
* @param pathA the first path
* @param pathB the second path
* @param ignoreDynamicParams don't compare dynamic parameter values
*/
static matching(pathA: PathNode[], pathB: PathNode[]): PathNode[] {
static matching(pathA: PathNode[], pathB: PathNode[], ignoreDynamicParams = true): PathNode[] {
let matching: PathNode[] = [];

for (let i = 0; i < pathA.length && i < pathB.length; i++) {
let a = pathA[i], b = pathB[i];

if (a.state !== b.state) break;
if (!Param.equals(a.paramSchema, a.paramValues, b.paramValues)) break;

let changedParams = Param.changed(a.paramSchema, a.paramValues, b.paramValues)
.filter(param => !(ignoreDynamicParams && param.dynamic));
if (changedParams.length) break;

matching.push(a);
}

Expand Down
35 changes: 34 additions & 1 deletion test/core/resolveSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import "../matchers.ts"

import { ResolveContext, State, PathNode, Resolvable } from "../../src/core";
import { omit, pick, forEach, copy } from "../../src/core";
import { copy } from "../../src/core";

import Spy = jasmine.Spy;
import {services} from "../../src/common/coreservices";
Expand Down Expand Up @@ -365,6 +365,39 @@ describe('Resolvables system:', function () {
done();
});
});

// Test for #2796
it("should not re-resolve data, when redirecting to self with dynamic parameter update", (done) => {
let $registry = router.stateRegistry;
let $state = router.stateService;
let $transitions = router.transitionService;
let resolveCount = 0;

$registry.register({
name: 'dynamic',
url: '/dynamic/{param}',
params: {
param: { dynamic: true }
},
resolve: {
data: () => {
new Promise(resolve => resolve('Expensive data ' + resolveCount++))
}
}
});

$transitions.onEnter({entering: "dynamic"}, trans => {
if (trans.params()['param'] === 'initial')
return $state.target("dynamic", { param: 'redirected' });
});

$state.go("dynamic", { param: 'initial'}).then(() => {
expect($state.current.name).toBe("dynamic");
expect($state.params['param']).toBe('redirected');
expect(resolveCount).toBe(1);
done();
});
});
});


0 comments on commit 98cd2d2

Please sign in to comment.