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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 } |
171 | 171 |
172 Future transform(Function transformation) { | 172 Future transform(Function transformation) { |
173 final completer = new Completer(); | 173 final completer = new Completer(); |
| 174 |
174 handleException((e) { | 175 handleException((e) { |
175 completer.completeException(e, this.stackTrace); | 176 completer.completeException(e, stackTrace); |
176 return true; | 177 return true; |
177 }); | 178 }); |
| 179 |
178 then((v) { | 180 then((v) { |
179 var transformed = null; | 181 var transformed = null; |
180 try { | 182 try { |
181 transformed = transformation(v); | 183 transformed = transformation(v); |
182 } catch (ex, stackTrace) { | 184 } catch (e, stackTrace) { |
183 completer.completeException(ex, stackTrace); | 185 completer.completeException(e, stackTrace); |
184 return; | 186 return; |
185 } | 187 } |
186 completer.complete(transformed); | 188 completer.complete(transformed); |
187 }); | 189 }); |
| 190 |
188 return completer.future; | 191 return completer.future; |
189 } | 192 } |
190 | 193 |
191 Future chain(Function transformation) { | 194 Future chain(Function transformation) { |
192 final completer = new Completer(); | 195 final completer = new Completer(); |
193 handleException((e) { | 196 handleException((e) { |
194 completer.completeException(e, this.stackTrace); | 197 completer.completeException(e, this.stackTrace); |
195 return true; | 198 return true; |
196 }); | 199 }); |
197 then((v) { | 200 then((v) { |
198 var future = null; | 201 var future = null; |
199 try { | 202 try { |
200 future = transformation(v); | 203 future = transformation(v); |
201 } catch (ex, stackTrace) { | 204 } catch (ex, stackTrace) { |
202 completer.completeException(ex, stackTrace); | 205 completer.completeException(ex, stackTrace); |
203 return; | 206 return; |
204 } | 207 } |
205 future.handleException((e) { | 208 future.handleException((e) { |
206 completer.completeException(e, future.stackTrace); | 209 completer.completeException(e, future.stackTrace); |
207 return true; | 210 return true; |
208 }); | 211 }); |
209 future.then((b) => completer.complete(b)); | 212 future.then((b) => completer.complete(b)); |
210 }); | 213 }); |
211 return completer.future; | 214 return completer.future; |
212 } | 215 } |
| 216 |
| 217 Future transformException(transformation(Object exception)) { |
| 218 final completer = new Completer(); |
| 219 |
| 220 handleException((ex) { |
| 221 try { |
| 222 completer.complete(transformation(ex)); |
| 223 } catch (innerException, stackTrace) { |
| 224 completer.completeException(innerException, stackTrace); |
| 225 } |
| 226 return true; |
| 227 }); |
| 228 |
| 229 then(completer.complete); |
| 230 |
| 231 return completer.future; |
| 232 } |
213 } | 233 } |
214 | 234 |
215 class CompleterImpl<T> implements Completer<T> { | 235 class CompleterImpl<T> implements Completer<T> { |
216 | 236 |
217 final FutureImpl<T> _futureImpl; | 237 final FutureImpl<T> _futureImpl; |
218 | 238 |
219 CompleterImpl() : _futureImpl = new FutureImpl() {} | 239 CompleterImpl() : _futureImpl = new FutureImpl() {} |
220 | 240 |
221 Future<T> get future() { | 241 Future<T> get future() { |
222 return _futureImpl; | 242 return _futureImpl; |
223 } | 243 } |
224 | 244 |
225 void complete(T value) { | 245 void complete(T value) { |
226 _futureImpl._setValue(value); | 246 _futureImpl._setValue(value); |
227 } | 247 } |
228 | 248 |
229 void completeException(Object exception, [Object stackTrace]) { | 249 void completeException(Object exception, [Object stackTrace]) { |
230 _futureImpl._setException(exception, stackTrace); | 250 _futureImpl._setException(exception, stackTrace); |
231 } | 251 } |
232 } | 252 } |
OLD | NEW |