Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: sdk/lib/async/future_impl.dart

Issue 136113014: Introduce and use Zone:_enter and Zone:_leave, in Future. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: cleanup Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/async/zone.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 return; 404 return;
405 } 405 }
406 if (listeners == null) return; 406 if (listeners == null) return;
407 _Future listener = listeners; 407 _Future listener = listeners;
408 if (listener._nextListener != null) { 408 if (listener._nextListener != null) {
409 // Usually futures only have one listener. If they have several, we 409 // Usually futures only have one listener. If they have several, we
410 // handle them specially. 410 // handle them specially.
411 _propagateMultipleListeners(source, listeners); 411 _propagateMultipleListeners(source, listeners);
412 return; 412 return;
413 } 413 }
414 if (hasError && !source._zone.inSameErrorZone(listener._zone)) { 414 Zone zone = listener._zone;
415 if (hasError && !source._zone.inSameErrorZone(zone)) {
415 // Don't cross zone boundaries with errors. 416 // Don't cross zone boundaries with errors.
416 _AsyncError asyncError = source._error; 417 _AsyncError asyncError = source._error;
417 source._zone.handleUncaughtError( 418 source._zone.handleUncaughtError(
418 asyncError.error, asyncError.stackTrace); 419 asyncError.error, asyncError.stackTrace);
419 return; 420 return;
420 } 421 }
421 if (!identical(Zone.current, listener._zone)) { 422 Zone oldZone;
422 // Run the propagation in the listener's zone to avoid 423 if (!identical(Zone.current, zone)) {
423 // zone transitions. The idea is that many chained futures will 424 // Change zone if it's not current.
424 // be in the same zone. 425 oldZone = Zone._enter(zone);
425 listener._zone.run(() {
426 _propagateToListeners(source, listener);
427 });
428 return;
429 } 426 }
430
431 // Do the actual propagation. 427 // Do the actual propagation.
432 // TODO(floitsch): Do we need to go through the zone even if we
433 // don't have a callback to execute?
434 bool listenerHasValue; 428 bool listenerHasValue;
435 var listenerValueOrError; 429 var listenerValueOrError;
436 // Set to true if a whenComplete needs to wait for a future. 430 // Set to true if a whenComplete needs to wait for a future.
437 // The whenComplete action will resume the propagation by itself. 431 // The whenComplete action will resume the propagation by itself.
438 bool isPropagationAborted = false; 432 bool isPropagationAborted = false;
439 // Even though we are already in the right zone (due to the optimization 433 // TODO(floitsch): mark the listener as pending completion. Currently
440 // above), we still need to go through the zone. The overhead of 434 // we can't do this, since the markPendingCompletion verifies that
441 // executeCallback is however smaller when it is already in the correct 435 // the future is not already marked (or chained).
442 // zone. 436
443 // TODO(floitsch): only run callbacks in the zone, not the whole 437 bool handleValueCallback() {
444 // handling code.
445 listener._zone.run(() {
446 // TODO(floitsch): mark the listener as pending completion. Currently
447 // we can't do this, since the markPendingCompletion verifies that
448 // the future is not already marked (or chained).
449 try { 438 try {
450 if (!hasError) { 439 listenerValueOrError = zone.runUnary(listener._onValue,
451 var value = source._value; 440 source._value);
452 if (listener._onValue != null) { 441 return true;
453 listenerValueOrError = listener._onValue(value); 442 } catch (e, s) {
454 listenerHasValue = true; 443 listenerValueOrError = new _AsyncError(e, s);
444 return false;
445 }
446 }
447
448 void handleError() {
449 _AsyncError asyncError = source._error;
450 _FutureErrorTest test = listener._errorTest;
451 bool matchesTest = true;
452 if (test != null) {
453 try {
454 matchesTest = zone.runUnary(test, asyncError.error);
455 } catch (e, s) {
456 // TODO(ajohnsen): Should we suport rethrow for test throws?
457 listenerValueOrError = identical(asyncError.error, e) ?
458 asyncError : new _AsyncError(e, s);
459 listenerHasValue = false;
460 return;
461 }
462 }
463 Function errorCallback = listener._onError;
464 if (matchesTest && errorCallback != null) {
465 try {
466 if (errorCallback is ZoneBinaryCallback) {
467 listenerValueOrError = zone.runBinary(errorCallback,
468 asyncError.error,
469 asyncError.stackTrace);
455 } else { 470 } else {
456 // Copy over the value from the source. 471 listenerValueOrError = zone.runUnary(errorCallback,
457 listenerValueOrError = value; 472 asyncError.error);
458 listenerHasValue = true;
459 } 473 }
460 } else { 474 } catch (e, s) {
461 _AsyncError asyncError = source._error; 475 listenerValueOrError = identical(asyncError.error, e) ?
462 _FutureErrorTest test = listener._errorTest; 476 asyncError : new _AsyncError(e, s);
463 bool matchesTest = true; 477 listenerHasValue = false;
464 if (test != null) { 478 return;
465 matchesTest = test(asyncError.error);
466 }
467 if (matchesTest && listener._onError != null) {
468 Function errorCallback = listener._onError;
469 listenerValueOrError = _invokeErrorHandler(errorCallback,
470 asyncError.error,
471 asyncError.stackTrace);
472 listenerHasValue = true;
473 } else {
474 // Copy over the error from the source.
475 listenerValueOrError = asyncError;
476 listenerHasValue = false;
477 }
478 } 479 }
480 listenerHasValue = true;
481 } else {
482 // Copy over the error from the source.
483 listenerValueOrError = asyncError;
484 listenerHasValue = false;
485 }
486 }
479 487
480 if (listener._whenCompleteAction != null) { 488 void handleWhenCompleteCallback() {
481 var completeResult = listener._whenCompleteAction(); 489 var completeResult;
482 if (completeResult is Future) { 490 try {
483 listener._isChained = true; 491 completeResult = zone.run(listener._whenCompleteAction);
484 completeResult.then((ignored) {
485 // Try again, but this time don't run the whenComplete callback.
486 _propagateToListeners(source, listener);
487 }, onError: (error, [stackTrace]) {
488 // When there is an error, we have to make the error the new
489 // result of the current listener.
490 if (completeResult is! _Future) {
491 // This should be a rare case.
492 completeResult = new _Future();
493 completeResult._setError(error, stackTrace);
494 }
495 _propagateToListeners(completeResult, listener);
496 });
497 isPropagationAborted = true;
498 }
499 }
500 } catch (e, s) { 492 } catch (e, s) {
501 // Set the exception as error unless the error is the same as the
502 // original one.
503 if (hasError && identical(source._error.error, e)) { 493 if (hasError && identical(source._error.error, e)) {
504 listenerValueOrError = source._error; 494 listenerValueOrError = source._error;
505 } else { 495 } else {
506 listenerValueOrError = new _AsyncError(e, s); 496 listenerValueOrError = new _AsyncError(e, s);
507 } 497 }
508 listenerHasValue = false; 498 listenerHasValue = false;
509 } 499 }
510 }); 500 if (completeResult is Future) {
501 listener._isChained = true;
502 isPropagationAborted = true;
503 completeResult.then((ignored) {
504 // Try again. Since the future is marked as chained it won't run
505 // the whenComplete again.
506 _propagateToListeners(source, listener);
507 }, onError: (error, [stackTrace]) {
508 // When there is an error, we have to make the error the new
509 // result of the current listener.
510 if (completeResult is! _Future) {
511 // This should be a rare case.
512 completeResult = new _Future();
513 completeResult._setError(error, stackTrace);
514 }
515 _propagateToListeners(completeResult, listener);
516 });
517 }
518 }
519
520 if (!hasError) {
521 if (listener._onValue != null) {
522 listenerHasValue = handleValueCallback();
523 } else {
524 listenerValueOrError = source._value;
525 listenerHasValue = true;
526 }
527 } else {
528 handleError();
529 }
530 if (listener._whenCompleteAction != null) {
531 handleWhenCompleteCallback();
532 }
533 // If we changed zone, oldZone will not be null.
534 if (oldZone != null) Zone._leave(oldZone);
511 if (isPropagationAborted) return; 535 if (isPropagationAborted) return;
512 // If the listener's value is a future we need to chain it. 536 // If the listener's value is a future we need to chain it.
513 if (listenerHasValue && listenerValueOrError is Future) { 537 if (listenerHasValue && listenerValueOrError is Future) {
514 Future chainSource = listenerValueOrError; 538 Future chainSource = listenerValueOrError;
515 // Shortcut if the chain-source is already completed. Just continue the 539 // Shortcut if the chain-source is already completed. Just continue the
516 // loop. 540 // loop.
517 if (chainSource is _Future && chainSource._isComplete) { 541 if (chainSource is _Future && chainSource._isComplete) {
518 // propagate the value (simulating a tail call). 542 // propagate the value (simulating a tail call).
519 listener._isChained = true; 543 listener._isChained = true;
520 source = chainSource; 544 source = chainSource;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 } 589 }
566 }, onError: (e, s) { 590 }, onError: (e, s) {
567 if (timer.isActive) { 591 if (timer.isActive) {
568 timer.cancel(); 592 timer.cancel();
569 result._completeError(e, s); 593 result._completeError(e, s);
570 } 594 }
571 }); 595 });
572 return result; 596 return result;
573 } 597 }
574 } 598 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/async/zone.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698