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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 /** Unique id to identify this model in an event batch. */ | 44 /** Unique id to identify this model in an event batch. */ |
45 final int uid; | 45 final int uid; |
46 | 46 |
47 /** The parent observable to notify when this child is changed. */ | 47 /** The parent observable to notify when this child is changed. */ |
48 final Observable parent; | 48 final Observable parent; |
49 | 49 |
50 /** Listeners on this model. */ | 50 /** Listeners on this model. */ |
51 List<ChangeListener> listeners; | 51 List<ChangeListener> listeners; |
52 | 52 |
53 /** Whether this object is currently observed by listeners or propagators. */ | 53 /** Whether this object is currently observed by listeners or propagators. */ |
54 bool get isObserved() { | 54 bool get isObserved { |
55 for (Observable obj = this; obj != null; obj = obj.parent) { | 55 for (Observable obj = this; obj != null; obj = obj.parent) { |
56 if (listeners.length > 0) { | 56 if (listeners.length > 0) { |
57 return true; | 57 return true; |
58 } | 58 } |
59 } | 59 } |
60 return false; | 60 return false; |
61 } | 61 } |
62 | 62 |
63 AbstractObservable([Observable this.parent = null]) | 63 AbstractObservable([Observable this.parent = null]) |
64 : uid = EventBatch.genUid(), | 64 : uid = EventBatch.genUid(), |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 ObservableList([Observable parent = null]) | 141 ObservableList([Observable parent = null]) |
142 : super(parent), _internal = new List<T>(); | 142 : super(parent), _internal = new List<T>(); |
143 | 143 |
144 T operator [](int index) => _internal[index]; | 144 T operator [](int index) => _internal[index]; |
145 | 145 |
146 void operator []=(int index, T value) { | 146 void operator []=(int index, T value) { |
147 recordListUpdate(index, value, _internal[index]); | 147 recordListUpdate(index, value, _internal[index]); |
148 _internal[index] = value; | 148 _internal[index] = value; |
149 } | 149 } |
150 | 150 |
151 int get length() => _internal.length; | 151 int get length => _internal.length; |
152 | 152 |
153 void set length(int value) { | 153 void set length(int value) { |
154 _internal.length = value; | 154 _internal.length = value; |
155 recordGlobalChange(); | 155 recordGlobalChange(); |
156 } | 156 } |
157 | 157 |
158 void clear() { | 158 void clear() { |
159 _internal.clear(); | 159 _internal.clear(); |
160 recordGlobalChange(); | 160 recordGlobalChange(); |
161 } | 161 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |