@@ -3,6 +3,21 @@ library angular.zone;
33import 'dart:async' as async;
44import 'package:stack_trace/stack_trace.dart' show Chain;
55
6+ /**
7+ * A [Zone] wrapper that lets you schedule tasks after its private microtask queue is exhausted but
8+ * before the next "VM turn", i.e. event loop iteration.
9+ *
10+ * This lets you freely schedule microtasks that prepare data, and set an [onTurnDone] handler that
11+ * will consume that data after it's ready but before the browser has a chance to re-render.
12+ *
13+ * A VM turn consist of a single macrotask followed 0 to many microtasks.
14+ *
15+ * The wrapper maintains an "inner" and "outer" [Zone] . The application code will executes
16+ * in the "inner" zone unless [runOutsideAngular] is explicitely called.
17+ *
18+ * A typical application will create a singleton [VmTurnZone] whose outer [Zone] is the root [Zone]
19+ * and whose default [onTurnDone] runs the Angular digest.
20+ */
621class VmTurnZone {
722 Function _onTurnStart;
823 Function _onTurnDone;
@@ -14,24 +29,75 @@ class VmTurnZone {
1429
1530 int _nestedRunCounter;
1631
32+ /**
33+ * Associates with this
34+ *
35+ * - an "outer" [Zone] , which is the one that created this.
36+ * - an "inner" [Zone] , which is a child of the outer [Zone] .
37+ *
38+ * @param {bool} enableLongStackTrace whether to enable long stack trace. They should only be
39+ * enabled in development mode as they significantly impact perf.
40+ */
1741 VmTurnZone ({bool enableLongStackTrace}) {
1842 _nestedRunCounter = 0 ;
1943 _outerZone = async .Zone .current;
2044 _innerZone = _createInnerZoneWithErrorHandling (enableLongStackTrace);
2145 }
2246
47+ /**
48+ * Initializes the zone hooks.
49+ *
50+ * @param {Function} onTurnStart called before code executes in the inner zone for each VM turn
51+ * @param {Function} onTurnDone called at the end of a VM turn if code has executed in the inner zone
52+ * @param {Function} onScheduleMicrotask
53+ * @param {Function} onErrorHandler called when an exception is thrown by a macro or micro task
54+ */
2355 initCallbacks ({Function onTurnStart, Function onTurnDone, Function onScheduleMicrotask, Function onErrorHandler}) {
2456 this ._onTurnStart = onTurnStart;
2557 this ._onTurnDone = onTurnDone;
2658 this ._onScheduleMicrotask = onScheduleMicrotask;
2759 this ._onErrorHandler = onErrorHandler;
2860 }
2961
62+ /**
63+ * Runs [fn] in the inner zone and returns whatever it returns.
64+ *
65+ * In a typical app where the inner zone is the Angular zone, this allows one to make use of the
66+ * Angular's auto digest mechanism.
67+ *
68+ * ```
69+ * VmTurnZone zone = <ref to the application zone>;
70+ *
71+ * void functionCalledFromJS() {
72+ * zone.run(() {
73+ * // auto-digest will run after this function is called from JS
74+ * });
75+ * }
76+ * ```
77+ */
3078 dynamic run (fn ()) => _innerZone.run (fn);
3179
80+ /**
81+ * Runs [fn] in the outer zone and returns whatever it returns.
82+ *
83+ * In a typical app where the inner zone is the Angular zone, this allows one to escape Angular's
84+ * auto-digest mechanism.
85+ *
86+ * ```
87+ * void myFunction(VmTurnZone zone, Element element) {
88+ * element.onClick.listen(() {
89+ * // auto-digest will run after element click.
90+ * });
91+ * zone.runOutsideAngular(() {
92+ * element.onMouseMove.listen(() {
93+ * // auto-digest will NOT run after mouse move
94+ * });
95+ * });
96+ * }
97+ * ```
98+ */
3299 dynamic runOutsideAngular (fn ()) => _outerZone.run (fn);
33100
34-
35101 async .Zone _createInnerZoneWithErrorHandling (bool enableLongStackTrace) {
36102 if (enableLongStackTrace) {
37103 return Chain .capture (() {
0 commit comments