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

Unified Diff: sdk/lib/async/stream.dart

Issue 14312010: Add Stream.forEach (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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
« no previous file with comments | « no previous file | tests/lib/async/stream_controller_async_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/async/stream.dart
diff --git a/sdk/lib/async/stream.dart b/sdk/lib/async/stream.dart
index 62174c86608657bbecec343747a67de1685c1c06..341626587b4dad9d56158de3ab6b19fe2b16509e 100644
--- a/sdk/lib/async/stream.dart
+++ b/sdk/lib/async/stream.dart
@@ -339,6 +339,34 @@ abstract class Stream<T> {
}
/**
+ * Executes [action] on each data event of the stream.
+ *
+ * Completes the returned [Future] when all events of the stream
+ * have been processed. Completes the future with an error if the
+ * stream has an error event, or if [action] throws.
+ */
+ Future forEach(void action(T element)) {
+ _FutureImpl future = new _FutureImpl();
+ StreamSubscription subscription;
+ subscription = this.listen(
+ // TODO(ahe): Restore type when feature is implemented in dart2js
+ // checked mode. http://dartbug.com/7733
+ (/*T*/ element) {
+ _runUserCode(
+ () => action(element),
+ (_) {},
+ _cancelAndError(subscription, future)
+ );
+ },
+ onError: future._setError,
+ onDone: () {
+ future._setValue(null);
+ },
+ cancelOnError: true);
+ return future;
+ }
+
+ /**
* Checks whether [test] accepts all elements provided by this stream.
*
* Completes the [Future] when the answer is known.
« no previous file with comments | « no previous file | tests/lib/async/stream_controller_async_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698