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