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

Side by Side Diff: sdk/lib/async/stream_controller.dart

Issue 269283007: Make errors from StreamController onCancel calls end up in the returned future. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 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.dart ('k') | no next file » | 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of dart.async; 5 part of dart.async;
6 6
7 // ------------------------------------------------------------------- 7 // -------------------------------------------------------------------
8 // Controller for creating and adding events to a stream. 8 // Controller for creating and adding events to a stream.
9 // ------------------------------------------------------------------- 9 // -------------------------------------------------------------------
10 10
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 * After closing, no further events may be added using [add] or [addError]. 413 * After closing, no further events may be added using [add] or [addError].
414 * 414 *
415 * You are allowed to close the controller more than once, but only the first 415 * You are allowed to close the controller more than once, but only the first
416 * call has any effect. 416 * call has any effect.
417 * 417 *
418 * The first time a controller is closed, a "done" event is sent to its 418 * The first time a controller is closed, a "done" event is sent to its
419 * stream. 419 * stream.
420 */ 420 */
421 Future close() { 421 Future close() {
422 if (isClosed) { 422 if (isClosed) {
423 _ensureDoneFuture(); 423 return _ensureDoneFuture();
424 return _doneFuture;
425 } 424 }
426 if (!_mayAddEvent) throw _badEventState(); 425 if (!_mayAddEvent) throw _badEventState();
427 _state |= _STATE_CLOSED; 426 _state |= _STATE_CLOSED;
428 if (hasListener) { 427 if (hasListener) {
429 _sendDone(); 428 _sendDone();
430 } else if (_isInitialState) { 429 } else if (_isInitialState) {
431 _ensurePendingEvents().add(const _DelayedDone()); 430 _ensurePendingEvents().add(const _DelayedDone());
432 } 431 }
433 _ensureDoneFuture(); 432 return _ensureDoneFuture();
434 return _doneFuture;
435 } 433 }
436 434
437 // EventSink interface. Used by the [addStream] events. 435 // EventSink interface. Used by the [addStream] events.
438 436
439 // Add data event, used both by the [addStream] events and by [add]. 437 // Add data event, used both by the [addStream] events and by [add].
440 void _add(T value) { 438 void _add(T value) {
441 if (hasListener) { 439 if (hasListener) {
442 _sendData(value); 440 _sendData(value);
443 } else if (_isInitialState) { 441 } else if (_isInitialState) {
444 _ensurePendingEvents().add(new _DelayedData<T>(value)); 442 _ensurePendingEvents().add(new _DelayedData<T>(value));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 } 480 }
483 subscription._setPendingEvents(pendingEvents); 481 subscription._setPendingEvents(pendingEvents);
484 subscription._guardCallback(() { 482 subscription._guardCallback(() {
485 _runGuarded(_onListen); 483 _runGuarded(_onListen);
486 }); 484 });
487 485
488 return subscription; 486 return subscription;
489 } 487 }
490 488
491 Future _recordCancel(StreamSubscription<T> subscription) { 489 Future _recordCancel(StreamSubscription<T> subscription) {
490 Future result;
492 if (_isAddingStream) { 491 if (_isAddingStream) {
493 _StreamControllerAddStreamState addState = _varData; 492 _StreamControllerAddStreamState addState = _varData;
494 addState.cancel(); 493 result = addState.cancel();
495 } 494 }
496 _varData = null; 495 _varData = null;
497 _state = 496 _state =
498 (_state & ~(_STATE_SUBSCRIBED | _STATE_ADDSTREAM)) | _STATE_CANCELED; 497 (_state & ~(_STATE_SUBSCRIBED | _STATE_ADDSTREAM)) | _STATE_CANCELED;
499 498
499 if (_onCancel != null) {
500 Future cancelFuture;
501 try {
Lasse Reichstein Nielsen 2014/05/07 14:33:32 This entire try-catch could just be `cancelFuture
502 var cancelReturn = _onCancel();
503 if (cancelReturn is Future) {
504 cancelFuture = cancelReturn;
505 }
506 } catch (e, s) {
507 cancelFuture = new _Future.immediateError(e, s);
508 }
509
510 if (cancelFuture != null) {
511 if (result != null) {
512 result = result.whenComplete(() => cancelFuture);
floitsch 2014/05/07 15:16:25 I think we should wait for the source-stream's can
Lasse Reichstein Nielsen 2014/05/07 17:40:26 Any reason for wanting to wait? We know we are goi
floitsch 2014/05/07 17:50:50 Let's discuss this tomorrow. I don't have a good a
513 } else {
514 result = cancelFuture;
515 }
516 }
517 }
518
500 void complete() { 519 void complete() {
501 if (_doneFuture != null && _doneFuture._mayComplete) { 520 if (_doneFuture != null && _doneFuture._mayComplete) {
502 _doneFuture._asyncComplete(null); 521 _doneFuture._asyncComplete(null);
503 } 522 }
504 } 523 }
505 524
506 Future future = _runGuarded(_onCancel); 525 if (result != null) {
507 if (future != null) { 526 result.whenComplete(complete);
508 future = future.whenComplete(complete);
509 } else { 527 } else {
510 complete(); 528 complete();
511 } 529 }
512 return future; 530
531 return result;
513 } 532 }
514 533
515 void _recordPause(StreamSubscription<T> subscription) { 534 void _recordPause(StreamSubscription<T> subscription) {
516 if (_isAddingStream) { 535 if (_isAddingStream) {
517 _StreamControllerAddStreamState addState = _varData; 536 _StreamControllerAddStreamState addState = _varData;
518 addState.pause(); 537 addState.pause();
519 } 538 }
520 _runGuarded(_onPause); 539 _runGuarded(_onPause);
521 } 540 }
522 541
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
697 716
698 void pause() { 717 void pause() {
699 addSubscription.pause(); 718 addSubscription.pause();
700 } 719 }
701 720
702 void resume() { 721 void resume() {
703 addSubscription.resume(); 722 addSubscription.resume();
704 } 723 }
705 724
706 void cancel() { 725 void cancel() {
707 addSubscription.cancel(); 726 addStreamFuture._asyncComplete(addSubscription.cancel());
708 complete();
709 } 727 }
710 728
711 void complete() { 729 void complete() {
712 addStreamFuture._asyncComplete(null); 730 addStreamFuture._asyncComplete(null);
713 } 731 }
714 } 732 }
715 733
716 class _StreamControllerAddStreamState<T> extends _AddStreamState<T> { 734 class _StreamControllerAddStreamState<T> extends _AddStreamState<T> {
717 // The subscription or pending data of a _StreamController. 735 // The subscription or pending data of a _StreamController.
718 // Stored here because we reuse the `_varData` field in the _StreamController 736 // Stored here because we reuse the `_varData` field in the _StreamController
719 // to store this state object. 737 // to store this state object.
720 var varData; 738 var varData;
721 739
722 _StreamControllerAddStreamState(_StreamController controller, 740 _StreamControllerAddStreamState(_StreamController controller,
723 this.varData, 741 this.varData,
724 Stream source, 742 Stream source,
725 bool cancelOnError) 743 bool cancelOnError)
726 : super(controller, source, cancelOnError) { 744 : super(controller, source, cancelOnError) {
727 if (controller.isPaused) { 745 if (controller.isPaused) {
728 addSubscription.pause(); 746 addSubscription.pause();
729 } 747 }
730 } 748 }
731 } 749 }
OLDNEW
« no previous file with comments | « sdk/lib/async/stream.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698