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:coreimpl'); | 7 #import('dart:coreimpl'); |
8 | 8 |
9 #source('ChangeEvent.dart'); | 9 #source('ChangeEvent.dart'); |
10 #source('EventBatch.dart'); | 10 #source('EventBatch.dart'); |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 | 260 |
261 // Iterable<T>: | 261 // Iterable<T>: |
262 Iterator<T> iterator() => _internal.iterator(); | 262 Iterator<T> iterator() => _internal.iterator(); |
263 | 263 |
264 // Collection<T>: | 264 // Collection<T>: |
265 Collection<T> filter(bool f(T element)) => _internal.filter(f); | 265 Collection<T> filter(bool f(T element)) => _internal.filter(f); |
266 Collection map(f(T element)) => _internal.map(f); | 266 Collection map(f(T element)) => _internal.map(f); |
267 bool every(bool f(T element)) => _internal.every(f); | 267 bool every(bool f(T element)) => _internal.every(f); |
268 bool some(bool f(T element)) => _internal.some(f); | 268 bool some(bool f(T element)) => _internal.some(f); |
269 void forEach(void f(T element)) { _internal.forEach(f); } | 269 void forEach(void f(T element)) { _internal.forEach(f); } |
270 bool isEmpty() => length == 0; | 270 bool get isEmpty => length == 0; |
271 } | 271 } |
272 | 272 |
273 // TODO(jmesserly): is this too granular? Other similar systems make whole | 273 // TODO(jmesserly): is this too granular? Other similar systems make whole |
274 // classes observable instead of individual fields. The memory cost of having | 274 // classes observable instead of individual fields. The memory cost of having |
275 // every field effectively boxed, plus having a listeners list is likely too | 275 // every field effectively boxed, plus having a listeners list is likely too |
276 // much. Also, making a value observable necessitates adding ".value" to lots | 276 // much. Also, making a value observable necessitates adding ".value" to lots |
277 // of places, and constructing all fields with the verbose | 277 // of places, and constructing all fields with the verbose |
278 // "new ObservableValue<DataType>(myValue)". | 278 // "new ObservableValue<DataType>(myValue)". |
279 /** A wrapper around a single value whose change can be observed. */ | 279 /** A wrapper around a single value whose change can be observed. */ |
280 class ObservableValue<T> extends AbstractObservable { | 280 class ObservableValue<T> extends AbstractObservable { |
281 ObservableValue(T value, [Observable parent = null]) | 281 ObservableValue(T value, [Observable parent = null]) |
282 : super(parent), _value = value; | 282 : super(parent), _value = value; |
283 | 283 |
284 T get value => _value; | 284 T get value => _value; |
285 | 285 |
286 void set value(T newValue) { | 286 void set value(T newValue) { |
287 // Only fire on an actual change. | 287 // Only fire on an actual change. |
288 // TODO(terry): An object identity test === is needed. Each DataSource has | 288 // TODO(terry): An object identity test === is needed. Each DataSource has |
289 // its own operator == which does a value compare. Which | 289 // its own operator == which does a value compare. Which |
290 // equality check should be done? | 290 // equality check should be done? |
291 if (newValue !== _value) { | 291 if (newValue !== _value) { |
292 final oldValue = _value; | 292 final oldValue = _value; |
293 _value = newValue; | 293 _value = newValue; |
294 recordPropertyUpdate("value", newValue, oldValue); | 294 recordPropertyUpdate("value", newValue, oldValue); |
295 } | 295 } |
296 } | 296 } |
297 | 297 |
298 T _value; | 298 T _value; |
299 } | 299 } |
OLD | NEW |