@@ -14,7 +14,7 @@ import {
1414import { FunctionWrapper , Type , isPresent , isBlank , CONST_EXPR } from 'angular2/src/facade/lang' ;
1515import { Key } from './key' ;
1616import { resolveForwardRef } from './forward_ref' ;
17- import { VisibilityMetadata , DEFAULT_VISIBILITY } from './metadata' ;
17+ import { VisibilityMetadata , DEFAULT_VISIBILITY , SelfMetadata , AncestorMetadata } from './metadata' ;
1818
1919const _constructing = CONST_EXPR ( new Object ( ) ) ;
2020const _notFound = CONST_EXPR ( new Object ( ) ) ;
@@ -175,7 +175,7 @@ export class ProtoInjectorDynamicStrategy implements ProtoInjectorStrategy {
175175export class ProtoInjector {
176176 _strategy : ProtoInjectorStrategy ;
177177
178- constructor ( bwv : BindingWithVisibility [ ] , public distanceToParent : number ) {
178+ constructor ( bwv : BindingWithVisibility [ ] ) {
179179 this . _strategy = bwv . length > _MAX_CONSTRUCTION_COUNTER ?
180180 new ProtoInjectorDynamicStrategy ( this , bwv ) :
181181 new ProtoInjectorInlineStrategy ( this , bwv ) ;
@@ -459,7 +459,7 @@ export class Injector {
459459 static fromResolvedBindings ( bindings : List < ResolvedBinding > ,
460460 depProvider : DependencyProvider = null ) : Injector {
461461 var bd = bindings . map ( b => new BindingWithVisibility ( b , PUBLIC ) ) ;
462- var proto = new ProtoInjector ( bd , 0 ) ;
462+ var proto = new ProtoInjector ( bd ) ;
463463 var inj = new Injector ( proto , null , depProvider ) ;
464464 return inj ;
465465 }
@@ -542,7 +542,7 @@ export class Injector {
542542 createChildFromResolved ( bindings : List < ResolvedBinding > ,
543543 depProvider : DependencyProvider = null ) : Injector {
544544 var bd = bindings . map ( b => new BindingWithVisibility ( b , PUBLIC ) ) ;
545- var proto = new ProtoInjector ( bd , 1 ) ;
545+ var proto = new ProtoInjector ( bd ) ;
546546 var inj = new Injector ( proto , null , depProvider ) ;
547547 inj . _parent = this ;
548548 return inj ;
@@ -678,48 +678,78 @@ export class Injector {
678678 return this ;
679679 }
680680
681- var inj = this ;
682- var lastInjector = false ;
683- var depth = depVisibility . depth ;
681+ if ( depVisibility instanceof SelfMetadata ) {
682+ return this . _getByKeySelf ( key , optional , bindingVisibility ) ;
683+
684+ } else if ( depVisibility instanceof AncestorMetadata ) {
685+ return this . _getByKeyAncestor ( key , optional , bindingVisibility , depVisibility . includeSelf ) ;
686+
687+ } else {
688+ return this . _getByKeyUnbounded ( key , optional , bindingVisibility , depVisibility . includeSelf ) ;
689+ }
690+ }
691+
692+ _throwOrNull ( key : Key , optional : boolean ) : any {
693+ if ( optional ) {
694+ return null ;
695+ } else {
696+ throw new NoBindingError ( key ) ;
697+ }
698+ }
684699
685- if ( ! depVisibility . includeSelf ) {
686- depth -= inj . _proto . distanceToParent ;
700+ _getByKeySelf ( key : Key , optional : boolean , bindingVisibility : number ) : any {
701+ var obj = this . _strategy . getObjByKeyId ( key . id , bindingVisibility ) ;
702+ return ( obj !== undefinedValue ) ? obj : this . _throwOrNull ( key , optional ) ;
703+ }
687704
705+ _getByKeyAncestor ( key : Key , optional : boolean , bindingVisibility : number ,
706+ includeSelf : boolean ) : any {
707+ var inj = this ;
708+
709+ if ( ! includeSelf ) {
688710 if ( inj . _isBoundary ) {
689- if ( depVisibility . crossBoundaries ) {
690- bindingVisibility = PUBLIC_AND_PRIVATE ;
691- } else {
692- bindingVisibility = PRIVATE ;
693- lastInjector = true ;
694- }
711+ return this . _getPrivateDependency ( key , optional , inj ) ;
712+ } else {
713+ inj = inj . _parent ;
695714 }
696- inj = inj . _parent ;
697715 }
698716
699- while ( inj != null && depth >= 0 ) {
717+ while ( inj != null ) {
700718 var obj = inj . _strategy . getObjByKeyId ( key . id , bindingVisibility ) ;
701719 if ( obj !== undefinedValue ) return obj ;
702720
703- depth -= inj . _proto . distanceToParent ;
721+ if ( isPresent ( inj . _parent ) && inj . _isBoundary ) {
722+ return this . _getPrivateDependency ( key , optional , inj ) ;
723+ } else {
724+ inj = inj . _parent ;
725+ }
726+ }
704727
705- if ( lastInjector ) break ;
728+ return this . _throwOrNull ( key , optional ) ;
729+ }
706730
707- if ( inj . _isBoundary ) {
708- if ( depVisibility . crossBoundaries ) {
709- bindingVisibility = PUBLIC_AND_PRIVATE ;
710- } else {
711- bindingVisibility = PRIVATE ;
712- lastInjector = true ;
713- }
714- }
731+ _getPrivateDependency ( key : Key , optional : boolean , inj : Injector ) : any {
732+ var obj = inj . _parent . _strategy . getObjByKeyId ( key . id , PRIVATE ) ;
733+ return ( obj !== undefinedValue ) ? obj : this . _throwOrNull ( key , optional ) ;
734+ }
735+
736+ _getByKeyUnbounded ( key : Key , optional : boolean , bindingVisibility : number ,
737+ includeSelf : boolean ) : any {
738+ var inj = this ;
739+ if ( ! includeSelf ) {
740+ bindingVisibility = inj . _isBoundary ? PUBLIC_AND_PRIVATE : PUBLIC ;
715741 inj = inj . _parent ;
716742 }
717743
718- if ( optional ) {
719- return null ;
720- } else {
721- throw new NoBindingError ( key ) ;
744+ while ( inj != null ) {
745+ var obj = inj . _strategy . getObjByKeyId ( key . id , bindingVisibility ) ;
746+ if ( obj !== undefinedValue ) return obj ;
747+
748+ bindingVisibility = inj . _isBoundary ? PUBLIC_AND_PRIVATE : PUBLIC ;
749+ inj = inj . _parent ;
722750 }
751+
752+ return this . _throwOrNull ( key , optional ) ;
723753 }
724754}
725755
0 commit comments