Rodrigo Lopez Peker

GetInstance docs

- improved comments.
@@ -3,11 +3,33 @@ import 'package:get/src/core/get_interface.dart'; @@ -3,11 +3,33 @@ import 'package:get/src/core/get_interface.dart';
3 import 'get_instance.dart'; 3 import 'get_instance.dart';
4 4
5 extension Inst on GetInterface { 5 extension Inst on GetInterface {
6 - void lazyPut<S>(InstanceBuilderCallback builder, 6 + /// Creates a new Instance<S> lazily from the [<S>builder()] callback.
  7 + ///
  8 + /// The first time you call [Get.find()], the [builder()] callback will create
  9 + /// the Instance and persisted as a Singleton (like you would use [Get.put()]).
  10 + ///
  11 + /// Using [GetConfig.smartManagement] as [SmartManagement.keepFactory] has the same outcome
  12 + /// as using [fenix:true] :
  13 + /// The internal register of [builder()] will remain in memory to recreate the Instance
  14 + /// if the Instance has been removed with [Get.delete()].
  15 + /// Therefore, future calls to [Get.find()] will return the same Instance.
  16 + ///
  17 + /// If you need to make use of GetxController's life-cycle ([onInit(), onStart(), onClose()])
  18 + /// [fenix] is a great choice to mix with [GetBuilder()] and [GetX()] widgets, and/or [GetMaterialApp] Navigation.
  19 + ///
  20 + /// You could use [Get.lazyPut(fenix:true)] in your app's [main()] instead of [Bindings()] for each [GetPage].
  21 + /// And the memory management will be similar.
  22 + ///
  23 + /// Subsequent calls to [Get.lazyPut()] with the same parameters (<[S]> and optionally [tag]
  24 + /// will **not** override the original).
  25 + void lazyPut<S>(InstanceBuilderCallback<S> builder,
7 {String tag, bool fenix = false}) { 26 {String tag, bool fenix = false}) {
8 - return GetInstance().lazyPut<S>(builder, tag: tag, fenix: fenix); 27 + GetInstance().lazyPut<S>(builder, tag: tag, fenix: fenix);
9 } 28 }
10 29
  30 + /// async version of [Get.put()].
  31 + /// Awaits for the resolution of the Future from [builder()] parameter and
  32 + /// stores the Instance returned.
11 Future<S> putAsync<S>(AsyncInstanceBuilderCallback<S> builder, 33 Future<S> putAsync<S>(AsyncInstanceBuilderCallback<S> builder,
12 {String tag, bool permanent = false}) async => 34 {String tag, bool permanent = false}) async =>
13 GetInstance().putAsync<S>(builder, tag: tag, permanent: permanent); 35 GetInstance().putAsync<S>(builder, tag: tag, permanent: permanent);
@@ -26,19 +48,21 @@ extension Inst on GetInterface { @@ -26,19 +48,21 @@ extension Inst on GetInterface {
26 {String name, bool permanent = true}) => 48 {String name, bool permanent = true}) =>
27 GetInstance().create<S>(builder, name: name, permanent: permanent); 49 GetInstance().create<S>(builder, name: name, permanent: permanent);
28 50
29 - /// Finds a instance of the required Class<[S]> (or [tag])  
30 - /// In the case of using Get.[create], it will create an instance  
31 - /// each time you call [find] 51 + /// Finds a Instance of the required Class <[S]>(or [tag])
  52 + /// In the case of using [Get.create()], it will generate an Instance
  53 + /// each time you call [Get.find()].
32 S find<S>({String tag}) => GetInstance().find<S>(tag: tag); 54 S find<S>({String tag}) => GetInstance().find<S>(tag: tag);
33 55
34 - /// Injects a Instance [S] in [GetInstance]. 56 + /// Injects an [Instance<S>] in memory.
35 /// 57 ///
36 /// No need to define the generic type <[S]> as it's inferred from the [dependency] 58 /// No need to define the generic type <[S]> as it's inferred from the [dependency]
  59 + /// parameter.
37 /// 60 ///
38 /// - [dependency] The Instance to be injected. 61 /// - [dependency] The Instance to be injected.
39 /// - [tag] optionally, use a [tag] as an "id" to create multiple records of the same Type<[S]> 62 /// - [tag] optionally, use a [tag] as an "id" to create multiple records of the same Type<[S]>
40 - /// - [permanent] keeps the Instance in memory, not following [GetConfig.smartManagement]  
41 - /// rules 63 + /// the [tag] does **not** conflict with the same tags used by other [dependencies] Types.
  64 + /// - [permanent] keeps the Instance in memory and persist it, not following [GetConfig.smartManagement]
  65 + /// rules. Although, can be removed by [GetInstance.reset()] and [Get.delete()]
42 /// - [builder] If defined, the [dependency] must be returned from here 66 /// - [builder] If defined, the [dependency] must be returned from here
43 S put<S>(S dependency, 67 S put<S>(S dependency,
44 {String tag, 68 {String tag,
@@ -50,15 +74,21 @@ extension Inst on GetInterface { @@ -50,15 +74,21 @@ extension Inst on GetInterface {
50 /// Clears all registered instances (and/or tags). 74 /// Clears all registered instances (and/or tags).
51 /// Even the persistent ones. 75 /// Even the persistent ones.
52 /// 76 ///
53 - /// [clearFactory] clears the callbacks registered by [lazyPut]  
54 - /// [clearRouteBindings] clears Instances associated with routes. 77 + /// - [clearFactory] clears the callbacks registered by [Get.lazyPut()]
  78 + /// - [clearRouteBindings] clears Instances associated with Routes when using
  79 + /// [GetMaterialApp].
55 bool reset({bool clearFactory = true, bool clearRouteBindings = true}) => 80 bool reset({bool clearFactory = true, bool clearRouteBindings = true}) =>
56 GetInstance().reset( 81 GetInstance().reset(
57 clearFactory: clearFactory, clearRouteBindings: clearRouteBindings); 82 clearFactory: clearFactory, clearRouteBindings: clearRouteBindings);
58 83
59 - /// Delete class instance on [S], cleaning the memory  
60 - Future<bool> delete<S>({String tag, String key}) async =>  
61 - GetInstance().delete<S>(tag: tag, key: key); 84 + /// Deletes the Instance<[S]>, cleaning the memory and closes any open
  85 + /// controllers ([DisposableInterface]).
  86 + ///
  87 + /// - [tag] Optional "tag" used to register the Instance
  88 + Future<bool> delete<S>({String tag}) async =>
  89 + GetInstance().delete<S>(tag: tag);
62 90
  91 + /// Check if a Class Instance<[S]> (or [tag]) is registered in memory.
  92 + /// - [tag] optional, if you use a [tag] to register the Instance.
63 bool isRegistered<S>({String tag}) => GetInstance().isRegistered<S>(tag: tag); 93 bool isRegistered<S>({String tag}) => GetInstance().isRegistered<S>(tag: tag);
64 } 94 }
@@ -18,23 +18,47 @@ class GetInstance { @@ -18,23 +18,47 @@ class GetInstance {
18 static GetInstance _getInstance; 18 static GetInstance _getInstance;
19 19
20 /// Holds references to every registered Instance when using 20 /// Holds references to every registered Instance when using
21 - /// Get.[put] 21 + /// [Get.put()]
22 static Map<String, _InstanceBuilderFactory> _singl = {}; 22 static Map<String, _InstanceBuilderFactory> _singl = {};
23 23
24 /// Holds a reference to every registered callback when using 24 /// Holds a reference to every registered callback when using
25 - /// Get.[lazyPut] 25 + /// [Get.lazyPut()]
26 static Map<String, _Lazy> _factory = {}; 26 static Map<String, _Lazy> _factory = {};
27 27
  28 + /// Holds a reference to [GetConfig.currentRoute] when the Instance was
  29 + /// created to manage the memory.
28 static Map<String, String> _routesKey = {}; 30 static Map<String, String> _routesKey = {};
29 31
30 static GetQueue _queue = GetQueue(); 32 static GetQueue _queue = GetQueue();
31 33
32 - void lazyPut<S>(InstanceBuilderCallback builder, 34 + /// Creates a new Instance<S> lazily from the [<S>builder()] callback.
  35 + ///
  36 + /// The first time you call [Get.find()], the [builder()] callback will create
  37 + /// the Instance and persisted as a Singleton (like you would use [Get.put()]).
  38 + ///
  39 + /// Using [GetConfig.smartManagement] as [SmartManagement.keepFactory] has the same outcome
  40 + /// as using [fenix:true] :
  41 + /// The internal register of [builder()] will remain in memory to recreate the Instance
  42 + /// if the Instance has been removed with [Get.delete()].
  43 + /// Therefore, future calls to [Get.find()] will return the same Instance.
  44 + ///
  45 + /// If you need to make use of GetxController's life-cycle ([onInit(), onStart(), onClose()])
  46 + /// [fenix] is a great choice to mix with [GetBuilder()] and [GetX()] widgets, and/or [GetMaterialApp] Navigation.
  47 + ///
  48 + /// You could use [Get.lazyPut(fenix:true)] in your app's [main()] instead of [Bindings()] for each [GetPage].
  49 + /// And the memory management will be similar.
  50 + ///
  51 + /// Subsequent calls to [Get.lazyPut()] with the same parameters (<[S]> and optionally [tag]
  52 + /// will **not** override the original).
  53 + void lazyPut<S>(InstanceBuilderCallback<S> builder,
33 {String tag, bool fenix = false}) { 54 {String tag, bool fenix = false}) {
34 String key = _getKey(S, tag); 55 String key = _getKey(S, tag);
35 _factory.putIfAbsent(key, () => _Lazy(builder, fenix)); 56 _factory.putIfAbsent(key, () => _Lazy(builder, fenix));
36 } 57 }
37 58
  59 + /// async version of [Get.put()].
  60 + /// Awaits for the resolution of the Future from [builder()] parameter and
  61 + /// stores the Instance returned.
38 Future<S> putAsync<S>(AsyncInstanceBuilderCallback<S> builder, 62 Future<S> putAsync<S>(AsyncInstanceBuilderCallback<S> builder,
39 {String tag, bool permanent = false}) async { 63 {String tag, bool permanent = false}) async {
40 return put<S>(await builder(), tag: tag, permanent: permanent); 64 return put<S>(await builder(), tag: tag, permanent: permanent);
@@ -196,10 +220,9 @@ class GetInstance { @@ -196,10 +220,9 @@ class GetInstance {
196 return _singl[key].getDependency() as S; 220 return _singl[key].getDependency() as S;
197 } else { 221 } else {
198 if (!_factory.containsKey(key)) 222 if (!_factory.containsKey(key))
199 - throw '"$S" not found. You need to call "Get.put<$S>($S())"'; 223 + throw '"$S" not found. You need to call "Get.put($S())" or "Get.lazyPut(()=>$S())"';
200 224
201 - // TODO: This message is not clear  
202 - GetConfig.log('"$S" instance was created at that time'); 225 + GetConfig.log('Lazy instance "$S" created');
203 226
204 S _value = put<S>(_factory[key].builder() as S); 227 S _value = put<S>(_factory[key].builder() as S);
205 228
@@ -214,6 +237,8 @@ class GetInstance { @@ -214,6 +237,8 @@ class GetInstance {
214 } 237 }
215 } 238 }
216 239
  240 + /// Generates the key based on [type] (and optionally a [name])
  241 + /// to register an Instance Builder in the hashmap.
217 String _getKey(Type type, String name) { 242 String _getKey(Type type, String name) {
218 return name == null ? type.toString() : type.toString() + name; 243 return name == null ? type.toString() : type.toString() + name;
219 } 244 }
@@ -239,6 +264,20 @@ class GetInstance { @@ -239,6 +264,20 @@ class GetInstance {
239 264
240 /// Delete registered Class Instance [S] (or [tag]) and, closes any open 265 /// Delete registered Class Instance [S] (or [tag]) and, closes any open
241 /// controllers [DisposableInterface], cleans up the memory 266 /// controllers [DisposableInterface], cleans up the memory
  267 + ///
  268 + /// /// Deletes the Instance<[S]>, cleaning the memory.
  269 + // ///
  270 + // /// - [tag] Optional "tag" used to register the Instance
  271 + // /// - [key] For internal usage, is the processed key used to register
  272 + // /// the Instance. **don't use** it unless you know what you are doing.
  273 +
  274 + /// Deletes the Instance<[S]>, cleaning the memory and closes any open
  275 + /// controllers ([DisposableInterface]).
  276 + ///
  277 + /// - [tag] Optional "tag" used to register the Instance
  278 + /// - [key] For internal usage, is the processed key used to register
  279 + /// the Instance. **don't use** it unless you know what you are doing.
  280 + /// - [force] Will delete an Instance even if marked as [permanent].
242 Future<bool> delete<S>({String tag, String key, bool force = false}) async { 281 Future<bool> delete<S>({String tag, String key, bool force = false}) async {
243 final newKey = key ?? _getKey(S, tag); 282 final newKey = key ?? _getKey(S, tag);
244 283
@@ -277,10 +316,13 @@ class GetInstance { @@ -277,10 +316,13 @@ class GetInstance {
277 }); 316 });
278 } 317 }
279 318
280 - /// Check if a Class instance [S] (or [tag]) is registered. 319 + /// Check if a Class Instance<[S]> (or [tag]) is registered in memory.
  320 + /// - [tag] optional, if you use a [tag] to register the Instance.
281 bool isRegistered<S>({String tag}) => _singl.containsKey(_getKey(S, tag)); 321 bool isRegistered<S>({String tag}) => _singl.containsKey(_getKey(S, tag));
282 322
283 - /// Check if Class instance [S] (or [tag]) is prepared to be used. 323 + /// Checks if a lazy factory callback that returns an Instance<[S]>
  324 + /// is registered.
  325 + /// - [tag] optional, if you use a [tag] to register the Instance.
284 bool isPrepared<S>({String tag}) => _factory.containsKey(_getKey(S, tag)); 326 bool isPrepared<S>({String tag}) => _factory.containsKey(_getKey(S, tag));
285 } 327 }
286 328
@@ -321,5 +363,6 @@ class _InstanceBuilderFactory<S> { @@ -321,5 +363,6 @@ class _InstanceBuilderFactory<S> {
321 class _Lazy { 363 class _Lazy {
322 bool fenix; 364 bool fenix;
323 InstanceBuilderCallback builder; 365 InstanceBuilderCallback builder;
  366 +
324 _Lazy(this.builder, this.fenix); 367 _Lazy(this.builder, this.fenix);
325 } 368 }