| OLD | NEW |
| 1 part of di; | 1 part of di; |
| 2 | 2 |
| 3 typedef dynamic FactoryFn(Injector injector); | 3 typedef dynamic FactoryFn(Injector injector); |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Creation strategy is asked to return an instance of the type after | 6 * Creation strategy is asked to return an instance of the type after |
| 7 * [Injector.get] locates the defining injector that has no instance cached. | 7 * [Injector.get] locates the defining injector that has no instance cached. |
| 8 * [directInstantation] is true when an instance is created directly from | 8 * [directInstantation] is true when an instance is created directly from |
| 9 * [Injector.instantiate]. | 9 * [Injector.instantiate]. |
| 10 */ | 10 */ |
| 11 typedef dynamic CreationStrategy( | 11 typedef dynamic CreationStrategy( |
| 12 Injector requesting, | 12 Injector requesting, |
| 13 Injector defining, | 13 Injector defining, |
| 14 dynamic factory() | 14 dynamic factory() |
| 15 ); | 15 ); |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Visibility determines if the instance in the defining module is visible to | 18 * Visibility determines if the instance in the defining module is visible to |
| 19 * the requesting injector. If true is returned, then the instance from the | 19 * the requesting injector. If true is returned, then the instance from the |
| 20 * defining injector is provided. If false is returned, the injector keeps | 20 * defining injector is provided. If false is returned, the injector keeps |
| 21 * walking up the tree to find another visible instance. | 21 * walking up the tree to find another visible instance. |
| 22 */ | 22 */ |
| 23 typedef bool Visibility(Injector requesting, Injector defining); | 23 typedef bool Visibility(Injector requesting, Injector defining); |
| 24 | 24 |
| 25 typedef Object TypeFactory(factory(Type)); | |
| 26 | 25 |
| 27 /** | 26 /** |
| 28 * A collection of type bindings. Once the module is passed into the injector, | 27 * A collection of type bindings. Once the module is passed into the injector, |
| 29 * the injector creates a copy of the module and all subsequent changes to the | 28 * the injector creates a copy of the module and all subsequent changes to the |
| 30 * module have no effect. | 29 * module have no effect. |
| 31 */ | 30 */ |
| 32 class Module { | 31 class Module { |
| 33 final Map<Type, _Provider> _providers = <Type, _Provider>{}; | 32 final Map<Type, _Provider> _providers = <Type, _Provider>{}; |
| 34 final List<Module> _childModules = <Module>[]; | 33 final List<Module> _childModules = <Module>[]; |
| 35 Map<Type, TypeFactory> _typeFactories = {}; | |
| 36 | |
| 37 Map<Type, TypeFactory> get typeFactories { | |
| 38 if (_childModules.isEmpty) { | |
| 39 return _typeFactories; | |
| 40 } | |
| 41 var tmp = new Map.from(_typeFactories); | |
| 42 _childModules.forEach((child) { | |
| 43 if (child.typeFactories != null) { | |
| 44 child.typeFactories.forEach((type, factory) { | |
| 45 tmp[type] = factory; | |
| 46 }); | |
| 47 } | |
| 48 }); | |
| 49 return tmp; | |
| 50 } | |
| 51 | |
| 52 set typeFactories(Map<Type, TypeFactory> factories) { | |
| 53 _typeFactories = factories; | |
| 54 } | |
| 55 | 34 |
| 56 Map<Type, _Provider> _providersCache; | 35 Map<Type, _Provider> _providersCache; |
| 57 | 36 |
| 58 /** | 37 /** |
| 59 * Compiles and returs bindings map by performing depth-first traversal of the | 38 * Compiles and returs bindings map by performing depth-first traversal of the |
| 60 * child (installed) modules. | 39 * child (installed) modules. |
| 61 */ | 40 */ |
| 62 Map<Type, _Provider> get _bindings { | 41 Map<Type, _Provider> get _bindings { |
| 63 if (_isDirty) { | 42 if (_providersCache == null) { |
| 64 _providersCache = <Type, _Provider>{}; | 43 _providersCache = <Type, _Provider>{}; |
| 65 _childModules.forEach((child) => _providersCache.addAll(child._bindings)); | 44 _childModules.forEach((child) => _providersCache.addAll(child._bindings)); |
| 66 _providersCache.addAll(_providers); | 45 _providersCache.addAll(_providers); |
| 67 } | 46 } |
| 68 return _providersCache; | 47 return _providersCache; |
| 69 } | 48 } |
| 70 | 49 |
| 71 /** | 50 /** |
| 72 * Register binding to a concrete value. | 51 * Register binding to a concrete value. |
| 73 * | 52 * |
| 74 * The [value] is what actually will be injected. | 53 * The [value] is what actually will be injected. |
| 75 */ | 54 */ |
| 76 void value(Type id, value, | 55 void value(Type id, value, |
| 77 {CreationStrategy creation, Visibility visibility}) { | 56 {CreationStrategy creation, Visibility visibility}) { |
| 78 _dirty(); | 57 _providersCache = null; |
| 79 _providers[id] = new _ValueProvider(value, creation, visibility); | 58 _providers[id] = new _ValueProvider(value, creation, visibility); |
| 80 } | 59 } |
| 81 | 60 |
| 82 /** | 61 /** |
| 83 * Register binding to a [Type]. | 62 * Register binding to a [Type]. |
| 84 * | 63 * |
| 85 * The [implementedBy] will be instantiated using [new] operator and the | 64 * The [implementedBy] will be instantiated using [new] operator and the |
| 86 * resulting instance will be injected. If no type is provided, then it's | 65 * resulting instance will be injected. If no type is provided, then it's |
| 87 * implied that [id] should be instantiated. | 66 * implied that [id] should be instantiated. |
| 88 */ | 67 */ |
| 89 void type(Type id, {Type implementedBy, CreationStrategy creation, | 68 void type(Type id, {Type implementedBy, CreationStrategy creation, |
| 90 Visibility visibility}) { | 69 Visibility visibility}) { |
| 91 _dirty(); | 70 _providersCache = null; |
| 92 _providers[id] = new _TypeProvider( | 71 _providers[id] = new _TypeProvider( |
| 93 implementedBy == null ? id : implementedBy, creation, visibility); | 72 implementedBy == null ? id : implementedBy, creation, visibility); |
| 94 } | 73 } |
| 95 | 74 |
| 96 /** | 75 /** |
| 97 * Register binding to a factory function.abstract | 76 * Register binding to a factory function.abstract |
| 98 * | 77 * |
| 99 * The [factoryFn] will be called and all its arguments will get injected. | 78 * The [factoryFn] will be called and all its arguments will get injected. |
| 100 * The result of that function is the value that will be injected. | 79 * The result of that function is the value that will be injected. |
| 101 */ | 80 */ |
| 102 void factory(Type id, FactoryFn factoryFn, | 81 void factory(Type id, FactoryFn factoryFn, |
| 103 {CreationStrategy creation, Visibility visibility}) { | 82 {CreationStrategy creation, Visibility visibility}) { |
| 104 _dirty(); | 83 _providersCache = null; |
| 105 _providers[id] = new _FactoryProvider(factoryFn, creation, visibility); | 84 _providers[id] = new _FactoryProvider(factoryFn, creation, visibility); |
| 106 } | 85 } |
| 107 | 86 |
| 108 /** | 87 /** |
| 109 * Installs another module into this module. Bindings defined on this module | 88 * Installs another module into this module. Bindings defined on this module |
| 110 * take precidence over the installed module. | 89 * take precidence over the installed module. |
| 111 */ | 90 */ |
| 112 void install(Module module) { | 91 void install(Module module) { |
| 113 _childModules.add(module); | 92 _childModules.add(module); |
| 114 _dirty(); | |
| 115 } | |
| 116 | |
| 117 _dirty() { | |
| 118 _providersCache = null; | 93 _providersCache = null; |
| 119 } | 94 } |
| 120 | |
| 121 bool get _isDirty => | |
| 122 _providersCache == null || _childModules.any((m) => m._isDirty); | |
| 123 } | 95 } |
| 124 | 96 |
| 125 /** Deafault creation strategy is to instantiate on the defining injector. */ | 97 /** Deafault creation strategy is to instantiate on the defining injector. */ |
| 126 dynamic _defaultCreationStrategy(Injector requesting, Injector defining, | 98 dynamic _defaultCreationStrategy(Injector requesting, Injector defining, |
| 127 dynamic factory()) => factory(); | 99 dynamic factory()) => factory(); |
| 128 | 100 |
| 129 /** By default all values are visible to child injectors. */ | 101 /** By default all values are visible to child injectors. */ |
| 130 bool _defaultVisibility(_, __) => true; | 102 bool _defaultVisibility(_, __) => true; |
| 131 | 103 |
| 132 | 104 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 class _FactoryProvider extends _Provider { | 139 class _FactoryProvider extends _Provider { |
| 168 final Function factoryFn; | 140 final Function factoryFn; |
| 169 | 141 |
| 170 _FactoryProvider(this.factoryFn, [CreationStrategy creationStrategy, | 142 _FactoryProvider(this.factoryFn, [CreationStrategy creationStrategy, |
| 171 Visibility visibility]) | 143 Visibility visibility]) |
| 172 : super(creationStrategy, visibility); | 144 : super(creationStrategy, visibility); |
| 173 | 145 |
| 174 dynamic get(Injector injector, Injector requestor, getInstanceByType, error) =
> | 146 dynamic get(Injector injector, Injector requestor, getInstanceByType, error) =
> |
| 175 factoryFn(injector); | 147 factoryFn(injector); |
| 176 } | 148 } |
| OLD | NEW |