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

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

Issue 196413005: Make zone variables accept any value as key. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Extended the zone_values_test test. Created 6 years, 9 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 | tests/lib/async/zone_value_test.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) 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 14 matching lines...) Expand all
25 Zone self, ZoneDelegate parent, Zone zone, f()); 25 Zone self, ZoneDelegate parent, Zone zone, f());
26 typedef Timer CreateTimerHandler( 26 typedef Timer CreateTimerHandler(
27 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f()); 27 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f());
28 typedef Timer CreatePeriodicTimerHandler( 28 typedef Timer CreatePeriodicTimerHandler(
29 Zone self, ZoneDelegate parent, Zone zone, 29 Zone self, ZoneDelegate parent, Zone zone,
30 Duration period, void f(Timer timer)); 30 Duration period, void f(Timer timer));
31 typedef void PrintHandler( 31 typedef void PrintHandler(
32 Zone self, ZoneDelegate parent, Zone zone, String line); 32 Zone self, ZoneDelegate parent, Zone zone, String line);
33 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone, 33 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone,
34 ZoneSpecification specification, 34 ZoneSpecification specification,
35 Map<Symbol, dynamic> zoneValues); 35 Map zoneValues);
36 36
37 /** 37 /**
38 * This class provides the specification for a forked zone. 38 * This class provides the specification for a forked zone.
39 * 39 *
40 * When forking a new zone (see [Zone.fork]) one can override the default 40 * When forking a new zone (see [Zone.fork]) one can override the default
41 * behavior of the zone by providing callbacks. These callbacks must be 41 * behavior of the zone by providing callbacks. These callbacks must be
42 * given in an instance of this class. 42 * given in an instance of this class.
43 * 43 *
44 * Handlers have the same signature as the same-named methods on [Zone] but 44 * Handlers have the same signature as the same-named methods on [Zone] but
45 * receive three additional arguments: 45 * receive three additional arguments:
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 Zone self, ZoneDelegate parent, Zone zone, f(arg1, arg2)): null, 102 Zone self, ZoneDelegate parent, Zone zone, f(arg1, arg2)): null,
103 void scheduleMicrotask( 103 void scheduleMicrotask(
104 Zone self, ZoneDelegate parent, Zone zone, f()): null, 104 Zone self, ZoneDelegate parent, Zone zone, f()): null,
105 Timer createTimer(Zone self, ZoneDelegate parent, Zone zone, 105 Timer createTimer(Zone self, ZoneDelegate parent, Zone zone,
106 Duration duration, void f()): null, 106 Duration duration, void f()): null,
107 Timer createPeriodicTimer(Zone self, ZoneDelegate parent, Zone zone, 107 Timer createPeriodicTimer(Zone self, ZoneDelegate parent, Zone zone,
108 Duration period, void f(Timer timer)): null, 108 Duration period, void f(Timer timer)): null,
109 void print(Zone self, ZoneDelegate parent, Zone zone, String line): null, 109 void print(Zone self, ZoneDelegate parent, Zone zone, String line): null,
110 Zone fork(Zone self, ZoneDelegate parent, Zone zone, 110 Zone fork(Zone self, ZoneDelegate parent, Zone zone,
111 ZoneSpecification specification, 111 ZoneSpecification specification,
112 Map<Symbol, dynamic> zoneValues): null 112 Map zoneValues): null
113 }) { 113 }) {
114 return new ZoneSpecification( 114 return new ZoneSpecification(
115 handleUncaughtError: handleUncaughtError != null 115 handleUncaughtError: handleUncaughtError != null
116 ? handleUncaughtError 116 ? handleUncaughtError
117 : other.handleUncaughtError, 117 : other.handleUncaughtError,
118 run: run != null ? run : other.run, 118 run: run != null ? run : other.run,
119 runUnary: runUnary != null ? runUnary : other.runUnary, 119 runUnary: runUnary != null ? runUnary : other.runUnary,
120 runBinary: runBinary != null ? runBinary : other.runBinary, 120 runBinary: runBinary != null ? runBinary : other.runBinary,
121 registerCallback: registerCallback != null 121 registerCallback: registerCallback != null
122 ? registerCallback 122 ? registerCallback
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 /** 248 /**
249 * Returns true if `this` and [otherZone] are in the same error zone. 249 * Returns true if `this` and [otherZone] are in the same error zone.
250 * 250 *
251 * Two zones are in the same error zone if they share the same 251 * Two zones are in the same error zone if they share the same
252 * [handleUncaughtError] callback. 252 * [handleUncaughtError] callback.
253 */ 253 */
254 bool inSameErrorZone(Zone otherZone); 254 bool inSameErrorZone(Zone otherZone);
255 255
256 /** 256 /**
257 * Creates a new zone as a child of `this`. 257 * Creates a new zone as a child of `this`.
258 *
259 * The new zone will have behavior like the current zone, except where
260 * overridden by functions in [specification].
261 *
262 * The new zone will have the same stored values (accessed through
263 * `operator []`) as this zone, but updated with the keys and values
264 * in [zoneValues]. If a key is in both this zone's values and in
265 * `zoneValues`, the new zone will use the value from `zoneValues``.
258 */ 266 */
259 Zone fork({ ZoneSpecification specification, 267 Zone fork({ ZoneSpecification specification,
260 Map<Symbol, dynamic> zoneValues }); 268 Map zoneValues });
261 269
262 /** 270 /**
263 * Executes the given function [f] in this zone. 271 * Executes the given function [f] in this zone.
264 */ 272 */
265 dynamic run(f()); 273 dynamic run(f());
266 274
267 /** 275 /**
268 * Executes the given callback [f] with argument [arg] in this zone. 276 * Executes the given callback [f] with argument [arg] in this zone.
269 */ 277 */
270 dynamic runUnary(f(arg), var arg); 278 dynamic runUnary(f(arg), var arg);
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 static void _leave(Zone previous) { 413 static void _leave(Zone previous) {
406 assert(previous != null); 414 assert(previous != null);
407 Zone._current = previous; 415 Zone._current = previous;
408 } 416 }
409 417
410 /** 418 /**
411 * Retrieves the zone-value associated with [key]. 419 * Retrieves the zone-value associated with [key].
412 * 420 *
413 * If this zone does not contain the value looks up the same key in the 421 * If this zone does not contain the value looks up the same key in the
414 * parent zone. If the [key] is not found returns `null`. 422 * parent zone. If the [key] is not found returns `null`.
423 *
424 * Any object can be used as key, as long as it has compatible `operator ==`
425 * and `hashCode` implementations.
426 * By controlling access to the key, a zone can grant or deny access to the
427 * zone value.
415 */ 428 */
416 operator[](Symbol key); 429 operator [](Object key);
417 } 430 }
418 431
419 class _ZoneDelegate implements ZoneDelegate { 432 class _ZoneDelegate implements ZoneDelegate {
420 final _BaseZone _degelationTarget; 433 final _BaseZone _degelationTarget;
421 434
422 Zone get _zone => _degelationTarget; 435 Zone get _zone => _degelationTarget;
423 436
424 const _ZoneDelegate(this._degelationTarget); 437 const _ZoneDelegate(this._degelationTarget);
425 438
426 dynamic handleUncaughtError(Zone zone, error, StackTrace stackTrace) { 439 dynamic handleUncaughtError(Zone zone, error, StackTrace stackTrace) {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 void print(Zone zone, String line) { 530 void print(Zone zone, String line) {
518 _BaseZone parent = _degelationTarget; 531 _BaseZone parent = _degelationTarget;
519 while (parent._specification.print == null) { 532 while (parent._specification.print == null) {
520 parent = parent.parent; 533 parent = parent.parent;
521 } 534 }
522 (parent._specification.print)( 535 (parent._specification.print)(
523 parent, new _ZoneDelegate(parent.parent), zone, line); 536 parent, new _ZoneDelegate(parent.parent), zone, line);
524 } 537 }
525 538
526 Zone fork(Zone zone, ZoneSpecification specification, 539 Zone fork(Zone zone, ZoneSpecification specification,
527 Map<Symbol, dynamic> zoneValues) { 540 Map zoneValues) {
528 _BaseZone parent = _degelationTarget; 541 _BaseZone parent = _degelationTarget;
529 while (parent._specification.fork == null) { 542 while (parent._specification.fork == null) {
530 parent = parent.parent; 543 parent = parent.parent;
531 } 544 }
532 _ZoneDelegate grandParent = new _ZoneDelegate(parent.parent); 545 _ZoneDelegate grandParent = new _ZoneDelegate(parent.parent);
533 return (parent._specification.fork)( 546 return (parent._specification.fork)(
534 parent, grandParent, zone, specification, zoneValues); 547 parent, grandParent, zone, specification, zoneValues);
535 } 548 }
536 } 549 }
537 550
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 624
612 625
613 /** 626 /**
614 * Default implementation of a [Zone]. 627 * Default implementation of a [Zone].
615 */ 628 */
616 class _CustomizedZone extends _BaseZone { 629 class _CustomizedZone extends _BaseZone {
617 final _BaseZone parent; 630 final _BaseZone parent;
618 final ZoneSpecification _specification; 631 final ZoneSpecification _specification;
619 632
620 /// The zone's value map. 633 /// The zone's value map.
621 final Map<Symbol, dynamic> _map; 634 final Map _map;
622 635
623 const _CustomizedZone(this.parent, this._specification, this._map); 636 const _CustomizedZone(this.parent, this._specification, this._map);
624 637
625 Zone get _errorZone { 638 Zone get _errorZone {
626 if (_specification.handleUncaughtError != null) return this; 639 if (_specification.handleUncaughtError != null) return this;
627 return parent._errorZone; 640 return parent._errorZone;
628 } 641 }
629 642
630 operator [](Symbol key) { 643 operator [](Object key) {
631 var result = _map[key]; 644 var result = _map[key];
632 if (result != null || _map.containsKey(key)) return result; 645 if (result != null || _map.containsKey(key)) return result;
633 // If we are not the root zone look up in the parent zone. 646 // If we are not the root zone look up in the parent zone.
634 if (parent != null) return parent[key]; 647 if (parent != null) return parent[key];
635 assert(this == Zone.ROOT); 648 assert(this == Zone.ROOT);
636 return null; 649 return null;
637 } 650 }
638 651
639 // Methods that can be customized by the zone specification. 652 // Methods that can be customized by the zone specification.
640 653
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) { 791 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) {
779 printToConsole(line); 792 printToConsole(line);
780 } 793 }
781 794
782 void _printToZone(String line) { 795 void _printToZone(String line) {
783 Zone.current.print(line); 796 Zone.current.print(line);
784 } 797 }
785 798
786 Zone _rootFork(Zone self, ZoneDelegate parent, Zone zone, 799 Zone _rootFork(Zone self, ZoneDelegate parent, Zone zone,
787 ZoneSpecification specification, 800 ZoneSpecification specification,
788 Map<Symbol, dynamic> zoneValues) { 801 Map zoneValues) {
789 // TODO(floitsch): it would be nice if we could get rid of this hack. 802 // TODO(floitsch): it would be nice if we could get rid of this hack.
790 // Change the static zoneOrDirectPrint function to go through zones 803 // Change the static zoneOrDirectPrint function to go through zones
791 // from now on. 804 // from now on.
792 printToZone = _printToZone; 805 printToZone = _printToZone;
793 806
794 if (specification == null) { 807 if (specification == null) {
795 specification = const ZoneSpecification(); 808 specification = const ZoneSpecification();
796 } else if (specification is! _ZoneSpecification) { 809 } else if (specification is! _ZoneSpecification) {
797 throw new ArgumentError("ZoneSpecifications must be instantiated" 810 throw new ArgumentError("ZoneSpecifications must be instantiated"
798 " with the provided constructor."); 811 " with the provided constructor.");
799 } 812 }
800 Map<Symbol, dynamic> copiedMap = new HashMap(); 813 Map copiedMap = new HashMap();
801 if (zoneValues != null) { 814 if (zoneValues != null) {
802 zoneValues.forEach((Symbol key, value) { 815 zoneValues.forEach((key, value) {
803 if (key == null) {
804 throw new ArgumentError("ZoneValue key must not be null");
805 }
806 copiedMap[key] = value; 816 copiedMap[key] = value;
807 }); 817 });
808 } 818 }
809 return new _CustomizedZone(zone, specification, copiedMap); 819 return new _CustomizedZone(zone, specification, copiedMap);
810 } 820 }
811 821
812 class _RootZoneSpecification implements ZoneSpecification { 822 class _RootZoneSpecification implements ZoneSpecification {
813 const _RootZoneSpecification(); 823 const _RootZoneSpecification();
814 824
815 HandleUncaughtErrorHandler get handleUncaughtError => 825 HandleUncaughtErrorHandler get handleUncaughtError =>
(...skipping 16 matching lines...) Expand all
832 842
833 class _RootZone extends _BaseZone { 843 class _RootZone extends _BaseZone {
834 const _RootZone(); 844 const _RootZone();
835 845
836 Zone get parent => null; 846 Zone get parent => null;
837 ZoneSpecification get _specification => const _RootZoneSpecification(); 847 ZoneSpecification get _specification => const _RootZoneSpecification();
838 Zone get _errorZone => this; 848 Zone get _errorZone => this;
839 849
840 bool inSameErrorZone(Zone otherZone) => otherZone._errorZone == this; 850 bool inSameErrorZone(Zone otherZone) => otherZone._errorZone == this;
841 851
842 operator [](Symbol key) => null; 852 operator [](Object key) => null;
843 853
844 // Methods that can be customized by the zone specification. 854 // Methods that can be customized by the zone specification.
845 855
846 dynamic handleUncaughtError(error, StackTrace stackTrace) => 856 dynamic handleUncaughtError(error, StackTrace stackTrace) =>
847 _rootHandleUncaughtError(this, null, this, error, stackTrace); 857 _rootHandleUncaughtError(this, null, this, error, stackTrace);
848 858
849 Zone fork({ZoneSpecification specification, Map zoneValues}) => 859 Zone fork({ZoneSpecification specification, Map zoneValues}) =>
850 _rootFork(this, null, this, specification, zoneValues); 860 _rootFork(this, null, this, specification, zoneValues);
851 861
852 dynamic run(f()) => _rootRun(this, null, this, f); 862 dynamic run(f()) => _rootRun(this, null, this, f);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 * }, onError: (e) { print("unused error handler"); }); 910 * }, onError: (e) { print("unused error handler"); });
901 * }, onError: (e) { print("catches error of first error-zone."); }); 911 * }, onError: (e) { print("catches error of first error-zone."); });
902 * 912 *
903 * Example: 913 * Example:
904 * 914 *
905 * runZoned(() { 915 * runZoned(() {
906 * new Future(() { throw "asynchronous error"; }); 916 * new Future(() { throw "asynchronous error"; });
907 * }, onError: print); // Will print "asynchronous error". 917 * }, onError: print); // Will print "asynchronous error".
908 */ 918 */
909 dynamic runZoned(body(), 919 dynamic runZoned(body(),
910 { Map<Symbol, dynamic> zoneValues, 920 { Map zoneValues,
911 ZoneSpecification zoneSpecification, 921 ZoneSpecification zoneSpecification,
912 Function onError }) { 922 Function onError }) {
913 HandleUncaughtErrorHandler errorHandler; 923 HandleUncaughtErrorHandler errorHandler;
914 if (onError != null) { 924 if (onError != null) {
915 errorHandler = (Zone self, ZoneDelegate parent, Zone zone, 925 errorHandler = (Zone self, ZoneDelegate parent, Zone zone,
916 error, StackTrace stackTrace) { 926 error, StackTrace stackTrace) {
917 try { 927 try {
918 if (onError is ZoneBinaryCallback) { 928 if (onError is ZoneBinaryCallback) {
919 return self.parent.runBinary(onError, error, stackTrace); 929 return self.parent.runBinary(onError, error, stackTrace);
920 } 930 }
(...skipping 16 matching lines...) Expand all
937 handleUncaughtError: errorHandler); 947 handleUncaughtError: errorHandler);
938 } 948 }
939 Zone zone = Zone.current.fork(specification: zoneSpecification, 949 Zone zone = Zone.current.fork(specification: zoneSpecification,
940 zoneValues: zoneValues); 950 zoneValues: zoneValues);
941 if (onError != null) { 951 if (onError != null) {
942 return zone.runGuarded(body); 952 return zone.runGuarded(body);
943 } else { 953 } else {
944 return zone.run(body); 954 return zone.run(body);
945 } 955 }
946 } 956 }
OLDNEW
« no previous file with comments | « no previous file | tests/lib/async/zone_value_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698