Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 510cd5a

Browse files
Chris Calabromhevery
Chris Calabro
authored andcommitted
docs: Added docs to NgZone
Closes #894
1 parent 5271307 commit 510cd5a

File tree

1 file changed

+65
-25
lines changed

1 file changed

+65
-25
lines changed

lib/core/zone.dart

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
part of angular.core_internal;
22

3+
/**
4+
* Handles an [NgZone] onTurnDone event.
5+
*/
36
typedef void ZoneOnTurn();
7+
8+
/**
9+
* Handles an [NgZone] onError event.
10+
*/
411
typedef void ZoneOnError(dynamic error, dynamic stacktrace,
512
LongStackTrace longStacktrace);
613

714
/**
8-
* Contains the locations of runAsync calls across VM turns.
15+
* Contains the locations of async calls across VM turns.
916
*/
1017
class LongStackTrace {
1118
final String reason;
@@ -26,25 +33,44 @@ class LongStackTrace {
2633
}
2734

2835
/**
29-
* A better zone API which implements onTurnDone.
36+
* A [Zone] wrapper that lets you schedule tasks after its private microtask
37+
* queue is exhausted but before the next "turn", i.e. event loop iteration.
38+
* This lets you freely schedule microtasks that prepare data, and set an
39+
* [onTurnDone] handler that will consume that data after it's ready but before
40+
* the browser has a chance to re-render.
41+
* The wrapper maintains an "inner" and "outer" [Zone] and a private queue of
42+
* all the microtasks scheduled on the inner [Zone].
43+
*
44+
* In a typical app, [ngDynamicApp] or [ngStaticApp] will create a singleton
45+
* [NgZone] whose outer [Zone] is the root [Zone] and whose default [onTurnDone]
46+
* runs the Angular digest. A component may want to inject this singleton if it
47+
* needs to run code _outside_ the Angular digest.
3048
*/
3149
class NgZone {
32-
final async.Zone _outerZone = async.Zone.current;
33-
async.Zone _zone;
50+
/// an "outer" [Zone], which is the one that created this.
51+
async.Zone _outerZone;
3452

53+
/// an "inner" [Zone], which is a child of the outer [Zone].
54+
async.Zone _innerZone;
55+
56+
/**
57+
* Associates with this
58+
*
59+
* Defaults [onError] to forward errors to the outer [Zone].
60+
* Defaults [onTurnDone] to a no-op.
61+
*/
3562
NgZone() {
36-
_zone = _outerZone.fork(specification: new async.ZoneSpecification(
63+
_outerZone = async.Zone.current;
64+
_innerZone = _outerZone.fork(specification: new async.ZoneSpecification(
3765
run: _onRun,
3866
runUnary: _onRunUnary,
3967
scheduleMicrotask: _onScheduleMicrotask,
4068
handleUncaughtError: _uncaughtError
4169
));
42-
// Prevent silently ignoring uncaught exceptions by forwarding such
43-
// exceptions to the outer zone by default.
44-
onError = (e, s, ls) => _outerZone.handleUncaughtError(e, s);
70+
onError = _defaultOnError;
71+
onTurnDone = _defaultOnTurnDone;
4572
}
4673

47-
4874
List _asyncQueue = [];
4975
bool _errorThrownFromOnRun = false;
5076

@@ -107,21 +133,27 @@ class NgZone {
107133
int _runningInTurn = 0;
108134

109135
/**
110-
* A function called with any errors from the zone.
136+
* Called with any errors from the inner zone.
111137
*/
112-
var onError = (e, s, ls) => print('$e\n$s\n$ls');
138+
ZoneOnError onError;
113139

114-
/**
115-
* A function that is called at the end of each VM turn in which the
116-
* in-zone code or any runAsync callbacks were run.
117-
*/
118-
var onTurnDone = () => null; // Type was ZoneOnTurn: dartbug 13519
140+
/// Prevent silently ignoring uncaught exceptions by forwarding such exceptions to the outer zone.
141+
void _defaultOnError(dynamic e, dynamic s, LongStackTrace ls) =>
142+
_outerZone.handleUncaughtError(e, s);
119143

120144
/**
121-
* A function that is called when uncaught errors are thrown inside the zone.
145+
* Called at the end of each VM turn in which inner zone code runs.
146+
* "At the end" means after the private microtask queue of the inner zone is
147+
* exhausted but before the next VM turn. Notes
148+
* - This won't wait for microtasks scheduled in zones other than the inner
149+
* zone, e.g. those scheduled with [runOutsideAngular].
150+
* - [onTurnDone] runs repeatedly until it fails to schedule any more
151+
* microtasks, so you usually don't want it to schedule any. For example,
152+
* if its first line of code is `new Future.value()`, the turn will _never_
153+
* end.
122154
*/
123-
// var onError = (dynamic e, dynamic s, LongStackTrace ls) => print('EXCEPTION: $e\n$s\n$ls');
124-
// Type was ZoneOnError: dartbug 13519
155+
ZoneOnTurn onTurnDone;
156+
void _defaultOnTurnDone() => null;
125157

126158
LongStackTrace _longStacktrace = null;
127159

@@ -140,15 +172,14 @@ class NgZone {
140172
}
141173

142174
/**
143-
* Runs the provided function in the zone. Any runAsync calls (e.g. futures)
144-
* will also be run in this zone.
145-
*
146-
* Returns the return value of body.
175+
* Runs [body] in the inner zone and returns whatever it returns.
147176
*/
148-
dynamic run(body()) => _zone.run(body);
177+
dynamic run(body()) => _innerZone.run(body);
149178

150179
/**
151-
* Allows one to escape the auto-digest mechanism of Angular.
180+
* Runs [body] in the outer zone and returns whatever it returns.
181+
* In a typical app where the inner zone is the Angular zone, this allows
182+
* one to escape Angular's auto-digest mechanism.
152183
*
153184
* myFunction(NgZone zone, Element element) {
154185
* element.onClick.listen(() {
@@ -163,10 +194,19 @@ class NgZone {
163194
*/
164195
dynamic runOutsideAngular(body()) => _outerZone.run(body);
165196

197+
/**
198+
* Throws an [AssertionError] if no task is currently running in the inner
199+
* zone. In a typical app where the inner zone is the Angular zone, this can
200+
* be used to assert that the digest will indeed run at the end of the current
201+
* turn.
202+
*/
166203
void assertInTurn() {
167204
assert(_runningInTurn > 0 || _inFinishTurn);
168205
}
169206

207+
/**
208+
* Same as [assertInTurn].
209+
*/
170210
void assertInZone() {
171211
assertInTurn();
172212
}

0 commit comments

Comments
 (0)