| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library observe.src.observable_map; | 5 library observe.src.observable_map; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'package:observe/observe.dart'; | 8 import 'package:observe/observe.dart'; |
| 9 | 9 |
| 10 | 10 |
| 11 // TODO(jmesserly): this needs to be faster. We currently require multiple | 11 // TODO(jmesserly): this needs to be faster. We currently require multiple |
| 12 // lookups per key to get the old value. | 12 // lookups per key to get the old value. |
| 13 // TODO(jmesserly): this doesn't implement the precise interfaces like | 13 // TODO(jmesserly): this doesn't implement the precise interfaces like |
| 14 // LinkedHashMap, SplayTreeMap or HashMap. However it can use them for the | 14 // LinkedHashMap, SplayTreeMap or HashMap. However it can use them for the |
| 15 // backing store. | 15 // backing store. |
| 16 | 16 |
| 17 // TODO(jmesserly): should we summarize map changes like we do for list changes? | 17 // TODO(jmesserly): should we summarize map changes like we do for list changes? |
| 18 class MapChangeRecord<K, V> extends ChangeRecord { | 18 class MapChangeRecord<K, V> extends ChangeRecord { |
| 19 // TODO(jmesserly): we could store this more compactly if it matters, with | 19 // TODO(jmesserly): we could store this more compactly if it matters, with |
| 20 // subtypes for inserted and removed. | 20 // subtypes for inserted and removed. |
| 21 | 21 |
| 22 /** The map key that changed. */ | 22 /// The map key that changed. |
| 23 final K key; | 23 final K key; |
| 24 | 24 |
| 25 /** The previous value associated with this key. */ | 25 /// The previous value associated with this key. |
| 26 final V oldValue; | 26 final V oldValue; |
| 27 | 27 |
| 28 /** The new value associated with this key. */ | 28 /// The new value associated with this key. |
| 29 final V newValue; | 29 final V newValue; |
| 30 | 30 |
| 31 /** True if this key was inserted. */ | 31 /// True if this key was inserted. |
| 32 final bool isInsert; | 32 final bool isInsert; |
| 33 | 33 |
| 34 /** True if this key was removed. */ | 34 /// True if this key was removed. |
| 35 final bool isRemove; | 35 final bool isRemove; |
| 36 | 36 |
| 37 MapChangeRecord(this.key, this.oldValue, this.newValue) | 37 MapChangeRecord(this.key, this.oldValue, this.newValue) |
| 38 : isInsert = false, isRemove = false; | 38 : isInsert = false, isRemove = false; |
| 39 | 39 |
| 40 MapChangeRecord.insert(this.key, this.newValue) | 40 MapChangeRecord.insert(this.key, this.newValue) |
| 41 : isInsert = true, isRemove = false, oldValue = null; | 41 : isInsert = true, isRemove = false, oldValue = null; |
| 42 | 42 |
| 43 MapChangeRecord.remove(this.key, this.oldValue) | 43 MapChangeRecord.remove(this.key, this.oldValue) |
| 44 : isInsert = false, isRemove = true, newValue = null; | 44 : isInsert = false, isRemove = true, newValue = null; |
| 45 | 45 |
| 46 String toString() { | 46 String toString() { |
| 47 var kind = isInsert ? 'insert' : isRemove ? 'remove' : 'set'; | 47 var kind = isInsert ? 'insert' : isRemove ? 'remove' : 'set'; |
| 48 return '#<MapChangeRecord $kind $key from: $oldValue to: $newValue>'; | 48 return '#<MapChangeRecord $kind $key from: $oldValue to: $newValue>'; |
| 49 } | 49 } |
| 50 } | 50 } |
| 51 | 51 |
| 52 /** | 52 /// Represents an observable map of model values. If any items are added, |
| 53 * Represents an observable map of model values. If any items are added, | 53 /// removed, or replaced, then observers that are listening to [changes] |
| 54 * removed, or replaced, then observers that are listening to [changes] | 54 /// will be notified. |
| 55 * will be notified. | |
| 56 */ | |
| 57 class ObservableMap<K, V> extends ChangeNotifier implements Map<K, V> { | 55 class ObservableMap<K, V> extends ChangeNotifier implements Map<K, V> { |
| 58 final Map<K, V> _map; | 56 final Map<K, V> _map; |
| 59 | 57 |
| 60 /** Creates an observable map. */ | 58 /// Creates an observable map. |
| 61 ObservableMap() : _map = new HashMap<K, V>(); | 59 ObservableMap() : _map = new HashMap<K, V>(); |
| 62 | 60 |
| 63 /** Creates a new observable map using a [LinkedHashMap]. */ | 61 /// Creates a new observable map using a [LinkedHashMap]. |
| 64 ObservableMap.linked() : _map = new LinkedHashMap<K, V>(); | 62 ObservableMap.linked() : _map = new LinkedHashMap<K, V>(); |
| 65 | 63 |
| 66 /** Creates a new observable map using a [SplayTreeMap]. */ | 64 /// Creates a new observable map using a [SplayTreeMap]. |
| 67 ObservableMap.sorted() : _map = new SplayTreeMap<K, V>(); | 65 ObservableMap.sorted() : _map = new SplayTreeMap<K, V>(); |
| 68 | 66 |
| 69 /** | 67 /// Creates an observable map that contains all key value pairs of [other]. |
| 70 * Creates an observable map that contains all key value pairs of [other]. | 68 /// It will attempt to use the same backing map type if the other map is a |
| 71 * It will attempt to use the same backing map type if the other map is a | 69 /// [LinkedHashMap], [SplayTreeMap], or [HashMap]. Otherwise it defaults to |
| 72 * [LinkedHashMap], [SplayTreeMap], or [HashMap]. Otherwise it defaults to | 70 /// [HashMap]. |
| 73 * [HashMap]. | 71 /// |
| 74 * | 72 /// Note this will perform a shallow conversion. If you want a deep conversion |
| 75 * Note this will perform a shallow conversion. If you want a deep conversion | 73 /// you should use [toObservable]. |
| 76 * you should use [toObservable]. | |
| 77 */ | |
| 78 factory ObservableMap.from(Map<K, V> other) { | 74 factory ObservableMap.from(Map<K, V> other) { |
| 79 return new ObservableMap<K, V>.createFromType(other)..addAll(other); | 75 return new ObservableMap<K, V>.createFromType(other)..addAll(other); |
| 80 } | 76 } |
| 81 | 77 |
| 82 /** Like [ObservableMap.from], but creates an empty map. */ | 78 /// Like [ObservableMap.from], but creates an empty map. |
| 83 factory ObservableMap.createFromType(Map<K, V> other) { | 79 factory ObservableMap.createFromType(Map<K, V> other) { |
| 84 ObservableMap result; | 80 ObservableMap result; |
| 85 if (other is SplayTreeMap) { | 81 if (other is SplayTreeMap) { |
| 86 result = new ObservableMap<K, V>.sorted(); | 82 result = new ObservableMap<K, V>.sorted(); |
| 87 } else if (other is LinkedHashMap) { | 83 } else if (other is LinkedHashMap) { |
| 88 result = new ObservableMap<K, V>.linked(); | 84 result = new ObservableMap<K, V>.linked(); |
| 89 } else { | 85 } else { |
| 90 result = new ObservableMap<K, V>(); | 86 result = new ObservableMap<K, V>(); |
| 91 } | 87 } |
| 92 return result; | 88 return result; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 }); | 150 }); |
| 155 notifyPropertyChange(#length, len, 0); | 151 notifyPropertyChange(#length, len, 0); |
| 156 } | 152 } |
| 157 _map.clear(); | 153 _map.clear(); |
| 158 } | 154 } |
| 159 | 155 |
| 160 void forEach(void f(K key, V value)) => _map.forEach(f); | 156 void forEach(void f(K key, V value)) => _map.forEach(f); |
| 161 | 157 |
| 162 String toString() => Maps.mapToString(this); | 158 String toString() => Maps.mapToString(this); |
| 163 } | 159 } |
| OLD | NEW |