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 library observe.src.observable; | 5 library observe.src.observable; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:smoke/smoke.dart' as smoke; | 10 import 'package:smoke/smoke.dart' as smoke; |
(...skipping 16 matching lines...) Expand all Loading... |
27 /// periodically to check for changes to your object. | 27 /// periodically to check for changes to your object. |
28 /// - extend or mixin [ChangeNotifier], and implement change notifications | 28 /// - extend or mixin [ChangeNotifier], and implement change notifications |
29 /// manually by calling [notifyPropertyChange] from your setters. | 29 /// manually by calling [notifyPropertyChange] from your setters. |
30 /// - implement this interface and provide your own implementation. | 30 /// - implement this interface and provide your own implementation. |
31 abstract class Observable { | 31 abstract class Observable { |
32 /// Performs dirty checking of objects that inherit from [Observable]. | 32 /// Performs dirty checking of objects that inherit from [Observable]. |
33 /// This scans all observed objects using mirrors and determines if any fields | 33 /// This scans all observed objects using mirrors and determines if any fields |
34 /// have changed. If they have, it delivers the changes for the object. | 34 /// have changed. If they have, it delivers the changes for the object. |
35 static void dirtyCheck() => dirtyCheckObservables(); | 35 static void dirtyCheck() => dirtyCheckObservables(); |
36 | 36 |
37 StreamController _changes; | 37 StreamController<List<ChangeRecord>> _changes; |
38 | 38 |
39 Map<Symbol, Object> _values; | 39 Map<Symbol, Object> _values; |
40 List<ChangeRecord> _records; | 40 List<ChangeRecord> _records; |
41 | 41 |
42 /// The stream of change records to this object. Records will be delivered | 42 /// The stream of change records to this object. Records will be delivered |
43 /// asynchronously. | 43 /// asynchronously. |
44 /// | 44 /// |
45 /// [deliverChanges] can be called to force synchronous delivery. | 45 /// [deliverChanges] can be called to force synchronous delivery. |
46 Stream<List<ChangeRecord>> get changes { | 46 Stream<List<ChangeRecord>> get changes { |
47 if (_changes == null) { | 47 if (_changes == null) { |
48 _changes = new StreamController.broadcast(sync: true, | 48 _changes = new StreamController.broadcast( |
49 onListen: _observed, onCancel: _unobserved); | 49 sync: true, onListen: _observed, onCancel: _unobserved); |
50 } | 50 } |
51 return _changes.stream; | 51 return _changes.stream; |
52 } | 52 } |
53 | 53 |
54 /// True if this object has any observers, and should call | 54 /// True if this object has any observers, and should call |
55 /// [notifyChange] for changes. | 55 /// [notifyChange] for changes. |
56 bool get hasObservers => _changes != null && _changes.hasListener; | 56 bool get hasObservers => _changes != null && _changes.hasListener; |
57 | 57 |
58 void _observed() { | 58 void _observed() { |
59 // Register this object for dirty checking purposes. | 59 // Register this object for dirty checking purposes. |
60 registerObservable(this); | 60 registerObservable(this); |
61 | 61 |
62 var values = new Map<Symbol, Object>(); | 62 var values = new Map<Symbol, Object>(); |
63 | 63 |
64 // Note: we scan for @observable regardless of whether the base type | 64 // Note: we scan for @observable regardless of whether the base type |
65 // actually includes this mixin. While perhaps too inclusive, it lets us | 65 // actually includes this mixin. While perhaps too inclusive, it lets us |
66 // avoid complex logic that walks "with" and "implements" clauses. | 66 // avoid complex logic that walks "with" and "implements" clauses. |
67 var queryOptions = new smoke.QueryOptions(includeInherited: true, | 67 var queryOptions = new smoke.QueryOptions( |
68 includeProperties: false, withAnnotations: const [ObservableProperty]); | 68 includeInherited: true, |
| 69 includeProperties: false, |
| 70 withAnnotations: const [ObservableProperty]); |
69 for (var decl in smoke.query(this.runtimeType, queryOptions)) { | 71 for (var decl in smoke.query(this.runtimeType, queryOptions)) { |
70 var name = decl.name; | 72 var name = decl.name; |
71 // Note: since this is a field, getting the value shouldn't execute | 73 // Note: since this is a field, getting the value shouldn't execute |
72 // user code, so we don't need to worry about errors. | 74 // user code, so we don't need to worry about errors. |
73 values[name] = smoke.read(this, name); | 75 values[name] = smoke.read(this, name); |
74 } | 76 } |
75 | 77 |
76 _values = values; | 78 _values = values; |
77 } | 79 } |
78 | 80 |
(...skipping 24 matching lines...) Expand all Loading... |
103 // Observable->Stream adapter. | 105 // Observable->Stream adapter. |
104 // | 106 // |
105 // Also: we should be delivering changes to the observer (subscription) based | 107 // Also: we should be delivering changes to the observer (subscription) based |
106 // on the birth order of the observer. This is for compatibility with ES | 108 // on the birth order of the observer. This is for compatibility with ES |
107 // Harmony as well as predictability for app developers. | 109 // Harmony as well as predictability for app developers. |
108 bool deliverChanges() { | 110 bool deliverChanges() { |
109 if (_values == null || !hasObservers) return false; | 111 if (_values == null || !hasObservers) return false; |
110 | 112 |
111 // Start with manually notified records (computed properties, etc), | 113 // Start with manually notified records (computed properties, etc), |
112 // then scan all fields for additional changes. | 114 // then scan all fields for additional changes. |
113 List records = _records; | 115 var records = _records; |
114 _records = null; | 116 _records = null; |
115 | 117 |
116 _values.forEach((name, oldValue) { | 118 _values.forEach((name, oldValue) { |
117 var newValue = smoke.read(this, name); | 119 var newValue = smoke.read(this, name); |
118 if (oldValue != newValue) { | 120 if (oldValue != newValue) { |
119 if (records == null) records = []; | 121 if (records == null) records = []; |
120 records.add(new PropertyChangeRecord(this, name, oldValue, newValue)); | 122 records.add(new PropertyChangeRecord(this, name, oldValue, newValue)); |
121 _values[name] = newValue; | 123 _values[name] = newValue; |
122 } | 124 } |
123 }); | 125 }); |
124 | 126 |
125 if (records == null) return false; | 127 if (records == null) return false; |
126 | 128 |
127 _changes.add(new UnmodifiableListView<ChangeRecord>(records)); | 129 _changes.add(new UnmodifiableListView<ChangeRecord>(records)); |
128 return true; | 130 return true; |
129 } | 131 } |
130 | 132 |
131 /// Notify that the field [name] of this object has been changed. | 133 /// Notify that the field [name] of this object has been changed. |
132 /// | 134 /// |
133 /// The [oldValue] and [newValue] are also recorded. If the two values are | 135 /// The [oldValue] and [newValue] are also recorded. If the two values are |
134 /// equal, no change will be recorded. | 136 /// equal, no change will be recorded. |
135 /// | 137 /// |
136 /// For convenience this returns [newValue]. | 138 /// For convenience this returns [newValue]. |
137 notifyPropertyChange(Symbol field, Object oldValue, Object newValue) | 139 /*=T*/ notifyPropertyChange /*<T>*/ ( |
138 => notifyPropertyChangeHelper(this, field, oldValue, newValue); | 140 Symbol field, /*=T*/ oldValue, /*=T*/ newValue) => |
| 141 notifyPropertyChangeHelper(this, field, oldValue, newValue); |
139 | 142 |
140 /// Notify observers of a change. | 143 /// Notify observers of a change. |
141 /// | 144 /// |
142 /// For most objects [Observable.notifyPropertyChange] is more convenient, but | 145 /// For most objects [Observable.notifyPropertyChange] is more convenient, but |
143 /// collections sometimes deliver other types of changes such as a | 146 /// collections sometimes deliver other types of changes such as a |
144 /// [ListChangeRecord]. | 147 /// [ListChangeRecord]. |
145 /// | 148 /// |
146 /// Notes: | 149 /// Notes: |
147 /// - This is *not* required for fields if you mixin or extend [Observable], | 150 /// - This is *not* required for fields if you mixin or extend [Observable], |
148 /// but you can use it for computed properties. | 151 /// but you can use it for computed properties. |
149 /// - Unlike [ChangeNotifier] this will not schedule [deliverChanges]; use | 152 /// - Unlike [ChangeNotifier] this will not schedule [deliverChanges]; use |
150 /// [Observable.dirtyCheck] instead. | 153 /// [Observable.dirtyCheck] instead. |
151 void notifyChange(ChangeRecord record) { | 154 void notifyChange(ChangeRecord record) { |
152 if (!hasObservers) return; | 155 if (!hasObservers) return; |
153 | 156 |
154 if (_records == null) _records = []; | 157 if (_records == null) _records = []; |
155 _records.add(record); | 158 _records.add(record); |
156 } | 159 } |
157 } | 160 } |
158 | 161 |
159 // TODO(jmesserly): remove the instance method and make this top-level method | 162 // TODO(jmesserly): remove the instance method and make this top-level method |
160 // public instead? | 163 // public instead? |
161 // NOTE: this is not exported publically. | 164 // NOTE: this is not exported publically. |
162 notifyPropertyChangeHelper(Observable obj, Symbol field, Object oldValue, | 165 /*=T*/ notifyPropertyChangeHelper /*<T>*/ ( |
163 Object newValue) { | 166 Observable obj, Symbol field, /*=T*/ oldValue, /*=T*/ newValue) { |
164 | |
165 if (obj.hasObservers && oldValue != newValue) { | 167 if (obj.hasObservers && oldValue != newValue) { |
166 obj.notifyChange(new PropertyChangeRecord(obj, field, oldValue, newValue)); | 168 obj.notifyChange(new PropertyChangeRecord(obj, field, oldValue, newValue)); |
167 } | 169 } |
168 return newValue; | 170 return newValue; |
169 } | 171 } |
OLD | NEW |