| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 observable; | 5 library observable; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 part 'ChangeEvent.dart'; | 9 part 'ChangeEvent.dart'; |
| 10 part 'EventBatch.dart'; | 10 part 'EventBatch.dart'; |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 } | 286 } |
| 287 T lastMatching(bool test(T value), {T orElse()}) { | 287 T lastMatching(bool test(T value), {T orElse()}) { |
| 288 return _internal.lastMatching(test, orElse: orElse); | 288 return _internal.lastMatching(test, orElse: orElse); |
| 289 } | 289 } |
| 290 T singleMatching(bool test(T value)) { | 290 T singleMatching(bool test(T value)) { |
| 291 return _internal.singleMatching(test); | 291 return _internal.singleMatching(test); |
| 292 } | 292 } |
| 293 T elementAt(int index) { | 293 T elementAt(int index) { |
| 294 return _internal.elementAt(index); | 294 return _internal.elementAt(index); |
| 295 } | 295 } |
| 296 Map<int, T> asMap() { |
| 297 return IterableMixinWorkaround.asMapList(_internal); |
| 298 } |
| 296 | 299 |
| 297 bool get isEmpty => length == 0; | 300 bool get isEmpty => length == 0; |
| 298 } | 301 } |
| 299 | 302 |
| 300 // TODO(jmesserly): is this too granular? Other similar systems make whole | 303 // TODO(jmesserly): is this too granular? Other similar systems make whole |
| 301 // classes observable instead of individual fields. The memory cost of having | 304 // classes observable instead of individual fields. The memory cost of having |
| 302 // every field effectively boxed, plus having a listeners list is likely too | 305 // every field effectively boxed, plus having a listeners list is likely too |
| 303 // much. Also, making a value observable necessitates adding ".value" to lots | 306 // much. Also, making a value observable necessitates adding ".value" to lots |
| 304 // of places, and constructing all fields with the verbose | 307 // of places, and constructing all fields with the verbose |
| 305 // "new ObservableValue<DataType>(myValue)". | 308 // "new ObservableValue<DataType>(myValue)". |
| 306 /** A wrapper around a single value whose change can be observed. */ | 309 /** A wrapper around a single value whose change can be observed. */ |
| 307 class ObservableValue<T> extends AbstractObservable { | 310 class ObservableValue<T> extends AbstractObservable { |
| 308 ObservableValue(T value, [Observable parent = null]) | 311 ObservableValue(T value, [Observable parent = null]) |
| 309 : super(parent), _value = value; | 312 : super(parent), _value = value; |
| 310 | 313 |
| 311 T get value => _value; | 314 T get value => _value; |
| 312 | 315 |
| 313 void set value(T newValue) { | 316 void set value(T newValue) { |
| 314 // Only fire on an actual change. | 317 // Only fire on an actual change. |
| 315 if (!identical(newValue, _value)) { | 318 if (!identical(newValue, _value)) { |
| 316 final oldValue = _value; | 319 final oldValue = _value; |
| 317 _value = newValue; | 320 _value = newValue; |
| 318 recordPropertyUpdate("value", newValue, oldValue); | 321 recordPropertyUpdate("value", newValue, oldValue); |
| 319 } | 322 } |
| 320 } | 323 } |
| 321 | 324 |
| 322 T _value; | 325 T _value; |
| 323 } | 326 } |
| OLD | NEW |