Skip to content

Commit

Permalink
Fix ObservableMap.cast/retype. (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
matanlurey authored Mar 19, 2018
1 parent 635268e commit 0f6ea75
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.22.0

* Added `ObservableMap.castFrom`, similar to `Map.castFrom`.

* Fixed a bug where `ObservableMap`'s `cast` and `retype` function would create
a new empty instance instead of a forwarding instance.

## 0.21.3

* Support Dart 2 collection methods where previously threw `UnimplementedError`.
Expand Down
37 changes: 35 additions & 2 deletions lib/src/observable_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,39 @@ import 'to_observable.dart';
/// removed, or replaced, then observers that are listening to [changes]
/// will be notified.
class ObservableMap<K, V> extends Observable implements Map<K, V> {
/*
* Adapts [source] to be a `Map<K2, V2>`.
*
* Any time the set would produce a key or value that is not a [K2] or [V2],
* the access will throw.
*
* Any time [K2] key or [V2] value is attempted added into the adapted map,
* the store will throw unless the key is also an instance of [K] and
* the value is also an instance of [V].
*
* If all accessed entries of [source] are have [K2] keys and [V2] values
* and if all entries added to the returned map have [K] keys and [V]] values,
* then the returned map can be used as a `Map<K2, V2>`.
*/

/// Adapts [source] to be a `ObservableMap<K2, V2>`.
///
/// Any time the map would produce a key or value that is not a [K2] or [V2]
/// the access will throw.
///
/// Any time [K2] key or [V2] value is attempted added into the adapted map,
/// the store will throw unless the key is also an instance of [K] and the
/// value is also an instance of [V].
///
/// If all accessed entries of [source] have [K2] keys and [V2] values and if
/// all entries added to the returned map have [K] keys and [V] values, then
/// the returned map can be used as a `Map<K2, V2>`.
static ObservableMap<K2, V2> castFrom<K, V, K2, V2>(
ObservableMap<K, V> source,
) {
return new ObservableMap<K2, V2>.spy(source._map.cast<K2, V2>());
}

final Map<K, V> _map;

/// Creates an observable map.
Expand Down Expand Up @@ -159,12 +192,12 @@ class ObservableMap<K, V> extends Observable implements Map<K, V> {
if (this is ObservableMap<K2, V2>) {
return this as ObservableMap<K2, V2>;
}
return new ObservableMap.createFromType(_map.cast<K2, V2>());
return ObservableMap.castFrom<K, V, K2, V2>(this);
}

@override
ObservableMap<K2, V2> retype<K2, V2>() {
return new ObservableMap.createFromType(_map.retype<K2, V2>());
return ObservableMap.castFrom<K, V, K2, V2>(this);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: observable
version: 0.21.3
version: 0.22.0
author: Dart Team <[email protected]>
description: Support for marking objects as observable
homepage: https://github.com/dart-lang/observable
Expand Down

0 comments on commit 0f6ea75

Please sign in to comment.