| 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 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 * | 43 * |
| 44 * If listeners are added after the stream has completed (sent a "done" event), | 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 | 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 | 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. | 47 * of view, the stream is completely inert when has completed. |
| 48 */ | 48 */ |
| 49 class StreamController<T> extends EventSink<T> { | 49 class StreamController<T> extends EventSink<T> { |
| 50 final _StreamImpl<T> stream; | 50 final _StreamImpl<T> stream; |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * A controller with a broadcast [stream].. | |
| 54 * | |
| 55 * The [onPauseStateChange] function is called when the stream becomes | |
| 56 * paused or resumes after being paused. The current pause state can | |
| 57 * be read from [isPaused]. Ignored if [:null:]. | |
| 58 * | |
| 59 * The [onSubscriptionStateChange] function is called when the stream | |
| 60 * receives its first listener or loses its last. The current subscription | |
| 61 * state can be read from [hasListener]. Ignored if [:null:]. | |
| 62 */ | |
| 63 StreamController.broadcast({void onPauseStateChange(), | |
| 64 void onSubscriptionStateChange()}) | |
| 65 : stream = new _MultiControllerStream<T>(onSubscriptionStateChange, | |
| 66 onPauseStateChange); | |
| 67 | |
| 68 /** | |
| 69 * A controller with a [stream] that supports only one single subscriber. | 53 * A controller with a [stream] that supports only one single subscriber. |
| 70 * | 54 * |
| 71 * The controller will buffer all incoming events until the subscriber is | 55 * The controller will buffer all incoming events until the subscriber is |
| 72 * registered. | 56 * registered. |
| 73 * | 57 * |
| 74 * The [onPauseStateChange] function is called when the stream becomes | 58 * The [onPauseStateChange] function is called when the stream becomes |
| 75 * paused or resumes after being paused. The current pause state can | 59 * paused or resumes after being paused. The current pause state can |
| 76 * be read from [isPaused]. Ignored if [:null:]. | 60 * be read from [isPaused]. Ignored if [:null:]. |
| 77 * | 61 * |
| 78 * The [onSubscriptionStateChange] function is called when the stream | 62 * The [onSubscriptionStateChange] function is called when the stream |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 * Send or enqueue a "done" message. | 118 * Send or enqueue a "done" message. |
| 135 * | 119 * |
| 136 * The "done" message should be sent at most once by a stream, and it | 120 * The "done" message should be sent at most once by a stream, and it |
| 137 * should be the last message sent. | 121 * should be the last message sent. |
| 138 */ | 122 */ |
| 139 void close() { stream._close(); } | 123 void close() { stream._close(); } |
| 140 } | 124 } |
| 141 | 125 |
| 142 typedef void _NotificationHandler(); | 126 typedef void _NotificationHandler(); |
| 143 | 127 |
| 144 class _MultiControllerStream<T> extends _MultiStreamImpl<T> { | |
| 145 _NotificationHandler _subscriptionHandler; | |
| 146 _NotificationHandler _pauseHandler; | |
| 147 | |
| 148 _MultiControllerStream(this._subscriptionHandler, this._pauseHandler); | |
| 149 | |
| 150 void _onSubscriptionStateChange() { | |
| 151 if (_subscriptionHandler != null) { | |
| 152 try { | |
| 153 _subscriptionHandler(); | |
| 154 } catch (e, s) { | |
| 155 new AsyncError(e, s).throwDelayed(); | |
| 156 } | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 void _onPauseStateChange() { | |
| 161 if (_pauseHandler != null) { | |
| 162 try { | |
| 163 _pauseHandler(); | |
| 164 } catch (e, s) { | |
| 165 new AsyncError(e, s).throwDelayed(); | |
| 166 } | |
| 167 } | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 class _SingleControllerStream<T> extends _SingleStreamImpl<T> { | 128 class _SingleControllerStream<T> extends _SingleStreamImpl<T> { |
| 172 _NotificationHandler _subscriptionHandler; | 129 _NotificationHandler _subscriptionHandler; |
| 173 _NotificationHandler _pauseHandler; | 130 _NotificationHandler _pauseHandler; |
| 174 | 131 |
| 175 _SingleControllerStream(this._subscriptionHandler, this._pauseHandler); | 132 _SingleControllerStream(this._subscriptionHandler, this._pauseHandler); |
| 176 | 133 |
| 177 void _onSubscriptionStateChange() { | 134 void _onSubscriptionStateChange() { |
| 178 if (_subscriptionHandler != null) { | 135 if (_subscriptionHandler != null) { |
| 179 try { | 136 try { |
| 180 _subscriptionHandler(); | 137 _subscriptionHandler(); |
| 181 } catch (e, s) { | 138 } catch (e, s) { |
| 182 new AsyncError(e, s).throwDelayed(); | 139 new AsyncError(e, s).throwDelayed(); |
| 183 } | 140 } |
| 184 } | 141 } |
| 185 } | 142 } |
| 186 | 143 |
| 187 void _onPauseStateChange() { | 144 void _onPauseStateChange() { |
| 188 if (_pauseHandler != null) { | 145 if (_pauseHandler != null) { |
| 189 try { | 146 try { |
| 190 _pauseHandler(); | 147 _pauseHandler(); |
| 191 } catch (e, s) { | 148 } catch (e, s) { |
| 192 new AsyncError(e, s).throwDelayed(); | 149 new AsyncError(e, s).throwDelayed(); |
| 193 } | 150 } |
| 194 } | 151 } |
| 195 } | 152 } |
| 196 } | 153 } |
| OLD | NEW |