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