| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 typedef dynamic ZoneCallback(); | 7 typedef dynamic ZoneCallback(); |
| 8 typedef dynamic ZoneUnaryCallback(arg); | 8 typedef dynamic ZoneUnaryCallback(arg); |
| 9 typedef dynamic ZoneBinaryCallback(arg1, arg2); | 9 typedef dynamic ZoneBinaryCallback(arg1, arg2); |
| 10 | 10 |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 parent = parent.parent; | 514 parent = parent.parent; |
| 515 } | 515 } |
| 516 _ZoneDelegate grandParent = new _ZoneDelegate(parent.parent); | 516 _ZoneDelegate grandParent = new _ZoneDelegate(parent.parent); |
| 517 return (parent._specification.fork)( | 517 return (parent._specification.fork)( |
| 518 parent, grandParent, zone, specification, zoneValues); | 518 parent, grandParent, zone, specification, zoneValues); |
| 519 } | 519 } |
| 520 } | 520 } |
| 521 | 521 |
| 522 | 522 |
| 523 /** | 523 /** |
| 524 * Default implementation of a [Zone]. | 524 * Base class for Zone implementations. |
| 525 */ | 525 */ |
| 526 class _CustomizedZone implements Zone { | 526 abstract class _BaseZone implements Zone { |
| 527 const _BaseZone(); |
| 528 |
| 527 /// The parent zone. | 529 /// The parent zone. |
| 528 final _CustomizedZone parent; | 530 _BaseZone get parent; |
| 529 /// The zone's handlers. | 531 /// The zone's handlers. |
| 530 final ZoneSpecification _specification; | 532 ZoneSpecification get _specification; |
| 531 /// The zone's value map. | 533 /** |
| 532 final Map<Symbol, dynamic> _map; | 534 * The closest error-handling zone. |
| 533 | 535 * |
| 534 const _CustomizedZone(this.parent, this._specification, this._map); | 536 * Returns `this` if `this` has an error-handler. Otherwise returns the |
| 535 | 537 * parent's error-zone. |
| 536 Zone get _errorZone { | 538 */ |
| 537 if (_specification.handleUncaughtError != null) return this; | 539 Zone get _errorZone; |
| 538 return parent._errorZone; | |
| 539 } | |
| 540 | 540 |
| 541 bool inSameErrorZone(Zone otherZone) => _errorZone == otherZone._errorZone; | 541 bool inSameErrorZone(Zone otherZone) => _errorZone == otherZone._errorZone; |
| 542 | 542 |
| 543 dynamic runGuarded(f()) { | 543 dynamic runGuarded(f()) { |
| 544 try { | 544 try { |
| 545 return run(f); | 545 return run(f); |
| 546 } catch (e, s) { | 546 } catch (e, s) { |
| 547 return handleUncaughtError(e, s); | 547 return handleUncaughtError(e, s); |
| 548 } | 548 } |
| 549 } | 549 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 | 584 |
| 585 ZoneBinaryCallback bindBinaryCallback( | 585 ZoneBinaryCallback bindBinaryCallback( |
| 586 f(arg1, arg2), { bool runGuarded: true }) { | 586 f(arg1, arg2), { bool runGuarded: true }) { |
| 587 ZoneBinaryCallback registered = registerBinaryCallback(f); | 587 ZoneBinaryCallback registered = registerBinaryCallback(f); |
| 588 if (runGuarded) { | 588 if (runGuarded) { |
| 589 return (arg1, arg2) => this.runBinaryGuarded(registered, arg1, arg2); | 589 return (arg1, arg2) => this.runBinaryGuarded(registered, arg1, arg2); |
| 590 } else { | 590 } else { |
| 591 return (arg1, arg2) => this.runBinary(registered, arg1, arg2); | 591 return (arg1, arg2) => this.runBinary(registered, arg1, arg2); |
| 592 } | 592 } |
| 593 } | 593 } |
| 594 } |
| 595 |
| 596 |
| 597 /** |
| 598 * Default implementation of a [Zone]. |
| 599 */ |
| 600 class _CustomizedZone extends _BaseZone { |
| 601 final _BaseZone parent; |
| 602 final ZoneSpecification _specification; |
| 603 |
| 604 /// The zone's value map. |
| 605 final Map<Symbol, dynamic> _map; |
| 606 |
| 607 const _CustomizedZone(this.parent, this._specification, this._map); |
| 608 |
| 609 Zone get _errorZone { |
| 610 if (_specification.handleUncaughtError != null) return this; |
| 611 return parent._errorZone; |
| 612 } |
| 594 | 613 |
| 595 operator [](Symbol key) { | 614 operator [](Symbol key) { |
| 596 var result = _map[key]; | 615 var result = _map[key]; |
| 597 if (result != null || _map.containsKey(key)) return result; | 616 if (result != null || _map.containsKey(key)) return result; |
| 598 // If we are not the root zone look up in the parent zone. | 617 // If we are not the root zone look up in the parent zone. |
| 599 if (parent != null) return parent[key]; | 618 if (parent != null) return parent[key]; |
| 600 assert(this == Zone.ROOT); | 619 assert(this == Zone.ROOT); |
| 601 return null; | 620 return null; |
| 602 } | 621 } |
| 603 | 622 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 zoneValues.forEach((Symbol key, value) { | 768 zoneValues.forEach((Symbol key, value) { |
| 750 if (key == null) { | 769 if (key == null) { |
| 751 throw new ArgumentError("ZoneValue key must not be null"); | 770 throw new ArgumentError("ZoneValue key must not be null"); |
| 752 } | 771 } |
| 753 copiedMap[key] = value; | 772 copiedMap[key] = value; |
| 754 }); | 773 }); |
| 755 } | 774 } |
| 756 return new _CustomizedZone(zone, specification, copiedMap); | 775 return new _CustomizedZone(zone, specification, copiedMap); |
| 757 } | 776 } |
| 758 | 777 |
| 759 const _ROOT_SPECIFICATION = const ZoneSpecification( | 778 class _RootZoneSpecification implements ZoneSpecification { |
| 760 handleUncaughtError: _rootHandleUncaughtError, | 779 const _RootZoneSpecification(); |
| 761 run: _rootRun, | |
| 762 runUnary: _rootRunUnary, | |
| 763 runBinary: _rootRunBinary, | |
| 764 registerCallback: _rootRegisterCallback, | |
| 765 registerUnaryCallback: _rootRegisterUnaryCallback, | |
| 766 registerBinaryCallback: _rootRegisterBinaryCallback, | |
| 767 scheduleMicrotask: _rootScheduleMicrotask, | |
| 768 createTimer: _rootCreateTimer, | |
| 769 createPeriodicTimer: _rootCreatePeriodicTimer, | |
| 770 fork: _rootFork | |
| 771 ); | |
| 772 | 780 |
| 773 const _ROOT_ZONE = | 781 HandleUncaughtErrorHandler get handleUncaughtError => |
| 774 const _CustomizedZone(null, _ROOT_SPECIFICATION, const <Symbol, dynamic>{}); | 782 _rootHandleUncaughtError; |
| 783 RunHandler get run => _rootRun; |
| 784 RunUnaryHandler get runUnary => _rootRunUnary; |
| 785 RunBinaryHandler get runBinary => _rootRunBinary; |
| 786 RegisterCallbackHandler get registerCallback => _rootRegisterCallback; |
| 787 RegisterUnaryCallbackHandler get registerUnaryCallback => |
| 788 _rootRegisterUnaryCallback; |
| 789 RegisterBinaryCallbackHandler get registerBinaryCallback => |
| 790 _rootRegisterBinaryCallback; |
| 791 ScheduleMicrotaskHandler get scheduleMicrotask => _rootScheduleMicrotask; |
| 792 @deprecated |
| 793 RunAsyncHandler get runAsync => null; |
| 794 CreateTimerHandler get createTimer => _rootCreateTimer; |
| 795 CreatePeriodicTimerHandler get createPeriodicTimer => |
| 796 _rootCreatePeriodicTimer; |
| 797 ForkHandler get fork => _rootFork; |
| 798 } |
| 799 |
| 800 class _RootZone extends _BaseZone { |
| 801 const _RootZone(); |
| 802 |
| 803 Zone get parent => null; |
| 804 ZoneSpecification get _specification => const _RootZoneSpecification(); |
| 805 Zone get _errorZone => this; |
| 806 |
| 807 bool inSameErrorZone(Zone otherZone) => otherZone._errorZone == this; |
| 808 |
| 809 operator [](Symbol key) => null; |
| 810 |
| 811 // Methods that can be customized by the zone specification. |
| 812 |
| 813 dynamic handleUncaughtError(error, StackTrace stackTrace) => |
| 814 _rootHandleUncaughtError(this, null, this, error, stackTrace); |
| 815 |
| 816 Zone fork({ZoneSpecification specification, Map zoneValues}) => |
| 817 _rootFork(this, null, this, specification, zoneValues); |
| 818 |
| 819 dynamic run(f()) => _rootRun(this, null, this, f); |
| 820 |
| 821 dynamic runUnary(f(arg), arg) => _rootRunUnary(this, null, this, f, arg); |
| 822 |
| 823 dynamic runBinary(f(arg1, arg2), arg1, arg2) => |
| 824 _rootRunBinary(this, null, this, f, arg1, arg2); |
| 825 |
| 826 ZoneCallback registerCallback(f()) => |
| 827 _rootRegisterCallback(this, null, this, f); |
| 828 |
| 829 ZoneUnaryCallback registerUnaryCallback(f(arg)) => |
| 830 _rootRegisterUnaryCallback(this, null, this, f); |
| 831 |
| 832 ZoneBinaryCallback registerBinaryCallback(f(arg1, arg2)) => |
| 833 _rootRegisterBinaryCallback(this, null, this, f); |
| 834 |
| 835 void scheduleMicrotask(void f()) { |
| 836 _rootScheduleMicrotask(this, null, this, f); |
| 837 } |
| 838 |
| 839 @deprecated |
| 840 void runAsync(void f()) { |
| 841 scheduleMicrotask(f); |
| 842 } |
| 843 |
| 844 Timer createTimer(Duration duration, void f()) => |
| 845 _rootCreateTimer(this, null, this, duration, f); |
| 846 |
| 847 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) => |
| 848 _rootCreatePeriodicTimer(this, null, this, duration, f); |
| 849 } |
| 850 |
| 851 const _ROOT_ZONE = const _RootZone(); |
| 775 | 852 |
| 776 | 853 |
| 777 /** | 854 /** |
| 778 * Runs [body] in its own zone. | 855 * Runs [body] in its own zone. |
| 779 * | 856 * |
| 780 * If [onError] is non-null the zone is considered an error zone. All uncaught | 857 * If [onError] is non-null the zone is considered an error zone. All uncaught |
| 781 * errors, synchronous or asynchronous, in the zone are caught and handled | 858 * errors, synchronous or asynchronous, in the zone are caught and handled |
| 782 * by the callback. | 859 * by the callback. |
| 783 * | 860 * |
| 784 * Errors may never cross error-zone boundaries. This is intuitive for leaving | 861 * Errors may never cross error-zone boundaries. This is intuitive for leaving |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 891 ZoneSpecification specification = | 968 ZoneSpecification specification = |
| 892 new ZoneSpecification(handleUncaughtError: errorHandler, | 969 new ZoneSpecification(handleUncaughtError: errorHandler, |
| 893 scheduleMicrotask: asyncHandler); | 970 scheduleMicrotask: asyncHandler); |
| 894 Zone zone = Zone.current.fork(specification: specification); | 971 Zone zone = Zone.current.fork(specification: specification); |
| 895 if (onError != null) { | 972 if (onError != null) { |
| 896 return zone.runGuarded(body); | 973 return zone.runGuarded(body); |
| 897 } else { | 974 } else { |
| 898 return zone.run(body); | 975 return zone.run(body); |
| 899 } | 976 } |
| 900 } | 977 } |
| OLD | NEW |