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

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

Issue 1177343004: Add Stream.empty. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Remove the null future "optimization". Fix some missing type parameters. Created 5 years, 6 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 | « sdk/lib/async/future_impl.dart ('k') | sdk/lib/async/stream_controller.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 77420d0b668a6298f3aeda736ed1f644e46541f9..3f11b91f080054036bd969eb3f7e77f3d32e12ff 100644
--- a/sdk/lib/async/stream.dart
+++ b/sdk/lib/async/stream.dart
@@ -75,6 +75,22 @@ abstract class Stream<T> {
Stream();
/**
+ * Internal use only. We do not want to promise that Stream stays const.
+ *
+ * If mixins become compatible with const constructors, we may use a
+ * stream mixin instead of extending Stream from a const class.
+ */
+ const Stream._internal();
+
+ /**
+ * Creates an empty broadcast stream.
+ *
+ * This is a stream which does nothing except sending a done event
+ * when it's listened to.
+ */
+ const factory Stream.empty() = _EmptyStream<T>;
+
+ /**
* Creates a new single-subscription stream from the future.
*
* When the future completes, the stream will fire one event, either
@@ -1423,11 +1439,19 @@ class StreamView<T> extends Stream<T> {
/**
- * The target of a [Stream.pipe] call.
+ * Abstract interface for a "sink" accepting multiple entire streams.
*
- * The [Stream.pipe] call will pass itself to this object, and then return
- * the resulting [Future]. The pipe should complete the future when it's
- * done.
+ * A consumer can accept a number of consequtive streams using [addStream],
+ * and when no further data need to be added, the [close] method tells the
+ * consumer to complete its work and shut down.
+ *
+ * This class is not just a [Sink<Stream>] because it is also combined with
+ * other [Sink] classes, like it's combined with [EventSink] in the
+ * [StreamSink] class.
+ *
+ * The [Stream.pipe] accepts a `StreamConsumer` and will pass the stream
+ * to the consumer's [addStream] method. When that completes, it will
+ * call [close] and then complete its own returned future.
*/
abstract class StreamConsumer<S> {
/**
@@ -1435,22 +1459,38 @@ abstract class StreamConsumer<S> {
*
* Listens on [stream] and does something for each event.
*
- * The consumer may stop listening after an error, or it may consume
- * all the errors and only stop at a done event.
+ * Returns a future which is completed when the stream is done being added,
+ * and the consumer is ready to accept a new stream.
+ * No further calls to [addStream] or [close] should happen before the
+ * returned future has completed.
+ *
+ * The consumer may stop listening to the stream after an error,
+ * it may consume all the errors and only stop at a done event,
+ * or it may be canceled early if the receiver don't want any further events.
+ *
+ * If the consumer stops listening because of some error preventing it
+ * from continuing, it may report this error in the returned future,
+ * otherwise it will just complete the future with `null`.
*/
Future addStream(Stream<S> stream);
/**
- * Tell the consumer that no futher streams will be added.
+ * Tells the consumer that no futher streams will be added.
*
- * Returns a future that is completed when the consumer is done handling
- * events.
+ * This allows the consumer to complete any remaining work and release
+ * resources that are no longer needed
+ *
+ * Returns a future which is completed when the consumer has shut down.
+ * If cleaning up can fail, the error may be reported in the returned future,
+ * otherwise it completes with `null`.
*/
Future close();
}
/**
+ * A object that accepts stream events both synchronously and asynchronously.
+ *
* A [StreamSink] unifies the asynchronous methods from [StreamConsumer] and
* the synchronous methods from [EventSink].
*
@@ -1467,11 +1507,26 @@ abstract class StreamConsumer<S> {
*
* When [close] is called, it will return the [done] [Future].
*/
-abstract class StreamSink<S> implements StreamConsumer<S>, EventSink<S> {
+abstract class StreamSink<S> implements EventSink<S>, StreamConsumer<S> {
/**
- * As [EventSink.close], but returns a future.
+ * Tells the stream sink that no futher streams will be added.
+ *
+ * This allows the stream sink to complete any remaining work and release
+ * resources that are no longer needed
+ *
+ * Returns a future which is completed when the stream sink has shut down.
+ * If cleaning up can fail, the error may be reported in the returned future,
+ * otherwise it completes with `null`.
*
* Returns the same future as [done].
+ *
+ * The stream sink may close before the [close] method is called, either due
+ * to an error or because it is itself provding events to someone who has
+ * stopped listening. In that case, the [done] future is completed first,
+ * and the `close` method will return the `done` future when called.
+ *
+ * Unifies [StreamConsumer.close] and [EventSink.close] which both mark their
+ * object as not expecting any further events.
*/
Future close();
« no previous file with comments | « sdk/lib/async/future_impl.dart ('k') | sdk/lib/async/stream_controller.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698