2
2
import { find , tail , uniqR , unnestR , inArray } from "../common/common" ;
3
3
import { propEq } from "../common/hof" ;
4
4
import { trace } from "../common/trace" ;
5
- import { services } from "../common/coreservices" ;
5
+ import { services , $InjectorLike } from "../common/coreservices" ;
6
6
import { resolvePolicies , PolicyWhen } from "./interface" ;
7
7
8
8
import { PathNode } from "../path/node" ;
@@ -13,10 +13,12 @@ import {stringify} from "../common/strings";
13
13
import { Transition } from "../transition/transition" ;
14
14
import { UIInjector } from "../common/interface" ;
15
15
16
- var when = resolvePolicies . when ;
16
+ const when = resolvePolicies . when ;
17
17
const ALL_WHENS = [ when . EAGER , when . LAZY ] ;
18
18
const EAGER_WHENS = [ when . EAGER ] ;
19
19
20
+ export const NATIVE_INJECTOR_TOKEN = "Native Injector" ;
21
+
20
22
/**
21
23
* Encapsulates Depenency Injection for a path of nodes
22
24
*
@@ -28,6 +30,7 @@ const EAGER_WHENS = [when.EAGER];
28
30
* The ResolveContext closes over the [[PathNode]]s, and provides DI for the last node in the path.
29
31
*/
30
32
export class ResolveContext {
33
+ _injector : UIInjector ;
31
34
32
35
constructor ( private _path : PathNode [ ] ) { }
33
36
@@ -131,7 +134,7 @@ export class ResolveContext {
131
134
}
132
135
133
136
injector ( ) : UIInjector {
134
- return new UIInjectorImpl ( this ) ;
137
+ return this . _injector || ( this . _injector = new UIInjectorImpl ( this ) ) ;
135
138
}
136
139
137
140
findNode ( resolvable : Resolvable ) : PathNode {
@@ -147,16 +150,16 @@ export class ResolveContext {
147
150
let node = this . findNode ( resolvable ) ;
148
151
// Find which other resolvables are "visible" to the `resolvable` argument
149
152
// subpath stopping at resolvable's node, or the whole path (if the resolvable isn't in the path)
150
- var subPath : PathNode [ ] = PathFactory . subPath ( this . _path , x => x === node ) || this . _path ;
151
- var availableResolvables : Resolvable [ ] = subPath
153
+ let subPath : PathNode [ ] = PathFactory . subPath ( this . _path , x => x === node ) || this . _path ;
154
+ let availableResolvables : Resolvable [ ] = subPath
152
155
. reduce ( ( acc , node ) => acc . concat ( node . resolvables ) , [ ] ) //all of subpath's resolvables
153
156
. filter ( res => res !== resolvable ) ; // filter out the `resolvable` argument
154
157
155
158
const getDependency = ( token : any ) => {
156
159
let matching = availableResolvables . filter ( r => r . token === token ) ;
157
160
if ( matching . length ) return tail ( matching ) ;
158
161
159
- let fromInjector = services . $ injector. get ( token ) ;
162
+ let fromInjector = this . injector ( ) . get ( token ) ;
160
163
if ( ! fromInjector ) {
161
164
throw new Error ( "Could not find Dependency Injection token: " + stringify ( token ) ) ;
162
165
}
@@ -169,7 +172,12 @@ export class ResolveContext {
169
172
}
170
173
171
174
class UIInjectorImpl implements UIInjector {
172
- constructor ( public context : ResolveContext ) { }
175
+ native : $InjectorLike ;
176
+
177
+ constructor ( public context : ResolveContext ) {
178
+ this . native = this . get ( NATIVE_INJECTOR_TOKEN ) || services . $injector ;
179
+ }
180
+
173
181
get ( token : any ) {
174
182
var resolvable = this . context . getResolvable ( token ) ;
175
183
if ( resolvable ) {
@@ -178,15 +186,12 @@ class UIInjectorImpl implements UIInjector {
178
186
}
179
187
return resolvable . data ;
180
188
}
181
- return services . $injector . get ( token ) ;
189
+ return this . native && this . native . get ( token ) ;
182
190
}
183
191
184
192
getAsync ( token : any ) {
185
193
var resolvable = this . context . getResolvable ( token ) ;
186
194
if ( resolvable ) return resolvable . get ( this . context ) ;
187
- return services . $q . when ( services . $injector . get ( token ) ) ;
195
+ return services . $q . when ( this . native . get ( token ) ) ;
188
196
}
189
-
190
- /** The native injector ($injector on ng1, Root Injector on ng2, justjs injector for everything else) */
191
- native = services . $injector ;
192
197
}
0 commit comments