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 error_group; | 5 library error_group; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'utils.dart'; | 9 import 'utils.dart'; |
10 | 10 |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 class _ErrorGroupStream extends Stream { | 218 class _ErrorGroupStream extends Stream { |
219 /// The parent [ErrorGroup]. | 219 /// The parent [ErrorGroup]. |
220 final ErrorGroup _group; | 220 final ErrorGroup _group; |
221 | 221 |
222 /// Whether [this] has completed, either successfully or with an error. | 222 /// Whether [this] has completed, either successfully or with an error. |
223 var _isDone = false; | 223 var _isDone = false; |
224 | 224 |
225 /// The underlying [StreamController] for [this]. | 225 /// The underlying [StreamController] for [this]. |
226 final StreamController _controller; | 226 final StreamController _controller; |
227 | 227 |
228 /// The controller's [Stream]. May be different than `_controller.stream` if | |
229 /// the wrapped stream is a broadcasting stream. | |
230 Stream _stream; | |
231 | |
232 /// The [StreamSubscription] that connects the wrapped [Stream] to | 228 /// The [StreamSubscription] that connects the wrapped [Stream] to |
233 /// [_controller]. | 229 /// [_controller]. |
234 StreamSubscription _subscription; | 230 StreamSubscription _subscription; |
235 | 231 |
236 /// Whether [this] has any listeners. | 232 /// Whether [this] has any listeners. |
237 bool get _hasListeners => _controller.hasListener; | 233 bool get _hasListeners => _controller.hasListener; |
238 | 234 |
239 /// Creates a new [_ErrorGroupFuture] that's a child of [_group] and wraps | 235 /// Creates a new [_ErrorGroupFuture] that's a child of [_group] and wraps |
240 /// [inner]. | 236 /// [inner]. |
241 _ErrorGroupStream(this._group, Stream inner) | 237 _ErrorGroupStream(this._group, Stream inner) |
242 : _controller = new StreamController(sync: true) { | 238 : _controller = |
243 this._stream = inner.isBroadcast | 239 inner.isBroadcast ? new StreamController.broadcast(sync: true) |
244 ? _controller.stream.asBroadcastStream() | 240 : new StreamController(sync: true) { |
245 : _controller.stream; | |
246 _subscription = inner.listen((v) { | 241 _subscription = inner.listen((v) { |
247 _controller.add(v); | 242 _controller.add(v); |
248 }, onError: (e) { | 243 }, onError: (e) { |
249 _group._signalError(e); | 244 _group._signalError(e); |
250 }, onDone: () { | 245 }, onDone: () { |
251 _isDone = true; | 246 _isDone = true; |
252 _group._signalStreamComplete(this); | 247 _group._signalStreamComplete(this); |
253 _controller.close(); | 248 _controller.close(); |
254 }); | 249 }); |
255 } | 250 } |
256 | 251 |
257 StreamSubscription listen(void onData(value), | 252 StreamSubscription listen(void onData(value), |
258 {void onError(var error), void onDone(), | 253 {void onError(var error), void onDone(), |
259 bool cancelOnError}) { | 254 bool cancelOnError}) { |
260 return _stream.listen(onData, | 255 return _controller.stream.listen(onData, |
261 onError: onError, | 256 onError: onError, |
262 onDone: onDone, | 257 onDone: onDone, |
263 cancelOnError: true); | 258 cancelOnError: true); |
264 } | 259 } |
265 | 260 |
266 /// Signal that an error from [_group] should be propagated through [this], | 261 /// Signal that an error from [_group] should be propagated through [this], |
267 /// unless it's already complete. | 262 /// unless it's already complete. |
268 void _signalError(var e) { | 263 void _signalError(var e) { |
269 if (_isDone) return; | 264 if (_isDone) return; |
270 _subscription.cancel(); | 265 _subscription.cancel(); |
271 // Call these asynchronously to work around issue 7913. | 266 // Call these asynchronously to work around issue 7913. |
272 new Future.value().then((_) { | 267 new Future.value().then((_) { |
273 _controller.addError(e); | 268 _controller.addError(e); |
274 _controller.close(); | 269 _controller.close(); |
275 }); | 270 }); |
276 } | 271 } |
277 } | 272 } |
OLD | NEW |