Chromium Code Reviews| 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 // The `then` call threw synchronously. This should never happen! | |
|
Søren Gjesse
2015/03/23 09:07:36
Maybe explain "This should never happen!" - I assu
Lasse Reichstein Nielsen
2015/03/23 09:43:54
The _Future implementation never reaches here. To
| |
| 322 scheduleMicrotask(() { | |
| 323 target._completeError(e, s); | |
| 318 }); | 324 }); |
| 325 } | |
| 319 } | 326 } |
| 320 | 327 |
| 321 // Take the value (when completed) of source and complete target with that | 328 // Take the value (when completed) of source and complete target with that |
| 322 // value (or error). This function expects that source is a _Future. | 329 // value (or error). This function expects that source is a _Future. |
| 323 static void _chainCoreFuture(_Future source, _Future target) { | 330 static void _chainCoreFuture(_Future source, _Future target) { |
| 324 assert(!target._isComplete); | 331 assert(!target._isComplete); |
| 325 assert(source is _Future); | 332 assert(source is _Future); |
| 326 | 333 |
| 327 // Mark the target as chained (and as such half-completed). | 334 // Mark the target as chained (and as such half-completed). |
| 328 target._isChained = true; | 335 target._isChained = true; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 _markPendingCompletion(); | 399 _markPendingCompletion(); |
| 393 _zone.scheduleMicrotask(() { | 400 _zone.scheduleMicrotask(() { |
| 394 _chainCoreFuture(coreFuture, this); | 401 _chainCoreFuture(coreFuture, this); |
| 395 }); | 402 }); |
| 396 } else { | 403 } else { |
| 397 _chainCoreFuture(coreFuture, this); | 404 _chainCoreFuture(coreFuture, this); |
| 398 } | 405 } |
| 399 } else { | 406 } else { |
| 400 // Case 2 from above. Chain the future immidiately. | 407 // Case 2 from above. Chain the future immidiately. |
| 401 // Note that we are still completing asynchronously (through | 408 // Note that we are still completing asynchronously (through |
| 402 // _chainForeignFuture).. | 409 // _chainForeignFuture). |
| 403 _chainForeignFuture(typedFuture, this); | 410 _chainForeignFuture(typedFuture, this); |
| 404 } | 411 } |
| 405 return; | 412 return; |
| 406 } else { | 413 } else { |
| 407 T typedValue = value; | 414 T typedValue = value; |
| 408 } | 415 } |
| 409 | 416 |
| 410 _markPendingCompletion(); | 417 _markPendingCompletion(); |
| 411 _zone.scheduleMicrotask(() { | 418 _zone.scheduleMicrotask(() { |
| 412 _completeWithValue(value); | 419 _completeWithValue(value); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 642 } | 649 } |
| 643 }, onError: (e, s) { | 650 }, onError: (e, s) { |
| 644 if (timer.isActive) { | 651 if (timer.isActive) { |
| 645 timer.cancel(); | 652 timer.cancel(); |
| 646 result._completeError(e, s); | 653 result._completeError(e, s); |
| 647 } | 654 } |
| 648 }); | 655 }); |
| 649 return result; | 656 return result; |
| 650 } | 657 } |
| 651 } | 658 } |
| OLD | NEW |