Chromium Code Reviews| 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 // This library itself is undocumented and not supported for end use. | 5 // This library itself is undocumented and not supported for end use. |
| 6 // Because dart:html must use some of this functionality, it has to be available | 6 // Because dart:html must use some of this functionality, it has to be available |
| 7 // via a dart:* library. The public APIs are reexported via package:mdv_observe. | 7 // via a dart:* library. The public APIs are reexported via package:mdv_observe. |
| 8 // Generally we try to keep this library minimal, with utility types and | 8 // Generally we try to keep this library minimal, with utility types and |
| 9 // functions in the package. | 9 // functions in the package. |
| 10 library dart.mdv_observe_impl; | 10 library dart.mdv_observe_impl; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 */ | 56 */ |
| 57 typedef ObservableBase = Object with ObservableMixin; | 57 typedef ObservableBase = Object with ObservableMixin; |
| 58 | 58 |
| 59 /** | 59 /** |
| 60 * Mixin for implementing [Observable] objects. | 60 * Mixin for implementing [Observable] objects. |
| 61 * | 61 * |
| 62 * When a field, property, or indexable item is changed, a derived class should | 62 * When a field, property, or indexable item is changed, a derived class should |
| 63 * call [notifyPropertyChange]. See that method for an example. | 63 * call [notifyPropertyChange]. See that method for an example. |
| 64 */ | 64 */ |
| 65 abstract class ObservableMixin implements Observable { | 65 abstract class ObservableMixin implements Observable { |
| 66 StreamController<List<ChangeRecord>> _observers; | 66 Set<StreamController<List<ChangeRecord>>> _observers; |
| 67 Stream<List<ChangeRecord>> _stream; | 67 Stream<List<ChangeRecord>> _stream; |
| 68 List<ChangeRecord> _changes; | 68 List<ChangeRecord> _changes; |
| 69 | 69 |
| 70 Stream<List<ChangeRecord>> get changes { | 70 Stream<List<ChangeRecord>> get changes { |
| 71 if (_observers == null) { | 71 if (_observers == null) { |
| 72 _observers = new StreamController<List<ChangeRecord>>(); | 72 _observers = new Set<StreamController<List<ChangeRecord>>>(); |
|
Jennifer Messerly
2013/05/24 23:28:08
fwiw... this seems awfully complicated for somethi
Lasse Reichstein Nielsen
2013/05/27 08:04:53
Yes, we introduced StreamController.multiplex afte
| |
| 73 _stream = _observers.stream.asBroadcastStream(); | |
| 74 } | 73 } |
| 75 return _stream; | 74 StreamController controller; |
| 75 controller = new StreamController( | |
| 76 onListen: () { | |
| 77 _observers.add(controller); | |
| 78 }, | |
| 79 onCancel: () { | |
| 80 _observers.remove(controller); | |
| 81 } | |
| 82 ); | |
| 83 return controller.stream; | |
| 76 } | 84 } |
| 77 | 85 |
| 78 void _deliverChanges() { | 86 void _deliverChanges() { |
| 79 var changes = _changes; | 87 var changes = _changes; |
| 80 _changes = null; | 88 _changes = null; |
| 81 if (hasObservers && changes != null) { | 89 if (hasObservers && changes != null) { |
| 90 List<StreamController<List<ChangeRecord>>> observers = | |
|
Jennifer Messerly
2013/05/24 23:28:08
style nit, I'd use "var" here and below:
http://w
Lasse Reichstein Nielsen
2013/05/27 08:04:53
It's your code, so ok.
| |
| 91 _observers.toList(); | |
| 92 for (StreamController<List<ChangeRecord>> observer in observers) { | |
| 93 if (_observers.contains(observer)) { | |
| 94 observer.add(changes); | |
| 95 } | |
| 96 } | |
| 82 // TODO(jmesserly): make "changes" immutable | 97 // TODO(jmesserly): make "changes" immutable |
|
Jennifer Messerly
2013/05/24 18:47:55
keep this comment by the "observer.add(changes);"
Lasse Reichstein Nielsen
2013/05/27 08:04:53
Ofcourse :)
| |
| 83 _observers.add(changes); | |
| 84 } | 98 } |
| 85 } | 99 } |
| 86 | 100 |
| 87 /** | 101 /** |
| 88 * True if this object has any observers, and should call | 102 * True if this object has any observers, and should call |
| 89 * [notifyPropertyChange] for changes. | 103 * [notifyPropertyChange] for changes. |
| 90 */ | 104 */ |
| 91 bool get hasObservers => _observers != null && _observers.hasListener; | 105 bool get hasObservers => _observers != null && !_observers.isEmpty; |
| 92 | 106 |
| 93 /** | 107 /** |
| 94 * Notify that the field [name] of this object has been changed. | 108 * Notify that the field [name] of this object has been changed. |
| 95 * | 109 * |
| 96 * The [oldValue] and [newValue] are also recorded. If the two values are | 110 * The [oldValue] and [newValue] are also recorded. If the two values are |
| 97 * identical, no change will be recorded. | 111 * identical, no change will be recorded. |
| 98 * | 112 * |
| 99 * For convenience this returns [newValue]. This makes it easy to use in a | 113 * For convenience this returns [newValue]. This makes it easy to use in a |
| 100 * setter: | 114 * setter: |
| 101 * | 115 * |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 /** Queues an action to happen during the [deliverChangeRecords] timeslice. */ | 231 /** Queues an action to happen during the [deliverChangeRecords] timeslice. */ |
| 218 void queueChangeRecords(void deliverChanges()) { | 232 void queueChangeRecords(void deliverChanges()) { |
| 219 if (_deliverCallbacks == null) { | 233 if (_deliverCallbacks == null) { |
| 220 _deliverCallbacks = new Queue<Function>(); | 234 _deliverCallbacks = new Queue<Function>(); |
| 221 runAsync(deliverChangeRecords); | 235 runAsync(deliverChangeRecords); |
| 222 } | 236 } |
| 223 _deliverCallbacks.add(deliverChanges); | 237 _deliverCallbacks.add(deliverChanges); |
| 224 } | 238 } |
| 225 | 239 |
| 226 Queue _deliverCallbacks; | 240 Queue _deliverCallbacks; |
| OLD | NEW |