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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 bool get hasListener => stream._hasListener; | 112 bool get hasListener => stream._hasListener; |
113 | 113 |
114 /** | 114 /** |
115 * Send or queue a data event. | 115 * Send or queue a data event. |
116 */ | 116 */ |
117 void add(T value) => stream._add(value); | 117 void add(T value) => stream._add(value); |
118 | 118 |
119 /** | 119 /** |
120 * Send or enqueue an error event. | 120 * Send or enqueue an error event. |
121 * | 121 * |
122 * If [error] is not an [AsyncError], [error] and an optional [stackTrace] | |
123 * is combined into an [AsyncError] and sent this stream's listeners. | |
124 * | |
125 * Otherwise, if [error] is an [AsyncError], it is used directly as the | |
126 * error object reported to listeners, and the [stackTrace] is ignored. | |
127 * | |
128 * If a subscription has requested to be unsubscribed on errors, | 122 * If a subscription has requested to be unsubscribed on errors, |
129 * it will be unsubscribed after receiving this event. | 123 * it will be unsubscribed after receiving this event. |
130 */ | 124 */ |
131 void addError(Object error, [Object stackTrace]) { | 125 void addError(Object error, [Object stackTrace]) { |
132 AsyncError asyncError; | 126 if (stackTrace != null) { |
133 if (error is AsyncError) { | 127 // Force stack trace overwrite. Even if the error already contained |
134 asyncError = error; | 128 // a stack trace. |
135 } else { | 129 _attachStackTrace(error, stackTrace); |
136 asyncError = new AsyncError(error, stackTrace); | |
137 } | 130 } |
138 stream._addError(asyncError); | 131 stream._addError(error); |
139 } | 132 } |
140 | 133 |
141 /** | 134 /** |
142 * Send or enqueue a "done" message. | 135 * Send or enqueue a "done" message. |
143 * | 136 * |
144 * 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 |
145 * should be the last message sent. | 138 * should be the last message sent. |
146 */ | 139 */ |
147 void close() { stream._close(); } | 140 void close() { stream._close(); } |
148 } | 141 } |
149 | 142 |
150 typedef void _NotificationHandler(); | 143 typedef void _NotificationHandler(); |
151 | 144 |
152 class _MultiControllerStream<T> extends _MultiStreamImpl<T> { | 145 class _MultiControllerStream<T> extends _MultiStreamImpl<T> { |
153 _NotificationHandler _onListen; | 146 _NotificationHandler _onListen; |
154 _NotificationHandler _onPause; | 147 _NotificationHandler _onPause; |
155 _NotificationHandler _onResume; | 148 _NotificationHandler _onResume; |
156 _NotificationHandler _onCancel; | 149 _NotificationHandler _onCancel; |
157 | 150 |
158 // TODO(floitsch): share this code with _SingleControllerStream. | 151 // TODO(floitsch): share this code with _SingleControllerStream. |
159 void _runGuarded(_NotificationHandler notificationHandler) { | 152 void _runGuarded(_NotificationHandler notificationHandler) { |
160 if (notificationHandler == null) return; | 153 if (notificationHandler == null) return; |
161 try { | 154 try { |
162 notificationHandler(); | 155 notificationHandler(); |
163 } catch (e, s) { | 156 } catch (e, s) { |
164 new AsyncError(e, s).throwDelayed(); | 157 _throwDelayed(e, s); |
165 } | 158 } |
166 } | 159 } |
167 | 160 |
168 _MultiControllerStream(this._onListen, | 161 _MultiControllerStream(this._onListen, |
169 this._onPause, | 162 this._onPause, |
170 this._onResume, | 163 this._onResume, |
171 this._onCancel); | 164 this._onCancel); |
172 | 165 |
173 void _onSubscriptionStateChange() { | 166 void _onSubscriptionStateChange() { |
174 _runGuarded(_hasListener ? _onListen : _onCancel); | 167 _runGuarded(_hasListener ? _onListen : _onCancel); |
175 } | 168 } |
176 | 169 |
177 void _onPauseStateChange() { | 170 void _onPauseStateChange() { |
178 _runGuarded(_isPaused ? _onPause : _onResume); | 171 _runGuarded(_isPaused ? _onPause : _onResume); |
179 } | 172 } |
180 } | 173 } |
181 | 174 |
182 class _SingleControllerStream<T> extends _SingleStreamImpl<T> { | 175 class _SingleControllerStream<T> extends _SingleStreamImpl<T> { |
183 _NotificationHandler _onListen; | 176 _NotificationHandler _onListen; |
184 _NotificationHandler _onPause; | 177 _NotificationHandler _onPause; |
185 _NotificationHandler _onResume; | 178 _NotificationHandler _onResume; |
186 _NotificationHandler _onCancel; | 179 _NotificationHandler _onCancel; |
187 | 180 |
188 // TODO(floitsch): share this code with _MultiControllerStream. | 181 // TODO(floitsch): share this code with _MultiControllerStream. |
189 _runGuarded(_NotificationHandler notificationHandler) { | 182 _runGuarded(_NotificationHandler notificationHandler) { |
190 if (notificationHandler == null) return; | 183 if (notificationHandler == null) return; |
191 try { | 184 try { |
192 notificationHandler(); | 185 notificationHandler(); |
193 } catch (e, s) { | 186 } catch (e, s) { |
194 new AsyncError(e, s).throwDelayed(); | 187 _throwDelayed(e, s); |
195 } | 188 } |
196 } | 189 } |
197 | 190 |
198 _SingleControllerStream(this._onListen, | 191 _SingleControllerStream(this._onListen, |
199 this._onPause, | 192 this._onPause, |
200 this._onResume, | 193 this._onResume, |
201 this._onCancel); | 194 this._onCancel); |
202 | 195 |
203 void _onSubscriptionStateChange() { | 196 void _onSubscriptionStateChange() { |
204 _runGuarded(_hasListener ? _onListen : _onCancel); | 197 _runGuarded(_hasListener ? _onListen : _onCancel); |
205 } | 198 } |
206 | 199 |
207 void _onPauseStateChange() { | 200 void _onPauseStateChange() { |
208 _runGuarded(_isPaused ? _onPause : _onResume); | 201 _runGuarded(_isPaused ? _onPause : _onResume); |
209 } | 202 } |
210 } | 203 } |
OLD | NEW |