Chromium Code Reviews| 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 handleException((e) { | 174 handleException((e) { |
| 175 completer.completeException(e); | 175 completer.completeException(e, this.stackTrace); |
| 176 return true; | 176 return true; |
| 177 }); | 177 }); |
| 178 then((v) { | 178 then((v) { |
| 179 var transformed = null; | 179 var transformed = null; |
| 180 try { | 180 try { |
| 181 transformed = transformation(v); | 181 transformed = transformation(v); |
| 182 } catch (final ex, final stackTrace) { | 182 } catch (Exception ex, final stackTrace) { |
|
Siggi Cherem (dart-lang)
2012/07/03 19:13:46
weird, why is this needed? what if it was 'var' or
nweiz
2012/07/03 21:39:26
With "var" or "final", the stack trace isn't captu
Siggi Cherem (dart-lang)
2012/07/03 22:10:06
weird... if you haven't already, file a bug on the
| |
| 183 completer.completeException(ex, stackTrace); | 183 completer.completeException(ex, stackTrace); |
| 184 return; | 184 return; |
| 185 } | 185 } |
| 186 completer.complete(transformed); | 186 completer.complete(transformed); |
| 187 }); | 187 }); |
| 188 return completer.future; | 188 return completer.future; |
| 189 } | 189 } |
| 190 | 190 |
| 191 Future chain(Function transformation) { | 191 Future chain(Function transformation) { |
| 192 final completer = new Completer(); | 192 final completer = new Completer(); |
| 193 handleException((e) { | 193 handleException((e) { |
| 194 completer.completeException(e); | 194 completer.completeException(e, this.stackTrace); |
| 195 return true; | 195 return true; |
| 196 }); | 196 }); |
| 197 then((v) { | 197 then((v) { |
| 198 var future = null; | 198 var future = null; |
| 199 try { | 199 try { |
| 200 future = transformation(v); | 200 future = transformation(v); |
| 201 } catch (final ex, final stackTrace) { | 201 } catch (Exception ex, final stackTrace) { |
| 202 completer.completeException(ex, stackTrace); | 202 completer.completeException(ex, stackTrace); |
| 203 return; | 203 return; |
| 204 } | 204 } |
| 205 future.handleException((e) { | 205 future.handleException((e) { |
| 206 completer.completeException(e); | 206 completer.completeException(e, this.stackTrace); |
| 207 return true; | 207 return true; |
| 208 }); | 208 }); |
| 209 future.then((b) => completer.complete(b)); | 209 future.then((b) => completer.complete(b)); |
| 210 }); | 210 }); |
| 211 return completer.future; | 211 return completer.future; |
| 212 } | 212 } |
| 213 } | 213 } |
| 214 | 214 |
| 215 class CompleterImpl<T> implements Completer<T> { | 215 class CompleterImpl<T> implements Completer<T> { |
| 216 | 216 |
| 217 final FutureImpl<T> _futureImpl; | 217 final FutureImpl<T> _futureImpl; |
| 218 | 218 |
| 219 CompleterImpl() : _futureImpl = new FutureImpl() {} | 219 CompleterImpl() : _futureImpl = new FutureImpl() {} |
| 220 | 220 |
| 221 Future<T> get future() { | 221 Future<T> get future() { |
| 222 return _futureImpl; | 222 return _futureImpl; |
| 223 } | 223 } |
| 224 | 224 |
| 225 void complete(T value) { | 225 void complete(T value) { |
| 226 _futureImpl._setValue(value); | 226 _futureImpl._setValue(value); |
| 227 } | 227 } |
| 228 | 228 |
| 229 void completeException(Object exception, [Object stackTrace]) { | 229 void completeException(Object exception, [Object stackTrace]) { |
| 230 _futureImpl._setException(exception, stackTrace); | 230 _futureImpl._setException(exception, stackTrace); |
| 231 } | 231 } |
| 232 } | 232 } |
| OLD | NEW |