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