Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // ------------------------------------------------------------------- | 7 // ------------------------------------------------------------------- |
| 8 // Core Stream types | 8 // Core Stream types |
| 9 // ------------------------------------------------------------------- | 9 // ------------------------------------------------------------------- |
| 10 | 10 |
| 11 /** | |
| 12 * A source of asynchronous data events. | |
| 13 * | |
| 14 * A Stream provides a sequence of events. Each event is either a data event or | |
| 15 * an error event, representing the result of a single computation. When the | |
| 16 * Stream is exhausted, it may send a single "done" event. | |
| 17 * | |
| 18 * You can [listen] on a stream to receive the events it sends. When you listen, | |
| 19 * you receive a [StreamSubscription] object that can be used to stop listening | |
| 20 * again, or to temporarily pause events from the stream. | |
|
floitsch
2013/01/14 16:42:45
-again- (remove)
Lasse Reichstein Nielsen
2013/01/15 09:08:54
Done.
| |
| 21 * | |
| 22 * When an event is fired, all listeners at that time are informed. | |
| 23 * If a listener is added or removed while an event is being fired, the change | |
|
floitsch
2013/01/14 16:42:45
maybe?
Subscription changes while an event is fire
Lasse Reichstein Nielsen
2013/01/15 09:08:54
I haven't talked about "subscriptions" as a concep
Lasse Reichstein Nielsen
2013/01/15 09:08:54
Also, not *really* true because subscriptions are
| |
| 24 * will only take effect after the event is completely fired. | |
| 25 * | |
| 26 * Streams always buffer events that come in while the Stream is paused, and | |
|
floitsch
2013/01/14 16:42:45
I wouldn't say this. Rather:
Streams always respec
Lasse Reichstein Nielsen
2013/01/15 09:08:54
Done.
| |
| 27 * generally try to throttle the source of their events while they | |
| 28 * are paused to avoid bloating buffers. | |
| 29 * | |
| 30 * There are two kinds of streams: Single-subscription streams and | |
| 31 * multi-subscription streams. | |
| 32 * | |
| 33 * A single-subscription stream allows only a single listener in its entire | |
| 34 * life-cycle. It holds back events until it gets a listener, and it exhausts | |
| 35 * itself when the listener is unsubscribed, even if the stream wasn't done. | |
| 36 * | |
| 37 * Single-subscription streams are generally used for streaming parts of | |
| 38 * contiguous data like file I/O. | |
| 39 * | |
| 40 * A multi-subscription stream allows any number of listeners, and it fires | |
| 41 * its events when they are ready, whether there are listeners or not. | |
| 42 * | |
| 43 * Multi-subscription streams are used for independent events/observers. | |
| 44 */ | |
| 11 abstract class Stream<T> { | 45 abstract class Stream<T> { |
| 12 Stream(); | 46 Stream(); |
| 13 | 47 |
| 48 /** | |
| 49 * Creates a new single-subscription stream from the future. | |
| 50 * | |
| 51 * When the future completes, the stream will fire one event, either | |
| 52 * data or error, and then close with a done-event. | |
| 53 */ | |
| 14 factory Stream.fromFuture(Future<T> future) { | 54 factory Stream.fromFuture(Future<T> future) { |
| 15 _StreamImpl<T> stream = new _MultiStreamImpl<T>(); | 55 _StreamImpl<T> stream = new _MultiStreamImpl<T>(); |
| 16 future.then((value) { | 56 future.then((value) { |
| 17 stream._add(value); | 57 stream._add(value); |
| 18 stream._close(); | 58 stream._close(); |
| 19 }, | 59 }, |
| 20 onError: (error) { | 60 onError: (error) { |
| 21 stream._signalError(error); | 61 stream._signalError(error); |
| 22 stream._close(); | 62 stream._close(); |
| 23 }); | 63 }); |
| (...skipping 848 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 872 sink.signalError(error); | 912 sink.signalError(error); |
| 873 } | 913 } |
| 874 | 914 |
| 875 /** | 915 /** |
| 876 * Handle an incoming done event. | 916 * Handle an incoming done event. |
| 877 */ | 917 */ |
| 878 void handleDone(StreamSink<T> sink) { | 918 void handleDone(StreamSink<T> sink) { |
| 879 sink.close(); | 919 sink.close(); |
| 880 } | 920 } |
| 881 } | 921 } |
| OLD | NEW |