| 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 if (error == null) throw new ArgumentError("Error must not be null"); |
| 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 = replacement.error; |
| 25 if (error == null) error = new NullThrownError(); |
| 25 stackTrace = replacement.stackTrace; | 26 stackTrace = replacement.stackTrace; |
| 26 } | 27 } |
| 27 _completeError(error, stackTrace); | 28 _completeError(error, stackTrace); |
| 28 } | 29 } |
| 29 | 30 |
| 30 void _completeError(Object error, StackTrace stackTrace); | 31 void _completeError(Object error, StackTrace stackTrace); |
| 31 | 32 |
| 32 // The future's _isComplete doesn't take into account pending completions. | 33 // The future's _isComplete doesn't take into account pending completions. |
| 33 // We therefore use _mayComplete. | 34 // We therefore use _mayComplete. |
| 34 bool get isCompleted => !future._mayComplete; | 35 bool get isCompleted => !future._mayComplete; |
| 35 } | 36 } |
| 36 | 37 |
| 37 class _AsyncCompleter<T> extends _Completer<T> { | 38 class _AsyncCompleter<T> extends _Completer<T> { |
| 38 | 39 |
| 39 void complete([value]) { | 40 void complete([value]) { |
| 40 if (!future._mayComplete) throw new StateError("Future already completed"); | 41 if (!future._mayComplete) throw new StateError("Future already completed"); |
| 41 future._asyncComplete(value); | 42 future._asyncComplete(value); |
| 42 } | 43 } |
| 43 | 44 |
| 44 void _completeError(Object error, StackTrace stackTrace) { | 45 void _completeError(Object error, StackTrace stackTrace) { |
| 45 future._asyncCompleteError(error, stackTrace); | 46 future._asyncCompleteError(error, stackTrace); |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 | 49 |
| 49 class _SyncCompleter<T> extends _Completer<T> { | 50 class _SyncCompleter<T> extends _Completer<T> { |
| 50 | |
| 51 void complete([value]) { | 51 void complete([value]) { |
| 52 if (!future._mayComplete) throw new StateError("Future already completed"); | 52 if (!future._mayComplete) throw new StateError("Future already completed"); |
| 53 future._complete(value); | 53 future._complete(value); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void _completeError(Object error, StackTrace stackTrace) { | 56 void _completeError(Object error, StackTrace stackTrace) { |
| 57 future._completeError(error, stackTrace); | 57 future._completeError(error, stackTrace); |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| (...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 } | 657 } |
| 658 }, onError: (e, s) { | 658 }, onError: (e, s) { |
| 659 if (timer.isActive) { | 659 if (timer.isActive) { |
| 660 timer.cancel(); | 660 timer.cancel(); |
| 661 result._completeError(e, s); | 661 result._completeError(e, s); |
| 662 } | 662 } |
| 663 }); | 663 }); |
| 664 return result; | 664 return result; |
| 665 } | 665 } |
| 666 } | 666 } |
| OLD | NEW |