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 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 111 } |
112 | 112 |
113 int len = _map.length; | 113 int len = _map.length; |
114 V oldValue = _map[key]; | 114 V oldValue = _map[key]; |
115 | 115 |
116 _map[key] = value; | 116 _map[key] = value; |
117 | 117 |
118 if (len != _map.length) { | 118 if (len != _map.length) { |
119 notifyPropertyChange(#length, len, _map.length); | 119 notifyPropertyChange(#length, len, _map.length); |
120 notifyChange(new MapChangeRecord.insert(key, value)); | 120 notifyChange(new MapChangeRecord.insert(key, value)); |
| 121 _notifyKeysValuesChanged(); |
121 } else if (oldValue != value) { | 122 } else if (oldValue != value) { |
122 notifyChange(new MapChangeRecord(key, oldValue, value)); | 123 notifyChange(new MapChangeRecord(key, oldValue, value)); |
| 124 _notifyValuesChanged(); |
123 } | 125 } |
124 } | 126 } |
125 | 127 |
126 void addAll(Map<K, V> other) { | 128 void addAll(Map<K, V> other) { |
127 other.forEach((K key, V value) { this[key] = value; }); | 129 other.forEach((K key, V value) { this[key] = value; }); |
128 } | 130 } |
129 | 131 |
130 V putIfAbsent(K key, V ifAbsent()) { | 132 V putIfAbsent(K key, V ifAbsent()) { |
131 int len = _map.length; | 133 int len = _map.length; |
132 V result = _map.putIfAbsent(key, ifAbsent); | 134 V result = _map.putIfAbsent(key, ifAbsent); |
133 if (hasObservers && len != _map.length) { | 135 if (hasObservers && len != _map.length) { |
134 notifyPropertyChange(#length, len, _map.length); | 136 notifyPropertyChange(#length, len, _map.length); |
135 notifyChange(new MapChangeRecord.insert(key, result)); | 137 notifyChange(new MapChangeRecord.insert(key, result)); |
| 138 _notifyKeysValuesChanged(); |
136 } | 139 } |
137 return result; | 140 return result; |
138 } | 141 } |
139 | 142 |
140 V remove(Object key) { | 143 V remove(Object key) { |
141 int len = _map.length; | 144 int len = _map.length; |
142 V result = _map.remove(key); | 145 V result = _map.remove(key); |
143 if (hasObservers && len != _map.length) { | 146 if (hasObservers && len != _map.length) { |
144 notifyChange(new MapChangeRecord.remove(key, result)); | 147 notifyChange(new MapChangeRecord.remove(key, result)); |
145 notifyPropertyChange(#length, len, _map.length); | 148 notifyPropertyChange(#length, len, _map.length); |
| 149 _notifyKeysValuesChanged(); |
146 } | 150 } |
147 return result; | 151 return result; |
148 } | 152 } |
149 | 153 |
150 void clear() { | 154 void clear() { |
151 int len = _map.length; | 155 int len = _map.length; |
152 if (hasObservers && len > 0) { | 156 if (hasObservers && len > 0) { |
153 _map.forEach((key, value) { | 157 _map.forEach((key, value) { |
154 notifyChange(new MapChangeRecord.remove(key, value)); | 158 notifyChange(new MapChangeRecord.remove(key, value)); |
155 }); | 159 }); |
156 notifyPropertyChange(#length, len, 0); | 160 notifyPropertyChange(#length, len, 0); |
| 161 _notifyKeysValuesChanged(); |
157 } | 162 } |
158 _map.clear(); | 163 _map.clear(); |
159 } | 164 } |
160 | 165 |
161 void forEach(void f(K key, V value)) => _map.forEach(f); | 166 void forEach(void f(K key, V value)) => _map.forEach(f); |
162 | 167 |
163 String toString() => Maps.mapToString(this); | 168 String toString() => Maps.mapToString(this); |
| 169 |
| 170 // Note: we don't really have a reasonable old/new value to use here. |
| 171 // But this should fix "keys" and "values" in templates with minimal overhead. |
| 172 void _notifyKeysValuesChanged() { |
| 173 notifyChange(new PropertyChangeRecord(this, #keys, null, null)); |
| 174 _notifyValuesChanged(); |
| 175 } |
| 176 |
| 177 void _notifyValuesChanged() { |
| 178 notifyChange(new PropertyChangeRecord(this, #values, null, null)); |
| 179 } |
164 } | 180 } |
OLD | NEW |