OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 5 /** |
6 * Capture asynchronous results into synchronous values, and release them again. | 6 * Capture asynchronous results into synchronous values, and release them again. |
7 * | 7 * |
8 * Capturing a result (either a returned value or a thrown error) | 8 * Capturing a result (either a returned value or a thrown error) |
9 * means converting it into a [Result] - | 9 * means converting it into a [Result] - |
10 * either a [ValueResult] or an [ErrorResult]. | 10 * either a [ValueResult] or an [ErrorResult]. |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 /** The thrown object that this result represents. */ | 191 /** The thrown object that this result represents. */ |
192 final error; | 192 final error; |
193 /** The stack trace, if any, associated with the throw. */ | 193 /** The stack trace, if any, associated with the throw. */ |
194 final StackTrace stackTrace; | 194 final StackTrace stackTrace; |
195 /** Create an error result with the given [error] and [stackTrace]. */ | 195 /** Create an error result with the given [error] and [stackTrace]. */ |
196 ErrorResult(this.error, this.stackTrace); | 196 ErrorResult(this.error, this.stackTrace); |
197 bool get isValue => false; | 197 bool get isValue => false; |
198 bool get isError => true; | 198 bool get isError => true; |
199 ValueResult get asValue => null; | 199 ValueResult get asValue => null; |
200 ErrorResult get asError => this; | 200 ErrorResult get asError => this; |
201 void complete(Completer<T> completer) { | 201 void complete(Completer completer) { |
202 completer.completeError(error, stackTrace); | 202 completer.completeError(error, stackTrace); |
203 } | 203 } |
204 void addTo(EventSink<T> sink) { | 204 void addTo(EventSink sink) { |
205 sink.addError(error, stackTrace); | 205 sink.addError(error, stackTrace); |
206 } | 206 } |
207 Future get asFuture => new Future.error(error, stackTrace); | 207 Future get asFuture => new Future.error(error, stackTrace); |
208 } | 208 } |
209 | 209 |
210 /** | 210 /** |
211 * A stream transformer that captures a stream of events into [Result]s. | 211 * A stream transformer that captures a stream of events into [Result]s. |
212 * | 212 * |
213 * The result of the transformation is a stream of [Result] values and | 213 * The result of the transformation is a stream of [Result] values and |
214 * no error events. | 214 * no error events. |
(...skipping 16 matching lines...) Expand all Loading... |
231 * The result of the transformation is a stream of values and | 231 * The result of the transformation is a stream of values and |
232 * error events. | 232 * error events. |
233 */ | 233 */ |
234 class ReleaseStreamTransformer<T> implements StreamTransformer<Result<T>, T> { | 234 class ReleaseStreamTransformer<T> implements StreamTransformer<Result<T>, T> { |
235 const ReleaseStreamTransformer(); | 235 const ReleaseStreamTransformer(); |
236 | 236 |
237 Stream<T> bind(Stream<Result<T>> source) { | 237 Stream<T> bind(Stream<Result<T>> source) { |
238 return new Stream<T>.eventTransformed(source, _createSink); | 238 return new Stream<T>.eventTransformed(source, _createSink); |
239 } | 239 } |
240 | 240 |
241 static EventSink _createSink(EventSink<Result> sink) { | 241 static EventSink<Result> _createSink(EventSink sink) { |
242 return new ReleaseSink(sink); | 242 return new ReleaseSink(sink); |
243 } | 243 } |
244 } | 244 } |
245 | 245 |
246 /** | 246 /** |
247 * An event sink wrapper that captures the incoming events. | 247 * An event sink wrapper that captures the incoming events. |
248 * | 248 * |
249 * Wraps an [EventSink] that expects [Result] values. | 249 * Wraps an [EventSink] that expects [Result] values. |
250 * Accepts any value and error result, | 250 * Accepts any value and error result, |
251 * and passes them to the wrapped sink as [Result] values. | 251 * and passes them to the wrapped sink as [Result] values. |
252 * | 252 * |
253 * The wrapped sink will never receive an error event. | 253 * The wrapped sink will never receive an error event. |
254 */ | 254 */ |
255 class CaptureSink<T> implements EventSink<T> { | 255 class CaptureSink<T> implements EventSink<T> { |
256 final EventSink _sink; | 256 final EventSink _sink; |
257 | 257 |
258 CaptureSink(EventSink<Result<T>> sink) : _sink = sink; | 258 CaptureSink(EventSink<Result<T>> sink) : _sink = sink; |
259 void add(T value) { _sink.add(new ValueResult(value)); } | 259 void add(T value) { _sink.add(new ValueResult(value)); } |
260 void addError(Object error, StackTrace stackTrace) { | 260 void addError(Object error, [StackTrace stackTrace]) { |
261 _sink.add(new ErrorResult(error, stackTrace)); | 261 _sink.add(new ErrorResult(error, stackTrace)); |
262 } | 262 } |
263 void close() { _sink.close(); } | 263 void close() { _sink.close(); } |
264 } | 264 } |
265 | 265 |
266 /** | 266 /** |
267 * An event sink wrapper that releases the incoming result events. | 267 * An event sink wrapper that releases the incoming result events. |
268 * | 268 * |
269 * Wraps an output [EventSink] that expects any result. | 269 * Wraps an output [EventSink] that expects any result. |
270 * Accepts [Result] values, and puts the result value or error into the | 270 * Accepts [Result] values, and puts the result value or error into the |
271 * corresponding output sink add method. | 271 * corresponding output sink add method. |
272 */ | 272 */ |
273 class ReleaseSink<T> implements EventSink<Result<T>> { | 273 class ReleaseSink<T> implements EventSink<Result<T>> { |
274 final EventSink _sink; | 274 final EventSink _sink; |
275 ReleaseSink(EventSink<T> sink) : _sink = sink; | 275 ReleaseSink(EventSink<T> sink) : _sink = sink; |
276 void add(Result<T> result) { | 276 void add(Result<T> result) { |
277 if (result.isValue) { | 277 if (result.isValue) { |
278 _sink.add(result.asValue.value); | 278 _sink.add(result.asValue.value); |
279 } else { | 279 } else { |
280 ErrorResult error = result.asError; | 280 ErrorResult error = result.asError; |
281 _sink.addError(error.error, error.stackTrace); | 281 _sink.addError(error.error, error.stackTrace); |
282 } | 282 } |
283 } | 283 } |
284 void addError(Object error, StackTrace stackTrace) { | 284 void addError(Object error, [StackTrace stackTrace]) { |
285 // Errors may be added by intermediate processing, even if it is never | 285 // Errors may be added by intermediate processing, even if it is never |
286 // added by CaptureSink. | 286 // added by CaptureSink. |
287 _sink.addError(error, stackTrace); | 287 _sink.addError(error, stackTrace); |
288 } | 288 } |
289 | 289 |
290 void close() { _sink.close(); } | 290 void close() { _sink.close(); } |
291 } | 291 } |
OLD | NEW |