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

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

Issue 2959163002: Make Zone API strong mode clean. (Closed)
Patch Set: Created 3 years, 5 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
« no previous file with comments | « sdk/lib/async/timer.dart ('k') | sdk/lib/html/dart2js/html_dart2js.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 R ZoneCallback<R>(); 7 typedef R ZoneCallback<R>();
8 typedef R ZoneUnaryCallback<R, T>(T arg); 8 typedef R ZoneUnaryCallback<R, T>(T arg);
9 typedef R ZoneBinaryCallback<R, T1, T2>(T1 arg1, T2 arg2); 9 typedef R ZoneBinaryCallback<R, T1, T2>(T1 arg1, T2 arg2);
10 10
11 // TODO(floitsch): we are abusing generic typedefs as typedefs for generic 11 typedef HandleUncaughtErrorHandler = void Function(Zone self,
12 // functions. 12 ZoneDelegate parent, Zone zone, Object error, StackTrace stackTrace);
13 /*ABUSE*/ 13 typedef RunHandler = R Function<R>(
14 typedef R HandleUncaughtErrorHandler<R>( 14 Zone self, ZoneDelegate parent, Zone zone, R Function() f);
15 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace); 15 typedef RunUnaryHandler = R Function<R, T>(
16 /*ABUSE*/ 16 Zone self, ZoneDelegate parent, Zone zone, R Function(T arg) f, T arg);
17 typedef R RunHandler<R>(Zone self, ZoneDelegate parent, Zone zone, R f()); 17 typedef RunBinaryHandler = R Function<R, T1, T2>(Zone self, ZoneDelegate parent,
18 /*ABUSE*/ 18 Zone zone, R Function(T1 arg1, T2 arg2) f, T1 arg1, T2 arg2);
19 typedef R RunUnaryHandler<R, T>( 19 typedef RegisterCallbackHandler = ZoneCallback<R> Function<R>(
20 Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg); 20 Zone self, ZoneDelegate parent, Zone zone, R Function() f);
21 /*ABUSE*/ 21 typedef RegisterUnaryCallbackHandler = ZoneUnaryCallback<R, T> Function<R, T>(
22 typedef R RunBinaryHandler<R, T1, T2>(Zone self, ZoneDelegate parent, Zone zone, 22 Zone self, ZoneDelegate parent, Zone zone, R Function(T arg) f);
23 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2); 23 typedef RegisterBinaryCallbackHandler
24 /*ABUSE*/ 24 = ZoneBinaryCallback<R, T1, T2> Function<R, T1, T2>(Zone self,
25 typedef ZoneCallback<R> RegisterCallbackHandler<R>( 25 ZoneDelegate parent, Zone zone, R Function(T1 arg1, T2 arg2) f);
26 Zone self, ZoneDelegate parent, Zone zone, R f());
27 /*ABUSE*/
28 typedef ZoneUnaryCallback<R, T> RegisterUnaryCallbackHandler<R, T>(
29 Zone self, ZoneDelegate parent, Zone zone, R f(T arg));
30 /*ABUSE*/
31 typedef ZoneBinaryCallback<R, T1, T2> RegisterBinaryCallbackHandler<R, T1, T2>(
32 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2));
33 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent, 26 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent,
34 Zone zone, Object error, StackTrace stackTrace); 27 Zone zone, Object error, StackTrace stackTrace);
35 typedef void ScheduleMicrotaskHandler( 28 typedef void ScheduleMicrotaskHandler(
36 Zone self, ZoneDelegate parent, Zone zone, void f()); 29 Zone self, ZoneDelegate parent, Zone zone, void f());
37 typedef Timer CreateTimerHandler( 30 typedef Timer CreateTimerHandler(
38 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f()); 31 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f());
39 typedef Timer CreatePeriodicTimerHandler(Zone self, ZoneDelegate parent, 32 typedef Timer CreatePeriodicTimerHandler(Zone self, ZoneDelegate parent,
40 Zone zone, Duration period, void f(Timer timer)); 33 Zone zone, Duration period, void f(Timer timer));
41 typedef void PrintHandler( 34 typedef void PrintHandler(
42 Zone self, ZoneDelegate parent, Zone zone, String line); 35 Zone self, ZoneDelegate parent, Zone zone, String line);
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 * 195 *
203 * While zones have access to their parent zone (through [Zone.parent]) it is 196 * While zones have access to their parent zone (through [Zone.parent]) it is
204 * recommended to call the methods on the provided parent delegate for two 197 * recommended to call the methods on the provided parent delegate for two
205 * reasons: 198 * reasons:
206 * 1. the delegate methods take an additional `zone` argument which is the 199 * 1. the delegate methods take an additional `zone` argument which is the
207 * zone the action has been initiated in. 200 * zone the action has been initiated in.
208 * 2. delegate calls are more efficient, since the implementation knows how 201 * 2. delegate calls are more efficient, since the implementation knows how
209 * to skip zones that would just delegate to their parents. 202 * to skip zones that would just delegate to their parents.
210 */ 203 */
211 abstract class ZoneDelegate { 204 abstract class ZoneDelegate {
212 R handleUncaughtError<R>(Zone zone, error, StackTrace stackTrace); 205 void handleUncaughtError(Zone zone, error, StackTrace stackTrace);
213 R run<R>(Zone zone, R f()); 206 R run<R>(Zone zone, R f());
214 R runUnary<R, T>(Zone zone, R f(T arg), T arg); 207 R runUnary<R, T>(Zone zone, R f(T arg), T arg);
215 R runBinary<R, T1, T2>(Zone zone, R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2); 208 R runBinary<R, T1, T2>(Zone zone, R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2);
216 ZoneCallback<R> registerCallback<R>(Zone zone, R f()); 209 ZoneCallback<R> registerCallback<R>(Zone zone, R f());
217 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(Zone zone, R f(T arg)); 210 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(Zone zone, R f(T arg));
218 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( 211 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>(
219 Zone zone, R f(T1 arg1, T2 arg2)); 212 Zone zone, R f(T1 arg1, T2 arg2));
220 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace); 213 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace);
221 void scheduleMicrotask(Zone zone, void f()); 214 void scheduleMicrotask(Zone zone, void f());
222 Timer createTimer(Zone zone, Duration duration, void f()); 215 Timer createTimer(Zone zone, Duration duration, void f());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 * also remembers the current zone so that it can later run the callback in 249 * also remembers the current zone so that it can later run the callback in
257 * that zone. 250 * that zone.
258 * 2. At a later point the registered callback is run in the remembered zone. 251 * 2. At a later point the registered callback is run in the remembered zone.
259 * 252 *
260 * This is all handled internally by the platform code and most users don't need 253 * This is all handled internally by the platform code and most users don't need
261 * to worry about it. However, developers of new asynchronous operations, 254 * to worry about it. However, developers of new asynchronous operations,
262 * provided by the underlying system or through native extensions, must follow 255 * provided by the underlying system or through native extensions, must follow
263 * the protocol to be zone compatible. 256 * the protocol to be zone compatible.
264 * 257 *
265 * For convenience, zones provide [bindCallback] (and the corresponding 258 * For convenience, zones provide [bindCallback] (and the corresponding
266 * [bindUnaryCallback] or [bindBinaryCallback]) to make it easier to respect the 259 * [bindUnaryCallback] and [bindBinaryCallback]) to make it easier to respect
267 * zone contract: these functions first invoke the corresponding `register` 260 * the zone contract: these functions first invoke the corresponding `register`
268 * functions and then wrap the returned function so that it runs in the current 261 * functions and then wrap the returned function so that it runs in the current
269 * zone when it is later asynchronously invoked. 262 * zone when it is later asynchronously invoked.
263 *
264 * Similarly, zones provide [bindCallbackGuarded] (and the corresponding
265 * [bindUnaryCallbackGuarded] and [bindBinaryCallbackGuarded]), when the
266 * callback should be invoked through [Zone.runGuarded].
270 */ 267 */
271 abstract class Zone { 268 abstract class Zone {
272 // Private constructor so that it is not possible instantiate a Zone class. 269 // Private constructor so that it is not possible instantiate a Zone class.
273 Zone._(); 270 Zone._();
274 271
275 /** 272 /**
276 * The root zone. 273 * The root zone.
277 * 274 *
278 * All isolate entry functions (`main` or spawned functions) start running in 275 * All isolate entry functions (`main` or spawned functions) start running in
279 * the root zone (that is, [Zone.current] is identical to [Zone.ROOT] when the 276 * the root zone (that is, [Zone.current] is identical to [Zone.ROOT] when the
(...skipping 25 matching lines...) Expand all
305 * chains, but for which no child registered an error handler. 302 * chains, but for which no child registered an error handler.
306 * Most asynchronous classes, like [Future] or [Stream] push errors to their 303 * Most asynchronous classes, like [Future] or [Stream] push errors to their
307 * listeners. Errors are propagated this way until either a listener handles 304 * listeners. Errors are propagated this way until either a listener handles
308 * the error (for example with [Future.catchError]), or no listener is 305 * the error (for example with [Future.catchError]), or no listener is
309 * available anymore. In the latter case, futures and streams invoke the 306 * available anymore. In the latter case, futures and streams invoke the
310 * zone's [handleUncaughtError]. 307 * zone's [handleUncaughtError].
311 * 308 *
312 * By default, when handled by the root zone, uncaught asynchronous errors are 309 * By default, when handled by the root zone, uncaught asynchronous errors are
313 * treated like uncaught synchronous exceptions. 310 * treated like uncaught synchronous exceptions.
314 */ 311 */
315 R handleUncaughtError<R>(error, StackTrace stackTrace); 312 void handleUncaughtError(error, StackTrace stackTrace);
316 313
317 /** 314 /**
318 * The parent zone of the this zone. 315 * The parent zone of the this zone.
319 * 316 *
320 * Is `null` if `this` is the [ROOT] zone. 317 * Is `null` if `this` is the [ROOT] zone.
321 * 318 *
322 * Zones are created by [fork] on an existing zone, or by [runZoned] which 319 * Zones are created by [fork] on an existing zone, or by [runZoned] which
323 * forks the [current] zone. The new zone's parent zone is the zone it was 320 * forks the [current] zone. The new zone's parent zone is the zone it was
324 * forked from. 321 * forked from.
325 */ 322 */
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 R runBinary<R, T1, T2>( 428 R runBinary<R, T1, T2>(
432 R action(T1 argument1, T2 argument2), T1 argument1, T2 argument2); 429 R action(T1 argument1, T2 argument2), T1 argument1, T2 argument2);
433 430
434 /** 431 /**
435 * Executes the given [action] in this zone and catches synchronous 432 * Executes the given [action] in this zone and catches synchronous
436 * errors. 433 * errors.
437 * 434 *
438 * This function is equivalent to: 435 * This function is equivalent to:
439 * ``` 436 * ```
440 * try { 437 * try {
441 * return this.run(action); 438 * this.run(action);
442 * } catch (e, s) { 439 * } catch (e, s) {
443 * return this.handleUncaughtError(e, s); 440 * this.handleUncaughtError(e, s);
444 * } 441 * }
445 * ``` 442 * ```
446 * 443 *
447 * See [run]. 444 * See [run].
448 */ 445 */
449 R runGuarded<R>(R action()); 446 void runGuarded(void action());
450 447
451 /** 448 /**
452 * Executes the given [action] with [argument] in this zone and 449 * Executes the given [action] with [argument] in this zone and
453 * catches synchronous errors. 450 * catches synchronous errors.
454 * 451 *
455 * See [runGuarded]. 452 * See [runGuarded].
456 */ 453 */
457 R runUnaryGuarded<R, T>(R action(T argument), T argument); 454 void runUnaryGuarded<T>(void action(T argument), T argument);
458 455
459 /** 456 /**
460 * Executes the given [action] with [argument1] and [argument2] in this 457 * Executes the given [action] with [argument1] and [argument2] in this
461 * zone and catches synchronous errors. 458 * zone and catches synchronous errors.
462 * 459 *
463 * See [runGuarded]. 460 * See [runGuarded].
464 */ 461 */
465 R runBinaryGuarded<R, T1, T2>( 462 void runBinaryGuarded<T1, T2>(
466 R action(T1 argument1, T2 argument2), T1 argument1, T2 argument2); 463 void action(T1 argument1, T2 argument2), T1 argument1, T2 argument2);
467 464
468 /** 465 /**
469 * Registers the given callback in this zone. 466 * Registers the given callback in this zone.
470 * 467 *
471 * When implementing an asynchronous primitive that uses callbacks, the 468 * When implementing an asynchronous primitive that uses callbacks, the
472 * callback must be registered using [registerCallback] at the point where the 469 * callback must be registered using [registerCallback] at the point where the
473 * user provides the callback. This allows zones to record other information 470 * user provides the callback. This allows zones to record other information
474 * that they need at the same time, perhaps even wrapping the callback, so 471 * that they need at the same time, perhaps even wrapping the callback, so
475 * that the callback is prepared when it is later run in the same zones 472 * that the callback is prepared when it is later run in the same zones
476 * (using [run]). For example, a zone may decide 473 * (using [run]). For example, a zone may decide
(...skipping 17 matching lines...) Expand all
494 491
495 /** 492 /**
496 * Registers the given callback in this zone. 493 * Registers the given callback in this zone.
497 * 494 *
498 * Similar to [registerCallback] but with a unary callback. 495 * Similar to [registerCallback] but with a unary callback.
499 */ 496 */
500 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( 497 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>(
501 R callback(T1 arg1, T2 arg2)); 498 R callback(T1 arg1, T2 arg2));
502 499
503 /** 500 /**
501 * Registers the provided [callback] and returns a function that will
502 * execute in this zone.
503 *
504 * Equivalent to: 504 * Equivalent to:
505 * 505 *
506 * ZoneCallback registered = this.registerCallback(action); 506 * ZoneCallback registered = this.registerCallback(callback);
507 * if (runGuarded) return () => this.runGuarded(registered);
508 * return () => this.run(registered); 507 * return () => this.run(registered);
509 * 508 *
510 */ 509 */
511 ZoneCallback<R> bindCallback<R>(R action(), {bool runGuarded: true}); 510 ZoneCallback<R> bindCallback<R>(R callback());
512 511
513 /** 512 /**
513 * Registers the provided [callback] and returns a function that will
514 * execute in this zone.
515 *
514 * Equivalent to: 516 * Equivalent to:
515 * 517 *
516 * ZoneCallback registered = this.registerUnaryCallback(action); 518 * ZoneCallback registered = this.registerUnaryCallback(callback);
517 * if (runGuarded) return (arg) => this.runUnaryGuarded(registered, arg);
518 * return (arg) => thin.runUnary(registered, arg); 519 * return (arg) => thin.runUnary(registered, arg);
519 */ 520 */
520 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>(R action(T argument), 521 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>(R callback(T argument));
521 {bool runGuarded: true});
522 522
523 /** 523 /**
524 * Registers the provided [callback] and returns a function that will
525 * execute in this zone.
526 *
524 * Equivalent to: 527 * Equivalent to:
525 * 528 *
526 * ZoneCallback registered = registerBinaryCallback(action); 529 * ZoneCallback registered = registerBinaryCallback(callback);
527 * if (runGuarded) {
528 * return (arg1, arg2) => this.runBinaryGuarded(registered, arg);
529 * }
530 * return (arg1, arg2) => thin.runBinary(registered, arg1, arg2); 530 * return (arg1, arg2) => thin.runBinary(registered, arg1, arg2);
531 */ 531 */
532 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>( 532 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>(
533 R action(T1 argument1, T2 argument2), 533 R callback(T1 argument1, T2 argument2));
534 {bool runGuarded: true}); 534
535 /**
536 * Registers the provided [callback] and returns a function that will
537 * execute in this zone.
538 *
539 * When the function executes, errors are caught and treated as uncaught
540 * errors.
541 *
542 * Equivalent to:
543 *
544 * ZoneCallback registered = this.registerCallback(callback);
545 * return () => this.runGuarded(registered);
546 *
547 */
548 void Function() bindCallbackGuarded(void callback());
549
550 /**
551 * Registers the provided [callback] and returns a function that will
552 * execute in this zone.
553 *
554 * When the function executes, errors are caught and treated as uncaught
555 * errors.
556 *
557 * Equivalent to:
558 *
559 * ZoneCallback registered = this.registerUnaryCallback(callback);
560 * return (arg) => this.runUnaryGuarded(registered, arg);
561 */
562 void Function(T) bindUnaryCallbackGuarded<T>(void callback(T argument));
563
564 /**
565 * Registers the provided [callback] and returns a function that will
566 * execute in this zone.
567 *
568 * Equivalent to:
569 *
570 * ZoneCallback registered = registerBinaryCallback(callback);
571 * return (arg1, arg2) => this.runBinaryGuarded(registered, arg1, arg2);
572 */
573 void Function(T1, T2) bindBinaryCallbackGuarded<T1, T2>(
574 void callback(T1 argument1, T2 argument2));
535 575
536 /** 576 /**
537 * Intercepts errors when added programmatically to a `Future` or `Stream`. 577 * Intercepts errors when added programmatically to a `Future` or `Stream`.
538 * 578 *
539 * When calling [Completer.completeError], [StreamController.addError], 579 * When calling [Completer.completeError], [StreamController.addError],
540 * or some [Future] constructors, the current zone is allowed to intercept 580 * or some [Future] constructors, the current zone is allowed to intercept
541 * and replace the error. 581 * and replace the error.
542 * 582 *
543 * Future constructors invoke this function when the error is received 583 * Future constructors invoke this function when the error is received
544 * directly, for example with [Future.error], or when the error is caught 584 * directly, for example with [Future.error], or when the error is caught
(...skipping 13 matching lines...) Expand all
558 * Custom zones may intercept this operation. 598 * Custom zones may intercept this operation.
559 * 599 *
560 * Implementations of a new asynchronous primitive that converts synchronous 600 * Implementations of a new asynchronous primitive that converts synchronous
561 * errors to asynchronous errors rarely need to invoke [errorCallback], since 601 * errors to asynchronous errors rarely need to invoke [errorCallback], since
562 * errors are usually reported through future completers or stream 602 * errors are usually reported through future completers or stream
563 * controllers. 603 * controllers.
564 */ 604 */
565 AsyncError errorCallback(Object error, StackTrace stackTrace); 605 AsyncError errorCallback(Object error, StackTrace stackTrace);
566 606
567 /** 607 /**
568 * Runs [action] asynchronously in this zone. 608 * Runs [callback] asynchronously in this zone.
569 * 609 *
570 * The global `scheduleMicrotask` delegates to the current zone's 610 * The global `scheduleMicrotask` delegates to the current zone's
571 * [scheduleMicrotask]. The root zone's implementation interacts with the 611 * [scheduleMicrotask]. The root zone's implementation interacts with the
572 * underlying system to schedule the given callback as a microtask. 612 * underlying system to schedule the given callback as a microtask.
573 * 613 *
574 * Custom zones may intercept this operation (for example to wrap the given 614 * Custom zones may intercept this operation (for example to wrap the given
575 * callback [action]). 615 * [callback]).
576 */ 616 */
577 void scheduleMicrotask(void action()); 617 void scheduleMicrotask(void callback());
578 618
579 /** 619 /**
580 * Creates a Timer where the callback is executed in this zone. 620 * Creates a Timer where the callback is executed in this zone.
581 */ 621 */
582 Timer createTimer(Duration duration, void callback()); 622 Timer createTimer(Duration duration, void callback());
583 623
584 /** 624 /**
585 * Creates a periodic Timer where the callback is executed in this zone. 625 * Creates a periodic Timer where the callback is executed in this zone.
586 */ 626 */
587 Timer createPeriodicTimer(Duration period, void callback(Timer timer)); 627 Timer createPeriodicTimer(Duration period, void callback(Timer timer));
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 ZoneDelegate _parentDelegate(_Zone zone) { 689 ZoneDelegate _parentDelegate(_Zone zone) {
650 if (zone.parent == null) return null; 690 if (zone.parent == null) return null;
651 return zone.parent._delegate; 691 return zone.parent._delegate;
652 } 692 }
653 693
654 class _ZoneDelegate implements ZoneDelegate { 694 class _ZoneDelegate implements ZoneDelegate {
655 final _Zone _delegationTarget; 695 final _Zone _delegationTarget;
656 696
657 _ZoneDelegate(this._delegationTarget); 697 _ZoneDelegate(this._delegationTarget);
658 698
659 R handleUncaughtError<R>(Zone zone, error, StackTrace stackTrace) { 699 void handleUncaughtError(Zone zone, error, StackTrace stackTrace) {
660 var implementation = _delegationTarget._handleUncaughtError; 700 var implementation = _delegationTarget._handleUncaughtError;
661 _Zone implZone = implementation.zone; 701 _Zone implZone = implementation.zone;
662 HandleUncaughtErrorHandler handler = implementation.function; 702 HandleUncaughtErrorHandler handler = implementation.function;
663 // TODO(floitsch): make this a generic method call on '<R>' once it's 703 return handler(
664 // supported. Remove the unnecessary cast. 704 implZone, _parentDelegate(implZone), zone, error, stackTrace);
665 return handler(implZone, _parentDelegate(implZone), zone, error, stackTrace)
666 as Object/*=R*/;
667 } 705 }
668 706
669 R run<R>(Zone zone, R f()) { 707 R run<R>(Zone zone, R f()) {
670 var implementation = _delegationTarget._run; 708 var implementation = _delegationTarget._run;
671 _Zone implZone = implementation.zone; 709 _Zone implZone = implementation.zone;
672 RunHandler handler = implementation.function; 710 RunHandler handler = implementation.function;
673 // TODO(floitsch): make this a generic method call on '<R>' once it's 711 return handler(implZone, _parentDelegate(implZone), zone, f);
674 // supported. Remove the unnecessary cast.
675 return handler(implZone, _parentDelegate(implZone), zone, f)
676 as Object/*=R*/;
677 } 712 }
678 713
679 R runUnary<R, T>(Zone zone, R f(T arg), T arg) { 714 R runUnary<R, T>(Zone zone, R f(T arg), T arg) {
680 var implementation = _delegationTarget._runUnary; 715 var implementation = _delegationTarget._runUnary;
681 _Zone implZone = implementation.zone; 716 _Zone implZone = implementation.zone;
682 RunUnaryHandler handler = implementation.function; 717 RunUnaryHandler handler = implementation.function;
683 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 718 return handler(implZone, _parentDelegate(implZone), zone, f, arg);
684 // supported. Remove the unnecessary cast.
685 return handler(implZone, _parentDelegate(implZone), zone, f, arg)
686 as Object/*=R*/;
687 } 719 }
688 720
689 R runBinary<R, T1, T2>(Zone zone, R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { 721 R runBinary<R, T1, T2>(Zone zone, R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) {
690 var implementation = _delegationTarget._runBinary; 722 var implementation = _delegationTarget._runBinary;
691 _Zone implZone = implementation.zone; 723 _Zone implZone = implementation.zone;
692 RunBinaryHandler handler = implementation.function; 724 RunBinaryHandler handler = implementation.function;
693 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 725 return handler(implZone, _parentDelegate(implZone), zone, f, arg1, arg2);
694 // it's supported. Remove the unnecessary cast.
695 return handler(implZone, _parentDelegate(implZone), zone, f, arg1, arg2)
696 as Object/*=R*/;
697 } 726 }
698 727
699 ZoneCallback<R> registerCallback<R>(Zone zone, R f()) { 728 ZoneCallback<R> registerCallback<R>(Zone zone, R f()) {
700 var implementation = _delegationTarget._registerCallback; 729 var implementation = _delegationTarget._registerCallback;
701 _Zone implZone = implementation.zone; 730 _Zone implZone = implementation.zone;
702 RegisterCallbackHandler handler = implementation.function; 731 RegisterCallbackHandler handler = implementation.function;
703 // TODO(floitsch): make this a generic method call on '<R>' once it's 732 return handler(implZone, _parentDelegate(implZone), zone, f);
704 // supported. Remove the unnecessary cast.
705 return handler(implZone, _parentDelegate(implZone), zone, f)
706 as Object/*=ZoneCallback<R>*/;
707 } 733 }
708 734
709 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(Zone zone, R f(T arg)) { 735 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(Zone zone, R f(T arg)) {
710 var implementation = _delegationTarget._registerUnaryCallback; 736 var implementation = _delegationTarget._registerUnaryCallback;
711 _Zone implZone = implementation.zone; 737 _Zone implZone = implementation.zone;
712 RegisterUnaryCallbackHandler handler = implementation.function; 738 RegisterUnaryCallbackHandler handler = implementation.function;
713 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 739 return handler(implZone, _parentDelegate(implZone), zone, f);
714 // supported. Remove the unnecessary cast.
715 return handler(implZone, _parentDelegate(implZone), zone, f)
716 as Object/*=ZoneUnaryCallback<R, T>*/;
717 } 740 }
718 741
719 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( 742 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>(
720 Zone zone, R f(T1 arg1, T2 arg2)) { 743 Zone zone, R f(T1 arg1, T2 arg2)) {
721 var implementation = _delegationTarget._registerBinaryCallback; 744 var implementation = _delegationTarget._registerBinaryCallback;
722 _Zone implZone = implementation.zone; 745 _Zone implZone = implementation.zone;
723 RegisterBinaryCallbackHandler handler = implementation.function; 746 RegisterBinaryCallbackHandler handler = implementation.function;
724 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 747 return handler(implZone, _parentDelegate(implZone), zone, f);
725 // it's supported. Remove the unnecessary cast.
726 return handler(implZone, _parentDelegate(implZone), zone, f)
727 as Object/*=ZoneBinaryCallback<R, T1, T2>*/;
728 } 748 }
729 749
730 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) { 750 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) {
731 var implementation = _delegationTarget._errorCallback; 751 var implementation = _delegationTarget._errorCallback;
732 _Zone implZone = implementation.zone; 752 _Zone implZone = implementation.zone;
733 if (identical(implZone, _ROOT_ZONE)) return null; 753 if (identical(implZone, _ROOT_ZONE)) return null;
734 ErrorCallbackHandler handler = implementation.function; 754 ErrorCallbackHandler handler = implementation.function;
735 return handler( 755 return handler(
736 implZone, _parentDelegate(implZone), zone, error, stackTrace); 756 implZone, _parentDelegate(implZone), zone, error, stackTrace);
737 } 757 }
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 } 909 }
890 910
891 /** 911 /**
892 * The closest error-handling zone. 912 * The closest error-handling zone.
893 * 913 *
894 * Returns `this` if `this` has an error-handler. Otherwise returns the 914 * Returns `this` if `this` has an error-handler. Otherwise returns the
895 * parent's error-zone. 915 * parent's error-zone.
896 */ 916 */
897 Zone get errorZone => _handleUncaughtError.zone; 917 Zone get errorZone => _handleUncaughtError.zone;
898 918
899 R runGuarded<R>(R f()) { 919 void runGuarded(void f()) {
900 try { 920 try {
901 return run(f); 921 run(f);
902 } catch (e, s) { 922 } catch (e, s) {
903 return handleUncaughtError(e, s); 923 handleUncaughtError(e, s);
904 } 924 }
905 } 925 }
906 926
907 R runUnaryGuarded<R, T>(R f(T arg), T arg) { 927 void runUnaryGuarded<T>(void f(T arg), T arg) {
908 try { 928 try {
909 return runUnary(f, arg); 929 runUnary(f, arg);
910 } catch (e, s) { 930 } catch (e, s) {
911 return handleUncaughtError(e, s); 931 handleUncaughtError(e, s);
912 } 932 }
913 } 933 }
914 934
915 R runBinaryGuarded<R, T1, T2>(R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { 935 void runBinaryGuarded<T1, T2>(void f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) {
916 try { 936 try {
917 return runBinary(f, arg1, arg2); 937 runBinary(f, arg1, arg2);
918 } catch (e, s) { 938 } catch (e, s) {
919 return handleUncaughtError(e, s); 939 handleUncaughtError(e, s);
920 } 940 }
921 } 941 }
922 942
923 ZoneCallback<R> bindCallback<R>(R f(), {bool runGuarded: true}) { 943 ZoneCallback<R> bindCallback<R>(R f()) {
924 var registered = registerCallback(f); 944 var registered = registerCallback(f);
925 if (runGuarded) { 945 return () => this.run(registered);
926 return () => this.runGuarded(registered);
927 } else {
928 return () => this.run(registered);
929 }
930 } 946 }
931 947
932 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>(R f(T arg), 948 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>(R f(T arg)) {
933 {bool runGuarded: true}) {
934 var registered = registerUnaryCallback(f); 949 var registered = registerUnaryCallback(f);
935 if (runGuarded) { 950 return (arg) => this.runUnary(registered, arg);
936 return (arg) => this.runUnaryGuarded(registered, arg);
937 } else {
938 return (arg) => this.runUnary(registered, arg);
939 }
940 } 951 }
941 952
942 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>( 953 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>(
943 R f(T1 arg1, T2 arg2), 954 R f(T1 arg1, T2 arg2)) {
944 {bool runGuarded: true}) {
945 var registered = registerBinaryCallback(f); 955 var registered = registerBinaryCallback(f);
946 if (runGuarded) { 956 return (arg1, arg2) => this.runBinary(registered, arg1, arg2);
947 return (arg1, arg2) => this.runBinaryGuarded(registered, arg1, arg2); 957 }
948 } else { 958
949 return (arg1, arg2) => this.runBinary(registered, arg1, arg2); 959 void Function() bindCallbackGuarded(void f()) {
950 } 960 var registered = registerCallback(f);
961 return () => this.runGuarded(registered);
962 }
963
964 void Function(T) bindUnaryCallbackGuarded<T>(void f(T arg)) {
965 var registered = registerUnaryCallback(f);
966 return (arg) => this.runUnaryGuarded(registered, arg);
967 }
968
969 void Function(T1, T2) bindBinaryCallbackGuarded<T1, T2>(
970 void f(T1 arg1, T2 arg2)) {
971 var registered = registerBinaryCallback(f);
972 return (arg1, arg2) => this.runBinaryGuarded(registered, arg1, arg2);
951 } 973 }
952 974
953 operator [](Object key) { 975 operator [](Object key) {
954 var result = _map[key]; 976 var result = _map[key];
955 if (result != null || _map.containsKey(key)) return result; 977 if (result != null || _map.containsKey(key)) return result;
956 // If we are not the root zone, look up in the parent zone. 978 // If we are not the root zone, look up in the parent zone.
957 if (parent != null) { 979 if (parent != null) {
958 // We do not optimize for repeatedly looking up a key which isn't 980 // We do not optimize for repeatedly looking up a key which isn't
959 // there. That would require storing the key and keeping it alive. 981 // there. That would require storing the key and keeping it alive.
960 // Copying the key/value from the parent does not keep any new values 982 // Copying the key/value from the parent does not keep any new values
961 // alive. 983 // alive.
962 var value = parent[key]; 984 var value = parent[key];
963 if (value != null) { 985 if (value != null) {
964 _map[key] = value; 986 _map[key] = value;
965 } 987 }
966 return value; 988 return value;
967 } 989 }
968 assert(this == _ROOT_ZONE); 990 assert(this == _ROOT_ZONE);
969 return null; 991 return null;
970 } 992 }
971 993
972 // Methods that can be customized by the zone specification. 994 // Methods that can be customized by the zone specification.
973 995
974 R handleUncaughtError<R>(error, StackTrace stackTrace) { 996 void handleUncaughtError(error, StackTrace stackTrace) {
975 var implementation = this._handleUncaughtError; 997 var implementation = this._handleUncaughtError;
976 assert(implementation != null); 998 assert(implementation != null);
977 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 999 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
978 HandleUncaughtErrorHandler handler = implementation.function; 1000 HandleUncaughtErrorHandler handler = implementation.function;
979 // TODO(floitsch): make this a generic method call on '<R>' once it's 1001 return handler(
980 // supported. Remove the unnecessary cast. 1002 implementation.zone, parentDelegate, this, error, stackTrace);
981 return handler(implementation.zone, parentDelegate, this, error, stackTrace)
982 as Object/*=R*/;
983 } 1003 }
984 1004
985 Zone fork({ZoneSpecification specification, Map zoneValues}) { 1005 Zone fork({ZoneSpecification specification, Map zoneValues}) {
986 var implementation = this._fork; 1006 var implementation = this._fork;
987 assert(implementation != null); 1007 assert(implementation != null);
988 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1008 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
989 ForkHandler handler = implementation.function; 1009 ForkHandler handler = implementation.function;
990 return handler( 1010 return handler(
991 implementation.zone, parentDelegate, this, specification, zoneValues); 1011 implementation.zone, parentDelegate, this, specification, zoneValues);
992 } 1012 }
993 1013
994 R run<R>(R f()) { 1014 R run<R>(R f()) {
995 var implementation = this._run; 1015 var implementation = this._run;
996 assert(implementation != null); 1016 assert(implementation != null);
997 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1017 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
998 RunHandler handler = implementation.function; 1018 RunHandler handler = implementation.function;
999 // TODO(floitsch): make this a generic method call on '<R>' once it's 1019 return handler(implementation.zone, parentDelegate, this, f);
1000 // supported. Remove the unnecessary cast.
1001 return handler(implementation.zone, parentDelegate, this, f)
1002 as Object/*=R*/;
1003 } 1020 }
1004 1021
1005 R runUnary<R, T>(R f(T arg), T arg) { 1022 R runUnary<R, T>(R f(T arg), T arg) {
1006 var implementation = this._runUnary; 1023 var implementation = this._runUnary;
1007 assert(implementation != null); 1024 assert(implementation != null);
1008 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1025 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1009 RunUnaryHandler handler = implementation.function; 1026 RunUnaryHandler handler = implementation.function;
1010 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 1027 return handler(implementation.zone, parentDelegate, this, f, arg);
1011 // supported. Remove the unnecessary cast.
1012 return handler(implementation.zone, parentDelegate, this, f, arg)
1013 as Object/*=R*/;
1014 } 1028 }
1015 1029
1016 R runBinary<R, T1, T2>(R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { 1030 R runBinary<R, T1, T2>(R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) {
1017 var implementation = this._runBinary; 1031 var implementation = this._runBinary;
1018 assert(implementation != null); 1032 assert(implementation != null);
1019 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1033 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1020 RunBinaryHandler handler = implementation.function; 1034 RunBinaryHandler handler = implementation.function;
1021 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 1035 return handler(implementation.zone, parentDelegate, this, f, arg1, arg2);
1022 // it's supported. Remove the unnecessary cast.
1023 return handler(implementation.zone, parentDelegate, this, f, arg1, arg2)
1024 as Object/*=R*/;
1025 } 1036 }
1026 1037
1027 ZoneCallback<R> registerCallback<R>(R callback()) { 1038 ZoneCallback<R> registerCallback<R>(R callback()) {
1028 var implementation = this._registerCallback; 1039 var implementation = this._registerCallback;
1029 assert(implementation != null); 1040 assert(implementation != null);
1030 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1041 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1031 RegisterCallbackHandler handler = implementation.function; 1042 RegisterCallbackHandler handler = implementation.function;
1032 // TODO(floitsch): make this a generic method call on '<R>' once it's 1043 return handler(implementation.zone, parentDelegate, this, callback);
1033 // supported. Remove the unnecessary cast.
1034 return handler(implementation.zone, parentDelegate, this, callback)
1035 as Object/*=ZoneCallback<R>*/;
1036 } 1044 }
1037 1045
1038 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(R callback(T arg)) { 1046 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(R callback(T arg)) {
1039 var implementation = this._registerUnaryCallback; 1047 var implementation = this._registerUnaryCallback;
1040 assert(implementation != null); 1048 assert(implementation != null);
1041 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1049 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1042 RegisterUnaryCallbackHandler handler = implementation.function; 1050 RegisterUnaryCallbackHandler handler = implementation.function;
1043 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 1051 return handler(implementation.zone, parentDelegate, this, callback);
1044 // supported. Remove the unnecessary cast.
1045 return handler(implementation.zone, parentDelegate, this, callback)
1046 as Object/*=ZoneUnaryCallback<R, T>*/;
1047 } 1052 }
1048 1053
1049 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( 1054 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>(
1050 R callback(T1 arg1, T2 arg2)) { 1055 R callback(T1 arg1, T2 arg2)) {
1051 var implementation = this._registerBinaryCallback; 1056 var implementation = this._registerBinaryCallback;
1052 assert(implementation != null); 1057 assert(implementation != null);
1053 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1058 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1054 RegisterBinaryCallbackHandler handler = implementation.function; 1059 RegisterBinaryCallbackHandler handler = implementation.function;
1055 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 1060 return handler(implementation.zone, parentDelegate, this, callback);
1056 // it's supported. Remove the unnecessary cast.
1057 return handler(implementation.zone, parentDelegate, this, callback)
1058 as Object/*=ZoneBinaryCallback<R, T1, T2>*/;
1059 } 1061 }
1060 1062
1061 AsyncError errorCallback(Object error, StackTrace stackTrace) { 1063 AsyncError errorCallback(Object error, StackTrace stackTrace) {
1062 var implementation = this._errorCallback; 1064 var implementation = this._errorCallback;
1063 assert(implementation != null); 1065 assert(implementation != null);
1064 final Zone implementationZone = implementation.zone; 1066 final Zone implementationZone = implementation.zone;
1065 if (identical(implementationZone, _ROOT_ZONE)) return null; 1067 if (identical(implementationZone, _ROOT_ZONE)) return null;
1066 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone); 1068 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone);
1067 ErrorCallbackHandler handler = implementation.function; 1069 ErrorCallbackHandler handler = implementation.function;
1068 return handler(implementationZone, parentDelegate, this, error, stackTrace); 1070 return handler(implementationZone, parentDelegate, this, error, stackTrace);
(...skipping 25 matching lines...) Expand all
1094 1096
1095 void print(String line) { 1097 void print(String line) {
1096 var implementation = this._print; 1098 var implementation = this._print;
1097 assert(implementation != null); 1099 assert(implementation != null);
1098 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1100 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1099 PrintHandler handler = implementation.function; 1101 PrintHandler handler = implementation.function;
1100 return handler(implementation.zone, parentDelegate, this, line); 1102 return handler(implementation.zone, parentDelegate, this, line);
1101 } 1103 }
1102 } 1104 }
1103 1105
1104 R _rootHandleUncaughtError<R>( 1106 void _rootHandleUncaughtError(
1105 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) { 1107 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) {
1106 _schedulePriorityAsyncCallback(() { 1108 _schedulePriorityAsyncCallback(() {
1107 if (error == null) error = new NullThrownError(); 1109 if (error == null) error = new NullThrownError();
1108 if (stackTrace == null) throw error; 1110 if (stackTrace == null) throw error;
1109 _rethrow(error, stackTrace); 1111 _rethrow(error, stackTrace);
1110 }); 1112 });
1111 } 1113 }
1112 1114
1113 external void _rethrow(Object error, StackTrace stackTrace); 1115 external void _rethrow(Object error, StackTrace stackTrace);
1114 1116
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 1161
1160 ZoneBinaryCallback<R, T1, T2> _rootRegisterBinaryCallback<R, T1, T2>( 1162 ZoneBinaryCallback<R, T1, T2> _rootRegisterBinaryCallback<R, T1, T2>(
1161 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)) { 1163 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)) {
1162 return f; 1164 return f;
1163 } 1165 }
1164 1166
1165 AsyncError _rootErrorCallback(Zone self, ZoneDelegate parent, Zone zone, 1167 AsyncError _rootErrorCallback(Zone self, ZoneDelegate parent, Zone zone,
1166 Object error, StackTrace stackTrace) => 1168 Object error, StackTrace stackTrace) =>
1167 null; 1169 null;
1168 1170
1169 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) { 1171 void _rootScheduleMicrotask(
1172 Zone self, ZoneDelegate parent, Zone zone, void f()) {
1170 if (!identical(_ROOT_ZONE, zone)) { 1173 if (!identical(_ROOT_ZONE, zone)) {
1171 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone); 1174 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone);
1172 f = zone.bindCallback(f, runGuarded: hasErrorHandler); 1175 if (hasErrorHandler) {
1176 f = zone.bindCallbackGuarded(f);
1177 } else {
1178 f = zone.bindCallback(f);
1179 }
1173 // Use root zone as event zone if the function is already bound. 1180 // Use root zone as event zone if the function is already bound.
1174 zone = _ROOT_ZONE; 1181 zone = _ROOT_ZONE;
1175 } 1182 }
1176 _scheduleAsyncCallback(f); 1183 _scheduleAsyncCallback(f);
1177 } 1184 }
1178 1185
1179 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone, 1186 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone,
1180 Duration duration, void callback()) { 1187 Duration duration, void callback()) {
1181 if (!identical(_ROOT_ZONE, zone)) { 1188 if (!identical(_ROOT_ZONE, zone)) {
1182 callback = zone.bindCallback(callback); 1189 callback = zone.bindCallback(callback);
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
1283 /** 1290 /**
1284 * The closest error-handling zone. 1291 * The closest error-handling zone.
1285 * 1292 *
1286 * Returns `this` if `this` has an error-handler. Otherwise returns the 1293 * Returns `this` if `this` has an error-handler. Otherwise returns the
1287 * parent's error-zone. 1294 * parent's error-zone.
1288 */ 1295 */
1289 Zone get errorZone => this; 1296 Zone get errorZone => this;
1290 1297
1291 // Zone interface. 1298 // Zone interface.
1292 1299
1293 R runGuarded<R>(R f()) { 1300 void runGuarded(void f()) {
1294 try { 1301 try {
1295 if (identical(_ROOT_ZONE, Zone._current)) { 1302 if (identical(_ROOT_ZONE, Zone._current)) {
1296 return f(); 1303 f();
1304 return;
1297 } 1305 }
1298 return _rootRun<R>(null, null, this, f); 1306 _rootRun(null, null, this, f);
1299 } catch (e, s) { 1307 } catch (e, s) {
1300 return handleUncaughtError<R>(e, s); 1308 handleUncaughtError(e, s);
1301 } 1309 }
1302 } 1310 }
1303 1311
1304 R runUnaryGuarded<R, T>(R f(T arg), T arg) { 1312 void runUnaryGuarded<T>(void f(T arg), T arg) {
1305 try { 1313 try {
1306 if (identical(_ROOT_ZONE, Zone._current)) { 1314 if (identical(_ROOT_ZONE, Zone._current)) {
1307 return f(arg); 1315 f(arg);
1316 return;
1308 } 1317 }
1309 return _rootRunUnary<R, T>(null, null, this, f, arg); 1318 _rootRunUnary(null, null, this, f, arg);
1310 } catch (e, s) { 1319 } catch (e, s) {
1311 return handleUncaughtError<R>(e, s); 1320 handleUncaughtError(e, s);
1312 } 1321 }
1313 } 1322 }
1314 1323
1315 R runBinaryGuarded<R, T1, T2>(R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { 1324 void runBinaryGuarded<T1, T2>(void f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) {
1316 try { 1325 try {
1317 if (identical(_ROOT_ZONE, Zone._current)) { 1326 if (identical(_ROOT_ZONE, Zone._current)) {
1318 return f(arg1, arg2); 1327 f(arg1, arg2);
1328 return;
1319 } 1329 }
1320 return _rootRunBinary<R, T1, T2>(null, null, this, f, arg1, arg2); 1330 _rootRunBinary(null, null, this, f, arg1, arg2);
1321 } catch (e, s) { 1331 } catch (e, s) {
1322 return handleUncaughtError<R>(e, s); 1332 handleUncaughtError(e, s);
1323 } 1333 }
1324 } 1334 }
1325 1335
1326 ZoneCallback<R> bindCallback<R>(R f(), {bool runGuarded: true}) { 1336 ZoneCallback<R> bindCallback<R>(R f()) {
1327 if (runGuarded) { 1337 return () => this.run<R>(f);
1328 return () => this.runGuarded<R>(f);
1329 } else {
1330 return () => this.run<R>(f);
1331 }
1332 } 1338 }
1333 1339
1334 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>(R f(T arg), 1340 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>(R f(T arg)) {
1335 {bool runGuarded: true}) { 1341 return (arg) => this.runUnary<R, T>(f, arg);
1336 if (runGuarded) {
1337 return (arg) => this.runUnaryGuarded<R, T>(f, arg);
1338 } else {
1339 return (arg) => this.runUnary<R, T>(f, arg);
1340 }
1341 } 1342 }
1342 1343
1343 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>( 1344 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>(
1344 R f(T1 arg1, T2 arg2), 1345 R f(T1 arg1, T2 arg2)) {
1345 {bool runGuarded: true}) { 1346 return (arg1, arg2) => this.runBinary<R, T1, T2>(f, arg1, arg2);
1346 if (runGuarded) { 1347 }
1347 return (arg1, arg2) => this.runBinaryGuarded<R, T1, T2>(f, arg1, arg2); 1348
1348 } else { 1349 void Function() bindCallbackGuarded(void f()) {
1349 return (arg1, arg2) => this.runBinary<R, T1, T2>(f, arg1, arg2); 1350 return () => this.runGuarded(f);
1350 } 1351 }
1352
1353 void Function(T) bindUnaryCallbackGuarded<T>(void f(T arg)) {
1354 return (arg) => this.runUnaryGuarded(f, arg);
1355 }
1356
1357 void Function(T1, T2) bindBinaryCallbackGuarded<T1, T2>(
1358 void f(T1 arg1, T2 arg2)) {
1359 return (arg1, arg2) => this.runBinaryGuarded(f, arg1, arg2);
1351 } 1360 }
1352 1361
1353 operator [](Object key) => null; 1362 operator [](Object key) => null;
1354 1363
1355 // Methods that can be customized by the zone specification. 1364 // Methods that can be customized by the zone specification.
1356 1365
1357 R handleUncaughtError<R>(error, StackTrace stackTrace) { 1366 void handleUncaughtError(error, StackTrace stackTrace) {
1358 return _rootHandleUncaughtError(null, null, this, error, stackTrace); 1367 _rootHandleUncaughtError(null, null, this, error, stackTrace);
1359 } 1368 }
1360 1369
1361 Zone fork({ZoneSpecification specification, Map zoneValues}) { 1370 Zone fork({ZoneSpecification specification, Map zoneValues}) {
1362 return _rootFork(null, null, this, specification, zoneValues); 1371 return _rootFork(null, null, this, specification, zoneValues);
1363 } 1372 }
1364 1373
1365 R run<R>(R f()) { 1374 R run<R>(R f()) {
1366 if (identical(Zone._current, _ROOT_ZONE)) return f(); 1375 if (identical(Zone._current, _ROOT_ZONE)) return f();
1367 return _rootRun(null, null, this, f); 1376 return _rootRun(null, null, this, f);
1368 } 1377 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1402 void print(String line) { 1411 void print(String line) {
1403 printToConsole(line); 1412 printToConsole(line);
1404 } 1413 }
1405 } 1414 }
1406 1415
1407 const _ROOT_ZONE = const _RootZone(); 1416 const _ROOT_ZONE = const _RootZone();
1408 1417
1409 /** 1418 /**
1410 * Runs [body] in its own zone. 1419 * Runs [body] in its own zone.
1411 * 1420 *
1421 * Returns the result of invoking [body].
1422 *
1412 * If [onError] is non-null the zone is considered an error zone. All uncaught 1423 * If [onError] is non-null the zone is considered an error zone. All uncaught
1413 * errors, synchronous or asynchronous, in the zone are caught and handled 1424 * errors, synchronous or asynchronous, in the zone are caught and handled
1414 * by the callback. 1425 * by the callback. When the error is synchronous, throwing in the [onError]
1426 * handler, leads to a synchronous exception.
1427 *
1428 * Returns `null` when [body] threw, and a provided [onError] function completed
1429 * without throwing.
1415 * 1430 *
1416 * Errors may never cross error-zone boundaries. This is intuitive for leaving 1431 * Errors may never cross error-zone boundaries. This is intuitive for leaving
1417 * a zone, but it also applies for errors that would enter an error-zone. 1432 * a zone, but it also applies for errors that would enter an error-zone.
1418 * Errors that try to cross error-zone boundaries are considered uncaught. 1433 * Errors that try to cross error-zone boundaries are considered uncaught.
1419 * 1434 *
1420 * var future = new Future.value(499); 1435 * var future = new Future.value(499);
1421 * runZoned(() { 1436 * runZoned(() {
1422 * future = future.then((_) { throw "error in first error-zone"; }); 1437 * future = future.then((_) { throw "error in first error-zone"; });
1423 * runZoned(() { 1438 * runZoned(() {
1424 * future = future.catchError((e) { print("Never reached!"); }); 1439 * future = future.catchError((e) { print("Never reached!"); });
1425 * }, onError: (e) { print("unused error handler"); }); 1440 * }, onError: (e) { print("unused error handler"); });
1426 * }, onError: (e) { print("catches error of first error-zone."); }); 1441 * }, onError: (e) { print("catches error of first error-zone."); });
1427 * 1442 *
1428 * Example: 1443 * Example:
1429 * 1444 *
1430 * runZoned(() { 1445 * runZoned(() {
1431 * new Future(() { throw "asynchronous error"; }); 1446 * new Future(() { throw "asynchronous error"; });
1432 * }, onError: print); // Will print "asynchronous error". 1447 * }, onError: print); // Will print "asynchronous error".
1433 */ 1448 */
1434 R runZoned<R>(R body(), 1449 R runZoned<R>(R body(),
1435 {Map zoneValues, ZoneSpecification zoneSpecification, Function onError}) { 1450 {Map zoneValues, ZoneSpecification zoneSpecification, Function onError}) {
1451 // TODO(floitsch): the return type should be `void` here.
1452 if (onError != null &&
1453 onError is! ZoneBinaryCallback<Object, Object, StackTrace> &&
1454 onError is! ZoneUnaryCallback<Object, Object>) {
1455 throw new ArgumentError("onError callback must take an Object (the error), "
1456 "or an Object (the error) and a StackTrace");
1457 }
1436 HandleUncaughtErrorHandler errorHandler; 1458 HandleUncaughtErrorHandler errorHandler;
1437 if (onError != null) { 1459 if (onError != null) {
1438 errorHandler = (Zone self, ZoneDelegate parent, Zone zone, error, 1460 errorHandler = (Zone self, ZoneDelegate parent, Zone zone, error,
1439 StackTrace stackTrace) { 1461 StackTrace stackTrace) {
1440 try { 1462 try {
1441 // TODO(floitsch): the return type should be 'void'. 1463 if (onError is void Function(Object, StackTrace)) {
1442 if (onError is ZoneBinaryCallback<dynamic, Object, StackTrace>) { 1464 self.parent.runBinary(onError, error, stackTrace);
1443 return self.parent.runBinary(onError, error, stackTrace); 1465 return;
1444 } 1466 }
1445 return self.parent.runUnary(onError, error); 1467 assert(onError is void Function(Object));
1468 self.parent.runUnary(onError, error);
1446 } catch (e, s) { 1469 } catch (e, s) {
1447 if (identical(e, error)) { 1470 if (identical(e, error)) {
1448 return parent.handleUncaughtError(zone, error, stackTrace); 1471 parent.handleUncaughtError(zone, error, stackTrace);
1449 } else { 1472 } else {
1450 return parent.handleUncaughtError(zone, e, s); 1473 parent.handleUncaughtError(zone, e, s);
1451 } 1474 }
1452 } 1475 }
1453 }; 1476 };
1454 } 1477 }
1455 if (zoneSpecification == null) { 1478 if (zoneSpecification == null) {
1456 zoneSpecification = 1479 zoneSpecification =
1457 new ZoneSpecification(handleUncaughtError: errorHandler); 1480 new ZoneSpecification(handleUncaughtError: errorHandler);
1458 } else if (errorHandler != null) { 1481 } else if (errorHandler != null) {
1459 zoneSpecification = new ZoneSpecification.from(zoneSpecification, 1482 zoneSpecification = new ZoneSpecification.from(zoneSpecification,
1460 handleUncaughtError: errorHandler); 1483 handleUncaughtError: errorHandler);
1461 } 1484 }
1462 Zone zone = Zone.current 1485 Zone zone = Zone.current
1463 .fork(specification: zoneSpecification, zoneValues: zoneValues); 1486 .fork(specification: zoneSpecification, zoneValues: zoneValues);
1464 if (onError != null) { 1487 if (onError != null) {
1465 return zone.runGuarded(body); 1488 try {
1489 return zone.run(body);
1490 } catch (e, stackTrace) {
1491 if (onError is ZoneBinaryCallback<R, Object, StackTrace>) {
1492 zone.runBinary(onError, e, stackTrace);
1493 return null;
1494 }
1495 assert(onError is ZoneUnaryCallback<R, Object>);
1496 zone.runUnary(onError, e);
1497 return null;
1498 }
1466 } else { 1499 } else {
1467 return zone.run(body); 1500 return zone.run(body);
1468 } 1501 }
1469 } 1502 }
OLDNEW
« no previous file with comments | « sdk/lib/async/timer.dart ('k') | sdk/lib/html/dart2js/html_dart2js.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698