| 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 part of observe; | 5 library observe.src.observable_map; |
| 6 |
| 7 import 'dart:collection'; |
| 8 import 'package:observe/observe.dart'; |
| 9 |
| 6 | 10 |
| 7 // TODO(jmesserly): this needs to be faster. We currently require multiple | 11 // TODO(jmesserly): this needs to be faster. We currently require multiple |
| 8 // lookups per key to get the old value. | 12 // lookups per key to get the old value. |
| 9 // TODO(jmesserly): this doesn't implement the precise interfaces like | 13 // TODO(jmesserly): this doesn't implement the precise interfaces like |
| 10 // LinkedHashMap, SplayTreeMap or HashMap. However it can use them for the | 14 // LinkedHashMap, SplayTreeMap or HashMap. However it can use them for the |
| 11 // backing store. | 15 // backing store. |
| 12 | 16 |
| 13 // 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? |
| 14 class MapChangeRecord<K, V> extends ChangeRecord { | 18 class MapChangeRecord<K, V> extends ChangeRecord { |
| 15 // 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 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 27 /** True if this key was inserted. */ | 31 /** True if this key was inserted. */ |
| 28 final bool isInsert; | 32 final bool isInsert; |
| 29 | 33 |
| 30 /** True if this key was removed. */ | 34 /** True if this key was removed. */ |
| 31 final bool isRemove; | 35 final bool isRemove; |
| 32 | 36 |
| 33 MapChangeRecord(this.key, this.oldValue, this.newValue) | 37 MapChangeRecord(this.key, this.oldValue, this.newValue) |
| 34 : isInsert = false, isRemove = false; | 38 : isInsert = false, isRemove = false; |
| 35 | 39 |
| 36 MapChangeRecord.insert(this.key, this.newValue) | 40 MapChangeRecord.insert(this.key, this.newValue) |
| 37 : isInsert = true, isRemove = false; | 41 : isInsert = true, isRemove = false, oldValue = null; |
| 38 | 42 |
| 39 MapChangeRecord.remove(this.key, this.oldValue) | 43 MapChangeRecord.remove(this.key, this.oldValue) |
| 40 : isInsert = false, isRemove = true; | 44 : isInsert = false, isRemove = true, newValue = null; |
| 41 | |
| 42 /// *Deprecated* compare [key]s instead. | |
| 43 @deprecated | |
| 44 bool changes(otherKey) => key == otherKey; | |
| 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 /** |
| 53 * 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, |
| 54 * removed, or replaced, then observers that are listening to [changes] | 54 * removed, or replaced, then observers that are listening to [changes] |
| (...skipping 14 matching lines...) Expand all Loading... |
| 69 /** | 69 /** |
| 70 * 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]. |
| 71 * 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 |
| 72 * [LinkedHashMap], [SplayTreeMap], or [HashMap]. Otherwise it defaults to | 72 * [LinkedHashMap], [SplayTreeMap], or [HashMap]. Otherwise it defaults to |
| 73 * [HashMap]. | 73 * [HashMap]. |
| 74 * | 74 * |
| 75 * 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 |
| 76 * you should use [toObservable]. | 76 * you should use [toObservable]. |
| 77 */ | 77 */ |
| 78 factory ObservableMap.from(Map<K, V> other) { | 78 factory ObservableMap.from(Map<K, V> other) { |
| 79 return new ObservableMap<K, V>._createFromType(other)..addAll(other); | 79 return new ObservableMap<K, V>.createFromType(other)..addAll(other); |
| 80 } | 80 } |
| 81 | 81 |
| 82 factory ObservableMap._createFromType(Map<K, V> other) { | 82 /** Like [ObservableMap.from], but creates an empty map. */ |
| 83 factory ObservableMap.createFromType(Map<K, V> other) { |
| 83 ObservableMap result; | 84 ObservableMap result; |
| 84 if (other is SplayTreeMap) { | 85 if (other is SplayTreeMap) { |
| 85 result = new ObservableMap<K, V>.sorted(); | 86 result = new ObservableMap<K, V>.sorted(); |
| 86 } else if (other is LinkedHashMap) { | 87 } else if (other is LinkedHashMap) { |
| 87 result = new ObservableMap<K, V>.linked(); | 88 result = new ObservableMap<K, V>.linked(); |
| 88 } else { | 89 } else { |
| 89 result = new ObservableMap<K, V>(); | 90 result = new ObservableMap<K, V>(); |
| 90 } | 91 } |
| 91 return result; | 92 return result; |
| 92 } | 93 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 }); | 154 }); |
| 154 notifyPropertyChange(#length, len, 0); | 155 notifyPropertyChange(#length, len, 0); |
| 155 } | 156 } |
| 156 _map.clear(); | 157 _map.clear(); |
| 157 } | 158 } |
| 158 | 159 |
| 159 void forEach(void f(K key, V value)) => _map.forEach(f); | 160 void forEach(void f(K key, V value)) => _map.forEach(f); |
| 160 | 161 |
| 161 String toString() => Maps.mapToString(this); | 162 String toString() => Maps.mapToString(this); |
| 162 } | 163 } |
| OLD | NEW |