OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.async; | 5 part of dart.async; |
6 | 6 |
7 /** | 7 /** |
8 * An object representing a delayed computation. | 8 * An object representing a delayed computation. |
9 * | 9 * |
10 * A [Future] is used to represent a potential value, or error, | 10 * A [Future] is used to represent a potential value, or error, |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 }); | 232 }); |
233 return result; | 233 return result; |
234 } | 234 } |
235 | 235 |
236 /** | 236 /** |
237 * Wait for all the given futures to complete and collect their values. | 237 * Wait for all the given futures to complete and collect their values. |
238 * | 238 * |
239 * Returns a future which will complete once all the futures in a list are | 239 * Returns a future which will complete once all the futures in a list are |
240 * complete. If any of the futures in the list completes with an error, | 240 * complete. If any of the futures in the list completes with an error, |
241 * the resulting future also completes with an error. Otherwise the value | 241 * the resulting future also completes with an error. Otherwise the value |
242 * of the returned future will be a list of all the values that were produced. | 242 * of the returned future will be a list of all the values that were |
| 243 * produced. |
243 * | 244 * |
244 * If `eagerError` is true, the future completes with an error immediately on | 245 * If `eagerError` is true, the future completes with an error immediately on |
245 * the first error from one of the futures. Otherwise all futures must | 246 * the first error from one of the futures. Otherwise all futures must |
246 * complete before the returned future is completed (still with the first | 247 * complete before the returned future is completed (still with the first |
247 * error to occur, the remaining errors are silently dropped). | 248 * error to occur, the remaining errors are silently dropped). |
| 249 * |
| 250 * If [cleanUp] is provided, in the case of an error, any non-null result of |
| 251 * a successful future is passed to `cleanUp`, which can then release any |
| 252 * resources that the successful operation allocated. |
| 253 * |
| 254 * The call to `cleanUp` should not throw. If it does, the error will be an |
| 255 * uncaught asynchronous error. |
248 */ | 256 */ |
249 static Future<List> wait(Iterable<Future> futures, {bool eagerError: false}) { | 257 static Future<List> wait(Iterable<Future> futures, |
| 258 {bool eagerError: false, |
| 259 void cleanUp(successValue)}) { |
250 final _Future<List> result = new _Future<List>(); | 260 final _Future<List> result = new _Future<List>(); |
251 List values; // Collects the values. Set to null on error. | 261 List values; // Collects the values. Set to null on error. |
252 int remaining = 0; // How many futures are we waiting for. | 262 int remaining = 0; // How many futures are we waiting for. |
253 var error; // The first error from a future. | 263 var error; // The first error from a future. |
254 StackTrace stackTrace; // The stackTrace that came with the error. | 264 StackTrace stackTrace; // The stackTrace that came with the error. |
255 | 265 |
256 // Handle an error from any of the futures. | 266 // Handle an error from any of the futures. |
257 handleError(theError, theStackTrace) { | 267 void handleError(theError, theStackTrace) { |
258 final bool isFirstError = (values != null); | |
259 values = null; | |
260 remaining--; | 268 remaining--; |
261 if (isFirstError) { | 269 if (values != null) { |
| 270 if (cleanUp != null) { |
| 271 for (var value in values) { |
| 272 if (value != null) { |
| 273 // Ensure errors from cleanUp are uncaught. |
| 274 new Future.sync(() { cleanUp(value); }); |
| 275 } |
| 276 } |
| 277 } |
| 278 values = null; |
262 if (remaining == 0 || eagerError) { | 279 if (remaining == 0 || eagerError) { |
263 result._completeError(theError, theStackTrace); | 280 result._completeError(theError, theStackTrace); |
264 } else { | 281 } else { |
265 error = theError; | 282 error = theError; |
266 stackTrace = theStackTrace; | 283 stackTrace = theStackTrace; |
267 } | 284 } |
268 } else if (remaining == 0 && !eagerError) { | 285 } else if (remaining == 0 && !eagerError) { |
269 result._completeError(error, stackTrace); | 286 result._completeError(error, stackTrace); |
270 } | 287 } |
271 } | 288 } |
272 | 289 |
273 // As each future completes, put its value into the corresponding | 290 // As each future completes, put its value into the corresponding |
274 // position in the list of values. | 291 // position in the list of values. |
275 for (Future future in futures) { | 292 for (Future future in futures) { |
276 int pos = remaining++; | 293 int pos = remaining++; |
277 future.then((Object value) { | 294 future.then((Object value) { |
278 remaining--; | 295 remaining--; |
279 if (values != null) { | 296 if (values != null) { |
280 values[pos] = value; | 297 values[pos] = value; |
281 if (remaining == 0) { | 298 if (remaining == 0) { |
282 result._completeWithValue(values); | 299 result._completeWithValue(values); |
283 } | 300 } |
284 } else if (remaining == 0 && !eagerError) { | 301 } else { |
285 result._completeError(error, stackTrace); | 302 if (cleanUp != null && value != null) { |
| 303 // Ensure errors from cleanUp are uncaught. |
| 304 new Future.sync(() { cleanUp(value); }); |
| 305 } |
| 306 if (remaining == 0 && !eagerError) { |
| 307 result._completeError(error, stackTrace); |
| 308 } |
286 } | 309 } |
287 }, onError: handleError); | 310 }, onError: handleError); |
288 } | 311 } |
289 if (remaining == 0) { | 312 if (remaining == 0) { |
290 return new Future.value(const []); | 313 return new Future.value(const []); |
291 } | 314 } |
292 values = new List(remaining); | 315 values = new List(remaining); |
293 return result; | 316 return result; |
294 } | 317 } |
295 | 318 |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 if (replacement != null) { | 720 if (replacement != null) { |
698 error = _nonNullError(replacement.error); | 721 error = _nonNullError(replacement.error); |
699 stackTrace = replacement.stackTrace; | 722 stackTrace = replacement.stackTrace; |
700 } | 723 } |
701 result._completeError(error, stackTrace); | 724 result._completeError(error, stackTrace); |
702 } | 725 } |
703 | 726 |
704 /** Helper function that converts `null` to a [NullThrownError]. */ | 727 /** Helper function that converts `null` to a [NullThrownError]. */ |
705 Object _nonNullError(Object error) => | 728 Object _nonNullError(Object error) => |
706 (error != null) ? error : new NullThrownError(); | 729 (error != null) ? error : new NullThrownError(); |
OLD | NEW |