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 28 matching lines...) Expand all Loading... |
39 * Whether to invoke a callback depends only on the state before and after | 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 | 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 | 41 * times during the action, and then ends up in the same state as before, no |
42 * callback is performed. | 42 * callback is performed. |
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> implements StreamSink<T> { | 49 class StreamController<T> implements EventSink<T> { |
50 final _StreamImpl<T> stream; | 50 final _StreamImpl<T> stream; |
51 | 51 |
52 /** | 52 /** |
53 * A controller with a broadcast [stream].. | 53 * A controller with a broadcast [stream].. |
54 * | 54 * |
55 * The [onPauseStateChange] function is called when the stream becomes | 55 * The [onPauseStateChange] function is called when the stream becomes |
56 * paused or resumes after being paused. The current pause state can | 56 * paused or resumes after being paused. The current pause state can |
57 * be read from [isPaused]. Ignored if [:null:]. | 57 * be read from [isPaused]. Ignored if [:null:]. |
58 * | 58 * |
59 * The [onSubscriptionStateChange] function is called when the stream | 59 * The [onSubscriptionStateChange] function is called when the stream |
(...skipping 18 matching lines...) Expand all Loading... |
78 * The [onSubscriptionStateChange] function is called when the stream | 78 * The [onSubscriptionStateChange] function is called when the stream |
79 * receives its first listener or loses its last. The current subscription | 79 * receives its first listener or loses its last. The current subscription |
80 * state can be read from [hasSubscribers]. Ignored if [:null:]. | 80 * state can be read from [hasSubscribers]. Ignored if [:null:]. |
81 */ | 81 */ |
82 StreamController({void onPauseStateChange(), | 82 StreamController({void onPauseStateChange(), |
83 void onSubscriptionStateChange()}) | 83 void onSubscriptionStateChange()}) |
84 : stream = new _SingleControllerStream<T>(onSubscriptionStateChange, | 84 : stream = new _SingleControllerStream<T>(onSubscriptionStateChange, |
85 onPauseStateChange); | 85 onPauseStateChange); |
86 | 86 |
87 /** | 87 /** |
88 * Returns a view of this object that only exposes the [StreamSink] interface. | 88 * Returns a view of this object that only exposes the [EventSink] interface. |
89 */ | 89 */ |
90 StreamSink<T> get sink => new StreamSinkView<T>(this); | 90 EventSink<T> get sink => new EventSinkView<T>(this); |
91 | 91 |
92 /** | 92 /** |
93 * Whether the stream is closed for adding more events. | 93 * Whether the stream is closed for adding more events. |
94 * | 94 * |
95 * 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 |
96 * scheduled, and it is too late to add more events. | 96 * scheduled, and it is too late to add more events. |
97 */ | 97 */ |
98 bool get isClosed => stream._isClosed; | 98 bool get isClosed => stream._isClosed; |
99 | 99 |
100 /** Whether one or more active subscribers have requested a pause. */ | 100 /** Whether one or more active subscribers have requested a pause. */ |
(...skipping 12 matching lines...) Expand all Loading... |
113 * | 113 * |
114 * If [error] is not an [AsyncError], [error] and an optional [stackTrace] | 114 * If [error] is not an [AsyncError], [error] and an optional [stackTrace] |
115 * is combined into an [AsyncError] and sent this stream's listeners. | 115 * is combined into an [AsyncError] and sent this stream's listeners. |
116 * | 116 * |
117 * Otherwise, if [error] is an [AsyncError], it is used directly as the | 117 * Otherwise, if [error] is an [AsyncError], it is used directly as the |
118 * error object reported to listeners, and the [stackTrace] is ignored. | 118 * error object reported to listeners, and the [stackTrace] is ignored. |
119 * | 119 * |
120 * If a subscription has requested to be unsubscribed on errors, | 120 * If a subscription has requested to be unsubscribed on errors, |
121 * it will be unsubscribed after receiving this event. | 121 * it will be unsubscribed after receiving this event. |
122 */ | 122 */ |
123 void signalError(Object error, [Object stackTrace]) { | 123 void addError(Object error, [Object stackTrace]) { |
124 AsyncError asyncError; | 124 AsyncError asyncError; |
125 if (error is AsyncError) { | 125 if (error is AsyncError) { |
126 asyncError = error; | 126 asyncError = error; |
127 } else { | 127 } else { |
128 asyncError = new AsyncError(error, stackTrace); | 128 asyncError = new AsyncError(error, stackTrace); |
129 } | 129 } |
130 stream._signalError(asyncError); | 130 stream._addError(asyncError); |
| 131 } |
| 132 |
| 133 void signalError(Object error, [Object stackTrace]) { |
| 134 addError(error, stackTrace); |
131 } | 135 } |
132 | 136 |
133 /** | 137 /** |
134 * Send or enqueue a "done" message. | 138 * Send or enqueue a "done" message. |
135 * | 139 * |
136 * The "done" message should be sent at most once by a stream, and it | 140 * The "done" message should be sent at most once by a stream, and it |
137 * should be the last message sent. | 141 * should be the last message sent. |
138 */ | 142 */ |
139 void close() { stream._close(); } | 143 void close() { stream._close(); } |
140 } | 144 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 void _onPauseStateChange() { | 191 void _onPauseStateChange() { |
188 if (_pauseHandler != null) { | 192 if (_pauseHandler != null) { |
189 try { | 193 try { |
190 _pauseHandler(); | 194 _pauseHandler(); |
191 } catch (e, s) { | 195 } catch (e, s) { |
192 new AsyncError(e, s).throwDelayed(); | 196 new AsyncError(e, s).throwDelayed(); |
193 } | 197 } |
194 } | 198 } |
195 } | 199 } |
196 } | 200 } |
OLD | NEW |