@@ -11,7 +11,6 @@ import {Resolvables, ResolvePolicy, IOptions1} from "./interface";
11
11
import { PathNode } from "../path/node" ;
12
12
import { Resolvable } from "./resolvable" ;
13
13
import { State } from "../state/stateObject" ;
14
- import { mergeR } from "../common/common" ;
15
14
import { PathFactory } from "../path/pathFactory" ;
16
15
import { stringify } from "../common/strings" ;
17
16
@@ -23,19 +22,7 @@ interface Promises { [key: string]: Promise<any>; }
23
22
24
23
export class ResolveContext {
25
24
26
- private _nodeFor : ( s : State ) => PathNode ;
27
- private _pathTo : ( s : State ) => PathNode [ ] ;
28
-
29
- constructor ( private _path : PathNode [ ] ) {
30
- extend ( this , {
31
- _nodeFor ( state : State ) : PathNode {
32
- return < PathNode > find ( this . _path , propEq ( 'state' , state ) ) ;
33
- } ,
34
- _pathTo ( state : State ) : PathNode [ ] {
35
- return PathFactory . subPath ( this . _path , node => node . state === state ) ;
36
- }
37
- } ) ;
38
- }
25
+ constructor ( private _path : PathNode [ ] ) { }
39
26
40
27
/** Gets all the tokens found in the resolve context, de-duplicated */
41
28
getTokens ( ) {
@@ -55,10 +42,12 @@ export class ResolveContext {
55
42
return tail ( matching ) ;
56
43
}
57
44
58
- /** Inspects a function `fn` for its dependencies. Returns an object containing any matching Resolvables */
59
- getResolvablesForFn ( fn : IInjectable ) : Resolvable [ ] {
60
- let deps = services . $injector . annotate ( < Function > fn , services . $injector . strictDi ) ;
61
- return deps . map ( token => this . getResolvable ( token ) ) ;
45
+ private _nodeFor ( state : State ) : PathNode {
46
+ return < PathNode > find ( this . _path , propEq ( 'state' , state ) ) ;
47
+ }
48
+
49
+ private _pathTo ( state : State ) : PathNode [ ] {
50
+ return PathFactory . subPath ( this . _path , node => node . state === state ) ;
62
51
}
63
52
64
53
isolateRootTo ( state : State ) : ResolveContext {
@@ -78,34 +67,27 @@ export class ResolveContext {
78
67
}
79
68
80
69
// Returns a promise for an array of resolved path Element promises
81
- resolvePath ( options : IOptions1 = { } ) : Promise < any > {
82
- trace . traceResolvePath ( this . _path , options ) ;
83
- const promiseForNode = ( node : PathNode ) => this . resolvePathElement ( node . state , options ) ;
84
- return services . $q . all ( < any > map ( this . _path , promiseForNode ) ) . then ( all => all . reduce ( mergeR , { } ) ) ;
85
- }
86
-
87
- // returns a promise for all the resolvables on this PathElement
88
- // options.resolvePolicy: only return promises for those Resolvables which are at
89
- // the specified policy, or above. i.e., options.resolvePolicy === 'lazy' will
90
- // resolve both 'lazy' and 'eager' resolves.
91
- resolvePathElement ( state : State , options : IOptions1 = { } ) : Promise < any > {
92
- // The caller can request the path be resolved for a given policy and "below"
70
+ resolvePath ( options : IOptions1 = { } ) : Promise < { token : any , value : any } [ ] > {
93
71
let policy : string = options && options . resolvePolicy ;
94
72
let policyOrdinal : number = ResolvePolicy [ policy || defaultResolvePolicy ] ;
95
- // Get path Resolvables available to this element
96
- let resolvables = this . getOwnResolvables ( state ) ;
73
+ // get the subpath to the state argument, if provided
74
+ trace . traceResolvePath ( this . _path , options ) ;
97
75
98
- const matchesRequestedPolicy = resolvable => getPolicy ( state . resolvePolicy , resolvable ) >= policyOrdinal ;
99
- let matchingResolves = filter ( resolvables , matchesRequestedPolicy ) ;
76
+ let promises : Promise < any > [ ] = this . _path . reduce ( ( acc , node ) => {
77
+ const matchesRequestedPolicy = resolvable => getPolicy ( node . state . resolvePolicy , resolvable ) >= policyOrdinal ;
78
+ let nodeResolvables = node . resolvables . filter ( matchesRequestedPolicy ) ;
79
+ let subContext = this . isolateRootTo ( node . state ) ;
100
80
101
- const getResolvePromise = ( resolvable : Resolvable ) => resolvable . get ( this . isolateRootTo ( state ) , options ) ;
102
- let resolvablePromises : Promises = < any > map ( matchingResolves , getResolvePromise ) ;
81
+ // For the matching Resolvables, start their async fetch process.
82
+ var getResult = ( r : Resolvable ) => r . get ( subContext , options )
83
+ // Return a tuple that includes the Resolvable's token
84
+ . then ( value => ( { token : r . token , value : value } ) ) ;
85
+ return acc . concat ( nodeResolvables . map ( getResult ) ) ;
86
+ } , [ ] ) ;
103
87
104
- trace . traceResolvePathElement ( this , matchingResolves , options ) ;
88
+ return services . $q . all ( promises ) ;
89
+ }
105
90
106
- return services . $q . all ( resolvablePromises ) ;
107
- }
108
-
109
91
injector ( ) : { get ( any ) : any } {
110
92
111
93
let get = ( token : any ) => {
@@ -117,6 +99,11 @@ export class ResolveContext {
117
99
return { get } ;
118
100
}
119
101
102
+ /**
103
+ * Gets the async dependencies of a Resolvable
104
+ *
105
+ * Given a Resolvable, returns its dependencies as a Resolvable[]
106
+ */
120
107
getDependencies ( resolvable : Resolvable ) : Resolvable [ ] {
121
108
// predicate that finds the node the resolvable belongs to
122
109
const nodeForResolvable = node => node . resolvables . indexOf ( resolvable ) !== - 1 ;
@@ -132,7 +119,9 @@ export class ResolveContext {
132
119
if ( matching . length ) return tail ( matching ) ;
133
120
134
121
let fromInjector = services . $injector . get ( token ) ;
135
- if ( ! fromInjector ) throw new Error ( "Could not find Dependency Injection token: " + stringify ( token ) ) ;
122
+ if ( ! fromInjector ) {
123
+ throw new Error ( "Could not find Dependency Injection token: " + stringify ( token ) ) ;
124
+ }
136
125
137
126
return new Resolvable ( token , ( ) => fromInjector , [ ] , fromInjector ) ;
138
127
} ;
0 commit comments