| 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 /// A type representing values that are either `Future<T>` or `T`. | 7 /// A type representing values that are either `Future<T>` or `T`. |
| 8 /// | 8 /// |
| 9 /// This class declaration is a public stand-in for an internal | 9 /// This class declaration is a public stand-in for an internal |
| 10 /// future-or-value generic type. References to this class are resolved to the | 10 /// future-or-value generic type. References to this class are resolved to the |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 * complete before the returned future is completed (still with the first | 287 * complete before the returned future is completed (still with the first |
| 288 * error to occur, the remaining errors are silently dropped). | 288 * error to occur, the remaining errors are silently dropped). |
| 289 * | 289 * |
| 290 * If [cleanUp] is provided, in the case of an error, any non-null result of | 290 * If [cleanUp] is provided, in the case of an error, any non-null result of |
| 291 * a successful future is passed to `cleanUp`, which can then release any | 291 * a successful future is passed to `cleanUp`, which can then release any |
| 292 * resources that the successful operation allocated. | 292 * resources that the successful operation allocated. |
| 293 * | 293 * |
| 294 * The call to `cleanUp` should not throw. If it does, the error will be an | 294 * The call to `cleanUp` should not throw. If it does, the error will be an |
| 295 * uncaught asynchronous error. | 295 * uncaught asynchronous error. |
| 296 */ | 296 */ |
| 297 static Future<List/*<T>*/> wait/*<T>*/(Iterable<Future/*<T>*/> futures, | 297 static Future<List<T>> wait<T>(Iterable<Future<T>> futures, |
| 298 {bool eagerError: false, | 298 {bool eagerError: false, |
| 299 void cleanUp(/*=T*/ successValue)}) { | 299 void cleanUp(T successValue)}) { |
| 300 final _Future<List/*<T>*/> result = new _Future<List/*<T>*/>(); | 300 final _Future<List<T>> result = new _Future<List<T>>(); |
| 301 List/*<T>*/ values; // Collects the values. Set to null on error. | 301 List<T> values; // Collects the values. Set to null on error. |
| 302 int remaining = 0; // How many futures are we waiting for. | 302 int remaining = 0; // How many futures are we waiting for. |
| 303 var error; // The first error from a future. | 303 var error; // The first error from a future. |
| 304 StackTrace stackTrace; // The stackTrace that came with the error. | 304 StackTrace stackTrace; // The stackTrace that came with the error. |
| 305 | 305 |
| 306 // Handle an error from any of the futures. | 306 // Handle an error from any of the futures. |
| 307 void handleError(theError, theStackTrace) { | 307 void handleError(theError, theStackTrace) { |
| 308 remaining--; | 308 remaining--; |
| 309 if (values != null) { | 309 if (values != null) { |
| 310 if (cleanUp != null) { | 310 if (cleanUp != null) { |
| 311 for (var value in values) { | 311 for (var value in values) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 325 } else if (remaining == 0 && !eagerError) { | 325 } else if (remaining == 0 && !eagerError) { |
| 326 result._completeError(error, stackTrace); | 326 result._completeError(error, stackTrace); |
| 327 } | 327 } |
| 328 } | 328 } |
| 329 | 329 |
| 330 try { | 330 try { |
| 331 // As each future completes, put its value into the corresponding | 331 // As each future completes, put its value into the corresponding |
| 332 // position in the list of values. | 332 // position in the list of values. |
| 333 for (Future future in futures) { | 333 for (Future future in futures) { |
| 334 int pos = remaining; | 334 int pos = remaining; |
| 335 future.then((Object/*=T*/ value) { | 335 future.then((T value) { |
| 336 remaining--; | 336 remaining--; |
| 337 if (values != null) { | 337 if (values != null) { |
| 338 values[pos] = value; | 338 values[pos] = value; |
| 339 if (remaining == 0) { | 339 if (remaining == 0) { |
| 340 result._completeWithValue(values); | 340 result._completeWithValue(values); |
| 341 } | 341 } |
| 342 } else { | 342 } else { |
| 343 if (cleanUp != null && value != null) { | 343 if (cleanUp != null && value != null) { |
| 344 // Ensure errors from cleanUp are uncaught. | 344 // Ensure errors from cleanUp are uncaught. |
| 345 new Future.sync(() { cleanUp(value); }); | 345 new Future.sync(() { cleanUp(value); }); |
| 346 } | 346 } |
| 347 if (remaining == 0 && !eagerError) { | 347 if (remaining == 0 && !eagerError) { |
| 348 result._completeError(error, stackTrace); | 348 result._completeError(error, stackTrace); |
| 349 } | 349 } |
| 350 } | 350 } |
| 351 }, onError: handleError); | 351 }, onError: handleError); |
| 352 // Increment the 'remaining' after the call to 'then'. | 352 // Increment the 'remaining' after the call to 'then'. |
| 353 // If that call throws, we don't expect any future callback from | 353 // If that call throws, we don't expect any future callback from |
| 354 // the future, and we also don't increment remaining. | 354 // the future, and we also don't increment remaining. |
| 355 remaining++; | 355 remaining++; |
| 356 } | 356 } |
| 357 if (remaining == 0) { | 357 if (remaining == 0) { |
| 358 return new Future.value(const []); | 358 return new Future.value(const []); |
| 359 } | 359 } |
| 360 values = new List/*<T>*/(remaining); | 360 values = new List<T>(remaining); |
| 361 } catch (e, st) { | 361 } catch (e, st) { |
| 362 // The error must have been thrown while iterating over the futures | 362 // The error must have been thrown while iterating over the futures |
| 363 // list, or while installing a callback handler on the future. | 363 // list, or while installing a callback handler on the future. |
| 364 if (remaining == 0 || eagerError) { | 364 if (remaining == 0 || eagerError) { |
| 365 // Throw a new Future.error. | 365 // Throw a new Future.error. |
| 366 // Don't just call `result._completeError` since that would propagate | 366 // Don't just call `result._completeError` since that would propagate |
| 367 // the error too eagerly, not giving the callers time to install | 367 // the error too eagerly, not giving the callers time to install |
| 368 // error handlers. | 368 // error handlers. |
| 369 // Also, don't use `_asyncCompleteError` since that one doesn't give | 369 // Also, don't use `_asyncCompleteError` since that one doesn't give |
| 370 // zones the chance to intercept the error. | 370 // zones the chance to intercept the error. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 383 /** | 383 /** |
| 384 * Returns the result of the first future in [futures] to complete. | 384 * Returns the result of the first future in [futures] to complete. |
| 385 * | 385 * |
| 386 * The returned future is completed with the result of the first | 386 * The returned future is completed with the result of the first |
| 387 * future in [futures] to report that it is complete. | 387 * future in [futures] to report that it is complete. |
| 388 * The results of all the other futures are discarded. | 388 * The results of all the other futures are discarded. |
| 389 * | 389 * |
| 390 * If [futures] is empty, or if none of its futures complete, | 390 * If [futures] is empty, or if none of its futures complete, |
| 391 * the returned future never completes. | 391 * the returned future never completes. |
| 392 */ | 392 */ |
| 393 static Future/*<T>*/ any/*<T>*/(Iterable<Future/*<T>*/> futures) { | 393 static Future<T> any<T>(Iterable<Future<T>> futures) { |
| 394 var completer = new Completer/*<T>*/.sync(); | 394 var completer = new Completer<T>.sync(); |
| 395 var onValue = (/*=T*/ value) { | 395 var onValue = (T value) { |
| 396 if (!completer.isCompleted) completer.complete(value); | 396 if (!completer.isCompleted) completer.complete(value); |
| 397 }; | 397 }; |
| 398 var onError = (error, stack) { | 398 var onError = (error, stack) { |
| 399 if (!completer.isCompleted) completer.completeError(error, stack); | 399 if (!completer.isCompleted) completer.completeError(error, stack); |
| 400 }; | 400 }; |
| 401 for (var future in futures) { | 401 for (var future in futures) { |
| 402 future.then(onValue, onError: onError); | 402 future.then(onValue, onError: onError); |
| 403 } | 403 } |
| 404 return completer.future; | 404 return completer.future; |
| 405 } | 405 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 * the future returned by `then` will be completed with | 490 * the future returned by `then` will be completed with |
| 491 * the same result as the future returned by the callback. | 491 * the same result as the future returned by the callback. |
| 492 * | 492 * |
| 493 * If [onError] is not given, and this future completes with an error, | 493 * If [onError] is not given, and this future completes with an error, |
| 494 * the error is forwarded directly to the returned future. | 494 * the error is forwarded directly to the returned future. |
| 495 * | 495 * |
| 496 * In most cases, it is more readable to use [catchError] separately, possibly | 496 * In most cases, it is more readable to use [catchError] separately, possibly |
| 497 * with a `test` parameter, instead of handling both value and error in a | 497 * with a `test` parameter, instead of handling both value and error in a |
| 498 * single [then] call. | 498 * single [then] call. |
| 499 */ | 499 */ |
| 500 Future/*<S>*/ then/*<S>*/(onValue(T value), { Function onError }); | 500 Future<S> then<S>(onValue(T value), { Function onError }); |
| 501 | 501 |
| 502 /** | 502 /** |
| 503 * Handles errors emitted by this [Future]. | 503 * Handles errors emitted by this [Future]. |
| 504 * | 504 * |
| 505 * This is the asynchronous equivalent of a "catch" block. | 505 * This is the asynchronous equivalent of a "catch" block. |
| 506 * | 506 * |
| 507 * Returns a new [Future] that will be completed with either the result of | 507 * Returns a new [Future] that will be completed with either the result of |
| 508 * this future or the result of calling the `onError` callback. | 508 * this future or the result of calling the `onError` callback. |
| 509 * | 509 * |
| 510 * If this future completes with a value, | 510 * If this future completes with a value, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 */ | 553 */ |
| 554 // The `Function` below can stand for several types: | 554 // The `Function` below can stand for several types: |
| 555 // - (dynamic) -> T | 555 // - (dynamic) -> T |
| 556 // - (dynamic, StackTrace) -> T | 556 // - (dynamic, StackTrace) -> T |
| 557 // - (dynamic) -> Future<T> | 557 // - (dynamic) -> Future<T> |
| 558 // - (dynamic, StackTrace) -> Future<T> | 558 // - (dynamic, StackTrace) -> Future<T> |
| 559 // Given that there is a `test` function that is usually used to do an | 559 // Given that there is a `test` function that is usually used to do an |
| 560 // `isCheck` we should also expect functions that take a specific argument. | 560 // `isCheck` we should also expect functions that take a specific argument. |
| 561 // Note: making `catchError` return a `Future<T>` in non-strong mode could be | 561 // Note: making `catchError` return a `Future<T>` in non-strong mode could be |
| 562 // a breaking change. | 562 // a breaking change. |
| 563 Future/*<T>*/ catchError(Function onError, | 563 Future<T> catchError(Function onError, |
| 564 {bool test(Object error)}); | 564 {bool test(Object error)}); |
| 565 | 565 |
| 566 /** | 566 /** |
| 567 * Register a function to be called when this future completes. | 567 * Register a function to be called when this future completes. |
| 568 * | 568 * |
| 569 * The [action] function is called when this future completes, whether it | 569 * The [action] function is called when this future completes, whether it |
| 570 * does so with a value or with an error. | 570 * does so with a value or with an error. |
| 571 * | 571 * |
| 572 * This is the asynchronous equivalent of a "finally" block. | 572 * This is the asynchronous equivalent of a "finally" block. |
| 573 * | 573 * |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 820 if (replacement != null) { | 820 if (replacement != null) { |
| 821 error = _nonNullError(replacement.error); | 821 error = _nonNullError(replacement.error); |
| 822 stackTrace = replacement.stackTrace; | 822 stackTrace = replacement.stackTrace; |
| 823 } | 823 } |
| 824 result._completeError(error, stackTrace); | 824 result._completeError(error, stackTrace); |
| 825 } | 825 } |
| 826 | 826 |
| 827 /** Helper function that converts `null` to a [NullThrownError]. */ | 827 /** Helper function that converts `null` to a [NullThrownError]. */ |
| 828 Object _nonNullError(Object error) => | 828 Object _nonNullError(Object error) => |
| 829 (error != null) ? error : new NullThrownError(); | 829 (error != null) ? error : new NullThrownError(); |
| OLD | NEW |