Skip to content

Commit

Permalink
feat: Adds useEquatable for creating observables
Browse files Browse the repository at this point in the history
Make the change in 2.2.3 optional. If you want the use this behavior , modify `@observable` to `@MakeObservable(useEquatable: true)`.
  • Loading branch information
amondnet committed Dec 18, 2023
1 parent 52515a1 commit c6c72a3
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions mobx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 4 additions & 1 deletion mobx/lib/src/api/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions mobx/lib/src/core/atom_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 17 in mobx/lib/src/core/atom_extensions.dart

View check run for this annotation

Codecov / codecov/patch

mobx/lib/src/core/atom_extensions.dart#L17

Added line #L17 was not covered by tests
: newValue == oldValue) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion mobx/lib/version.dart
Original file line number Diff line number Diff line change
@@ -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';
2 changes: 1 addition & 1 deletion mobx/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions mobx_codegen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
4 changes: 3 additions & 1 deletion mobx_codegen/lib/src/template/observable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ObservableTemplate {
this.isReadOnly = false,
this.isPrivate = false,
this.equals,
this.useEquatable,
});

final StoreTemplate storeTemplate;
Expand All @@ -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
Expand Down Expand Up @@ -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' : ''});
}""";
}
2 changes: 1 addition & 1 deletion mobx_codegen/lib/version.dart
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 2 additions & 2 deletions mobx_codegen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down

0 comments on commit c6c72a3

Please sign in to comment.