@@ -2,20 +2,26 @@ import {FIELD, Type, isBlank, isPresent} from 'angular2/src/facade/lang';
22import { List , MapWrapper , ListWrapper } from 'angular2/src/facade/collection' ;
33import { reflector } from 'angular2/src/reflection/reflection' ;
44import { Key } from './key' ;
5- import { Inject , InjectLazy , InjectPromise , DependencyAnnotation } from './annotations' ;
5+ import { Inject , InjectLazy , InjectPromise , Optional , DependencyAnnotation } from './annotations' ;
66import { NoAnnotationError } from './exceptions' ;
77
88export class Dependency {
99 key :Key ;
1010 asPromise:boolean ;
1111 lazy:boolean ;
12+ optional:boolean ;
1213 properties:List ;
13- constructor ( key :Key , asPromise :boolean , lazy :boolean , properties :List ) {
14+ constructor ( key :Key , asPromise :boolean , lazy :boolean , optional : boolean , properties :List ) {
1415 this . key = key ;
1516 this . asPromise = asPromise ;
1617 this . lazy = lazy ;
18+ this . optional = optional ;
1719 this . properties = properties ;
1820 }
21+
22+ static fromKey ( key :Key ) {
23+ return new Dependency ( key , false , false , false , [ ] ) ;
24+ }
1925}
2026
2127export class Binding {
@@ -64,7 +70,7 @@ export class BindingBuilder {
6470 return new Binding (
6571 Key . get ( this . token ) ,
6672 ( aliasInstance ) => aliasInstance ,
67- [ new Dependency ( Key . get ( aliasToken ) , false , false , [ ] ) ] ,
73+ [ Dependency . fromKey ( Key . get ( aliasToken ) ) ] ,
6874 false
6975 ) ;
7076 }
@@ -90,7 +96,7 @@ export class BindingBuilder {
9096 _constructDependencies ( factoryFunction :Function , dependencies :List ) {
9197 return isBlank ( dependencies ) ?
9298 _dependenciesFor ( factoryFunction ) :
93- ListWrapper . map ( dependencies , ( t ) => new Dependency ( Key . get ( t ) , false , false , [ ] ) ) ;
99+ ListWrapper . map ( dependencies , ( t ) => Dependency . fromKey ( Key . get ( t ) ) ) ;
94100 }
95101}
96102
@@ -102,36 +108,44 @@ function _dependenciesFor(typeOrFunc):List {
102108}
103109
104110function _extractToken ( typeOrFunc , annotations ) {
105- var type ;
106111 var depProps = [ ] ;
112+ var token = null ;
113+ var optional = false ;
114+ var lazy = false ;
115+ var asPromise = false ;
107116
108117 for ( var i = 0 ; i < annotations . length ; ++ i ) {
109118 var paramAnnotation = annotations [ i ] ;
110119
111120 if ( paramAnnotation instanceof Type ) {
112- type = paramAnnotation ;
121+ token = paramAnnotation ;
113122
114123 } else if ( paramAnnotation instanceof Inject ) {
115- return _createDependency ( paramAnnotation . token , false , false , [ ] ) ;
124+ token = paramAnnotation . token ;
116125
117126 } else if ( paramAnnotation instanceof InjectPromise ) {
118- return _createDependency ( paramAnnotation . token , true , false , [ ] ) ;
127+ token = paramAnnotation . token ;
128+ asPromise = true ;
119129
120130 } else if ( paramAnnotation instanceof InjectLazy ) {
121- return _createDependency ( paramAnnotation . token , false , true , [ ] ) ;
131+ token = paramAnnotation . token ;
132+ lazy = true ;
133+
134+ } else if ( paramAnnotation instanceof Optional ) {
135+ optional = true ;
122136
123137 } else if ( paramAnnotation instanceof DependencyAnnotation ) {
124138 ListWrapper . push ( depProps , paramAnnotation ) ;
125139 }
126140 }
127141
128- if ( isPresent ( type ) ) {
129- return _createDependency ( type , false , false , depProps ) ;
142+ if ( isPresent ( token ) ) {
143+ return _createDependency ( token , asPromise , lazy , optional , depProps ) ;
130144 } else {
131145 throw new NoAnnotationError ( typeOrFunc ) ;
132146 }
133147}
134148
135- function _createDependency ( token , asPromise , lazy , depProps ) :Dependency {
136- return new Dependency ( Key . get ( token ) , asPromise , lazy , depProps ) ;
149+ function _createDependency ( token , asPromise , lazy , optional , depProps ) :Dependency {
150+ return new Dependency ( Key . get ( token ) , asPromise , lazy , optional , depProps ) ;
137151}
0 commit comments