OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 } | 142 } |
143 | 143 |
144 /** | 144 /** |
145 * Reports whether this stream is a broadcast stream. | 145 * Reports whether this stream is a broadcast stream. |
146 */ | 146 */ |
147 bool get isBroadcast => false; | 147 bool get isBroadcast => false; |
148 | 148 |
149 /** | 149 /** |
150 * Returns a multi-subscription stream that produces the same events as this. | 150 * Returns a multi-subscription stream that produces the same events as this. |
151 * | 151 * |
| 152 * If this stream is already a broadcast stream, it is returned unmodified. |
| 153 * |
152 * If this stream is single-subscription, return a new stream that allows | 154 * If this stream is single-subscription, return a new stream that allows |
153 * multiple subscribers. It will subscribe to this stream when its first | 155 * multiple subscribers. It will subscribe to this stream when its first |
154 * subscriber is added, and unsubscribe again when the last subscription is | 156 * subscriber is added, and will stay subscribed until this stream ends, |
155 * canceled. | 157 * or a callback cancels the subscription. |
156 * | 158 * |
157 * If this stream is already a broadcast stream, it is returned unmodified. | 159 * If [onListen] is provided, it is called with a subscription-like object |
| 160 * that represents the underlying subscription to this stream. It is |
| 161 * possible to pause, resume or cancel the subscription during the call |
| 162 * to [onListen]. It is not possible to change the event handlers, including |
| 163 * using [StreamSubscription.asFuture]. |
| 164 * |
| 165 * If [onCancel] is provided, it is called in a similar way to [onListen] |
| 166 * when the returned stream stops having listener. If it later gets |
| 167 * a new listener, the [onListen] function is called again. |
| 168 * |
| 169 * Use the callbacks, for example, for pausing the underlying subscription |
| 170 * while having no subscribers to prevent losing events, or canceling the |
| 171 * subscription when there are no listeners. |
158 */ | 172 */ |
159 Stream<T> asBroadcastStream() { | 173 Stream<T> asBroadcastStream({ |
| 174 void onListen(StreamSubscription<T> subscription), |
| 175 void onCancel(StreamSubscription<T> subscription) }) { |
160 if (isBroadcast) return this; | 176 if (isBroadcast) return this; |
161 return new _AsBroadcastStream<T>(this); | 177 return new _AsBroadcastStream<T>(this, onListen, onCancel); |
162 } | 178 } |
163 | 179 |
164 /** | 180 /** |
165 * Adds a subscription to this stream. | 181 * Adds a subscription to this stream. |
166 * | 182 * |
167 * On each data event from this stream, the subscriber's [onData] handler | 183 * On each data event from this stream, the subscriber's [onData] handler |
168 * is called. If [onData] is null, nothing happens. | 184 * is called. If [onData] is null, nothing happens. |
169 * | 185 * |
170 * On errors from this stream, the [onError] handler is given a | 186 * On errors from this stream, the [onError] handler is given a |
171 * object describing the error. | 187 * object describing the error. |
(...skipping 730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 | 918 |
903 | 919 |
904 /** [Stream] wrapper that only exposes the [Stream] interface. */ | 920 /** [Stream] wrapper that only exposes the [Stream] interface. */ |
905 class StreamView<T> extends Stream<T> { | 921 class StreamView<T> extends Stream<T> { |
906 Stream<T> _stream; | 922 Stream<T> _stream; |
907 | 923 |
908 StreamView(this._stream); | 924 StreamView(this._stream); |
909 | 925 |
910 bool get isBroadcast => _stream.isBroadcast; | 926 bool get isBroadcast => _stream.isBroadcast; |
911 | 927 |
912 Stream<T> asBroadcastStream() => _stream.asBroadcastStream(); | 928 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), |
| 929 void onCancel(StreamSubscription subscription)}) |
| 930 => _stream.asBroadcastStream(onListen: onListen, onCancel: onCancel); |
913 | 931 |
914 StreamSubscription<T> listen(void onData(T value), | 932 StreamSubscription<T> listen(void onData(T value), |
915 { void onError(error), | 933 { void onError(error), |
916 void onDone(), | 934 void onDone(), |
917 bool cancelOnError }) { | 935 bool cancelOnError }) { |
918 return _stream.listen(onData, onError: onError, onDone: onDone, | 936 return _stream.listen(onData, onError: onError, onDone: onDone, |
919 cancelOnError: cancelOnError); | 937 cancelOnError: cancelOnError); |
920 } | 938 } |
921 } | 939 } |
922 | 940 |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1241 * | 1259 * |
1242 * If a [moveNext] call has been made, it will complete with `false` as value, | 1260 * If a [moveNext] call has been made, it will complete with `false` as value, |
1243 * as will all further calls to [moveNext]. | 1261 * as will all further calls to [moveNext]. |
1244 * | 1262 * |
1245 * If you need to stop listening for values before the stream iterator is | 1263 * If you need to stop listening for values before the stream iterator is |
1246 * automatically closed, you must call [cancel] to ensure that the stream | 1264 * automatically closed, you must call [cancel] to ensure that the stream |
1247 * is properly closed. | 1265 * is properly closed. |
1248 */ | 1266 */ |
1249 void cancel(); | 1267 void cancel(); |
1250 } | 1268 } |
OLD | NEW |