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

Side by Side Diff: mojo/public/dart/src/event_stream.dart

Issue 982673002: Dart: Removes need to call listen(). (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Merge Created 5 years, 9 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 part of core; 5 part of core;
6 6
7 class MojoEventStream extends Stream<List<int>> { 7 class MojoEventStream extends Stream<List<int>> {
8 // The underlying Mojo handle. 8 // The underlying Mojo handle.
9 MojoHandle _handle; 9 MojoHandle _handle;
10 10
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 } 117 }
118 } 118 }
119 119
120 bool get readyRead => _handle.readyRead; 120 bool get readyRead => _handle.readyRead;
121 bool get readyWrite => _handle.readyWrite; 121 bool get readyWrite => _handle.readyWrite;
122 122
123 String toString() => "$_handle"; 123 String toString() => "$_handle";
124 } 124 }
125 125
126 abstract class Listener { 126 abstract class Listener {
127 StreamSubscription<List<int>> listen({Function onClosed}); 127 StreamSubscription<List<int>> listen();
128 } 128 }
129 129
130 class MojoEventStreamListener { 130 class MojoEventStreamListener {
131 MojoMessagePipeEndpoint _endpoint; 131 MojoMessagePipeEndpoint _endpoint;
132 MojoEventStream _eventStream; 132 MojoEventStream _eventStream;
133 bool _isOpen = false; 133 bool _isOpen = false;
134 bool _isInHandler = false; 134 bool _isInHandler = false;
135 StreamSubscription subscription; 135 StreamSubscription subscription;
136 Function _onError;
136 137
137 MojoEventStreamListener.fromEndpoint(MojoMessagePipeEndpoint endpoint, 138 MojoEventStreamListener.fromEndpoint(MojoMessagePipeEndpoint endpoint)
138 {bool doListen: true, Function onClosed})
139 : _endpoint = endpoint, 139 : _endpoint = endpoint,
140 _eventStream = new MojoEventStream(endpoint.handle), 140 _eventStream = new MojoEventStream(endpoint.handle),
141 _isOpen = false { 141 _isOpen = false {
142 if (doListen) { 142 listen();
143 listen(onClosed: onClosed);
144 }
145 } 143 }
146 144
147 MojoEventStreamListener.fromHandle(MojoHandle handle, {bool doListen: true, 145 MojoEventStreamListener.fromHandle(MojoHandle handle) {
148 Function onClosed}) {
149 _endpoint = new MojoMessagePipeEndpoint(handle); 146 _endpoint = new MojoMessagePipeEndpoint(handle);
150 _eventStream = new MojoEventStream(handle); 147 _eventStream = new MojoEventStream(handle);
151 _isOpen = false; 148 _isOpen = false;
152 if (doListen) { 149 listen();
153 listen(onClosed: onClosed);
154 }
155 } 150 }
156 151
157 MojoEventStreamListener.unbound() 152 MojoEventStreamListener.unbound()
158 : _endpoint = null, 153 : _endpoint = null,
159 _eventStream = null, 154 _eventStream = null,
160 _isOpen = false; 155 _isOpen = false;
161 156
162 void bind(MojoMessagePipeEndpoint endpoint) { 157 void bind(MojoMessagePipeEndpoint endpoint) {
163 assert(!isBound); 158 assert(!isBound);
164 _endpoint = endpoint; 159 _endpoint = endpoint;
165 _eventStream = new MojoEventStream(endpoint.handle); 160 _eventStream = new MojoEventStream(endpoint.handle);
166 _isOpen = false; 161 _isOpen = false;
167 } 162 }
168 163
169 void bindFromHandle(MojoHandle handle) { 164 void bindFromHandle(MojoHandle handle) {
170 assert(!isBound); 165 assert(!isBound);
171 _endpoint = new MojoMessagePipeEndpoint(handle); 166 _endpoint = new MojoMessagePipeEndpoint(handle);
172 _eventStream = new MojoEventStream(handle); 167 _eventStream = new MojoEventStream(handle);
173 _isOpen = false; 168 _isOpen = false;
174 } 169 }
175 170
176 StreamSubscription<List<int>> listen({Function onClosed}) { 171 StreamSubscription<List<int>> listen() {
177 assert(isBound && (subscription == null)); 172 assert(isBound && (subscription == null));
178 _isOpen = true; 173 _isOpen = true;
179 subscription = _eventStream.listen((List<int> event) { 174 subscription = _eventStream.listen((List<int> event) {
180 var signalsWatched = new MojoHandleSignals(event[0]); 175 var signalsWatched = new MojoHandleSignals(event[0]);
181 var signalsReceived = new MojoHandleSignals(event[1]); 176 var signalsReceived = new MojoHandleSignals(event[1]);
182 if (signalsReceived.isPeerClosed) { 177 if (signalsReceived.isPeerClosed) {
183 if (onClosed != null) onClosed(); 178 if (onError != null) onError();
184 close(); 179 close();
185 // The peer being closed obviates any other signal we might 180 // The peer being closed obviates any other signal we might
186 // have received since we won't be able to read or write the handle. 181 // have received since we won't be able to read or write the handle.
187 // Thus, we just return before invoking other handlers. 182 // Thus, we just return before invoking other handlers.
188 return; 183 return;
189 } 184 }
190 _isInHandler = true; 185 _isInHandler = true;
191 if (signalsReceived.isReadable) { 186 if (signalsReceived.isReadable) {
192 assert(_eventStream.readyRead); 187 assert(_eventStream.readyRead);
193 handleRead(); 188 handleRead();
(...skipping 16 matching lines...) Expand all
210 subscription = null; 205 subscription = null;
211 if (_eventStream != null) { 206 if (_eventStream != null) {
212 _eventStream.close(); 207 _eventStream.close();
213 _eventStream = null; 208 _eventStream = null;
214 } 209 }
215 } 210 }
216 211
217 void handleRead() {} 212 void handleRead() {}
218 void handleWrite() {} 213 void handleWrite() {}
219 214
215 Function get onError => _onError;
216 set onError(Function f) {
217 if (f == null) {
218 _onError = null;
219 } else {
220 Function cachedOnError = _onError;
hansmuller1 2015/03/05 00:35:32 Why are we accumulating onError functions here? De
zra 2015/03/05 16:39:29 I was worried about people accidentally overwritin
221 _onError = (() {
222 if (cachedOnError != null) {
223 cachedOnError();
224 }
225 f();
226 });
227 }
228 }
229
220 MojoMessagePipeEndpoint get endpoint => _endpoint; 230 MojoMessagePipeEndpoint get endpoint => _endpoint;
221 bool get isOpen => _isOpen; 231 bool get isOpen => _isOpen;
222 bool get isInHandler => _isInHandler; 232 bool get isInHandler => _isInHandler;
223 bool get isBound => _endpoint != null; 233 bool get isBound => _endpoint != null;
224 } 234 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698