| 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } | 151 } |
| 152 if (_isComplete) { | 152 if (_isComplete) { |
| 153 throw new FutureAlreadyCompleteException(); | 153 throw new FutureAlreadyCompleteException(); |
| 154 } | 154 } |
| 155 _exception = exception; | 155 _exception = exception; |
| 156 _complete(); | 156 _complete(); |
| 157 } | 157 } |
| 158 | 158 |
| 159 Future transform(Function transformation) { | 159 Future transform(Function transformation) { |
| 160 final completer = new Completer(); | 160 final completer = new Completer(); |
| 161 onComplete((f) { | 161 handleException((e) { |
| 162 if (!f.hasValue) { | 162 completer.completeException(e); |
| 163 completer.completeException(f.exception); | 163 return true; |
| 164 return; | 164 }); |
| 165 } | 165 then((v) { |
| 166 var transformed = null; | 166 var transformed = null; |
| 167 try { | 167 try { |
| 168 transformed = transformation(f.value); | 168 transformed = transformation(v); |
| 169 } catch (final e) { | 169 } catch (final e) { |
| 170 completer.completeException(e); | 170 completer.completeException(e); |
| 171 return; | 171 return; |
| 172 } | 172 } |
| 173 completer.complete(transformed); | 173 completer.complete(transformed); |
| 174 }); | 174 }); |
| 175 return completer.future; | 175 return completer.future; |
| 176 } | 176 } |
| 177 | 177 |
| 178 Future chain(Function transformation) { | 178 Future chain(Function transformation) { |
| 179 final completer = new Completer(); | 179 final completer = new Completer(); |
| 180 onComplete((f) { | 180 handleException((e) { |
| 181 if (!f.hasValue) { | 181 completer.completeException(e); |
| 182 completer.completeException(f.exception); | 182 return true; |
| 183 return; | 183 }); |
| 184 } | 184 then((v) { |
| 185 var future = null; | 185 var future = null; |
| 186 try { | 186 try { |
| 187 future = transformation(f.value); | 187 future = transformation(v); |
| 188 } catch (final e) { | 188 } catch (final e) { |
| 189 completer.completeException(e); | 189 completer.completeException(e); |
| 190 return; | 190 return; |
| 191 } | 191 } |
| 192 future.onComplete((g) => g.hasValue | 192 future.handleException((e) { |
| 193 ? completer.complete(g.value) | 193 completer.completeException(e); |
| 194 : completer.completeException(g.exception)); | 194 return true; |
| 195 }); |
| 196 future.then((b) => completer.complete(b)); |
| 195 }); | 197 }); |
| 196 return completer.future; | 198 return completer.future; |
| 197 } | 199 } |
| 198 } | 200 } |
| 199 | 201 |
| 200 class CompleterImpl<T> implements Completer<T> { | 202 class CompleterImpl<T> implements Completer<T> { |
| 201 | 203 |
| 202 final FutureImpl<T> _futureImpl; | 204 final FutureImpl<T> _futureImpl; |
| 203 | 205 |
| 204 CompleterImpl() : _futureImpl = new FutureImpl() {} | 206 CompleterImpl() : _futureImpl = new FutureImpl() {} |
| 205 | 207 |
| 206 Future<T> get future() { | 208 Future<T> get future() { |
| 207 return _futureImpl; | 209 return _futureImpl; |
| 208 } | 210 } |
| 209 | 211 |
| 210 void complete(T value) { | 212 void complete(T value) { |
| 211 _futureImpl._setValue(value); | 213 _futureImpl._setValue(value); |
| 212 } | 214 } |
| 213 | 215 |
| 214 void completeException(var exception) { | 216 void completeException(var exception) { |
| 215 _futureImpl._setException(exception); | 217 _futureImpl._setException(exception); |
| 216 } | 218 } |
| 217 } | 219 } |
| OLD | NEW |