| 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 // Controller for creating and adding events to a stream. | 8 // Controller for creating and adding events to a stream. |
| 9 // ------------------------------------------------------------------- | 9 // ------------------------------------------------------------------- |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * A controller with the stream it controls. | 12 * A controller with the stream it controls. |
| 13 * | 13 * |
| 14 * This controller allows sending data, error and done events on | 14 * This controller allows sending data, error and done events on |
| 15 * its [stream]. | 15 * its [stream]. |
| 16 * This class can be used to create a simple stream that others | 16 * This class can be used to create a simple stream that others |
| 17 * can listen on, and to push events to that stream. | 17 * can listen on, and to push events to that stream. |
| 18 * | 18 * |
| 19 * It's possible to check whether the stream is paused or not, and whether | 19 * It's possible to check whether the stream is paused or not, and whether |
| 20 * it has subscribers or not, as well as getting a callback when either of | 20 * it has subscribers or not, as well as getting a callback when either of |
| 21 * these change. | 21 * these change. |
| 22 * |
| 23 * If the stream starts or stops having listeners (first listener subscribing, |
| 24 * last listener unsubscribing), the `onSubscriptionStateChange` callback |
| 25 * is notified as soon as possible. If the subscription stat changes during |
| 26 * an event firing or a callback being executed, the change will not be reported |
| 27 * until the current event or callback has finished. |
| 28 * If the pause state has also changed during an event or callback, only the |
| 29 * subscription state callback is notified. |
| 30 * |
| 31 * If the subscriber state has not changed, but the pause state has, the |
| 32 * `onPauseStateChange` callback is notified as soon as possible, after firing |
| 33 * a current event or completing another callback. This happens if the stream |
| 34 * is not paused, and a listener pauses it, or if the stream has been resumed |
| 35 * from pause and has no pending events. If the listeners resume a paused stream |
| 36 * while it still has queued events, the controller will still consider the |
| 37 * stream paused until all queued events have been dispatched. |
| 38 * |
| 39 * Whether to invoke a callback depends only on the state before and after |
| 40 * a stream action, for example firing an event. If the state changes multiple |
| 41 * times during the action, and then ends up in the same state as before, no |
| 42 * callback is performed. |
| 43 * |
| 44 * If listeners are added after the stream has completed (sent a "done" event), |
| 45 * the listeners will be sent a "done" event eventually, but they won't affect |
| 46 * the stream at all, and won't trigger callbacks. From the controller's point |
| 47 * of view, the stream is completely inert when has completed. |
| 22 */ | 48 */ |
| 23 class StreamController<T> implements StreamSink<T> { | 49 class StreamController<T> implements StreamSink<T> { |
| 24 final _StreamImpl<T> stream; | 50 final _StreamImpl<T> stream; |
| 25 | 51 |
| 26 /** | 52 /** |
| 27 * A controller with a broadcast [stream].. | 53 * A controller with a broadcast [stream].. |
| 28 * | 54 * |
| 29 * The [onPauseStateChange] function is called when the stream becomes | 55 * The [onPauseStateChange] function is called when the stream becomes |
| 30 * paused or resumes after being paused. The current pause state can | 56 * paused or resumes after being paused. The current pause state can |
| 31 * be read from [isPaused]. Ignored if [:null:]. | 57 * be read from [isPaused]. Ignored if [:null:]. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 91 |
| 66 /** | 92 /** |
| 67 * Whether the stream is closed for adding more events. | 93 * Whether the stream is closed for adding more events. |
| 68 * | 94 * |
| 69 * If true, the "done" event might not have fired yet, but it has been | 95 * If true, the "done" event might not have fired yet, but it has been |
| 70 * scheduled, and it is too late to add more events. | 96 * scheduled, and it is too late to add more events. |
| 71 */ | 97 */ |
| 72 bool get isClosed => stream._isClosed; | 98 bool get isClosed => stream._isClosed; |
| 73 | 99 |
| 74 /** Whether one or more active subscribers have requested a pause. */ | 100 /** Whether one or more active subscribers have requested a pause. */ |
| 75 bool get isPaused => stream._isPaused; | 101 bool get isPaused => stream._isInputPaused; |
| 76 | 102 |
| 77 /** Whether there are currently any subscribers on this [Stream]. */ | 103 /** Whether there are currently any subscribers on this [Stream]. */ |
| 78 bool get hasSubscribers => stream._hasSubscribers; | 104 bool get hasSubscribers => stream._hasSubscribers; |
| 79 | 105 |
| 80 /** | 106 /** |
| 81 * Send or queue a data event. | 107 * Send or queue a data event. |
| 82 */ | 108 */ |
| 83 void add(T value) => stream._add(value); | 109 void add(T value) => stream._add(value); |
| 84 | 110 |
| 85 /** | 111 /** |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 _SingleControllerStream(this._subscriptionHandler, this._pauseHandler); | 163 _SingleControllerStream(this._subscriptionHandler, this._pauseHandler); |
| 138 | 164 |
| 139 void _onSubscriptionStateChange() { | 165 void _onSubscriptionStateChange() { |
| 140 if (_subscriptionHandler != null) _subscriptionHandler(); | 166 if (_subscriptionHandler != null) _subscriptionHandler(); |
| 141 } | 167 } |
| 142 | 168 |
| 143 void _onPauseStateChange() { | 169 void _onPauseStateChange() { |
| 144 if (_pauseHandler != null) _pauseHandler(); | 170 if (_pauseHandler != null) _pauseHandler(); |
| 145 } | 171 } |
| 146 } | 172 } |
| OLD | NEW |