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 |
235 /// Creates a new [_ErrorGroupFuture] that's a child of [_group] and wraps | 239 /// Creates a new [_ErrorGroupFuture] that's a child of [_group] and wraps |
236 /// [inner]. | 240 /// [inner]. |
237 _ErrorGroupStream(this._group, Stream inner) | 241 _ErrorGroupStream(this._group, Stream inner) |
238 : _controller = inner.isBroadcast ? | 242 : _controller = inner.isBroadcast ? |
239 new StreamController.broadcast() : | 243 new StreamController.broadcast() : |
240 new StreamController() { | 244 new StreamController() { |
241 _subscription = inner.listen(_controller.add, | 245 _subscription = inner.listen((v) { |
242 onError: (e) => _group._signalError(e), | 246 if (!_cancelled) _controller.add(v); |
243 onDone: () { | 247 }, onError: (e) { |
244 _isDone = true; | 248 if (!_cancelled) _group._signalError(e); |
245 _group._signalStreamComplete(this); | 249 }, onDone: () { |
246 _controller.close(); | 250 if (!_cancelled) { |
247 }); | 251 _isDone = true; |
| 252 _group._signalStreamComplete(this); |
| 253 _controller.close(); |
| 254 } |
| 255 }); |
248 } | 256 } |
249 | 257 |
250 StreamSubscription listen(void onData(value), | 258 StreamSubscription listen(void onData(value), |
251 {void onError(AsyncError error), void onDone(), | 259 {void onError(AsyncError error), void onDone(), |
252 bool unsubscribeOnError}) { | 260 bool unsubscribeOnError}) { |
253 return _controller.stream.listen(onData, | 261 return _controller.stream.listen(onData, |
254 onError: onError, | 262 onError: onError, |
255 onDone: onDone, | 263 onDone: onDone, |
256 unsubscribeOnError: true); | 264 unsubscribeOnError: true); |
257 } | 265 } |
258 | 266 |
259 /// Signal that an error from [_group] should be propagated through [this], | 267 /// Signal that an error from [_group] should be propagated through [this], |
260 /// unless it's already complete. | 268 /// unless it's already complete. |
261 void _signalError(AsyncError e) { | 269 void _signalError(AsyncError e) { |
262 if (_isDone) return; | 270 if (_isDone) return; |
| 271 _cancelled = true; |
263 _subscription.cancel(); | 272 _subscription.cancel(); |
264 // Call these asynchronously to work around issue 7913. | 273 // Call these asynchronously to work around issue 7913. |
265 defer(() { | 274 defer(() { |
266 _controller.signalError(e.error, e.stackTrace); | 275 _controller.signalError(e.error, e.stackTrace); |
267 _controller.close(); | 276 _controller.close(); |
268 }); | 277 }); |
269 } | 278 } |
270 } | 279 } |
OLD | NEW |