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 * Utility function to attach a stack trace to an [error] if it doesn't have | 8 * Utility function to attach a stack trace to an [error] if it doesn't have |
9 * one already. | 9 * one already. |
10 */ | 10 */ |
11 _asyncError(Object error, Object stackTrace) { | 11 _asyncError(Object error, Object stackTrace) { |
12 if (stackTrace == null) return error; | 12 if (stackTrace == null) return error; |
13 if (getAttachedStackTrace(error) != null) return error; | 13 if (getAttachedStackTrace(error) != null) return error; |
14 _attachStackTrace(error, stackTrace); | 14 _attachStackTrace(error, stackTrace); |
15 return error; | 15 return error; |
16 } | 16 } |
17 | 17 |
18 /** Runs user code and takes actions depending on success or failure. */ | 18 /** Runs user code and takes actions depending on success or failure. */ |
19 _runUserCode(userCode(), | 19 _runUserCode(userCode(), |
20 onSuccess(value), | 20 onSuccess(value), |
21 onError(error, StackTrace stackTrace)) { | 21 onError(error, StackTrace stackTrace)) { |
22 try { | 22 try { |
23 onSuccess(userCode()); | 23 onSuccess(userCode()); |
24 } catch (e, s) { | 24 } catch (e, s) { |
25 onError(_asyncError(e, s), s); | 25 onError(_asyncError(e, s), s); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
| 29 /** Helper function to cancel a subscription and wait for the potential future, |
| 30 before completing with an error. */ |
| 31 void _cancelAndError(StreamSubscription subscription, |
| 32 _Future future, |
| 33 error, |
| 34 StackTrace stackTrace) { |
| 35 var cancelFuture = subscription.cancel(); |
| 36 if (cancelFuture is Future) { |
| 37 cancelFuture.whenComplete(() => future._completeError(error, stackTrace)); |
| 38 } else { |
| 39 future._completeError(error, stackTrace); |
| 40 } |
| 41 } |
| 42 |
29 /** Helper function to make an onError argument to [_runUserCode]. */ | 43 /** Helper function to make an onError argument to [_runUserCode]. */ |
30 _cancelAndError(StreamSubscription subscription, _Future future) => | 44 _cancelAndErrorClosure(StreamSubscription subscription, _Future future) => |
31 (error, StackTrace stackTrace) { | 45 ((error, StackTrace stackTrace) => _cancelAndError( |
32 subscription.cancel(); | 46 subscription, future, error, stackTrace)); |
33 future._completeError(error, stackTrace); | 47 |
34 }; | 48 /** Helper function to cancel a subscription and wait for the potential future, |
| 49 before completing with a value. */ |
| 50 void _cancelAndValue(StreamSubscription subscription, _Future future, value) { |
| 51 var cancelFuture = subscription.cancel(); |
| 52 if (cancelFuture is Future) { |
| 53 cancelFuture.whenComplete(() => future._complete(value)); |
| 54 } else { |
| 55 future._complete(value); |
| 56 } |
| 57 } |
35 | 58 |
36 | 59 |
37 /** | 60 /** |
38 * A [Stream] that forwards subscriptions to another stream. | 61 * A [Stream] that forwards subscriptions to another stream. |
39 * | 62 * |
40 * This stream implements [Stream], but forwards all subscriptions | 63 * This stream implements [Stream], but forwards all subscriptions |
41 * to an underlying stream, and wraps the returned subscription to | 64 * to an underlying stream, and wraps the returned subscription to |
42 * modify the events on the way. | 65 * modify the events on the way. |
43 * | 66 * |
44 * This class is intended for internal use only. | 67 * This class is intended for internal use only. |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 sink._addError(_asyncError(e, s), s); | 408 sink._addError(_asyncError(e, s), s); |
386 return null; | 409 return null; |
387 } | 410 } |
388 if (!isEqual) { | 411 if (!isEqual) { |
389 sink._add(inputEvent); | 412 sink._add(inputEvent); |
390 _previous = inputEvent; | 413 _previous = inputEvent; |
391 } | 414 } |
392 } | 415 } |
393 } | 416 } |
394 } | 417 } |
OLD | NEW |