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 * The call to `cleanUp` must not throw. | |
Søren Gjesse
2015/01/09 10:04:11
Please explain what happens if cleanUp actually th
Lasse Reichstein Nielsen
2015/01/09 10:52:09
Done.
| |
248 */ | 254 */ |
249 static Future<List> wait(Iterable<Future> futures, {bool eagerError: false}) { | 255 static Future<List> wait(Iterable<Future> futures, |
256 {bool eagerError: false, | |
257 void cleanUp(successValue)}) { | |
250 final _Future<List> result = new _Future<List>(); | 258 final _Future<List> result = new _Future<List>(); |
251 List values; // Collects the values. Set to null on error. | 259 List values; // Collects the values. Set to null on error. |
252 int remaining = 0; // How many futures are we waiting for. | 260 int remaining = 0; // How many futures are we waiting for. |
253 var error; // The first error from a future. | 261 var error; // The first error from a future. |
254 StackTrace stackTrace; // The stackTrace that came with the error. | 262 StackTrace stackTrace; // The stackTrace that came with the error. |
255 | 263 |
256 // Handle an error from any of the futures. | 264 // Handle an error from any of the futures. |
257 handleError(theError, theStackTrace) { | 265 void handleError(theError, theStackTrace) { |
258 final bool isFirstError = (values != null); | |
259 values = null; | |
260 remaining--; | 266 remaining--; |
261 if (isFirstError) { | 267 if (values != null) { |
268 if (cleanUp != null) { | |
269 for (var value in values) { | |
270 if (value != null) { | |
271 // Ensure errors from cleanUp are uncaught. | |
272 new Future.sync(() { cleanUp(value); }); | |
273 } | |
274 } | |
275 } | |
276 values = null; | |
262 if (remaining == 0 || eagerError) { | 277 if (remaining == 0 || eagerError) { |
263 result._completeError(theError, theStackTrace); | 278 result._completeError(theError, theStackTrace); |
264 } else { | 279 } else { |
265 error = theError; | 280 error = theError; |
266 stackTrace = theStackTrace; | 281 stackTrace = theStackTrace; |
267 } | 282 } |
268 } else if (remaining == 0 && !eagerError) { | 283 } else if (remaining == 0 && !eagerError) { |
269 result._completeError(error, stackTrace); | 284 result._completeError(error, stackTrace); |
270 } | 285 } |
271 } | 286 } |
272 | 287 |
273 // As each future completes, put its value into the corresponding | 288 // As each future completes, put its value into the corresponding |
274 // position in the list of values. | 289 // position in the list of values. |
275 for (Future future in futures) { | 290 for (Future future in futures) { |
276 int pos = remaining++; | 291 int pos = remaining++; |
277 future.then((Object value) { | 292 future.then((Object value) { |
278 remaining--; | 293 remaining--; |
279 if (values != null) { | 294 if (values != null) { |
280 values[pos] = value; | 295 values[pos] = value; |
281 if (remaining == 0) { | 296 if (remaining == 0) { |
282 result._completeWithValue(values); | 297 result._completeWithValue(values); |
283 } | 298 } |
284 } else if (remaining == 0 && !eagerError) { | 299 } else { |
285 result._completeError(error, stackTrace); | 300 if (cleanUp != null && value != null) { |
301 // Ensure errors from cleanUp are uncaught. | |
302 new Future.sync(() { cleanUp(value); }); | |
303 } | |
304 if (remaining == 0 && !eagerError) { | |
305 result._completeError(error, stackTrace); | |
306 } | |
286 } | 307 } |
287 }, onError: handleError); | 308 }, onError: handleError); |
288 } | 309 } |
289 if (remaining == 0) { | 310 if (remaining == 0) { |
290 return new Future.value(const []); | 311 return new Future.value(const []); |
291 } | 312 } |
292 values = new List(remaining); | 313 values = new List(remaining); |
293 return result; | 314 return result; |
294 } | 315 } |
295 | 316 |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
697 if (replacement != null) { | 718 if (replacement != null) { |
698 error = _nonNullError(replacement.error); | 719 error = _nonNullError(replacement.error); |
699 stackTrace = replacement.stackTrace; | 720 stackTrace = replacement.stackTrace; |
700 } | 721 } |
701 result._completeError(error, stackTrace); | 722 result._completeError(error, stackTrace); |
702 } | 723 } |
703 | 724 |
704 /** Helper function that converts `null` to a [NullThrownError]. */ | 725 /** Helper function that converts `null` to a [NullThrownError]. */ |
705 Object _nonNullError(Object error) => | 726 Object _nonNullError(Object error) => |
706 (error != null) ? error : new NullThrownError(); | 727 (error != null) ? error : new NullThrownError(); |
OLD | NEW |