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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 /// The underlying [StreamController] for [this]. | 225 /// The underlying [StreamController] for [this]. |
226 final StreamController _controller; | 226 final StreamController _controller; |
227 | 227 |
228 /// The [StreamSubscription] that connects the wrapped [Stream] to | 228 /// The [StreamSubscription] that connects the wrapped [Stream] to |
229 /// [_controller]. | 229 /// [_controller]. |
230 StreamSubscription _subscription; | 230 StreamSubscription _subscription; |
231 | 231 |
232 /// Whether [this] has any listeners. | 232 /// Whether [this] has any listeners. |
233 bool get _hasListeners => _controller.hasSubscribers; | 233 bool get _hasListeners => _controller.hasSubscribers; |
234 | 234 |
235 // TODO(nweiz): Remove this when issue 8512 is fixed. | |
236 /// Whether the subscription has been cancelled. | |
237 bool _cancelled = false; | |
238 | |
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 = inner.isBroadcast ? | 238 : _controller = inner.isBroadcast ? |
243 new StreamController.broadcast() : | 239 new StreamController.broadcast() : |
244 new StreamController() { | 240 new StreamController() { |
245 _subscription = inner.listen((v) { | 241 _subscription = inner.listen((v) { |
246 if (!_cancelled) _controller.add(v); | 242 _controller.add(v); |
247 }, onError: (e) { | 243 }, onError: (e) { |
248 if (!_cancelled) _group._signalError(e); | 244 _group._signalError(e); |
249 }, onDone: () { | 245 }, onDone: () { |
250 if (!_cancelled) { | 246 _isDone = true; |
251 _isDone = true; | 247 _group._signalStreamComplete(this); |
252 _group._signalStreamComplete(this); | 248 _controller.close(); |
253 _controller.close(); | |
254 } | |
255 }); | 249 }); |
256 } | 250 } |
257 | 251 |
258 StreamSubscription listen(void onData(value), | 252 StreamSubscription listen(void onData(value), |
259 {void onError(AsyncError error), void onDone(), | 253 {void onError(AsyncError error), void onDone(), |
260 bool unsubscribeOnError}) { | 254 bool unsubscribeOnError}) { |
261 return _controller.stream.listen(onData, | 255 return _controller.stream.listen(onData, |
262 onError: onError, | 256 onError: onError, |
263 onDone: onDone, | 257 onDone: onDone, |
264 unsubscribeOnError: true); | 258 unsubscribeOnError: true); |
265 } | 259 } |
266 | 260 |
267 /// Signal that an error from [_group] should be propagated through [this], | 261 /// Signal that an error from [_group] should be propagated through [this], |
268 /// unless it's already complete. | 262 /// unless it's already complete. |
269 void _signalError(AsyncError e) { | 263 void _signalError(AsyncError e) { |
270 if (_isDone) return; | 264 if (_isDone) return; |
271 _cancelled = true; | |
272 _subscription.cancel(); | 265 _subscription.cancel(); |
273 // Call these asynchronously to work around issue 7913. | 266 // Call these asynchronously to work around issue 7913. |
274 defer(() { | 267 defer(() { |
275 _controller.addError(e.error, e.stackTrace); | 268 _controller.addError(e.error, e.stackTrace); |
276 _controller.close(); | 269 _controller.close(); |
277 }); | 270 }); |
278 } | 271 } |
279 } | 272 } |
OLD | NEW |