diff --git a/CHANGELOG.md b/CHANGELOG.md index 88e1ba3..9636dfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/lib/src/observable_map.dart b/lib/src/observable_map.dart index 4f43d13..b5c1f1b 100644 --- a/lib/src/observable_map.dart +++ b/lib/src/observable_map.dart @@ -20,6 +20,39 @@ import 'to_observable.dart'; /// removed, or replaced, then observers that are listening to [changes] /// will be notified. class ObservableMap extends Observable implements Map { + /* + * Adapts [source] to be a `Map`. + * + * 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`. + */ + + /// Adapts [source] to be a `ObservableMap`. + /// + /// 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`. + static ObservableMap castFrom( + ObservableMap source, + ) { + return new ObservableMap.spy(source._map.cast()); + } + final Map _map; /// Creates an observable map. @@ -159,12 +192,12 @@ class ObservableMap extends Observable implements Map { if (this is ObservableMap) { return this as ObservableMap; } - return new ObservableMap.createFromType(_map.cast()); + return ObservableMap.castFrom(this); } @override ObservableMap retype() { - return new ObservableMap.createFromType(_map.retype()); + return ObservableMap.castFrom(this); } @override diff --git a/pubspec.yaml b/pubspec.yaml index 4f1b31d..0a67bfc 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: observable -version: 0.21.3 +version: 0.22.0 author: Dart Team description: Support for marking objects as observable homepage: https://github.com/dart-lang/observable