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

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

Issue 11794044: Add Stream.fromIterable (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added the test file too. Created 7 years, 11 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
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 // States shared by single/multi stream implementations. 7 // States shared by single/multi stream implementations.
8 8
9 /// Initial and default state where the stream can receive and send events. 9 /// Initial and default state where the stream can receive and send events.
10 const int _STREAM_OPEN = 0; 10 const int _STREAM_OPEN = 0;
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 * This is a low-level action that doesn't call [_onSubscriptionStateChange]. 618 * This is a low-level action that doesn't call [_onSubscriptionStateChange].
619 * or [_onPauseStateChange]. 619 * or [_onPauseStateChange].
620 */ 620 */
621 void _removeListener(_StreamListener listener) { 621 void _removeListener(_StreamListener listener) {
622 int pauseCount = listener._setUnsubscribed(); 622 int pauseCount = listener._setUnsubscribed();
623 _updatePauseCount(-pauseCount); 623 _updatePauseCount(-pauseCount);
624 _InternalLinkList.remove(listener); 624 _InternalLinkList.remove(listener);
625 } 625 }
626 } 626 }
627 627
628
629 /** Abstract superclass for streams that generate their own events. */
630 abstract class _GeneratedSingleStreamImpl<T> extends _SingleStreamImpl<T> {
631
632 void _onSubscriptionStateChange() {
633 if (_hasSubscribers && !_isClosed) {
634 new Timer(0, (_) {
635 _handlePendingEvents();
636 });
637 }
638 }
639
640 void _generateNextEvent();
641
642 void _handlePendingEvents() {
643 while (!_isPaused) {
644 super._handlePendingEvents();
645 if (!_isPaused && !_isClosed) {
646 _generateNextEvent();
647 }
648 }
649 }
650 }
651
652
653 /** Stream that gets its events from an [Iterable]. */
654 class _IterableSingleStreamImpl<T> extends _GeneratedSingleStreamImpl<T> {
655 Iterator<T> _iterator;
656
657 _IterableSingleStreamImpl(Iterable<T> data) : _iterator = data.iterator;
658
659 void _generateNextEvent() {
660 try {
661 if (_iterator.moveNext()) {
662 _add(_iterator.current);
663 return;
664 }
665 } catch (e, s) {
666 _signalError(new AsyncError(e, s));
667 }
668 _close();
669 }
670 }
671
672
628 /** 673 /**
629 * The subscription class that the [StreamController] uses. 674 * The subscription class that the [StreamController] uses.
630 * 675 *
631 * The [StreamController.createSubscription] method should 676 * The [StreamController.createSubscription] method should
632 * create an object of this type, or another subclass of [_StreamListener]. 677 * create an object of this type, or another subclass of [_StreamListener].
633 * A subclass of [StreamController] can specify which subclass 678 * A subclass of [StreamController] can specify which subclass
634 * of [_StreamSubscriptionImpl] it uses by overriding 679 * of [_StreamSubscriptionImpl] it uses by overriding
635 * [StreamController.createSubscription]. 680 * [StreamController.createSubscription].
636 * 681 *
637 * The subscription is in one of three states: 682 * The subscription is in one of three states:
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
1008 if (_isComplete) { 1053 if (_isComplete) {
1009 throw new StateError("Subscription has been canceled."); 1054 throw new StateError("Subscription has been canceled.");
1010 } 1055 }
1011 if (_timer != null) { 1056 if (_timer != null) {
1012 _timer.cancel(); 1057 _timer.cancel();
1013 _timer = null; 1058 _timer = null;
1014 } 1059 }
1015 _pauseCount = 0; 1060 _pauseCount = 0;
1016 } 1061 }
1017 } 1062 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698