From 6ac04c7dc8523d94ceeaaf53ac60991137402d29 Mon Sep 17 00:00:00 2001 From: Altynbek Aidarbekov <48729942+altynbek132@users.noreply.github.com> Date: Thu, 28 Mar 2024 12:36:48 +0500 Subject: [PATCH] fix: Observable.value setter (covariant) (#993) * fix(observable): check if new value is an instance of WillChangeNotification unchanged before setting it * chore(observable): Bump version to 2.3.2 and update CHANGELOG.md with bug fix for Observable value setter --- mobx/CHANGELOG.md | 4 ++++ mobx/lib/src/core/observable.dart | 3 ++- mobx/lib/version.dart | 2 +- mobx/pubspec.yaml | 2 +- mobx/test/observable_test.dart | 15 +++++++++++++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/mobx/CHANGELOG.md b/mobx/CHANGELOG.md index c3b694fb3..968ed6cb1 100644 --- a/mobx/CHANGELOG.md +++ b/mobx/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.3.2 + +- Fix Observable.value setter when class with operator ==(covariant) is used as a value + ## 2.3.1 - Fix preserving stacktrace in Computed and Reaction when exception thrown inside argument function diff --git a/mobx/lib/src/core/observable.dart b/mobx/lib/src/core/observable.dart index 70290a094..20ef6e8fc 100644 --- a/mobx/lib/src/core/observable.dart +++ b/mobx/lib/src/core/observable.dart @@ -61,7 +61,8 @@ class Observable extends Atom final oldValue = _value; final newValueDynamic = _prepareNewValue(value); - if (newValueDynamic == WillChangeNotification.unchanged) { + if (newValueDynamic is WillChangeNotification && + newValueDynamic == WillChangeNotification.unchanged) { return; } final newValue = newValueDynamic as T; diff --git a/mobx/lib/version.dart b/mobx/lib/version.dart index 962a8a1cb..529365805 100644 --- a/mobx/lib/version.dart +++ b/mobx/lib/version.dart @@ -1,4 +1,4 @@ // Generated via set_version.dart. !!!DO NOT MODIFY BY HAND!!! /// The current version as per `pubspec.yaml`. -const version = '2.3.1'; +const version = '2.3.2'; diff --git a/mobx/pubspec.yaml b/mobx/pubspec.yaml index 2857b5e3d..974ccca2b 100644 --- a/mobx/pubspec.yaml +++ b/mobx/pubspec.yaml @@ -1,5 +1,5 @@ name: mobx -version: 2.3.1 +version: 2.3.2 description: "MobX is a library for reactively managing the state of your applications. Use the power of observables, actions, and reactions to supercharge your Dart and Flutter apps." repository: https://github.com/mobxjs/mobx.dart diff --git a/mobx/test/observable_test.dart b/mobx/test/observable_test.dart index 8e54f3a3c..4a7a9c3a8 100644 --- a/mobx/test/observable_test.dart +++ b/mobx/test/observable_test.dart @@ -80,6 +80,11 @@ void main() { x.value = 100; expect(x.nonObservableValue, 100); }); + + test('set value covariant', () async { + final x = Observable<_Covariant?>(null); + x.value = _Covariant(); + }); }); group('createAtom()', () { @@ -111,3 +116,13 @@ void main() { }); }); } + +class _Covariant { + @override + // ignore: hash_and_equals + bool operator ==(covariant _Covariant other) { + if (identical(this, other)) return true; + + return other is _Covariant; + } +}