Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 // Dart core library. | 2 // Dart core library. |
| 3 | 3 |
| 4 class FutureImpl<T> implements Future<T> { | 4 class FutureImpl<T> implements Future<T> { |
| 5 | 5 |
| 6 bool _isComplete; | 6 bool _isComplete = false; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Value that was provided to this Future by the Completer | 9 * Value that was provided to this Future by the Completer |
| 10 */ | 10 */ |
| 11 T _value; | 11 T _value; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Exception that occured, if there was a problem providing | 14 * Exception that occured, if there was a problem providing |
| 15 * Value. | 15 * Value. |
| 16 */ | 16 */ |
| 17 Object _exception; | 17 Object _exception; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * true, if any onException handler handled the exception. | 20 * true, if any onException handler handled the exception. |
| 21 */ | 21 */ |
| 22 bool _exceptionHandled; | 22 bool _exceptionHandled = false; |
|
Siggi Cherem (dart-lang)
2012/04/27 22:32:51
yay!
| |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * Listeners waiting to receive the value of this future. | 25 * Listeners waiting to receive the value of this future. |
| 26 */ | 26 */ |
| 27 final List<Function> _listeners; | 27 final List<Function> _listeners; |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * Exception handlers waiting for exceptions. | 30 * Exception handlers waiting for exceptions. |
| 31 */ | 31 */ |
| 32 final List<Function> _exceptionHandlers; | 32 final List<Function> _exceptionHandlers; |
| 33 | 33 |
| 34 FutureImpl() : _listeners = new List(), _exceptionHandlers = new List() { | 34 FutureImpl() |
| 35 _isComplete = false; | 35 : _listeners = [], |
| 36 _exceptionHandled = false; | 36 _exceptionHandlers = []; |
| 37 } | |
| 38 | 37 |
| 39 factory FutureImpl.immediate(T value) { | 38 factory FutureImpl.immediate(T value) { |
| 40 final res = new FutureImpl(); | 39 final res = new FutureImpl(); |
| 41 res._setValue(value); | 40 res._setValue(value); |
| 42 return res; | 41 return res; |
| 43 } | 42 } |
| 44 | 43 |
| 45 T get value() { | 44 T get value() { |
| 46 if (!isComplete) { | 45 if (!isComplete) { |
| 47 throw new FutureNotCompleteException(); | 46 throw new FutureNotCompleteException(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 } | 84 } |
| 86 } else { | 85 } else { |
| 87 _exceptionHandlers.add(onException); | 86 _exceptionHandlers.add(onException); |
| 88 } | 87 } |
| 89 } | 88 } |
| 90 | 89 |
| 91 void _complete() { | 90 void _complete() { |
| 92 _isComplete = true; | 91 _isComplete = true; |
| 93 if (_exception !== null) { | 92 if (_exception !== null) { |
| 94 for (Function handler in _exceptionHandlers) { | 93 for (Function handler in _exceptionHandlers) { |
| 95 if (handler(_exception)) { | 94 // Explicitly check for true here so that if the handler returns null, |
|
Siggi Cherem (dart-lang)
2012/04/27 22:32:51
booo! :(
| |
| 95 // we don't get an exception in checked mode. | |
| 96 if (handler(_exception) == true) { | |
| 96 _exceptionHandled = true; | 97 _exceptionHandled = true; |
| 97 break; | 98 break; |
| 98 } | 99 } |
| 99 } | 100 } |
| 100 } | 101 } |
| 102 | |
| 101 if (hasValue) { | 103 if (hasValue) { |
| 102 for (Function listener in _listeners) { | 104 for (Function listener in _listeners) { |
| 103 listener(value); | 105 listener(value); |
| 104 } | 106 } |
| 105 } else { | 107 } else { |
| 106 if (!_exceptionHandled && _listeners.length > 0) { | 108 if (!_exceptionHandled && _listeners.length > 0) { |
| 107 throw _exception; | 109 throw _exception; |
| 108 } | 110 } |
| 109 } | 111 } |
| 110 } | 112 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 } | 185 } |
| 184 | 186 |
| 185 void complete(T value) { | 187 void complete(T value) { |
| 186 _futureImpl._setValue(value); | 188 _futureImpl._setValue(value); |
| 187 } | 189 } |
| 188 | 190 |
| 189 void completeException(var exception) { | 191 void completeException(var exception) { |
| 190 _futureImpl._setException(exception); | 192 _futureImpl._setException(exception); |
| 191 } | 193 } |
| 192 } | 194 } |
| OLD | NEW |