From c6c72a38f19c0769bc4c0ebd723b4b611780bdbd Mon Sep 17 00:00:00 2001
From: Minsu Lee <amond@amond.net>
Date: Mon, 18 Dec 2023 16:49:05 +0900
Subject: [PATCH] feat: Adds `useEquatable` for creating observables

Make the change in 2.2.3 optional. If you want the use this behavior , modify `@observable` to `@MakeObservable(useEquatable: true)`.
---
 mobx/CHANGELOG.md                             | 6 ++++++
 mobx/lib/src/api/annotations.dart             | 5 ++++-
 mobx/lib/src/core/atom_extensions.dart        | 8 +++++---
 mobx/lib/version.dart                         | 2 +-
 mobx/pubspec.yaml                             | 2 +-
 mobx_codegen/CHANGELOG.md                     | 4 ++++
 mobx_codegen/lib/src/template/observable.dart | 4 +++-
 mobx_codegen/lib/version.dart                 | 2 +-
 mobx_codegen/pubspec.yaml                     | 4 ++--
 9 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/mobx/CHANGELOG.md b/mobx/CHANGELOG.md
index 1a2639f0..7d8b26cb 100644
--- a/mobx/CHANGELOG.md
+++ b/mobx/CHANGELOG.md
@@ -1,3 +1,9 @@
+## 2.2.3+1
+
+Make the change in 2.2.3 optional. If you want the use this behavior , modify `@observable` to `@MakeObservable(useEquatable: true)`.
+
+- Adds `useEquatable` for creating observables by [@amondnet](https://github.com/amondnet)
+
 ## 2.2.3
 
 - Avoid unnecessary observable notifications of `@observable` `Iterable` or `Map` fields of Stores by [@amondnet](https://github.com/amondnet)  in [#951](https://github.com/mobxjs/mobx.dart/pull/951)
diff --git a/mobx/lib/src/api/annotations.dart b/mobx/lib/src/api/annotations.dart
index 602ce7a3..f2b27e6a 100644
--- a/mobx/lib/src/api/annotations.dart
+++ b/mobx/lib/src/api/annotations.dart
@@ -25,7 +25,7 @@ class StoreConfig {
 /// String withEquals = 'world';
 /// ```
 class MakeObservable {
-  const MakeObservable({this.readOnly = false, this.equals});
+  const MakeObservable({this.readOnly = false, this.equals, this.useEquatable = true});
 
   final bool readOnly;
   /// A [Function] to use check whether the value of an observable has changed.
@@ -38,6 +38,9 @@ class MakeObservable {
   /// If no function is provided, the default behavior is to only trigger if
   /// : `oldValue != newValue`.
   final Function? equals;
+
+  /// [equatable] is not used by default.
+  final bool useEquatable;
 }
 
 bool observableAlwaysNotEqual(_, __) => false;
diff --git a/mobx/lib/src/core/atom_extensions.dart b/mobx/lib/src/core/atom_extensions.dart
index 3dfb5aee..bcf8de93 100644
--- a/mobx/lib/src/core/atom_extensions.dart
+++ b/mobx/lib/src/core/atom_extensions.dart
@@ -9,11 +9,13 @@ extension AtomSpyReporter on Atom {
   }
 
   void reportWrite<T>(T newValue, T oldValue, void Function() setNewValue,
-      {EqualityComparer<T>? equals}) {
-    final areEqual = equals ?? equatable;
+      {EqualityComparer<T>? equals, bool? useEquatable}) {
+    final areEqual = equals ?? (useEquatable ?? false ? equatable : null);
 
     // Avoid unnecessary observable notifications of @observable fields of Stores
-    if (areEqual(newValue, oldValue)) {
+    if (areEqual != null
+        ? areEqual(newValue, oldValue)
+        : newValue == oldValue) {
       return;
     }
 
diff --git a/mobx/lib/version.dart b/mobx/lib/version.dart
index 2a69223b..d3f0725f 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.2.3';
+const version = '2.2.3+1';
diff --git a/mobx/pubspec.yaml b/mobx/pubspec.yaml
index 4c59a2ea..ea2d898d 100644
--- a/mobx/pubspec.yaml
+++ b/mobx/pubspec.yaml
@@ -1,5 +1,5 @@
 name: mobx
-version: 2.2.3
+version: 2.2.3+1
 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."
 
 homepage: https://github.com/mobxjs/mobx.dart
diff --git a/mobx_codegen/CHANGELOG.md b/mobx_codegen/CHANGELOG.md
index 6f58bd96..721be681 100644
--- a/mobx_codegen/CHANGELOG.md
+++ b/mobx_codegen/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.4.1
+
+- Adds `useEquatable` for creating observables by [@amondnet](https://github.com/amondnet)
+
 ## 2.4.0
 
 - Require `analyzer: ^5.12.0`
diff --git a/mobx_codegen/lib/src/template/observable.dart b/mobx_codegen/lib/src/template/observable.dart
index bb89577d..edaa4461 100644
--- a/mobx_codegen/lib/src/template/observable.dart
+++ b/mobx_codegen/lib/src/template/observable.dart
@@ -12,6 +12,7 @@ class ObservableTemplate {
     this.isReadOnly = false,
     this.isPrivate = false,
     this.equals,
+    this.useEquatable,
   });
 
   final StoreTemplate storeTemplate;
@@ -21,6 +22,7 @@ class ObservableTemplate {
   final bool isPrivate;
   final bool isReadOnly;
   final ExecutableElement? equals;
+  final bool? useEquatable;
 
   /// Formats the `name` from `_foo_bar` to `foo_bar`
   /// such that the getter gets public
@@ -61,6 +63,6 @@ ${_buildGetters()}
   set $name($type value) {
     $atomName.reportWrite(value, super.$name, () {
       super.$name = value;
-    }${equals != null ? ', equals: ${equals!.name}' : ''});
+    }${equals != null ? ', equals: ${equals!.name}' : ''}${useEquatable != null ? ', useEquatable: $useEquatable' : ''});
   }""";
 }
diff --git a/mobx_codegen/lib/version.dart b/mobx_codegen/lib/version.dart
index 5c81252f..d2bed9e4 100644
--- a/mobx_codegen/lib/version.dart
+++ b/mobx_codegen/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.4.0';
+const version = '2.4.1';
diff --git a/mobx_codegen/pubspec.yaml b/mobx_codegen/pubspec.yaml
index 6e988cdd..b14e3cd8 100644
--- a/mobx_codegen/pubspec.yaml
+++ b/mobx_codegen/pubspec.yaml
@@ -1,6 +1,6 @@
 name: mobx_codegen
 description: Code generator for MobX that adds support for annotating your code with @observable, @computed, @action and also creating Store classes.
-version: 2.4.0
+version: 2.4.1
 
 homepage: https://github.com/mobxjs/mobx.dart
 issue_tracker: https://github.com/mobxjs/mobx.dart/issues
@@ -13,7 +13,7 @@ dependencies:
   build: ^2.2.1
   build_resolvers: ^2.0.6
   meta: ^1.3.0
-  mobx: ^2.2.0
+  mobx: ^2.2.3+1
   path: ^1.8.0
   source_gen: ^1.2.1