Jonatas

improve iterable observable api

... ... @@ -32,6 +32,10 @@ extension Inst on GetInterface {
GetInstance().lazyPut<S>(builder, tag: tag, fenix: fenix);
}
void printInstanceStack() {
GetInstance().printInstanceStack();
}
/// async version of [Get.put()].
/// Awaits for the resolution of the Future from [builder()] parameter and
/// stores the Instance returned.
... ...
... ... @@ -85,6 +85,10 @@ class GetInstance {
);
}
void printInstanceStack() {
Get.log(_routesKey.toString());
}
void injector<S>(
InjectorBuilderCallback<S> fn, {
String tag,
... ...
... ... @@ -74,45 +74,9 @@ class RxList<E> extends ListMixin<E>
refresh();
}
/// Add [item] to [List<E>] only if [item] is not null.
void addNonNull(E item) {
if (item != null) add(item);
}
/// Add [Iterable<E>] to [List<E>] only if [Iterable<E>] is not null.
void addAllNonNull(Iterable<E> item) {
if (item != null) addAll(item);
}
/// Add [item] to [List<E>] only if [condition] is true.
void addIf(dynamic condition, E item) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) add(item);
}
/// Adds [Iterable<E>] to [List<E>] only if [condition] is true.
void addAllIf(dynamic condition, Iterable<E> items) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) addAll(items);
}
@override
int get length => value.length;
/// Replaces all existing items of this list with [item]
void assign(E item) {
_value ??= <E>[];
clear();
add(item);
}
/// Replaces all existing items of this list with [items]
void assignAll(Iterable<E> items) {
_value ??= <E>[];
clear();
addAll(items);
}
@override
@protected
List<E> get value {
... ... @@ -164,7 +128,46 @@ class RxList<E> extends ListMixin<E>
}
extension ListExtension<E> on List<E> {
RxList<E> get obs {
return RxList<E>(this);
RxList<E> get obs => RxList<E>(this);
/// Add [item] to [List<E>] only if [item] is not null.
void addNonNull(E item) {
if (item != null) add(item);
}
/// Add [Iterable<E>] to [List<E>] only if [Iterable<E>] is not null.
void addAllNonNull(Iterable<E> item) {
if (item != null) addAll(item);
}
/// Add [item] to [List<E>] only if [condition] is true.
void addIf(dynamic condition, E item) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) add(item);
}
/// Adds [Iterable<E>] to [List<E>] only if [condition] is true.
void addAllIf(dynamic condition, Iterable<E> items) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) addAll(items);
}
/// Replaces all existing items of this list with [item]
void assign(E item) {
if (this is RxList) {
(this as RxList)._value ??= <E>[];
}
clear();
add(item);
}
/// Replaces all existing items of this list with [items]
void assignAll(Iterable<E> items) {
if (this is RxList) {
(this as RxList)._value ??= <E>[];
}
clear();
addAll(items);
}
}
... ...
... ... @@ -64,20 +64,6 @@ class RxMap<K, V> extends MapMixin<K, V>
return _value;
}
void assign(K key, V val) {
_value ??= <K, V>{};
_value.clear();
_value[key] = val;
refresh();
}
void assignAll(Map<K, V> val) {
if (_value == val) return;
_value ??= <K, V>{};
_value = val;
refresh();
}
@override
@protected
@Deprecated('Map.value is deprecated. use [yourMap.assignAll(newMap)]')
... ... @@ -86,12 +72,17 @@ class RxMap<K, V> extends MapMixin<K, V>
_value = val;
refresh();
}
}
extension MapExtension<K, V> on Map<K, V> {
RxMap<K, V> get obs {
return RxMap<K, V>(this);
}
void addIf(dynamic condition, K key, V value) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) {
_value[key] = value;
refresh();
this[key] = value;
}
}
... ... @@ -99,10 +90,29 @@ class RxMap<K, V> extends MapMixin<K, V>
if (condition is Condition) condition = condition();
if (condition is bool && condition) addAll(values);
}
}
extension MapExtension<K, V> on Map<K, V> {
RxMap<K, V> get obs {
return RxMap<K, V>(this);
void assign(K key, V val) {
if (this is RxMap) {
final map = (this as RxMap);
map._value ??= <K, V>{};
map._value.clear();
this[key] = val;
} else {
clear();
this[key] = val;
}
}
void assignAll(Map<K, V> val) {
if (this is RxMap) {
final map = (this as RxMap);
if (map._value == val || map == val) return;
map._value = val;
map.refresh();
} else {
if (this == val) return;
clear();
addAll(val);
}
}
}
... ...
... ... @@ -9,18 +9,6 @@ class RxSet<E> extends SetMixin<E>
}
}
/// Adds [item] only if [condition] resolves to true.
void addIf(dynamic condition, E item) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) add(item);
}
/// Adds all [items] only if [condition] resolves to true.
void addAllIf(dynamic condition, Iterable<E> items) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) addAll(items);
}
/// Special override to push() element(s) in a reactive way
/// inside the List,
RxSet<E> operator +(Set<E> val) {
... ... @@ -29,35 +17,11 @@ class RxSet<E> extends SetMixin<E>
return this;
}
/// Adds only if [item] is not null.
void addNonNull(E item) {
if (item != null) add(item);
}
/// Adds only if [item] is not null.
void addAllNonNull(Iterable<E> item) {
if (item != null) addAll(item);
}
/// Replaces all existing items of this list with [item]
void assign(E item) {
_value ??= <E>{};
clear();
add(item);
}
void update(void fn(Iterable<E> value)) {
fn(value);
refresh();
}
/// Replaces all existing items of this list with [items]
void assignAll(Iterable<E> items) {
_value ??= <E>{};
clear();
addAll(items);
}
@override
@protected
Set<E> get value {
... ... @@ -151,4 +115,45 @@ extension SetExtension<E> on Set<E> {
return RxSet<E>(null);
}
}
/// Add [item] to [List<E>] only if [item] is not null.
void addNonNull(E item) {
if (item != null) add(item);
}
/// Add [Iterable<E>] to [List<E>] only if [Iterable<E>] is not null.
void addAllNonNull(Iterable<E> item) {
if (item != null) addAll(item);
}
/// Add [item] to [List<E>] only if [condition] is true.
void addIf(dynamic condition, E item) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) add(item);
}
/// Adds [Iterable<E>] to [List<E>] only if [condition] is true.
void addAllIf(dynamic condition, Iterable<E> items) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) addAll(items);
}
/// Replaces all existing items of this list with [item]
void assign(E item) {
if (this is RxSet) {
(this as RxSet)._value ??= <E>{};
}
clear();
add(item);
}
/// Replaces all existing items of this list with [items]
void assignAll(Iterable<E> items) {
if (this is RxSet) {
(this as RxSet)._value ??= <E>{};
}
clear();
addAll(items);
}
}
... ...
... ... @@ -43,6 +43,14 @@ class _InheritedGetxController<T extends GetxController>
(oldWidget.version != version);
}
extension WatchEtx on GetxController {
T watch<T extends GetxController>() {
final instance = Get.find<T>();
_GetBuilderState._currentState.watch(instance.update);
return instance;
}
}
class GetBuilder<T extends GetxController> extends StatefulWidget {
final GetControllerBuilder<T> builder;
final bool global;
... ... @@ -101,9 +109,17 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>>
bool isCreator = false;
VoidCallback remove;
Object _selector;
List<VoidCallback> _watchs;
static _GetBuilderState _currentState;
void watch(VoidCallback listener) {
(_watchs ??= <VoidCallback>[]).add(listener);
}
@override
void initState() {
_GetBuilderState._currentState = this;
super.initState();
widget.initState?.call(this);
... ... @@ -174,6 +190,7 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>>
isCreator = null;
remove = null;
_selector = null;
_watchs = null;
}
@override
... ...