Jonatas

improve iterable observable api

@@ -32,6 +32,10 @@ extension Inst on GetInterface { @@ -32,6 +32,10 @@ extension Inst on GetInterface {
32 GetInstance().lazyPut<S>(builder, tag: tag, fenix: fenix); 32 GetInstance().lazyPut<S>(builder, tag: tag, fenix: fenix);
33 } 33 }
34 34
  35 + void printInstanceStack() {
  36 + GetInstance().printInstanceStack();
  37 + }
  38 +
35 /// async version of [Get.put()]. 39 /// async version of [Get.put()].
36 /// Awaits for the resolution of the Future from [builder()] parameter and 40 /// Awaits for the resolution of the Future from [builder()] parameter and
37 /// stores the Instance returned. 41 /// stores the Instance returned.
@@ -85,6 +85,10 @@ class GetInstance { @@ -85,6 +85,10 @@ class GetInstance {
85 ); 85 );
86 } 86 }
87 87
  88 + void printInstanceStack() {
  89 + Get.log(_routesKey.toString());
  90 + }
  91 +
88 void injector<S>( 92 void injector<S>(
89 InjectorBuilderCallback<S> fn, { 93 InjectorBuilderCallback<S> fn, {
90 String tag, 94 String tag,
@@ -74,45 +74,9 @@ class RxList<E> extends ListMixin<E> @@ -74,45 +74,9 @@ class RxList<E> extends ListMixin<E>
74 refresh(); 74 refresh();
75 } 75 }
76 76
77 - /// Add [item] to [List<E>] only if [item] is not null.  
78 - void addNonNull(E item) {  
79 - if (item != null) add(item);  
80 - }  
81 -  
82 - /// Add [Iterable<E>] to [List<E>] only if [Iterable<E>] is not null.  
83 - void addAllNonNull(Iterable<E> item) {  
84 - if (item != null) addAll(item);  
85 - }  
86 -  
87 - /// Add [item] to [List<E>] only if [condition] is true.  
88 - void addIf(dynamic condition, E item) {  
89 - if (condition is Condition) condition = condition();  
90 - if (condition is bool && condition) add(item);  
91 - }  
92 -  
93 - /// Adds [Iterable<E>] to [List<E>] only if [condition] is true.  
94 - void addAllIf(dynamic condition, Iterable<E> items) {  
95 - if (condition is Condition) condition = condition();  
96 - if (condition is bool && condition) addAll(items);  
97 - }  
98 -  
99 @override 77 @override
100 int get length => value.length; 78 int get length => value.length;
101 79
102 - /// Replaces all existing items of this list with [item]  
103 - void assign(E item) {  
104 - _value ??= <E>[];  
105 - clear();  
106 - add(item);  
107 - }  
108 -  
109 - /// Replaces all existing items of this list with [items]  
110 - void assignAll(Iterable<E> items) {  
111 - _value ??= <E>[];  
112 - clear();  
113 - addAll(items);  
114 - }  
115 -  
116 @override 80 @override
117 @protected 81 @protected
118 List<E> get value { 82 List<E> get value {
@@ -164,7 +128,46 @@ class RxList<E> extends ListMixin<E> @@ -164,7 +128,46 @@ class RxList<E> extends ListMixin<E>
164 } 128 }
165 129
166 extension ListExtension<E> on List<E> { 130 extension ListExtension<E> on List<E> {
167 - RxList<E> get obs {  
168 - return RxList<E>(this); 131 + RxList<E> get obs => RxList<E>(this);
  132 +
  133 + /// Add [item] to [List<E>] only if [item] is not null.
  134 + void addNonNull(E item) {
  135 + if (item != null) add(item);
  136 + }
  137 +
  138 + /// Add [Iterable<E>] to [List<E>] only if [Iterable<E>] is not null.
  139 + void addAllNonNull(Iterable<E> item) {
  140 + if (item != null) addAll(item);
  141 + }
  142 +
  143 + /// Add [item] to [List<E>] only if [condition] is true.
  144 + void addIf(dynamic condition, E item) {
  145 + if (condition is Condition) condition = condition();
  146 + if (condition is bool && condition) add(item);
  147 + }
  148 +
  149 + /// Adds [Iterable<E>] to [List<E>] only if [condition] is true.
  150 + void addAllIf(dynamic condition, Iterable<E> items) {
  151 + if (condition is Condition) condition = condition();
  152 + if (condition is bool && condition) addAll(items);
  153 + }
  154 +
  155 + /// Replaces all existing items of this list with [item]
  156 + void assign(E item) {
  157 + if (this is RxList) {
  158 + (this as RxList)._value ??= <E>[];
  159 + }
  160 +
  161 + clear();
  162 + add(item);
  163 + }
  164 +
  165 + /// Replaces all existing items of this list with [items]
  166 + void assignAll(Iterable<E> items) {
  167 + if (this is RxList) {
  168 + (this as RxList)._value ??= <E>[];
  169 + }
  170 + clear();
  171 + addAll(items);
169 } 172 }
170 } 173 }
@@ -64,20 +64,6 @@ class RxMap<K, V> extends MapMixin<K, V> @@ -64,20 +64,6 @@ class RxMap<K, V> extends MapMixin<K, V>
64 return _value; 64 return _value;
65 } 65 }
66 66
67 - void assign(K key, V val) {  
68 - _value ??= <K, V>{};  
69 - _value.clear();  
70 - _value[key] = val;  
71 - refresh();  
72 - }  
73 -  
74 - void assignAll(Map<K, V> val) {  
75 - if (_value == val) return;  
76 - _value ??= <K, V>{};  
77 - _value = val;  
78 - refresh();  
79 - }  
80 -  
81 @override 67 @override
82 @protected 68 @protected
83 @Deprecated('Map.value is deprecated. use [yourMap.assignAll(newMap)]') 69 @Deprecated('Map.value is deprecated. use [yourMap.assignAll(newMap)]')
@@ -86,12 +72,17 @@ class RxMap<K, V> extends MapMixin<K, V> @@ -86,12 +72,17 @@ class RxMap<K, V> extends MapMixin<K, V>
86 _value = val; 72 _value = val;
87 refresh(); 73 refresh();
88 } 74 }
  75 +}
  76 +
  77 +extension MapExtension<K, V> on Map<K, V> {
  78 + RxMap<K, V> get obs {
  79 + return RxMap<K, V>(this);
  80 + }
89 81
90 void addIf(dynamic condition, K key, V value) { 82 void addIf(dynamic condition, K key, V value) {
91 if (condition is Condition) condition = condition(); 83 if (condition is Condition) condition = condition();
92 if (condition is bool && condition) { 84 if (condition is bool && condition) {
93 - _value[key] = value;  
94 - refresh(); 85 + this[key] = value;
95 } 86 }
96 } 87 }
97 88
@@ -99,10 +90,29 @@ class RxMap<K, V> extends MapMixin<K, V> @@ -99,10 +90,29 @@ class RxMap<K, V> extends MapMixin<K, V>
99 if (condition is Condition) condition = condition(); 90 if (condition is Condition) condition = condition();
100 if (condition is bool && condition) addAll(values); 91 if (condition is bool && condition) addAll(values);
101 } 92 }
102 -}  
103 93
104 -extension MapExtension<K, V> on Map<K, V> {  
105 - RxMap<K, V> get obs {  
106 - return RxMap<K, V>(this); 94 + void assign(K key, V val) {
  95 + if (this is RxMap) {
  96 + final map = (this as RxMap);
  97 + map._value ??= <K, V>{};
  98 + map._value.clear();
  99 + this[key] = val;
  100 + } else {
  101 + clear();
  102 + this[key] = val;
  103 + }
  104 + }
  105 +
  106 + void assignAll(Map<K, V> val) {
  107 + if (this is RxMap) {
  108 + final map = (this as RxMap);
  109 + if (map._value == val || map == val) return;
  110 + map._value = val;
  111 + map.refresh();
  112 + } else {
  113 + if (this == val) return;
  114 + clear();
  115 + addAll(val);
  116 + }
107 } 117 }
108 } 118 }
@@ -9,18 +9,6 @@ class RxSet<E> extends SetMixin<E> @@ -9,18 +9,6 @@ class RxSet<E> extends SetMixin<E>
9 } 9 }
10 } 10 }
11 11
12 - /// Adds [item] only if [condition] resolves to true.  
13 - void addIf(dynamic condition, E item) {  
14 - if (condition is Condition) condition = condition();  
15 - if (condition is bool && condition) add(item);  
16 - }  
17 -  
18 - /// Adds all [items] only if [condition] resolves to true.  
19 - void addAllIf(dynamic condition, Iterable<E> items) {  
20 - if (condition is Condition) condition = condition();  
21 - if (condition is bool && condition) addAll(items);  
22 - }  
23 -  
24 /// Special override to push() element(s) in a reactive way 12 /// Special override to push() element(s) in a reactive way
25 /// inside the List, 13 /// inside the List,
26 RxSet<E> operator +(Set<E> val) { 14 RxSet<E> operator +(Set<E> val) {
@@ -29,35 +17,11 @@ class RxSet<E> extends SetMixin<E> @@ -29,35 +17,11 @@ class RxSet<E> extends SetMixin<E>
29 return this; 17 return this;
30 } 18 }
31 19
32 - /// Adds only if [item] is not null.  
33 - void addNonNull(E item) {  
34 - if (item != null) add(item);  
35 - }  
36 -  
37 - /// Adds only if [item] is not null.  
38 - void addAllNonNull(Iterable<E> item) {  
39 - if (item != null) addAll(item);  
40 - }  
41 -  
42 - /// Replaces all existing items of this list with [item]  
43 - void assign(E item) {  
44 - _value ??= <E>{};  
45 - clear();  
46 - add(item);  
47 - }  
48 -  
49 void update(void fn(Iterable<E> value)) { 20 void update(void fn(Iterable<E> value)) {
50 fn(value); 21 fn(value);
51 refresh(); 22 refresh();
52 } 23 }
53 24
54 - /// Replaces all existing items of this list with [items]  
55 - void assignAll(Iterable<E> items) {  
56 - _value ??= <E>{};  
57 - clear();  
58 - addAll(items);  
59 - }  
60 -  
61 @override 25 @override
62 @protected 26 @protected
63 Set<E> get value { 27 Set<E> get value {
@@ -151,4 +115,45 @@ extension SetExtension<E> on Set<E> { @@ -151,4 +115,45 @@ extension SetExtension<E> on Set<E> {
151 return RxSet<E>(null); 115 return RxSet<E>(null);
152 } 116 }
153 } 117 }
  118 +
  119 + /// Add [item] to [List<E>] only if [item] is not null.
  120 + void addNonNull(E item) {
  121 + if (item != null) add(item);
  122 + }
  123 +
  124 + /// Add [Iterable<E>] to [List<E>] only if [Iterable<E>] is not null.
  125 + void addAllNonNull(Iterable<E> item) {
  126 + if (item != null) addAll(item);
  127 + }
  128 +
  129 + /// Add [item] to [List<E>] only if [condition] is true.
  130 + void addIf(dynamic condition, E item) {
  131 + if (condition is Condition) condition = condition();
  132 + if (condition is bool && condition) add(item);
  133 + }
  134 +
  135 + /// Adds [Iterable<E>] to [List<E>] only if [condition] is true.
  136 + void addAllIf(dynamic condition, Iterable<E> items) {
  137 + if (condition is Condition) condition = condition();
  138 + if (condition is bool && condition) addAll(items);
  139 + }
  140 +
  141 + /// Replaces all existing items of this list with [item]
  142 + void assign(E item) {
  143 + if (this is RxSet) {
  144 + (this as RxSet)._value ??= <E>{};
  145 + }
  146 +
  147 + clear();
  148 + add(item);
  149 + }
  150 +
  151 + /// Replaces all existing items of this list with [items]
  152 + void assignAll(Iterable<E> items) {
  153 + if (this is RxSet) {
  154 + (this as RxSet)._value ??= <E>{};
  155 + }
  156 + clear();
  157 + addAll(items);
  158 + }
154 } 159 }
@@ -43,6 +43,14 @@ class _InheritedGetxController<T extends GetxController> @@ -43,6 +43,14 @@ class _InheritedGetxController<T extends GetxController>
43 (oldWidget.version != version); 43 (oldWidget.version != version);
44 } 44 }
45 45
  46 +extension WatchEtx on GetxController {
  47 + T watch<T extends GetxController>() {
  48 + final instance = Get.find<T>();
  49 + _GetBuilderState._currentState.watch(instance.update);
  50 + return instance;
  51 + }
  52 +}
  53 +
46 class GetBuilder<T extends GetxController> extends StatefulWidget { 54 class GetBuilder<T extends GetxController> extends StatefulWidget {
47 final GetControllerBuilder<T> builder; 55 final GetControllerBuilder<T> builder;
48 final bool global; 56 final bool global;
@@ -101,9 +109,17 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>> @@ -101,9 +109,17 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>>
101 bool isCreator = false; 109 bool isCreator = false;
102 VoidCallback remove; 110 VoidCallback remove;
103 Object _selector; 111 Object _selector;
  112 + List<VoidCallback> _watchs;
  113 +
  114 + static _GetBuilderState _currentState;
  115 +
  116 + void watch(VoidCallback listener) {
  117 + (_watchs ??= <VoidCallback>[]).add(listener);
  118 + }
104 119
105 @override 120 @override
106 void initState() { 121 void initState() {
  122 + _GetBuilderState._currentState = this;
107 super.initState(); 123 super.initState();
108 widget.initState?.call(this); 124 widget.initState?.call(this);
109 125
@@ -174,6 +190,7 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>> @@ -174,6 +190,7 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>>
174 isCreator = null; 190 isCreator = null;
175 remove = null; 191 remove = null;
176 _selector = null; 192 _selector = null;
  193 + _watchs = null;
177 } 194 }
178 195
179 @override 196 @override