| 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 | |
| 11 // TODO(jmesserly): this needs to be faster. We currently require multiple | 10 // TODO(jmesserly): this needs to be faster. We currently require multiple |
| 12 // lookups per key to get the old value. | 11 // lookups per key to get the old value. |
| 13 // TODO(jmesserly): this doesn't implement the precise interfaces like | 12 // TODO(jmesserly): this doesn't implement the precise interfaces like |
| 14 // LinkedHashMap, SplayTreeMap or HashMap. However it can use them for the | 13 // LinkedHashMap, SplayTreeMap or HashMap. However it can use them for the |
| 15 // backing store. | 14 // backing store. |
| 16 | 15 |
| 17 // TODO(jmesserly): should we summarize map changes like we do for list changes? | 16 // TODO(jmesserly): should we summarize map changes like we do for list changes? |
| 18 class MapChangeRecord<K, V> extends ChangeRecord { | 17 class MapChangeRecord<K, V> extends ChangeRecord { |
| 19 // TODO(jmesserly): we could store this more compactly if it matters, with | 18 // TODO(jmesserly): we could store this more compactly if it matters, with |
| 20 // subtypes for inserted and removed. | 19 // subtypes for inserted and removed. |
| 21 | 20 |
| 22 /// The map key that changed. | 21 /// The map key that changed. |
| 23 final K key; | 22 final K key; |
| 24 | 23 |
| 25 /// The previous value associated with this key. | 24 /// The previous value associated with this key. |
| 26 final V oldValue; | 25 final V oldValue; |
| 27 | 26 |
| 28 /// The new value associated with this key. | 27 /// The new value associated with this key. |
| 29 final V newValue; | 28 final V newValue; |
| 30 | 29 |
| 31 /// True if this key was inserted. | 30 /// True if this key was inserted. |
| 32 final bool isInsert; | 31 final bool isInsert; |
| 33 | 32 |
| 34 /// True if this key was removed. | 33 /// True if this key was removed. |
| 35 final bool isRemove; | 34 final bool isRemove; |
| 36 | 35 |
| 37 MapChangeRecord(this.key, this.oldValue, this.newValue) | 36 MapChangeRecord(this.key, this.oldValue, this.newValue) |
| 38 : isInsert = false, isRemove = false; | 37 : isInsert = false, |
| 38 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, |
| 42 isRemove = false, |
| 43 oldValue = null; |
| 42 | 44 |
| 43 MapChangeRecord.remove(this.key, this.oldValue) | 45 MapChangeRecord.remove(this.key, this.oldValue) |
| 44 : isInsert = false, isRemove = true, newValue = null; | 46 : isInsert = false, |
| 47 isRemove = true, |
| 48 newValue = null; |
| 45 | 49 |
| 46 String toString() { | 50 String toString() { |
| 47 var kind = isInsert ? 'insert' : isRemove ? 'remove' : 'set'; | 51 var kind = isInsert ? 'insert' : isRemove ? 'remove' : 'set'; |
| 48 return '#<MapChangeRecord $kind $key from: $oldValue to: $newValue>'; | 52 return '#<MapChangeRecord $kind $key from: $oldValue to: $newValue>'; |
| 49 } | 53 } |
| 50 } | 54 } |
| 51 | 55 |
| 52 /// Represents an observable map of model values. If any items are added, | 56 /// Represents an observable map of model values. If any items are added, |
| 53 /// removed, or replaced, then observers that are listening to [changes] | 57 /// removed, or replaced, then observers that are listening to [changes] |
| 54 /// will be notified. | 58 /// will be notified. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 70 /// [HashMap]. | 74 /// [HashMap]. |
| 71 /// | 75 /// |
| 72 /// Note this will perform a shallow conversion. If you want a deep conversion | 76 /// Note this will perform a shallow conversion. If you want a deep conversion |
| 73 /// you should use [toObservable]. | 77 /// you should use [toObservable]. |
| 74 factory ObservableMap.from(Map<K, V> other) { | 78 factory ObservableMap.from(Map<K, V> other) { |
| 75 return new ObservableMap<K, V>.createFromType(other)..addAll(other); | 79 return new ObservableMap<K, V>.createFromType(other)..addAll(other); |
| 76 } | 80 } |
| 77 | 81 |
| 78 /// Like [ObservableMap.from], but creates an empty map. | 82 /// Like [ObservableMap.from], but creates an empty map. |
| 79 factory ObservableMap.createFromType(Map<K, V> other) { | 83 factory ObservableMap.createFromType(Map<K, V> other) { |
| 80 ObservableMap result; | 84 ObservableMap<K, V> result; |
| 81 if (other is SplayTreeMap) { | 85 if (other is SplayTreeMap) { |
| 82 result = new ObservableMap<K, V>.sorted(); | 86 result = new ObservableMap<K, V>.sorted(); |
| 83 } else if (other is LinkedHashMap) { | 87 } else if (other is LinkedHashMap) { |
| 84 result = new ObservableMap<K, V>.linked(); | 88 result = new ObservableMap<K, V>.linked(); |
| 85 } else { | 89 } else { |
| 86 result = new ObservableMap<K, V>(); | 90 result = new ObservableMap<K, V>(); |
| 87 } | 91 } |
| 88 return result; | 92 return result; |
| 89 } | 93 } |
| 90 | 94 |
| 91 @reflectable Iterable<K> get keys => _map.keys; | 95 @reflectable Iterable<K> get keys => _map.keys; |
| 92 | 96 |
| 93 @reflectable Iterable<V> get values => _map.values; | 97 @reflectable Iterable<V> get values => _map.values; |
| 94 | 98 |
| 95 @reflectable int get length =>_map.length; | 99 @reflectable int get length => _map.length; |
| 96 | 100 |
| 97 @reflectable bool get isEmpty => length == 0; | 101 @reflectable bool get isEmpty => length == 0; |
| 98 | 102 |
| 99 @reflectable bool get isNotEmpty => !isEmpty; | 103 @reflectable bool get isNotEmpty => !isEmpty; |
| 100 | 104 |
| 101 @reflectable bool containsValue(Object value) => _map.containsValue(value); | 105 @reflectable bool containsValue(Object value) => _map.containsValue(value); |
| 102 | 106 |
| 103 @reflectable bool containsKey(Object key) => _map.containsKey(key); | 107 @reflectable bool containsKey(Object key) => _map.containsKey(key); |
| 104 | 108 |
| 105 @reflectable V operator [](Object key) => _map[key]; | 109 @reflectable V operator [](Object key) => _map[key]; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 119 notifyPropertyChange(#length, len, _map.length); | 123 notifyPropertyChange(#length, len, _map.length); |
| 120 notifyChange(new MapChangeRecord.insert(key, value)); | 124 notifyChange(new MapChangeRecord.insert(key, value)); |
| 121 _notifyKeysValuesChanged(); | 125 _notifyKeysValuesChanged(); |
| 122 } else if (oldValue != value) { | 126 } else if (oldValue != value) { |
| 123 notifyChange(new MapChangeRecord(key, oldValue, value)); | 127 notifyChange(new MapChangeRecord(key, oldValue, value)); |
| 124 _notifyValuesChanged(); | 128 _notifyValuesChanged(); |
| 125 } | 129 } |
| 126 } | 130 } |
| 127 | 131 |
| 128 void addAll(Map<K, V> other) { | 132 void addAll(Map<K, V> other) { |
| 129 other.forEach((K key, V value) { this[key] = value; }); | 133 other.forEach((K key, V value) { |
| 134 this[key] = value; |
| 135 }); |
| 130 } | 136 } |
| 131 | 137 |
| 132 V putIfAbsent(K key, V ifAbsent()) { | 138 V putIfAbsent(K key, V ifAbsent()) { |
| 133 int len = _map.length; | 139 int len = _map.length; |
| 134 V result = _map.putIfAbsent(key, ifAbsent); | 140 V result = _map.putIfAbsent(key, ifAbsent); |
| 135 if (hasObservers && len != _map.length) { | 141 if (hasObservers && len != _map.length) { |
| 136 notifyPropertyChange(#length, len, _map.length); | 142 notifyPropertyChange(#length, len, _map.length); |
| 137 notifyChange(new MapChangeRecord.insert(key, result)); | 143 notifyChange(new MapChangeRecord.insert(key, result)); |
| 138 _notifyKeysValuesChanged(); | 144 _notifyKeysValuesChanged(); |
| 139 } | 145 } |
| 140 return result; | 146 return result; |
| 141 } | 147 } |
| 142 | 148 |
| 143 V remove(Object key) { | 149 V remove(Object key) { |
| 144 int len = _map.length; | 150 int len = _map.length; |
| 145 V result = _map.remove(key); | 151 V result = _map.remove(key); |
| 146 if (hasObservers && len != _map.length) { | 152 if (hasObservers && len != _map.length) { |
| 147 notifyChange(new MapChangeRecord.remove(key, result)); | 153 notifyChange(new MapChangeRecord.remove(key, result)); |
| 148 notifyPropertyChange(#length, len, _map.length); | 154 notifyPropertyChange(#length, len, _map.length); |
| 149 _notifyKeysValuesChanged(); | 155 _notifyKeysValuesChanged(); |
| 150 } | 156 } |
| 151 return result; | 157 return result; |
| 152 } | 158 } |
| 153 | 159 |
| 154 void clear() { | 160 void clear() { |
| 155 int len = _map.length; | 161 int len = _map.length; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 171 // But this should fix "keys" and "values" in templates with minimal overhead. | 177 // But this should fix "keys" and "values" in templates with minimal overhead. |
| 172 void _notifyKeysValuesChanged() { | 178 void _notifyKeysValuesChanged() { |
| 173 notifyChange(new PropertyChangeRecord(this, #keys, null, null)); | 179 notifyChange(new PropertyChangeRecord(this, #keys, null, null)); |
| 174 _notifyValuesChanged(); | 180 _notifyValuesChanged(); |
| 175 } | 181 } |
| 176 | 182 |
| 177 void _notifyValuesChanged() { | 183 void _notifyValuesChanged() { |
| 178 notifyChange(new PropertyChangeRecord(this, #values, null, null)); | 184 notifyChange(new PropertyChangeRecord(this, #values, null, null)); |
| 179 } | 185 } |
| 180 } | 186 } |
| OLD | NEW |