Skip to content

Commit

Permalink
fix(ResolvePolicy): Fix resolve policy config loading
Browse files Browse the repository at this point in the history
Closes #2945
  • Loading branch information
christopherthielen committed Aug 31, 2016
1 parent 5b6198c commit 4440811
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
10 changes: 5 additions & 5 deletions src/state/stateBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ function includesBuilder(state: State) {
* ]
*/
export function resolvablesBuilder(state: State): Resolvable[] {
interface Tuple { token: any, val: any, deps: any[], policy?: string }
interface Tuple { token: any, val: any, deps: any[], policy: ResolvePolicy }

/** convert a resolve: {} object to an array of tuples */
const obj2Tuples = (obj: Obj) =>
Object.keys(obj || {}).map(token => ({token, val: obj[token], deps: undefined}));
/** convert resolve: {} and resolvePolicy: {} objects to an array of tuples */
const objects2Tuples = (resolveObj: Obj, resolvePolicies: { [key: string]: ResolvePolicy }) =>
Object.keys(resolveObj || {}).map(token => ({token, val: resolveObj[token], deps: undefined, policy: resolvePolicies[token]}));

/** fetch DI annotations from a function or ng1-style array */
const annotate = (fn: Function) =>
Expand Down Expand Up @@ -180,7 +180,7 @@ export function resolvablesBuilder(state: State): Resolvable[] {
// If resolveBlock is already an array, use it as-is.
// Otherwise, assume it's an object and convert to an Array of tuples
let decl = state.resolve;
let items: any[] = isArray(decl) ? decl : obj2Tuples(decl);
let items: any[] = isArray(decl) ? decl : objects2Tuples(decl, state.resolvePolicy || {});
return items.map(item2Resolvable);
}

Expand Down
55 changes: 27 additions & 28 deletions test/core/resolveSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { omit, pick, forEach, copy } from "../../src/core";
import Spy = jasmine.Spy;
import {services} from "../../src/common/coreservices";
import {resolvablesBuilder} from "../../src/state/stateBuilder";
import {tree2Array} from "../testUtils.ts";
import {UIRouter} from "../../src/router";

///////////////////////////////////////////////

var states, statesTree, statesMap: { [key:string]: State } = {};
var vals, counts, expectCounts;
var asyncCount;
let router, states, statesMap: { [key:string]: State } = {};
let vals, counts, expectCounts;
let asyncCount;

function invokeLater(fn: Function, ctx: ResolveContext) {
return new Resolvable("", fn, services.$injector.annotate(fn)).get(ctx)
Expand All @@ -35,7 +37,14 @@ function getStates() {
},
I: { resolve: { _I: function(_I) { return "I"; } } }
},
J: { resolve: { _J: function() { counts['_J']++; return "J"; }, _J2: function(_J) { counts['_J2']++; return _J + "J2"; } },
J: {
resolve: {
_J: function() { counts['_J']++; return "J"; },
_J2: function(_J) { counts['_J2']++; return _J + "J2"; }
},
resolvePolicy: {
_J: { when: 'EAGER' }
},
K: { resolve: { _K: function(_J2) { counts['_K']++; return _J2 + "K"; }},
L: { resolve: { _L: function(_K) { counts['_L']++; return _K + "L"; }},
M: { resolve: { _M: function(_L) { counts['_M']++; return _L + "M"; }} }
Expand All @@ -57,35 +66,17 @@ function getStates() {
};
}


beforeEach(function () {
router = new UIRouter();
router.stateRegistry.stateQueue.autoFlush(router.stateService);

counts = { _J: 0, _J2: 0, _K: 0, _L: 0, _M: 0, _Q: 0 };
vals = { _Q: null };
expectCounts = copy(counts);
states = getStates();

var stateProps = ["resolve", "resolvePolicy"];
statesTree = loadStates({}, states, '');

function loadStates(parent, state, name) {
var thisState = pick.apply(null, [state].concat(stateProps));
var substates = omit.apply(null, [state].concat(stateProps));
var resolve = thisState.resolve || {};

thisState.resolvables = resolvablesBuilder(<any> { resolve });
thisState.template = thisState.template || "empty";
thisState.name = name;
thisState.parent = parent.name;
thisState.params = {};
thisState.data = { children: [] };

forEach(substates, function (value, key) {
thisState.data.children.push(loadStates(thisState, value, key));
});
thisState = new State(thisState);
statesMap[name] = thisState;
return thisState;
}
tree2Array(getStates(), false).forEach(state => router.stateRegistry.register(state));
statesMap = router.stateRegistry.get()
.reduce((acc, state) => ((acc[state.name] = state.$$state()), acc), {});
});

function makePath(names: string[]): PathNode[] {
Expand Down Expand Up @@ -126,6 +117,14 @@ describe('Resolvables system:', function () {
}).then(done);
});

it('should resolve only eager resolves when run with "eager" policy', done => {
let path = makePath([ "J", "K" ]);
let ctx = new ResolveContext(path);
ctx.resolvePath("EAGER").then(function () {
expect(getResolvedData(ctx)).toEqualData({ _J: "J" });
}).then(done);
});

it('should resolve lazy and eager resolves when run with "lazy" policy', done => {
let path = makePath([ "J", "N" ]);
let ctx = new ResolveContext(path);
Expand Down

0 comments on commit 4440811

Please sign in to comment.