@@ -25,20 +25,23 @@ class ObservableList<T>
2525 ListMixin <T >
2626 implements
2727 Listenable <ListChange <T >> {
28- ObservableList ({ReactiveContext ? context, String ? name})
29- : this ._wrap (context, _observableListAtom <T >(context, name), []);
28+ ObservableList (
29+ {ReactiveContext ? context, String ? name, EqualityComparer <T >? equals})
30+ : this ._wrap (context, _observableListAtom <T >(context, name), [], equals);
3031
3132 ObservableList .of (Iterable <T > elements,
32- {ReactiveContext ? context, String ? name})
33+ {ReactiveContext ? context, String ? name, EqualityComparer < T > ? equals })
3334 : this ._wrap (context, _observableListAtom <T >(context, name),
34- List <T >.of (elements, growable: true ));
35+ List <T >.of (elements, growable: true ), equals );
3536
36- ObservableList ._wrap (ReactiveContext ? context, this ._atom, this ._list)
37+ ObservableList ._wrap (
38+ ReactiveContext ? context, this ._atom, this ._list, this ._equals)
3739 : _context = context ?? mainContext;
3840
3941 final ReactiveContext _context;
4042 final Atom _atom;
4143 final List <T > _list;
44+ final EqualityComparer <T >? _equals;
4245
4346 List <T > get nonObservableInner => _list;
4447
@@ -96,7 +99,7 @@ class ObservableList<T>
9699 _context.conditionallyRunInAction (() {
97100 final oldValue = _list[index];
98101
99- if (oldValue != value) {
102+ if (! _areEquals ( oldValue, value) ) {
100103 _list[index] = value;
101104 _notifyElementUpdate (index, value, oldValue);
102105 }
@@ -167,10 +170,18 @@ class ObservableList<T>
167170 }
168171
169172 @override
170- Map <int , T > asMap () => ObservableMap ._wrap (_context, _list.asMap (), _atom);
173+ Map <int , T > asMap () =>
174+ ObservableMap ._wrap (_context, _list.asMap (), _atom, _equals);
171175
172176 @override
173- List <R > cast <R >() => ObservableList ._wrap (_context, _atom, _list.cast <R >());
177+ List <R > cast <R >([EqualityComparer <R >? equals]) => ObservableList ._wrap (
178+ _context,
179+ _atom,
180+ _list.cast <R >(),
181+ equals ??
182+ (_equals != null
183+ ? (R ? a, R ? b) => _equals !(a as T ? , b as T ? )
184+ : null ));
174185
175186 @override
176187 List <T > toList ({bool growable = true }) {
@@ -184,7 +195,7 @@ class ObservableList<T>
184195 set first (T value) {
185196 _context.conditionallyRunInAction (() {
186197 final oldValue = _list.first;
187- if (oldValue != value) {
198+ if (! _areEquals ( oldValue, value) ) {
188199 _list.first = value;
189200 _notifyElementUpdate (0 , value, oldValue);
190201 }
@@ -376,7 +387,7 @@ class ObservableList<T>
376387 for (var i = 0 ; i < _list.length; ++ i) {
377388 final oldValue = oldList[i];
378389 final newValue = _list[i];
379- if (newValue != oldValue) {
390+ if (! _areEquals ( oldValue, newValue) ) {
380391 changes.add (ElementChange (
381392 index: i, oldValue: oldValue, newValue: newValue));
382393 }
@@ -398,7 +409,7 @@ class ObservableList<T>
398409 for (var i = 0 ; i < _list.length; ++ i) {
399410 final oldValue = oldList[i];
400411 final newValue = _list[i];
401- if (newValue != oldValue) {
412+ if (! _areEquals ( oldValue, newValue) ) {
402413 changes.add (ElementChange (
403414 index: i, oldValue: oldValue, newValue: newValue));
404415 }
@@ -456,6 +467,14 @@ class ObservableList<T>
456467
457468 _listeners.notifyListeners (change);
458469 }
470+
471+ bool _areEquals (T ? a, T ? b) {
472+ if (_equals != null ) {
473+ return _equals !(a, b);
474+ } else {
475+ return equatable (a, b);
476+ }
477+ }
459478}
460479
461480typedef ListChangeListener <TNotification > = void Function (
@@ -520,4 +539,4 @@ class ListChange<T> {
520539/// Used during testing for wrapping a regular `List<T>` as an `ObservableList<T>`
521540@visibleForTesting
522541ObservableList <T > wrapInObservableList <T >(Atom atom, List <T > list) =>
523- ObservableList ._wrap (mainContext, atom, list);
542+ ObservableList ._wrap (mainContext, atom, list, null );
0 commit comments