Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Side by Side Diff: sdk/lib/mdv_observe_impl/mdv_observe_impl.dart

Issue 16003002: Add StreamController.multiplex constructor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove unused code Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/async/stream_controller.dart ('k') | sdk/lib/utf/utf_stream.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 Set<StreamController<List<ChangeRecord>>> _observers; 66 StreamController _multiplexController;
67 Stream<List<ChangeRecord>> _stream;
68 List<ChangeRecord> _changes; 67 List<ChangeRecord> _changes;
69 68
70 Stream<List<ChangeRecord>> get changes { 69 Stream<List<ChangeRecord>> get changes {
71 if (_observers == null) { 70 if (_multiplexController == null) {
72 _observers = new Set<StreamController<List<ChangeRecord>>>(); 71 _multiplexController =
72 new StreamController<List<ChangeRecord>>.multiplex();
73 } 73 }
74 StreamController controller; 74 return _multiplexController.stream;
75 controller = new StreamController(
76 onListen: () {
77 _observers.add(controller);
78 },
79 onCancel: () {
80 _observers.remove(controller);
81 }
82 );
83 return controller.stream;
84 } 75 }
85 76
86 void _deliverChanges() { 77 void _deliverChanges() {
87 var changes = _changes; 78 var changes = _changes;
88 _changes = null; 79 _changes = null;
89 if (hasObservers && changes != null) { 80 if (hasObservers && changes != null) {
90 var observers = 81 // TODO(jmesserly): make "changes" immutable
91 _observers.toList(); 82 _multiplexController.add(changes);
92 for (var observer in observers) {
93 if (_observers.contains(observer)) {
94 // TODO(jmesserly): make "changes" immutable
95 observer.add(changes);
96 }
97 }
98 } 83 }
99 } 84 }
100 85
101 /** 86 /**
102 * True if this object has any observers, and should call 87 * True if this object has any observers, and should call
103 * [notifyPropertyChange] for changes. 88 * [notifyPropertyChange] for changes.
104 */ 89 */
105 bool get hasObservers => _observers != null && !_observers.isEmpty; 90 bool get hasObservers => _multiplexController != null &&
91 _multiplexController.hasListener;
106 92
107 /** 93 /**
108 * Notify that the field [name] of this object has been changed. 94 * Notify that the field [name] of this object has been changed.
109 * 95 *
110 * The [oldValue] and [newValue] are also recorded. If the two values are 96 * The [oldValue] and [newValue] are also recorded. If the two values are
111 * identical, no change will be recorded. 97 * identical, no change will be recorded.
112 * 98 *
113 * For convenience this returns [newValue]. This makes it easy to use in a 99 * For convenience this returns [newValue]. This makes it easy to use in a
114 * setter: 100 * setter:
115 * 101 *
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 /** Queues an action to happen during the [deliverChangeRecords] timeslice. */ 217 /** Queues an action to happen during the [deliverChangeRecords] timeslice. */
232 void queueChangeRecords(void deliverChanges()) { 218 void queueChangeRecords(void deliverChanges()) {
233 if (_deliverCallbacks == null) { 219 if (_deliverCallbacks == null) {
234 _deliverCallbacks = new Queue<Function>(); 220 _deliverCallbacks = new Queue<Function>();
235 runAsync(deliverChangeRecords); 221 runAsync(deliverChangeRecords);
236 } 222 }
237 _deliverCallbacks.add(deliverChanges); 223 _deliverCallbacks.add(deliverChanges);
238 } 224 }
239 225
240 Queue _deliverCallbacks; 226 Queue _deliverCallbacks;
OLDNEW
« no previous file with comments | « sdk/lib/async/stream_controller.dart ('k') | sdk/lib/utf/utf_stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698