forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathng_zone.ts
More file actions
211 lines (196 loc) · 6.79 KB
/
ng_zone.ts
File metadata and controls
211 lines (196 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
import {normalizeBlank, isPresent, global} from 'angular2/src/facade/lang';
/**
* A wrapper around zones that lets you schedule tasks after it has executed a task.
*
* The wrapper maintains an "inner" and an "mount" `Zone`. The application code will executes
* in the "inner" zone unless `runOutsideAngular` is explicitely called.
*
* A typical application will create a singleton `NgZone`. The outer `Zone` is a fork of the root
* `Zone`. The default `onTurnDone` runs the Angular change detection.
*
* @exportedAs angular2/core
*/
export class NgZone {
// Code executed in _mountZone does not trigger the onTurnDone.
_mountZone;
// _innerZone is the child of _mountZone. Any code executed in this zone will trigger the
// onTurnDone hook at the end of the current VM turn.
_innerZone;
_onTurnStart: () => void;
_onTurnDone: () => void;
_onErrorHandler: (error, stack) => void;
// Number of microtasks pending from _innerZone (& descendants)
_pendingMicrotasks: number;
// Whether some code has been executed in the _innerZone (& descendants) in the current turn
_hasExecutedCodeInInnerZone: boolean;
// run() call depth in _mountZone. 0 at the end of a macrotask
// zone.run(() => { // top-level call
// zone.run(() => {}); // nested call -> in-turn
// });
_nestedRun: number;
// TODO(vicb): implement this class properly for node.js environment
// This disabled flag is only here to please cjs tests
_disabled: boolean;
/**
* Associates with this
*
* - a "root" zone, which the one that instantiated this.
* - an "inner" zone, which is a child of the root zone.
*
* @param {bool} enableLongStackTrace whether to enable long stack trace. They should only be
* enabled in development mode as they significantly impact perf.
*/
constructor({enableLongStackTrace}) {
this._onTurnStart = null;
this._onTurnDone = null;
this._onErrorHandler = null;
this._pendingMicrotasks = 0;
this._hasExecutedCodeInInnerZone = false;
this._nestedRun = 0;
if (global.zone) {
this._disabled = false;
this._mountZone = global.zone;
this._innerZone = this._createInnerZone(this._mountZone, enableLongStackTrace)
} else {
this._disabled = true;
this._mountZone = null;
}
}
/**
* Initializes the zone hooks.
*
* @param {() => void} onTurnStart called before code executes in the inner zone for each VM turn
* @param {() => void} onTurnDone called at the end of a VM turn if code has executed in the inner
* zone
* @param {(error, stack) => void} onErrorHandler called when an exception is thrown by a macro or
* micro task
*/
initCallbacks({onTurnStart, onTurnDone, onErrorHandler}: {
onTurnStart?: /*() => void*/ Function,
onTurnDone?: /*() => void*/ Function,
onErrorHandler?: /*(error, stack) => void*/ Function
} = {}) {
this._onTurnStart = normalizeBlank(onTurnStart);
this._onTurnDone = normalizeBlank(onTurnDone);
this._onErrorHandler = normalizeBlank(onErrorHandler);
}
/**
* Runs `fn` in the inner zone and returns whatever it returns.
*
* In a typical app where the inner zone is the Angular zone, this allows one to make use of the
* Angular's auto digest mechanism.
*
* ```
* var zone: NgZone = [ref to the application zone];
*
* zone.run(() => {
* // the change detection will run after this function and the microtasks it enqueues have
* executed.
* });
* ```
*/
run(fn) {
if (this._disabled) {
return fn();
} else {
return this._innerZone.run(fn);
}
}
/**
* Runs `fn` in the outer zone and returns whatever it returns.
*
* In a typical app where the inner zone is the Angular zone, this allows one to escape Angular's
* auto-digest mechanism.
*
* ```
* var zone: NgZone = [ref to the application zone];
*
* zone.runOusideAngular(() => {
* element.onClick(() => {
* // Clicking on the element would not trigger the change detection
* });
* });
* ```
*/
runOutsideAngular(fn) {
if (this._disabled) {
return fn();
} else {
return this._mountZone.run(fn);
}
}
_createInnerZone(zone, enableLongStackTrace) {
var ngZone = this;
var errorHandling;
if (enableLongStackTrace) {
errorHandling = StringMapWrapper.merge(Zone.longStackTraceZone,
{onError: function(e) { ngZone._onError(this, e) }});
} else {
errorHandling = {
onError: function(e) { ngZone._onError(this, e) }
};
}
return zone.fork(errorHandling)
.fork({
'$run': function(parentRun) {
return function() {
try {
ngZone._nestedRun++;
if (!ngZone._hasExecutedCodeInInnerZone) {
ngZone._hasExecutedCodeInInnerZone = true;
if (ngZone._onTurnStart) {
parentRun.call(ngZone._innerZone, ngZone._onTurnStart);
}
}
return parentRun.apply(this, arguments);
} finally {
ngZone._nestedRun--;
// If there are no more pending microtasks, we are at the end of a VM turn (or in
// onTurnStart)
// _nestedRun will be 0 at the end of a macrotasks (it could be > 0 when there are
// nested calls
// to run()).
if (ngZone._pendingMicrotasks == 0 && ngZone._nestedRun == 0) {
if (ngZone._onTurnDone && ngZone._hasExecutedCodeInInnerZone) {
try {
parentRun.call(ngZone._innerZone, ngZone._onTurnDone);
} finally {
ngZone._hasExecutedCodeInInnerZone = false;
}
}
}
}
}
},
'$scheduleMicrotask': function(parentScheduleMicrotask) {
return function(fn) {
ngZone._pendingMicrotasks++;
var microtask = function() {
try {
fn();
} finally {
ngZone._pendingMicrotasks--;
}
};
parentScheduleMicrotask.call(this, microtask);
}
},
_innerZone: true
});
}
_onError(zone, e): void {
if (isPresent(this._onErrorHandler)) {
var trace = [normalizeBlank(e.stack)];
while (zone && zone.constructedAtException) {
trace.push(zone.constructedAtException.get());
zone = zone.parent;
}
this._onErrorHandler(e, trace);
} else {
console.log('## _onError ##');
console.log(e.stack);
throw e;
}
}
}