| 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 */ |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 } |
| OLD | NEW |