| OLD | NEW |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 // Dart core library. | 2 // Dart core library. |
| 3 | 3 |
| 4 /** | 4 /** |
| 5 * Thrown if client tries to obtain value or exception | 5 * Thrown if client tries to obtain value or exception |
| 6 * before a future has completed. | 6 * before a future has completed. |
| 7 */ | 7 */ |
| 8 class FutureNotCompleteException implements Exception { | 8 class FutureNotCompleteException implements Exception { |
| 9 FutureNotCompleteException() {} | 9 FutureNotCompleteException() {} |
| 10 String toString() => "Exception: future has not been completed"; | 10 String toString() => "Exception: future has not been completed"; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 /** | 48 /** |
| 49 * Exception handlers waiting for exceptions. | 49 * Exception handlers waiting for exceptions. |
| 50 */ | 50 */ |
| 51 final List<Function> _exceptionHandlers; | 51 final List<Function> _exceptionHandlers; |
| 52 | 52 |
| 53 FutureImpl() : _listeners = new List(), _exceptionHandlers = new List() { | 53 FutureImpl() : _listeners = new List(), _exceptionHandlers = new List() { |
| 54 _isComplete = false; | 54 _isComplete = false; |
| 55 _exceptionHandled = false; | 55 _exceptionHandled = false; |
| 56 } | 56 } |
| 57 | 57 |
| 58 FutureImpl.immediate(T value) : this() { |
| 59 _setValue(value); |
| 60 } |
| 61 |
| 58 T get value() { | 62 T get value() { |
| 59 if (!isComplete) { | 63 if (!isComplete) { |
| 60 throw new FutureNotCompleteException(); | 64 throw new FutureNotCompleteException(); |
| 61 } | 65 } |
| 62 if (_exception !== null) { | 66 if (_exception !== null) { |
| 63 throw _exception; | 67 throw _exception; |
| 64 } | 68 } |
| 65 return _value; | 69 return _value; |
| 66 } | 70 } |
| 67 | 71 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 if (exception === null) { | 131 if (exception === null) { |
| 128 // null is not a legal value for the exception of a Future | 132 // null is not a legal value for the exception of a Future |
| 129 throw new IllegalArgumentException(null); | 133 throw new IllegalArgumentException(null); |
| 130 } | 134 } |
| 131 if (_isComplete) { | 135 if (_isComplete) { |
| 132 throw new FutureAlreadyCompleteException(); | 136 throw new FutureAlreadyCompleteException(); |
| 133 } | 137 } |
| 134 _exception = exception; | 138 _exception = exception; |
| 135 _complete(); | 139 _complete(); |
| 136 } | 140 } |
| 141 |
| 142 Future transform(Function transformation) { |
| 143 final completer = new Completer(); |
| 144 handleException((e) { |
| 145 completer.completeException(e); |
| 146 return true; |
| 147 }); |
| 148 then((v) { |
| 149 var transformed = null; |
| 150 try { |
| 151 transformed = transformation(v); |
| 152 } catch (final e) { |
| 153 completer.completeException(e); |
| 154 return; |
| 155 } |
| 156 completer.complete(transformed); |
| 157 }); |
| 158 return completer.future; |
| 159 } |
| 160 |
| 161 Future chain(Function transformation) { |
| 162 final completer = new Completer(); |
| 163 handleException((e) { |
| 164 completer.completeException(e); |
| 165 return true; |
| 166 }); |
| 167 then((v) { |
| 168 var future = null; |
| 169 try { |
| 170 future = transformation(v); |
| 171 } catch (final e) { |
| 172 completer.completeException(e); |
| 173 return; |
| 174 } |
| 175 future.handleException((e) { |
| 176 completer.completeException(e); |
| 177 return true; |
| 178 }); |
| 179 future.then((b) => completer.complete(b)); |
| 180 }); |
| 181 return completer.future; |
| 182 } |
| 137 } | 183 } |
| 138 | 184 |
| 139 class CompleterImpl<T> implements Completer<T> { | 185 class CompleterImpl<T> implements Completer<T> { |
| 140 | 186 |
| 141 final FutureImpl<T> _futureImpl; | 187 final FutureImpl<T> _futureImpl; |
| 142 | 188 |
| 143 CompleterImpl() : _futureImpl = new FutureImpl() {} | 189 CompleterImpl() : _futureImpl = new FutureImpl() {} |
| 144 | 190 |
| 145 Future<T> get future() { | 191 Future<T> get future() { |
| 146 return _futureImpl; | 192 return _futureImpl; |
| 147 } | 193 } |
| 148 | 194 |
| 149 void complete(T value) { | 195 void complete(T value) { |
| 150 _futureImpl._setValue(value); | 196 _futureImpl._setValue(value); |
| 151 } | 197 } |
| 152 | 198 |
| 153 void completeException(var exception) { | 199 void completeException(var exception) { |
| 154 _futureImpl._setException(exception); | 200 _futureImpl._setException(exception); |
| 155 } | 201 } |
| 156 } | 202 } |
| OLD | NEW |