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