Skip to content

Commit abfe175

Browse files
vicbmhevery
authored andcommitted
doc(VmTurnZone): inline doc
Closes angular#1427
1 parent 0fc66da commit abfe175

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed

modules/angular2/src/core/zone/vm_turn_zone.dart

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ library angular.zone;
33
import 'dart:async' as async;
44
import '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+
*/
621
class 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(() {

modules/angular2/src/core/zone/vm_turn_zone.es6

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
22
import {normalizeBlank, isPresent, global} from 'angular2/src/facade/lang';
33

4+
/**
5+
* A wrapper around zones that lets you schedule tasks after it has executed a task.
6+
*
7+
* The wrapper maintains an "inner" and "outer" [Zone]. The application code will executes
8+
* in the "inner" zone unless [runOutsideAngular] is explicitely called.
9+
*
10+
* A typical application will create a singleton [VmTurnZone] whose outer [Zone] is the root [Zone]
11+
* and whose default [onTurnDone] runs the Angular digest.
12+
*
13+
* @exportedAs angular2/core
14+
*/
415
export class VmTurnZone {
516
_outerZone;
617
_innerZone;
@@ -11,6 +22,15 @@ export class VmTurnZone {
1122

1223
_nestedRunCounter:number;
1324

25+
/**
26+
* Associates with this
27+
*
28+
* - an "outer" zone, which is the one that created this.
29+
* - an "inner" zone, which is a child of the outer zone.
30+
*
31+
* @param {bool} enableLongStackTrace whether to enable long stack trace. They should only be
32+
* enabled in development mode as they significantly impact perf.
33+
*/
1434
constructor({enableLongStackTrace}) {
1535
this._nestedRunCounter = 0;
1636
this._onTurnStart = null;
@@ -21,16 +41,54 @@ export class VmTurnZone {
2141
this._innerZone = this._createInnerZone(this._outerZone, enableLongStackTrace);
2242
}
2343

44+
/**
45+
* Initializes the zone hooks.
46+
*
47+
* @param {Function} onTurnStart called before code executes in the inner zone for each VM turn
48+
* @param {Function} onTurnDone called at the end of a VM turn if code has executed in the inner zone
49+
* @param {Function} onScheduleMicrotask
50+
* @param {Function} onErrorHandler called when an exception is thrown by a macro or micro task
51+
*/
2452
initCallbacks({onTurnStart, onTurnDone, onScheduleMicrotask, onErrorHandler} = {}) {
2553
this._onTurnStart = normalizeBlank(onTurnStart);
2654
this._onTurnDone = normalizeBlank(onTurnDone);
2755
this._onErrorHandler = normalizeBlank(onErrorHandler);
2856
}
2957

58+
/**
59+
* Runs [fn] in the inner zone and returns whatever it returns.
60+
*
61+
* In a typical app where the inner zone is the Angular zone, this allows one to make use of the
62+
* Angular's auto digest mechanism.
63+
*
64+
* ```
65+
* var zone: VmTurnZone = <ref to the application zone>;
66+
*
67+
* zone.run(() => {
68+
* // auto-digest will run after this function is called from JS
69+
* });
70+
* ```
71+
*/
3072
run(fn) {
3173
return this._innerZone.run(fn);
3274
}
3375

76+
/**
77+
* Runs [fn] in the outer zone and returns whatever it returns.
78+
*
79+
* In a typical app where the inner zone is the Angular zone, this allows one to escape Angular's
80+
* auto-digest mechanism.
81+
*
82+
* ```
83+
* var zone: VmTurnZone = <ref to the application zone>;
84+
*
85+
* zone.runOusideAngular(() => {
86+
* element.onClick(() => {
87+
* // Clicking on the element would not trigger the change detection
88+
* });
89+
* });
90+
* ```
91+
*/
3492
runOutsideAngular(fn) {
3593
return this._outerZone.run(fn);
3694
}
@@ -86,4 +144,4 @@ export class VmTurnZone {
86144
throw e;
87145
}
88146
}
89-
}
147+
}

0 commit comments

Comments
 (0)