| 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 part of observe; |
| 6 | 6 |
| 7 // TODO(jmesserly): this needs to be faster. We currently require multiple | 7 // TODO(jmesserly): this needs to be faster. We currently require multiple |
| 8 // lookups per key to get the old value. | 8 // lookups per key to get the old value. |
| 9 // TODO(jmesserly): this doesn't implement the precise interfaces like | 9 // TODO(jmesserly): this doesn't implement the precise interfaces like |
| 10 // LinkedHashMap, SplayTreeMap or HashMap. However it can use them for the | 10 // LinkedHashMap, SplayTreeMap or HashMap. However it can use them for the |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 /** | 60 /** |
| 61 * Creates an observable map that contains all key value pairs of [other]. | 61 * Creates an observable map that contains all key value pairs of [other]. |
| 62 * It will attempt to use the same backing map type if the other map is a | 62 * It will attempt to use the same backing map type if the other map is a |
| 63 * [LinkedHashMap], [SplayTreeMap], or [HashMap]. Otherwise it defaults to | 63 * [LinkedHashMap], [SplayTreeMap], or [HashMap]. Otherwise it defaults to |
| 64 * [HashMap]. | 64 * [HashMap]. |
| 65 * | 65 * |
| 66 * Note this will perform a shallow conversion. If you want a deep conversion | 66 * Note this will perform a shallow conversion. If you want a deep conversion |
| 67 * you should use [toObservable]. | 67 * you should use [toObservable]. |
| 68 */ | 68 */ |
| 69 factory ObservableMap.from(Map<K, V> other) { | 69 factory ObservableMap.from(Map<K, V> other) { |
| 70 var result = new ObservableMap<K, V>._createFromType(other); | 70 return new ObservableMap<K, V>._createFromType(other)..addAll(other); |
| 71 other.forEach((key, value) { result[key] = value; }); | |
| 72 return result; | |
| 73 } | 71 } |
| 74 | 72 |
| 75 factory ObservableMap._createFromType(Map<K, V> other) { | 73 factory ObservableMap._createFromType(Map<K, V> other) { |
| 76 ObservableMap result; | 74 ObservableMap result; |
| 77 if (other is SplayTreeMap) { | 75 if (other is SplayTreeMap) { |
| 78 result = new ObservableMap<K, V>.sorted(); | 76 result = new ObservableMap<K, V>.sorted(); |
| 79 } else if (other is LinkedHashMap) { | 77 } else if (other is LinkedHashMap) { |
| 80 result = new ObservableMap<K, V>.linked(); | 78 result = new ObservableMap<K, V>.linked(); |
| 81 } else { | 79 } else { |
| 82 result = new ObservableMap<K, V>(); | 80 result = new ObservableMap<K, V>(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 107 if (hasObservers) { | 105 if (hasObservers) { |
| 108 if (len != _map.length) { | 106 if (len != _map.length) { |
| 109 notifyPropertyChange(_LENGTH, len, _map.length); | 107 notifyPropertyChange(_LENGTH, len, _map.length); |
| 110 notifyChange(new MapChangeRecord(key, isInsert: true)); | 108 notifyChange(new MapChangeRecord(key, isInsert: true)); |
| 111 } else if (!identical(oldValue, value)) { | 109 } else if (!identical(oldValue, value)) { |
| 112 notifyChange(new MapChangeRecord(key)); | 110 notifyChange(new MapChangeRecord(key)); |
| 113 } | 111 } |
| 114 } | 112 } |
| 115 } | 113 } |
| 116 | 114 |
| 115 void addAll(Map<K, V> other) { |
| 116 other.forEach((K key, V value) { this[key] = value; }); |
| 117 } |
| 118 |
| 117 V putIfAbsent(K key, V ifAbsent()) { | 119 V putIfAbsent(K key, V ifAbsent()) { |
| 118 int len = _map.length; | 120 int len = _map.length; |
| 119 V result = _map.putIfAbsent(key, ifAbsent); | 121 V result = _map.putIfAbsent(key, ifAbsent); |
| 120 if (hasObservers && len != _map.length) { | 122 if (hasObservers && len != _map.length) { |
| 121 notifyPropertyChange(_LENGTH, len, _map.length); | 123 notifyPropertyChange(_LENGTH, len, _map.length); |
| 122 notifyChange(new MapChangeRecord(key, isInsert: true)); | 124 notifyChange(new MapChangeRecord(key, isInsert: true)); |
| 123 } | 125 } |
| 124 return result; | 126 return result; |
| 125 } | 127 } |
| 126 | 128 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 142 }); | 144 }); |
| 143 notifyPropertyChange(_LENGTH, len, 0); | 145 notifyPropertyChange(_LENGTH, len, 0); |
| 144 } | 146 } |
| 145 _map.clear(); | 147 _map.clear(); |
| 146 } | 148 } |
| 147 | 149 |
| 148 void forEach(void f(K key, V value)) => _map.forEach(f); | 150 void forEach(void f(K key, V value)) => _map.forEach(f); |
| 149 | 151 |
| 150 String toString() => Maps.mapToString(this); | 152 String toString() => Maps.mapToString(this); |
| 151 } | 153 } |
| OLD | NEW |