| 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 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 T lastWhere(bool test(T value), {T orElse()}) { | 309 T lastWhere(bool test(T value), {T orElse()}) { |
| 310 return _internal.lastWhere(test, orElse: orElse); | 310 return _internal.lastWhere(test, orElse: orElse); |
| 311 } | 311 } |
| 312 T singleWhere(bool test(T value)) { | 312 T singleWhere(bool test(T value)) { |
| 313 return _internal.singleWhere(test); | 313 return _internal.singleWhere(test); |
| 314 } | 314 } |
| 315 T elementAt(int index) { | 315 T elementAt(int index) { |
| 316 return _internal.elementAt(index); | 316 return _internal.elementAt(index); |
| 317 } | 317 } |
| 318 Map<int, T> asMap() { | 318 Map<int, T> asMap() { |
| 319 return IterableMixinWorkaround.asMapList(_internal); | 319 return _internal.asMap(); |
| 320 } | 320 } |
| 321 | 321 |
| 322 bool get isEmpty => length == 0; | 322 bool get isEmpty => length == 0; |
| 323 } | 323 } |
| 324 | 324 |
| 325 // TODO(jmesserly): is this too granular? Other similar systems make whole | 325 // TODO(jmesserly): is this too granular? Other similar systems make whole |
| 326 // classes observable instead of individual fields. The memory cost of having | 326 // classes observable instead of individual fields. The memory cost of having |
| 327 // every field effectively boxed, plus having a listeners list is likely too | 327 // every field effectively boxed, plus having a listeners list is likely too |
| 328 // much. Also, making a value observable necessitates adding ".value" to lots | 328 // much. Also, making a value observable necessitates adding ".value" to lots |
| 329 // of places, and constructing all fields with the verbose | 329 // of places, and constructing all fields with the verbose |
| 330 // "new ObservableValue<DataType>(myValue)". | 330 // "new ObservableValue<DataType>(myValue)". |
| 331 /** A wrapper around a single value whose change can be observed. */ | 331 /** A wrapper around a single value whose change can be observed. */ |
| 332 class ObservableValue<T> extends AbstractObservable { | 332 class ObservableValue<T> extends AbstractObservable { |
| 333 ObservableValue(T value, [Observable parent = null]) | 333 ObservableValue(T value, [Observable parent = null]) |
| 334 : super(parent), _value = value; | 334 : super(parent), _value = value; |
| 335 | 335 |
| 336 T get value => _value; | 336 T get value => _value; |
| 337 | 337 |
| 338 void set value(T newValue) { | 338 void set value(T newValue) { |
| 339 // Only fire on an actual change. | 339 // Only fire on an actual change. |
| 340 if (!identical(newValue, _value)) { | 340 if (!identical(newValue, _value)) { |
| 341 final oldValue = _value; | 341 final oldValue = _value; |
| 342 _value = newValue; | 342 _value = newValue; |
| 343 recordPropertyUpdate("value", newValue, oldValue); | 343 recordPropertyUpdate("value", newValue, oldValue); |
| 344 } | 344 } |
| 345 } | 345 } |
| 346 | 346 |
| 347 T _value; | 347 T _value; |
| 348 } | 348 } |
| OLD | NEW |