Skip to content

Commit 4440811

Browse files
fix(ResolvePolicy): Fix resolve policy config loading
Closes #2945
1 parent 5b6198c commit 4440811

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

src/state/stateBuilder.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ function includesBuilder(state: State) {
132132
* ]
133133
*/
134134
export function resolvablesBuilder(state: State): Resolvable[] {
135-
interface Tuple { token: any, val: any, deps: any[], policy?: string }
135+
interface Tuple { token: any, val: any, deps: any[], policy: ResolvePolicy }
136136

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

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

test/core/resolveSpec.ts

+27-28
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import { omit, pick, forEach, copy } from "../../src/core";
88
import Spy = jasmine.Spy;
99
import {services} from "../../src/common/coreservices";
1010
import {resolvablesBuilder} from "../../src/state/stateBuilder";
11+
import {tree2Array} from "../testUtils.ts";
12+
import {UIRouter} from "../../src/router";
1113

1214
///////////////////////////////////////////////
1315

14-
var states, statesTree, statesMap: { [key:string]: State } = {};
15-
var vals, counts, expectCounts;
16-
var asyncCount;
16+
let router, states, statesMap: { [key:string]: State } = {};
17+
let vals, counts, expectCounts;
18+
let asyncCount;
1719

1820
function invokeLater(fn: Function, ctx: ResolveContext) {
1921
return new Resolvable("", fn, services.$injector.annotate(fn)).get(ctx)
@@ -35,7 +37,14 @@ function getStates() {
3537
},
3638
I: { resolve: { _I: function(_I) { return "I"; } } }
3739
},
38-
J: { resolve: { _J: function() { counts['_J']++; return "J"; }, _J2: function(_J) { counts['_J2']++; return _J + "J2"; } },
40+
J: {
41+
resolve: {
42+
_J: function() { counts['_J']++; return "J"; },
43+
_J2: function(_J) { counts['_J2']++; return _J + "J2"; }
44+
},
45+
resolvePolicy: {
46+
_J: { when: 'EAGER' }
47+
},
3948
K: { resolve: { _K: function(_J2) { counts['_K']++; return _J2 + "K"; }},
4049
L: { resolve: { _L: function(_K) { counts['_L']++; return _K + "L"; }},
4150
M: { resolve: { _M: function(_L) { counts['_M']++; return _L + "M"; }} }
@@ -57,35 +66,17 @@ function getStates() {
5766
};
5867
}
5968

60-
6169
beforeEach(function () {
70+
router = new UIRouter();
71+
router.stateRegistry.stateQueue.autoFlush(router.stateService);
72+
6273
counts = { _J: 0, _J2: 0, _K: 0, _L: 0, _M: 0, _Q: 0 };
6374
vals = { _Q: null };
6475
expectCounts = copy(counts);
65-
states = getStates();
6676

67-
var stateProps = ["resolve", "resolvePolicy"];
68-
statesTree = loadStates({}, states, '');
69-
70-
function loadStates(parent, state, name) {
71-
var thisState = pick.apply(null, [state].concat(stateProps));
72-
var substates = omit.apply(null, [state].concat(stateProps));
73-
var resolve = thisState.resolve || {};
74-
75-
thisState.resolvables = resolvablesBuilder(<any> { resolve });
76-
thisState.template = thisState.template || "empty";
77-
thisState.name = name;
78-
thisState.parent = parent.name;
79-
thisState.params = {};
80-
thisState.data = { children: [] };
81-
82-
forEach(substates, function (value, key) {
83-
thisState.data.children.push(loadStates(thisState, value, key));
84-
});
85-
thisState = new State(thisState);
86-
statesMap[name] = thisState;
87-
return thisState;
88-
}
77+
tree2Array(getStates(), false).forEach(state => router.stateRegistry.register(state));
78+
statesMap = router.stateRegistry.get()
79+
.reduce((acc, state) => ((acc[state.name] = state.$$state()), acc), {});
8980
});
9081

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

120+
it('should resolve only eager resolves when run with "eager" policy', done => {
121+
let path = makePath([ "J", "K" ]);
122+
let ctx = new ResolveContext(path);
123+
ctx.resolvePath("EAGER").then(function () {
124+
expect(getResolvedData(ctx)).toEqualData({ _J: "J" });
125+
}).then(done);
126+
});
127+
129128
it('should resolve lazy and eager resolves when run with "lazy" policy', done => {
130129
let path = makePath([ "J", "N" ]);
131130
let ctx = new ResolveContext(path);

0 commit comments

Comments
 (0)