| 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 /** The onValue and onError handlers return either a value or a future */ | 7 /** The onValue and onError handlers return either a value or a future */ |
| 8 typedef FutureOr<T> _FutureOnValue<S, T>(S value); | 8 typedef FutureOr<T> _FutureOnValue<S, T>(S value); |
| 9 /** Test used by [Future.catchError] to handle skip some errors. */ | 9 /** Test used by [Future.catchError] to handle skip some errors. */ |
| 10 typedef bool _FutureErrorTest(var error); | 10 typedef bool _FutureErrorTest(var error); |
| 11 /** Used by [WhenFuture]. */ | 11 /** Used by [WhenFuture]. */ |
| 12 typedef _FutureAction(); | 12 typedef _FutureAction(); |
| 13 | 13 |
| 14 abstract class _Completer<T> implements Completer<T> { | 14 abstract class _Completer<T> implements Completer<T> { |
| 15 final _Future<T> future = new _Future<T>(); | 15 final _Future<T> future = new _Future<T>(); |
| 16 | 16 |
| 17 void complete([FutureOr<T> value]); | 17 void complete([value]); |
| 18 | 18 |
| 19 void completeError(Object error, [StackTrace stackTrace]) { | 19 void completeError(Object error, [StackTrace stackTrace]) { |
| 20 error = _nonNullError(error); | 20 error = _nonNullError(error); |
| 21 if (!future._mayComplete) throw new StateError("Future already completed"); | 21 if (!future._mayComplete) throw new StateError("Future already completed"); |
| 22 AsyncError replacement = Zone.current.errorCallback(error, stackTrace); | 22 AsyncError replacement = Zone.current.errorCallback(error, stackTrace); |
| 23 if (replacement != null) { | 23 if (replacement != null) { |
| 24 error = _nonNullError(replacement.error); | 24 error = _nonNullError(replacement.error); |
| 25 stackTrace = replacement.stackTrace; | 25 stackTrace = replacement.stackTrace; |
| 26 } | 26 } |
| 27 _completeError(error, stackTrace); | 27 _completeError(error, stackTrace); |
| 28 } | 28 } |
| 29 | 29 |
| 30 void _completeError(Object error, StackTrace stackTrace); | 30 void _completeError(Object error, StackTrace stackTrace); |
| 31 | 31 |
| 32 // The future's _isComplete doesn't take into account pending completions. | 32 // The future's _isComplete doesn't take into account pending completions. |
| 33 // We therefore use _mayComplete. | 33 // We therefore use _mayComplete. |
| 34 bool get isCompleted => !future._mayComplete; | 34 bool get isCompleted => !future._mayComplete; |
| 35 } | 35 } |
| 36 | 36 |
| 37 class _AsyncCompleter<T> extends _Completer<T> { | 37 class _AsyncCompleter<T> extends _Completer<T> { |
| 38 | 38 |
| 39 void complete([FutureOr<T> value]) { | 39 void complete([value]) { |
| 40 if (!future._mayComplete) throw new StateError("Future already completed"); | 40 if (!future._mayComplete) throw new StateError("Future already completed"); |
| 41 future._asyncComplete(value); | 41 future._asyncComplete(value); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void _completeError(Object error, StackTrace stackTrace) { | 44 void _completeError(Object error, StackTrace stackTrace) { |
| 45 future._asyncCompleteError(error, stackTrace); | 45 future._asyncCompleteError(error, stackTrace); |
| 46 } | 46 } |
| 47 } | 47 } |
| 48 | 48 |
| 49 class _SyncCompleter<T> extends _Completer<T> { | 49 class _SyncCompleter<T> extends _Completer<T> { |
| 50 void complete([FutureOr<T> value]) { | 50 void complete([value]) { |
| 51 if (!future._mayComplete) throw new StateError("Future already completed"); | 51 if (!future._mayComplete) throw new StateError("Future already completed"); |
| 52 future._complete(value); | 52 future._complete(value); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void _completeError(Object error, StackTrace stackTrace) { | 55 void _completeError(Object error, StackTrace stackTrace) { |
| 56 future._completeError(error, stackTrace); | 56 future._completeError(error, stackTrace); |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 class _FutureListener<S, T> { | 60 class _FutureListener<S, T> { |
| (...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 } | 737 } |
| 738 }, onError: (e, s) { | 738 }, onError: (e, s) { |
| 739 if (timer.isActive) { | 739 if (timer.isActive) { |
| 740 timer.cancel(); | 740 timer.cancel(); |
| 741 result._completeError(e, s); | 741 result._completeError(e, s); |
| 742 } | 742 } |
| 743 }); | 743 }); |
| 744 return result; | 744 return result; |
| 745 } | 745 } |
| 746 } | 746 } |
| OLD | NEW |