| 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 dynamic _FutureOnValue<T>(T value); | 8 typedef dynamic _FutureOnValue<T>(T 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([value]); | 17 void complete([value]); |
| 18 | 18 |
| 19 void completeError(Object error, [StackTrace stackTrace]) { | 19 void completeError(Object error, [StackTrace stackTrace]) { |
| 20 if (error == null) throw new ArgumentError("Error must not be null"); | 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 = 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([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 | |
| 51 void complete([value]) { | 50 void complete([value]) { |
| 52 if (!future._mayComplete) throw new StateError("Future already completed"); | 51 if (!future._mayComplete) throw new StateError("Future already completed"); |
| 53 future._complete(value); | 52 future._complete(value); |
| 54 } | 53 } |
| 55 | 54 |
| 56 void _completeError(Object error, StackTrace stackTrace) { | 55 void _completeError(Object error, StackTrace stackTrace) { |
| 57 future._completeError(error, stackTrace); | 56 future._completeError(error, stackTrace); |
| 58 } | 57 } |
| 59 } | 58 } |
| 60 | 59 |
| (...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 } | 656 } |
| 658 }, onError: (e, s) { | 657 }, onError: (e, s) { |
| 659 if (timer.isActive) { | 658 if (timer.isActive) { |
| 660 timer.cancel(); | 659 timer.cancel(); |
| 661 result._completeError(e, s); | 660 result._completeError(e, s); |
| 662 } | 661 } |
| 663 }); | 662 }); |
| 664 return result; | 663 return result; |
| 665 } | 664 } |
| 666 } | 665 } |
| OLD | NEW |