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 handleException((e) { | 174 |
175 completer.completeException(e, this.stackTrace); | 175 handleException((ex) { |
nweiz
2012/09/04 20:26:14
Why are you renaming this to "ex"? "catch (e)" is
Bob Nystrom
2012/09/04 21:14:43
Stale change. Removed.
| |
176 completer.completeException(ex, 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 (ex, stackTrace) { |
183 completer.completeException(ex, stackTrace); | 185 completer.completeException(ex, 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(bool transformation(Object exception)) { | |
218 final completer = new Completer(); | |
219 | |
220 handleException((ex) { | |
221 var result = false; | |
222 var resultStackTrace = stackTrace; | |
223 try { | |
224 result = transformation(ex); | |
225 } catch (innerException, innerStackTrace) { | |
226 ex = innerException; | |
227 resultStackTrace = innerStackTrace; | |
228 } | |
229 | |
230 if (result != true) { | |
231 completer.completeException(ex, resultStackTrace); | |
232 } | |
233 | |
234 return true; | |
235 }); | |
236 | |
237 then(completer.complete); | |
238 | |
239 return completer.future; | |
240 } | |
213 } | 241 } |
214 | 242 |
215 class CompleterImpl<T> implements Completer<T> { | 243 class CompleterImpl<T> implements Completer<T> { |
216 | 244 |
217 final FutureImpl<T> _futureImpl; | 245 final FutureImpl<T> _futureImpl; |
218 | 246 |
219 CompleterImpl() : _futureImpl = new FutureImpl() {} | 247 CompleterImpl() : _futureImpl = new FutureImpl() {} |
220 | 248 |
221 Future<T> get future() { | 249 Future<T> get future() { |
222 return _futureImpl; | 250 return _futureImpl; |
223 } | 251 } |
224 | 252 |
225 void complete(T value) { | 253 void complete(T value) { |
226 _futureImpl._setValue(value); | 254 _futureImpl._setValue(value); |
227 } | 255 } |
228 | 256 |
229 void completeException(Object exception, [Object stackTrace]) { | 257 void completeException(Object exception, [Object stackTrace]) { |
230 _futureImpl._setException(exception, stackTrace); | 258 _futureImpl._setException(exception, stackTrace); |
231 } | 259 } |
232 } | 260 } |
OLD | NEW |