| 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 } |
| 171 | 171 |
| 172 Future transform(Function transformation) { | 172 Future transform(Function transformation) { |
| 173 final completer = new Completer(); | 173 final completer = new Completer(); |
| 174 | 174 |
| 175 handleException((e) { | 175 _forwardException(this, completer); |
| 176 completer.completeException(e, stackTrace); | |
| 177 return true; | |
| 178 }); | |
| 179 | 176 |
| 180 then((v) { | 177 then((v) { |
| 181 var transformed = null; | 178 var transformed = null; |
| 182 try { | 179 try { |
| 183 transformed = transformation(v); | 180 transformed = transformation(v); |
| 184 } catch (e, stackTrace) { | 181 } catch (e, stackTrace) { |
| 185 completer.completeException(e, stackTrace); | 182 completer.completeException(e, stackTrace); |
| 186 return; | 183 return; |
| 187 } | 184 } |
| 188 completer.complete(transformed); | 185 completer.complete(transformed); |
| 189 }); | 186 }); |
| 190 | 187 |
| 191 return completer.future; | 188 return completer.future; |
| 192 } | 189 } |
| 193 | 190 |
| 194 Future chain(Function transformation) { | 191 Future chain(Function transformation) { |
| 195 final completer = new Completer(); | 192 final completer = new Completer(); |
| 196 handleException((e) { | 193 |
| 197 completer.completeException(e, this.stackTrace); | 194 _forwardException(this, completer); |
| 198 return true; | |
| 199 }); | |
| 200 then((v) { | 195 then((v) { |
| 201 var future = null; | 196 var future = null; |
| 202 try { | 197 try { |
| 203 future = transformation(v); | 198 future = transformation(v); |
| 204 } catch (ex, stackTrace) { | 199 } catch (ex, stackTrace) { |
| 205 completer.completeException(ex, stackTrace); | 200 completer.completeException(ex, stackTrace); |
| 206 return; | 201 return; |
| 207 } | 202 } |
| 208 future.handleException((e) { | 203 |
| 209 completer.completeException(e, future.stackTrace); | 204 _forward(future, completer); |
| 210 return true; | |
| 211 }); | |
| 212 future.then((b) => completer.complete(b)); | |
| 213 }); | 205 }); |
| 214 return completer.future; | 206 return completer.future; |
| 215 } | 207 } |
| 216 | 208 |
| 217 Future transformException(transformation(Object exception)) { | 209 Future transformException(transformation(Object exception)) { |
| 218 final completer = new Completer(); | 210 final completer = new Completer(); |
| 219 | 211 |
| 220 handleException((ex) { | 212 handleException((ex) { |
| 221 try { | 213 try { |
| 222 completer.complete(transformation(ex)); | 214 final result = transformation(ex); |
| 215 |
| 216 // If the transformation itself returns a future, then we will |
| 217 // complete to what that completes to. |
| 218 if (result is Future) { |
| 219 _forward(result, completer); |
| 220 } else { |
| 221 completer.complete(result); |
| 222 } |
| 223 } catch (innerException, stackTrace) { | 223 } catch (innerException, stackTrace) { |
| 224 completer.completeException(innerException, stackTrace); | 224 completer.completeException(innerException, stackTrace); |
| 225 } | 225 } |
| 226 return true; | 226 return true; |
| 227 }); | 227 }); |
| 228 | 228 |
| 229 then(completer.complete); | 229 then(completer.complete); |
| 230 | 230 |
| 231 return completer.future; | 231 return completer.future; |
| 232 } | 232 } |
| 233 |
| 234 /** |
| 235 * Forwards the success or error completion from [future] to [completer]. |
| 236 */ |
| 237 _forward(Future future, Completer completer) { |
| 238 _forwardException(future, completer); |
| 239 future.then(completer.complete); |
| 240 } |
| 241 |
| 242 /** |
| 243 * Forwards the exception completion from [future] to [completer]. |
| 244 */ |
| 245 _forwardException(Future future, Completer completer) { |
| 246 future.handleException((e) { |
| 247 completer.completeException(e, future.stackTrace); |
| 248 return true; |
| 249 }); |
| 250 } |
| 233 } | 251 } |
| 234 | 252 |
| 235 class CompleterImpl<T> implements Completer<T> { | 253 class CompleterImpl<T> implements Completer<T> { |
| 236 | 254 |
| 237 final FutureImpl<T> _futureImpl; | 255 final FutureImpl<T> _futureImpl; |
| 238 | 256 |
| 239 CompleterImpl() : _futureImpl = new FutureImpl() {} | 257 CompleterImpl() : _futureImpl = new FutureImpl() {} |
| 240 | 258 |
| 241 Future<T> get future { | 259 Future<T> get future { |
| 242 return _futureImpl; | 260 return _futureImpl; |
| 243 } | 261 } |
| 244 | 262 |
| 245 void complete(T value) { | 263 void complete(T value) { |
| 246 _futureImpl._setValue(value); | 264 _futureImpl._setValue(value); |
| 247 } | 265 } |
| 248 | 266 |
| 249 void completeException(Object exception, [Object stackTrace]) { | 267 void completeException(Object exception, [Object stackTrace]) { |
| 250 _futureImpl._setException(exception, stackTrace); | 268 _futureImpl._setException(exception, stackTrace); |
| 251 } | 269 } |
| 252 } | 270 } |
| OLD | NEW |