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

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

Issue 18271015: Add Stream.join (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 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 9ce21c6f926f302da3be42e3ff5816b3f96ca2b7..d63dc7c2f8659431db5b8873090e6b69cd9892f3 100644
--- a/sdk/lib/async/stream.dart
+++ b/sdk/lib/async/stream.dart
@@ -328,6 +328,44 @@ abstract class Stream<T> {
}
/**
+ * Collects string of data events' string representations.
+ *
+ * If [separator] is provided, it is inserted between any two
+ * elements.
+ *
+ * Any error in the stream causes the future to complete with that
+ * error. Otherwise it completes with the collected string when
+ * the "done" event arrives.
+ */
+ Future<String> join([String separator = ""]) {
+ _FutureImpl<String> result = new _FutureImpl<String>();
+ StringBuffer buffer = new StringBuffer();
+ StreamSubscription subscription;
+ bool first = true;
+ subscription = this.listen(
+ (T element) {
+ if (!first) {
+ buffer.write(separator);
+ }
+ first = false;
+ try {
+ buffer.write(element);
+ } catch (e, s) {
+ subscription.cancel();
+ result._setError(_asyncError(e, s));
+ }
+ },
+ onError: (e) {
+ result._setError(e);
+ },
+ onDone: () {
+ result._setValue(buffer.toString());
+ },
+ cancelOnError: true);
+ return result;
+ }
+
+ /**
* Checks whether [needle] occurs in the 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