From c51eb3b406adee736d218c0aedb9e7af41a87b59 Mon Sep 17 00:00:00 2001 From: tylandercasper <5085108+tylandercasper@users.noreply.github.com> Date: Tue, 31 Dec 2024 15:53:37 -0600 Subject: [PATCH] add notifyWhenEqual to ObservableMap --- lib/src/async/observable_map.dart | 33 ++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/src/async/observable_map.dart b/lib/src/async/observable_map.dart index 95f7b44..7e0be98 100644 --- a/lib/src/async/observable_map.dart +++ b/lib/src/async/observable_map.dart @@ -37,19 +37,24 @@ class ObservableMap static ObservableMap castFrom( ObservableMap source, ) { - return ObservableMap.spy(source._map.cast()); + return ObservableMap.spy(source._map.cast(), + notifyWhenEqual: source.notifyWhenEqual); } final Map _map; + /// Whether to notify when an index is set to the same value. + final bool notifyWhenEqual; + /// Creates an observable map. - ObservableMap() : _map = HashMap(); + ObservableMap({this.notifyWhenEqual = false}) : _map = HashMap(); /// Creates a new observable map using a [LinkedHashMap]. - ObservableMap.linked() : _map = {}; + ObservableMap.linked({this.notifyWhenEqual = false}) : _map = {}; /// Creates a new observable map using a [SplayTreeMap]. - ObservableMap.sorted() : _map = SplayTreeMap(); + ObservableMap.sorted({this.notifyWhenEqual = false}) + : _map = SplayTreeMap(); /// Creates an observable map that contains all key value pairs of [other]. /// It will attempt to use the same backing map type if the other map is a @@ -58,25 +63,29 @@ class ObservableMap /// /// Note this will perform a shallow conversion. If you want a deep conversion /// you should use [toObservable]. - factory ObservableMap.from(Map other) { - return ObservableMap.createFromType(other)..addAll(other); + factory ObservableMap.from(Map other, {bool notifyWhenEqual = false}) { + return ObservableMap.createFromType(other, + notifyWhenEqual: notifyWhenEqual) + ..addAll(other); } /// Like [ObservableMap.from], but creates an empty map. - factory ObservableMap.createFromType(Map other) { + factory ObservableMap.createFromType(Map other, + {bool notifyWhenEqual = false}) { ObservableMap result; if (other is SplayTreeMap) { - result = ObservableMap.sorted(); + result = ObservableMap.sorted(notifyWhenEqual: notifyWhenEqual); } else if (other is LinkedHashMap) { - result = ObservableMap.linked(); + result = ObservableMap.linked(notifyWhenEqual: notifyWhenEqual); } else { - result = ObservableMap(); + result = ObservableMap(notifyWhenEqual: notifyWhenEqual); } return result; } /// Creates a new observable map wrapping [other]. - ObservableMap.spy(Map other) : _map = other; + ObservableMap.spy(Map other, {this.notifyWhenEqual = false}) + : _map = other; @override Iterable get keys => _map.keys; @@ -118,7 +127,7 @@ class ObservableMap notifyPropertyChange(#length, len, _map.length); notifyChange(MapChangeRecord.insert(key, value)); _notifyKeysValuesChanged(); - } else if (oldValue != value) { + } else if (notifyWhenEqual || oldValue != value) { notifyChange(MapChangeRecord(key, oldValue, value)); _notifyValuesChanged(); }