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 * or to temporarily pause events from the stream. |
| 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 |
| 24 * will only take effect after the event is completely fired. |
| 25 * |
| 26 * Streams always respect "pause" requests. If necessary they need to buffer |
| 27 * their input, but often, and preferably, they can simply request their input |
| 28 * to pause too. |
| 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 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 sink.signalError(error); | 921 sink.signalError(error); |
882 } | 922 } |
883 | 923 |
884 /** | 924 /** |
885 * Handle an incoming done event. | 925 * Handle an incoming done event. |
886 */ | 926 */ |
887 void handleDone(StreamSink<T> sink) { | 927 void handleDone(StreamSink<T> sink) { |
888 sink.close(); | 928 sink.close(); |
889 } | 929 } |
890 } | 930 } |
OLD | NEW |