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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/async/stream_impl.dart
diff --git a/sdk/lib/async/stream_impl.dart b/sdk/lib/async/stream_impl.dart
index 617a616b25037a38a629cf357a5b3800f4e8ea0d..d27dfef5d5f070877f181628970fc2cabf02d1f9 100644
--- a/sdk/lib/async/stream_impl.dart
+++ b/sdk/lib/async/stream_impl.dart
@@ -625,6 +625,51 @@ class _MultiStreamImpl<T> extends _StreamImpl<T>
}
}
+
+/** Abstract superclass for streams that generate their own events. */
+abstract class _GeneratedSingleStreamImpl<T> extends _SingleStreamImpl<T> {
+
+ void _onSubscriptionStateChange() {
+ if (_hasSubscribers && !_isClosed) {
+ new Timer(0, (_) {
+ _handlePendingEvents();
+ });
+ }
+ }
+
+ void _generateNextEvent();
+
+ void _handlePendingEvents() {
+ while (!_isPaused) {
+ super._handlePendingEvents();
+ if (!_isPaused && !_isClosed) {
+ _generateNextEvent();
+ }
+ }
+ }
+}
+
+
+/** Stream that gets its events from an [Iterable]. */
+class _IterableSingleStreamImpl<T> extends _GeneratedSingleStreamImpl<T> {
+ Iterator<T> _iterator;
+
+ _IterableSingleStreamImpl(Iterable<T> data) : _iterator = data.iterator;
+
+ void _generateNextEvent() {
+ try {
+ if (_iterator.moveNext()) {
+ _add(_iterator.current);
+ return;
+ }
+ } catch (e, s) {
+ _signalError(new AsyncError(e, s));
+ }
+ _close();
+ }
+}
+
+
/**
* The subscription class that the [StreamController] uses.
*

Powered by Google App Engine
This is Rietveld 408576698