-
-
Notifications
You must be signed in to change notification settings - Fork 316
Allow a custom equals parameter for observable collections #970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amondnet
wants to merge
3
commits into
mobxjs:main
Choose a base branch
from
amondnet:add-equals-observable-collection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,6 +10,24 @@ Atom _observableMapAtom<K, V>(ReactiveContext? context, String? name) { | |||||||||||||||||
/// | ||||||||||||||||||
/// As the name suggests, this is the Observable-counterpart to the standard Dart `Map<K,V>`. | ||||||||||||||||||
/// | ||||||||||||||||||
/// ## Custom Equality for Values | ||||||||||||||||||
/// | ||||||||||||||||||
/// You can provide a custom `equals` parameter to control when values are considered | ||||||||||||||||||
/// equal. This only affects value comparisons - keys are always compared using standard | ||||||||||||||||||
/// equality: | ||||||||||||||||||
/// | ||||||||||||||||||
/// ```dart | ||||||||||||||||||
/// // Only notify changes when person names are different | ||||||||||||||||||
/// final map = ObservableMap<String, Person>( | ||||||||||||||||||
/// equals: (a, b) => a?.name == b?.name | ||||||||||||||||||
/// ); | ||||||||||||||||||
/// | ||||||||||||||||||
/// map['key'] = Person('Alice', 25); | ||||||||||||||||||
/// map['key'] = Person('Alice', 30); // No notification - same name | ||||||||||||||||||
/// ``` | ||||||||||||||||||
/// | ||||||||||||||||||
/// ## Basic Usage | ||||||||||||||||||
/// | ||||||||||||||||||
/// ```dart | ||||||||||||||||||
/// final map = ObservableMap<String, int>.of({'first': 1}); | ||||||||||||||||||
/// | ||||||||||||||||||
|
@@ -26,37 +44,45 @@ class ObservableMap<K, V> | |||||||||||||||||
MapMixin<K, V> | ||||||||||||||||||
implements | ||||||||||||||||||
Listenable<MapChange<K, V>> { | ||||||||||||||||||
ObservableMap({ReactiveContext? context, String? name}) | ||||||||||||||||||
ObservableMap( | ||||||||||||||||||
{ReactiveContext? context, String? name, EqualityComparer<V>? equals}) | ||||||||||||||||||
: _context = context ?? mainContext, | ||||||||||||||||||
_atom = _observableMapAtom<K, V>(context, name), | ||||||||||||||||||
_map = <K, V>{}; | ||||||||||||||||||
_map = <K, V>{}, | ||||||||||||||||||
_equals = equals; | ||||||||||||||||||
|
||||||||||||||||||
ObservableMap.of(Map<K, V> other, {ReactiveContext? context, String? name}) | ||||||||||||||||||
ObservableMap.of(Map<K, V> other, | ||||||||||||||||||
{ReactiveContext? context, String? name, EqualityComparer<V>? equals}) | ||||||||||||||||||
: _context = context ?? mainContext, | ||||||||||||||||||
_atom = _observableMapAtom<K, V>(context, name), | ||||||||||||||||||
_map = Map.of(other); | ||||||||||||||||||
_map = Map.of(other), | ||||||||||||||||||
_equals = equals; | ||||||||||||||||||
|
||||||||||||||||||
ObservableMap.linkedHashMapFrom(Map<K, V> other, | ||||||||||||||||||
{ReactiveContext? context, String? name}) | ||||||||||||||||||
{ReactiveContext? context, String? name, EqualityComparer<V>? equals}) | ||||||||||||||||||
: _context = context ?? mainContext, | ||||||||||||||||||
_atom = _observableMapAtom<K, V>(context, name), | ||||||||||||||||||
_map = LinkedHashMap.from(other); | ||||||||||||||||||
_map = LinkedHashMap.from(other), | ||||||||||||||||||
_equals = equals; | ||||||||||||||||||
|
||||||||||||||||||
ObservableMap.splayTreeMapFrom(Map<K, V> other, | ||||||||||||||||||
{int Function(K, K)? compare, | ||||||||||||||||||
// ignore: avoid_annotating_with_dynamic | ||||||||||||||||||
bool Function(dynamic)? isValidKey, | ||||||||||||||||||
ReactiveContext? context, | ||||||||||||||||||
String? name}) | ||||||||||||||||||
String? name, | ||||||||||||||||||
EqualityComparer<V>? equals}) | ||||||||||||||||||
: _context = context ?? mainContext, | ||||||||||||||||||
_atom = _observableMapAtom<K, V>(context, name), | ||||||||||||||||||
_map = SplayTreeMap.from(other, compare, isValidKey); | ||||||||||||||||||
_map = SplayTreeMap.from(other, compare, isValidKey), | ||||||||||||||||||
_equals = equals; | ||||||||||||||||||
|
||||||||||||||||||
ObservableMap._wrap(this._context, this._map, this._atom); | ||||||||||||||||||
ObservableMap._wrap(this._context, this._map, this._atom, this._equals); | ||||||||||||||||||
|
||||||||||||||||||
final ReactiveContext _context; | ||||||||||||||||||
final Atom _atom; | ||||||||||||||||||
final Map<K, V> _map; | ||||||||||||||||||
final EqualityComparer<V>? _equals; | ||||||||||||||||||
|
||||||||||||||||||
Map<K, V> get nonObservableInner => _map; | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -94,7 +120,7 @@ class ObservableMap<K, V> | |||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if (!_map.containsKey(key) || value != oldValue) { | ||||||||||||||||||
if (!_map.containsKey(key) || !_areEquals(value, oldValue)) { | ||||||||||||||||||
_map[key] = value; | ||||||||||||||||||
if (type == 'update') { | ||||||||||||||||||
_reportUpdate(key, value, oldValue); | ||||||||||||||||||
|
@@ -127,8 +153,15 @@ class ObservableMap<K, V> | |||||||||||||||||
Iterable<K> get keys => MapKeysIterable(_map.keys, _atom); | ||||||||||||||||||
|
||||||||||||||||||
@override | ||||||||||||||||||
Map<RK, RV> cast<RK, RV>() => | ||||||||||||||||||
ObservableMap._wrap(_context, super.cast(), _atom); | ||||||||||||||||||
Map<RK, RV> cast<RK, RV>([EqualityComparer<RV>? equals]) => | ||||||||||||||||||
ObservableMap._wrap( | ||||||||||||||||||
_context, | ||||||||||||||||||
super.cast(), | ||||||||||||||||||
_atom, | ||||||||||||||||||
equals ?? | ||||||||||||||||||
(_equals == null | ||||||||||||||||||
? null | ||||||||||||||||||
: (RV? a, RV? b) => _equals!(a as V?, b as V?))); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsafe casting from RV? to V? without type checking. If RV and V are incompatible types, this will cause a runtime exception. Should add type compatibility checks or use a safer casting approach.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||
|
||||||||||||||||||
@override | ||||||||||||||||||
V? remove(Object? key) { | ||||||||||||||||||
|
@@ -231,13 +264,21 @@ class ObservableMap<K, V> | |||||||||||||||||
} | ||||||||||||||||||
return _listeners.add(listener); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
bool _areEquals(V? a, V? b) { | ||||||||||||||||||
if (_equals != null) { | ||||||||||||||||||
return _equals!(a, b); | ||||||||||||||||||
} else { | ||||||||||||||||||
return equatable(a, b); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/// A convenience method to wrap the standard `Map<K,V>` in an `ObservableMap<K,V>`. | ||||||||||||||||||
/// This is mostly to aid in testing. | ||||||||||||||||||
@visibleForTesting | ||||||||||||||||||
ObservableMap<K, V> wrapInObservableMap<K, V>(Atom atom, Map<K, V> map) => | ||||||||||||||||||
ObservableMap._wrap(mainContext, map, atom); | ||||||||||||||||||
ObservableMap._wrap(mainContext, map, atom, null); | ||||||||||||||||||
|
||||||||||||||||||
typedef MapChangeListener<K, V> = void Function(MapChange<K, V>); | ||||||||||||||||||
|
||||||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsafe casting from R? to T? without type checking. If R and T are incompatible types, this will cause a runtime exception. Should add type compatibility checks or use a safer casting approach.
Copilot uses AI. Check for mistakes.