OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of observe; | 5 part of observe; |
6 | 6 |
7 /** | 7 /** |
8 * Use `@observable` to make a field automatically observable. | 8 * Use `@observable` to make a field automatically observable. |
9 */ | 9 */ |
10 const Object observable = const _ObservableAnnotation(); | 10 const ObservableProperty observable = const ObservableProperty(); |
11 | 11 |
12 /** | 12 /** |
13 * Interface representing an observable object. This is used by data in | 13 * Interface representing an observable object. This is used by data in |
14 * model-view architectures to notify interested parties of [changes]. | 14 * model-view architectures to notify interested parties of [changes]. |
15 * | 15 * |
16 * This object does not require any specific technique to implement | 16 * This object does not require any specific technique to implement |
17 * observability. If you mixin [ObservableMixin], [dirtyCheck] will know to | 17 * observability. If you mixin [ObservableMixin], [dirtyCheck] will know to |
18 * check for changes on the object. You may also implement change notification | 18 * check for changes on the object. You may also implement change notification |
19 * yourself, by calling [notifyChange]. | 19 * yourself, by calling [notifyChange]. |
20 * | 20 * |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 var values = new Map<Symbol, Object>(); | 117 var values = new Map<Symbol, Object>(); |
118 | 118 |
119 // Note: we scan for @observable regardless of whether the base type | 119 // Note: we scan for @observable regardless of whether the base type |
120 // actually includes this mixin. While perhaps too inclusive, it lets us | 120 // actually includes this mixin. While perhaps too inclusive, it lets us |
121 // avoid complex logic that walks "with" and "implements" clauses. | 121 // avoid complex logic that walks "with" and "implements" clauses. |
122 for (var type = mirror.type; type != _objectType; type = type.superclass) { | 122 for (var type = mirror.type; type != _objectType; type = type.superclass) { |
123 for (var field in type.variables.values) { | 123 for (var field in type.variables.values) { |
124 if (field.isFinal || field.isStatic || field.isPrivate) continue; | 124 if (field.isFinal || field.isStatic || field.isPrivate) continue; |
125 | 125 |
126 for (var meta in field.metadata) { | 126 for (var meta in field.metadata) { |
127 if (identical(observable, meta.reflectee)) { | 127 if (meta.reflectee is ObservableProperty) { |
128 var name = field.simpleName; | 128 var name = field.simpleName; |
129 // Note: since this is a field, getting the value shouldn't execute | 129 // Note: since this is a field, getting the value shouldn't execute |
130 // user code, so we don't need to worry about errors. | 130 // user code, so we don't need to worry about errors. |
131 values[name] = mirror.getField(name).reflectee; | 131 values[name] = mirror.getField(name).reflectee; |
132 break; | 132 break; |
133 } | 133 } |
134 } | 134 } |
135 } | 135 } |
136 } | 136 } |
137 | 137 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 // TODO(jmesserly): should this be == instead of identical, to prevent | 213 // TODO(jmesserly): should this be == instead of identical, to prevent |
214 // spurious loops? | 214 // spurious loops? |
215 if (obj.hasObservers && !identical(oldValue, newValue)) { | 215 if (obj.hasObservers && !identical(oldValue, newValue)) { |
216 obj.notifyChange(new PropertyChangeRecord(field)); | 216 obj.notifyChange(new PropertyChangeRecord(field)); |
217 } | 217 } |
218 return newValue; | 218 return newValue; |
219 } | 219 } |
220 | 220 |
221 | 221 |
222 /** | 222 /** |
223 * The type of the `@observable` annotation. | 223 * An annotation that is used to make a property observable. |
| 224 * Normally this is used via the [observable] constant, for example: |
224 * | 225 * |
225 * Library private because you should be able to use the [observable] field | 226 * class Monster { |
226 * to get the one and only instance. We could make it public though, if anyone | 227 * @observable int health; |
227 * needs it for some reason. | 228 * } |
| 229 * |
| 230 * If needed, you can subclass this to create another annotation that will also |
| 231 * be treated as observable. |
228 */ | 232 */ |
229 class _ObservableAnnotation { | 233 class ObservableProperty { |
230 const _ObservableAnnotation(); | 234 const ObservableProperty(); |
231 } | 235 } |
OLD | NEW |