| 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 /** The onValue and onError handlers return either a value or a future */ | 7 /** The onValue and onError handlers return either a value or a future */ |
| 8 typedef dynamic _FutureOnValue<T>(T value); | 8 typedef dynamic _FutureOnValue<T>(T value); |
| 9 /** Test used by [Future.catchError] to handle skip some errors. */ | 9 /** Test used by [Future.catchError] to handle skip some errors. */ |
| 10 typedef bool _FutureErrorTest(var error); | 10 typedef bool _FutureErrorTest(var error); |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 297 |
| 298 // Take the value (when completed) of source and complete target with that | 298 // Take the value (when completed) of source and complete target with that |
| 299 // value (or error). This function can chain all Futures, but is slower | 299 // value (or error). This function can chain all Futures, but is slower |
| 300 // for _Future than _chainCoreFuture - Use _chainCoreFuture in that case. | 300 // for _Future than _chainCoreFuture - Use _chainCoreFuture in that case. |
| 301 static void _chainForeignFuture(Future source, _Future target) { | 301 static void _chainForeignFuture(Future source, _Future target) { |
| 302 assert(!target._isComplete); | 302 assert(!target._isComplete); |
| 303 assert(source is! _Future); | 303 assert(source is! _Future); |
| 304 | 304 |
| 305 // Mark the target as chained (and as such half-completed). | 305 // Mark the target as chained (and as such half-completed). |
| 306 target._isChained = true; | 306 target._isChained = true; |
| 307 source.then((value) { | 307 try { |
| 308 assert(target._isChained); | 308 source.then((value) { |
| 309 target._completeWithValue(value); | 309 assert(target._isChained); |
| 310 }, | 310 target._completeWithValue(value); |
| 311 // TODO(floitsch): eventually we would like to make this non-optional | 311 }, |
| 312 // and dependent on the listeners of the target future. If none of | 312 // TODO(floitsch): eventually we would like to make this non-optional |
| 313 // the target future's listeners want to have the stack trace we don't | 313 // and dependent on the listeners of the target future. If none of |
| 314 // need a trace. | 314 // the target future's listeners want to have the stack trace we don't |
| 315 onError: (error, [stackTrace]) { | 315 // need a trace. |
| 316 assert(target._isChained); | 316 onError: (error, [stackTrace]) { |
| 317 target._completeError(error, stackTrace); | 317 assert(target._isChained); |
| 318 target._completeError(error, stackTrace); |
| 319 }); |
| 320 } catch (e, s) { |
| 321 // This only happens if the `then` call threw synchronously when given |
| 322 // valid arguments. |
| 323 // That requires a non-conforming implementation of the Future interface, |
| 324 // which should, hopefully, never happen. |
| 325 scheduleMicrotask(() { |
| 326 target._completeError(e, s); |
| 318 }); | 327 }); |
| 328 } |
| 319 } | 329 } |
| 320 | 330 |
| 321 // Take the value (when completed) of source and complete target with that | 331 // Take the value (when completed) of source and complete target with that |
| 322 // value (or error). This function expects that source is a _Future. | 332 // value (or error). This function expects that source is a _Future. |
| 323 static void _chainCoreFuture(_Future source, _Future target) { | 333 static void _chainCoreFuture(_Future source, _Future target) { |
| 324 assert(!target._isComplete); | 334 assert(!target._isComplete); |
| 325 assert(source is _Future); | 335 assert(source is _Future); |
| 326 | 336 |
| 327 // Mark the target as chained (and as such half-completed). | 337 // Mark the target as chained (and as such half-completed). |
| 328 target._isChained = true; | 338 target._isChained = true; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 _markPendingCompletion(); | 402 _markPendingCompletion(); |
| 393 _zone.scheduleMicrotask(() { | 403 _zone.scheduleMicrotask(() { |
| 394 _chainCoreFuture(coreFuture, this); | 404 _chainCoreFuture(coreFuture, this); |
| 395 }); | 405 }); |
| 396 } else { | 406 } else { |
| 397 _chainCoreFuture(coreFuture, this); | 407 _chainCoreFuture(coreFuture, this); |
| 398 } | 408 } |
| 399 } else { | 409 } else { |
| 400 // Case 2 from above. Chain the future immidiately. | 410 // Case 2 from above. Chain the future immidiately. |
| 401 // Note that we are still completing asynchronously (through | 411 // Note that we are still completing asynchronously (through |
| 402 // _chainForeignFuture).. | 412 // _chainForeignFuture). |
| 403 _chainForeignFuture(typedFuture, this); | 413 _chainForeignFuture(typedFuture, this); |
| 404 } | 414 } |
| 405 return; | 415 return; |
| 406 } else { | 416 } else { |
| 407 T typedValue = value; | 417 T typedValue = value; |
| 408 } | 418 } |
| 409 | 419 |
| 410 _markPendingCompletion(); | 420 _markPendingCompletion(); |
| 411 _zone.scheduleMicrotask(() { | 421 _zone.scheduleMicrotask(() { |
| 412 _completeWithValue(value); | 422 _completeWithValue(value); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 } | 652 } |
| 643 }, onError: (e, s) { | 653 }, onError: (e, s) { |
| 644 if (timer.isActive) { | 654 if (timer.isActive) { |
| 645 timer.cancel(); | 655 timer.cancel(); |
| 646 result._completeError(e, s); | 656 result._completeError(e, s); |
| 647 } | 657 } |
| 648 }); | 658 }); |
| 649 return result; | 659 return result; |
| 650 } | 660 } |
| 651 } | 661 } |
| OLD | NEW |