| OLD | NEW |
| 1 // Copyright 2012 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 = false; | 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 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 34 /** | 34 /** |
| 35 * Exception handlers waiting for exceptions. | 35 * Exception handlers waiting for exceptions. |
| 36 */ | 36 */ |
| 37 final List<Function> _exceptionHandlers; | 37 final List<Function> _exceptionHandlers; |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * Listeners waiting to be called when the future completes. | 40 * Listeners waiting to be called when the future completes. |
| 41 */ | 41 */ |
| 42 final List<Function> _completionListeners; | 42 final List<Function> _completionListeners; |
| 43 | 43 |
| 44 FutureImpl() | 44 _FutureImpl() |
| 45 : _successListeners = [], | 45 : _successListeners = [], |
| 46 _exceptionHandlers = [], | 46 _exceptionHandlers = [], |
| 47 _completionListeners = []; | 47 _completionListeners = []; |
| 48 | 48 |
| 49 factory FutureImpl.immediate(T value) { | 49 factory _FutureImpl.immediate(T value) { |
| 50 final res = new FutureImpl(); | 50 final res = new _FutureImpl(); |
| 51 res._setValue(value); | 51 res._setValue(value); |
| 52 return res; | 52 return res; |
| 53 } | 53 } |
| 54 | 54 |
| 55 T get value { | 55 T get value { |
| 56 if (!isComplete) { | 56 if (!isComplete) { |
| 57 throw new FutureNotCompleteException(); | 57 throw new FutureNotCompleteException(); |
| 58 } | 58 } |
| 59 if (_exception !== null) { | 59 if (_exception != null) { |
| 60 throw new FutureUnhandledException(_exception, stackTrace); | 60 throw new FutureUnhandledException(_exception, stackTrace); |
| 61 } | 61 } |
| 62 return _value; | 62 return _value; |
| 63 } | 63 } |
| 64 | 64 |
| 65 Object get exception { | 65 Object get exception { |
| 66 if (!isComplete) { | 66 if (!isComplete) { |
| 67 throw new FutureNotCompleteException(); | 67 throw new FutureNotCompleteException(); |
| 68 } | 68 } |
| 69 return _exception; | 69 return _exception; |
| 70 } | 70 } |
| 71 | 71 |
| 72 Object get stackTrace { | 72 Object get stackTrace { |
| 73 if (!isComplete) { | 73 if (!isComplete) { |
| 74 throw new FutureNotCompleteException(); | 74 throw new FutureNotCompleteException(); |
| 75 } | 75 } |
| 76 return _stackTrace; | 76 return _stackTrace; |
| 77 } | 77 } |
| 78 | 78 |
| 79 bool get isComplete { | 79 bool get isComplete { |
| 80 return _isComplete; | 80 return _isComplete; |
| 81 } | 81 } |
| 82 | 82 |
| 83 bool get hasValue { | 83 bool get hasValue { |
| 84 return isComplete && _exception === null; | 84 return isComplete && _exception == null; |
| 85 } | 85 } |
| 86 | 86 |
| 87 void then(void onSuccess(T value)) { | 87 void then(void onSuccess(T value)) { |
| 88 if (hasValue) { | 88 if (hasValue) { |
| 89 onSuccess(value); | 89 onSuccess(value); |
| 90 } else if (!isComplete) { | 90 } else if (!isComplete) { |
| 91 _successListeners.add(onSuccess); | 91 _successListeners.add(onSuccess); |
| 92 } else if (!_exceptionHandled) { | 92 } else if (!_exceptionHandled) { |
| 93 throw new FutureUnhandledException(_exception, stackTrace); | 93 throw new FutureUnhandledException(_exception, stackTrace); |
| 94 } | 94 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 112 } catch (e) {} | 112 } catch (e) {} |
| 113 } else { | 113 } else { |
| 114 _completionListeners.add(complete); | 114 _completionListeners.add(complete); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 void _complete() { | 118 void _complete() { |
| 119 _isComplete = true; | 119 _isComplete = true; |
| 120 | 120 |
| 121 try { | 121 try { |
| 122 if (_exception !== null) { | 122 if (_exception != null) { |
| 123 for (Function handler in _exceptionHandlers) { | 123 for (Function handler in _exceptionHandlers) { |
| 124 // Explicitly check for true here so that if the handler returns null, | 124 // Explicitly check for true here so that if the handler returns null, |
| 125 // we don't get an exception in checked mode. | 125 // we don't get an exception in checked mode. |
| 126 if (handler(_exception) == true) { | 126 if (handler(_exception) == true) { |
| 127 _exceptionHandled = true; | 127 _exceptionHandled = true; |
| 128 break; | 128 break; |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 | 132 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 150 | 150 |
| 151 void _setValue(T value) { | 151 void _setValue(T value) { |
| 152 if (_isComplete) { | 152 if (_isComplete) { |
| 153 throw new FutureAlreadyCompleteException(); | 153 throw new FutureAlreadyCompleteException(); |
| 154 } | 154 } |
| 155 _value = value; | 155 _value = value; |
| 156 _complete(); | 156 _complete(); |
| 157 } | 157 } |
| 158 | 158 |
| 159 void _setException(Object exception, Object stackTrace) { | 159 void _setException(Object exception, Object stackTrace) { |
| 160 if (exception === null) { | 160 if (exception == null) { |
| 161 // null is not a legal value for the exception of a Future. | 161 // null is not a legal value for the exception of a Future. |
| 162 throw new ArgumentError(null); | 162 throw new ArgumentError(null); |
| 163 } | 163 } |
| 164 if (_isComplete) { | 164 if (_isComplete) { |
| 165 throw new FutureAlreadyCompleteException(); | 165 throw new FutureAlreadyCompleteException(); |
| 166 } | 166 } |
| 167 _exception = exception; | 167 _exception = exception; |
| 168 _stackTrace = stackTrace; | 168 _stackTrace = stackTrace; |
| 169 _complete(); | 169 _complete(); |
| 170 } | 170 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 * Forwards the exception completion from [future] to [completer]. | 243 * Forwards the exception completion from [future] to [completer]. |
| 244 */ | 244 */ |
| 245 _forwardException(Future future, Completer completer) { | 245 _forwardException(Future future, Completer completer) { |
| 246 future.handleException((e) { | 246 future.handleException((e) { |
| 247 completer.completeException(e, future.stackTrace); | 247 completer.completeException(e, future.stackTrace); |
| 248 return true; | 248 return true; |
| 249 }); | 249 }); |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 | 252 |
| 253 class CompleterImpl<T> implements Completer<T> { | 253 class _CompleterImpl<T> implements Completer<T> { |
| 254 | 254 |
| 255 final FutureImpl<T> _futureImpl; | 255 final _FutureImpl<T> _futureImpl; |
| 256 | 256 |
| 257 CompleterImpl() : _futureImpl = new FutureImpl() {} | 257 _CompleterImpl() : _futureImpl = new _FutureImpl() {} |
| 258 | 258 |
| 259 Future<T> get future { | 259 Future<T> get future { |
| 260 return _futureImpl; | 260 return _futureImpl; |
| 261 } | 261 } |
| 262 | 262 |
| 263 void complete(T value) { | 263 void complete(T value) { |
| 264 _futureImpl._setValue(value); | 264 _futureImpl._setValue(value); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void completeException(Object exception, [Object stackTrace]) { | 267 void completeException(Object exception, [Object stackTrace]) { |
| 268 _futureImpl._setException(exception, stackTrace); | 268 _futureImpl._setException(exception, stackTrace); |
| 269 } | 269 } |
| 270 } | 270 } |
| 271 |
| OLD | NEW |