@@ -3,6 +3,9 @@ library angular.zone;
33import 'dart:async' ;
44import 'package:stack_trace/stack_trace.dart' show Chain;
55
6+ typedef void ZeroArgFunction ();
7+ typedef void ErrorHandlingFn (error, stackTrace);
8+
69/**
710 * A `Zone` wrapper that lets you schedule tasks after its private microtask queue is exhausted but
811 * before the next "VM turn", i.e. event loop iteration.
@@ -19,9 +22,10 @@ import 'package:stack_trace/stack_trace.dart' show Chain;
1922 * instantiated. The default `onTurnDone` runs the Angular change detection.
2023 */
2124class NgZone {
22- Function _onTurnStart;
23- Function _onTurnDone;
24- Function _onErrorHandler;
25+ ZeroArgFunction _onTurnStart;
26+ ZeroArgFunction _onTurnDone;
27+ ZeroArgFunction _onEventDone;
28+ ErrorHandlingFn _onErrorHandler;
2529
2630 // Code executed in _mountZone does not trigger the onTurnDone.
2731 Zone _mountZone;
@@ -65,21 +69,40 @@ class NgZone {
6569 }
6670
6771 /**
68- * Initializes the zone hooks.
69- *
70- * The given error handler should re-throw the passed exception. Otherwise, exceptions will not
71- * propagate outside of the [NgZone] and can alter the application execution flow.
72- * Not re-throwing could be used to help testing the code or advanced use cases.
72+ * Sets the zone hook that is called just before Angular event turn starts.
73+ * It is called once per browser event.
74+ */
75+ void overrideOnTurnStart (ZeroArgFunction onTurnStartFn) {
76+ this ._onTurnStart = onTurnStartFn;
77+ }
78+
79+ /**
80+ * Sets the zone hook that is called immediately after Angular processes
81+ * all pending microtasks.
82+ */
83+ void overrideOnTurnDone (ZeroArgFunction onTurnDoneFn) {
84+ this ._onTurnDone = onTurnDoneFn;
85+ }
86+
87+ /**
88+ * Sets the zone hook that is called immediately after the last turn in the
89+ * current event completes. At this point Angular will no longer attempt to
90+ * sync the UI. Any changes to the data model will not be reflected in the
91+ * DOM. {@link onEventDoneFn} is executed outside Angular zone.
7392 *
74- * @param {Function} onTurnStart called before code executes in the inner zone for each VM turn
75- * @param {Function} onTurnDone called at the end of a VM turn if code has executed in the inner zone
76- * @param {Function} onErrorHandler called when an exception is thrown by a macro or micro task
93+ * This hook is useful for validating application state (e.g. in a test).
7794 */
78- void initCallbacks (
79- {Function onTurnStart, Function onTurnDone, Function onErrorHandler}) {
80- _onTurnStart = onTurnStart;
81- _onTurnDone = onTurnDone;
82- _onErrorHandler = onErrorHandler;
95+ void overrideOnEventDone (ZeroArgFunction onEventDoneFn) {
96+ this ._onEventDone = onEventDoneFn;
97+ }
98+
99+ /**
100+ * Sets the zone hook that is called when an error is uncaught in the
101+ * Angular zone. The first argument is the error. The second argument is
102+ * the stack trace.
103+ */
104+ void overrideOnErrorHandler (ErrorHandlingFn errorHandlingFn) {
105+ this ._onErrorHandler = errorHandlingFn;
83106 }
84107
85108 /**
@@ -150,7 +173,11 @@ class NgZone {
150173 // Trigger onTurnDone at the end of a turn if _innerZone has executed some code
151174 try {
152175 _inVmTurnDone = true ;
153- parent.run (_innerZone, _onTurnDone);
176+ parent.run (_innerZone, _onTurnDone);
177+
178+ if (_pendingMicrotasks == 0 && _onEventDone != null ) {
179+ runOutsideAngular (_onEventDone);
180+ }
154181 } finally {
155182 _inVmTurnDone = false ;
156183 _hasExecutedCodeInInnerZone = false ;
0 commit comments