This repository was archived by the owner on Feb 22, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -29,16 +29,20 @@ class LongStackTrace {
29
29
* A better zone API which implements onTurnDone.
30
30
*/
31
31
class NgZone {
32
- NgZone () {
33
- _zone = async .Zone .current.fork (specification: new async .ZoneSpecification (
32
+ final async .Zone _outerZone;
33
+ async .Zone _zone;
34
+
35
+ NgZone ()
36
+ : _outerZone = async .Zone .current
37
+ {
38
+ _zone = _outerZone.fork (specification: new async .ZoneSpecification (
34
39
run: _onRun,
35
40
runUnary: _onRunUnary,
36
41
scheduleMicrotask: _onScheduleMicrotask,
37
42
handleUncaughtError: _uncaughtError
38
43
));
39
44
}
40
45
41
- async .Zone _zone;
42
46
43
47
List _asyncQueue = [];
44
48
bool _errorThrownFromOnRun = false ;
@@ -142,6 +146,22 @@ class NgZone {
142
146
*/
143
147
run (body ()) => _zone.run (body);
144
148
149
+ /**
150
+ * Allows one to escape the auto-digest mechanism of Angular.
151
+ *
152
+ * myFunction(NgZone zone, Element element) {
153
+ * element.onClick.listen(() {
154
+ * // auto-digest will run after element click.
155
+ * });
156
+ * zone.runOutsideAngular(() {
157
+ * element.onMouseMove.listen(() {
158
+ * // auto-digest will NOT run after mouse move
159
+ * });
160
+ * });
161
+ * }
162
+ */
163
+ runOutsideAngular (body ()) => _outerZone.run (body);
164
+
145
165
assertInTurn () {
146
166
assert (_runningInTurn > 0 || _inFinishTurn);
147
167
}
Original file line number Diff line number Diff line change @@ -46,6 +46,23 @@ main() => describe('zone', () {
46
46
})));
47
47
48
48
49
+ it ('should allow executing code outside the zone' , inject (() {
50
+ var zone = new NgZone ();
51
+ var outerZone = Zone .current;
52
+ var ngZone;
53
+ var outsideZone;
54
+ zone.run (() {
55
+ ngZone = Zone .current;
56
+ zone.runOutsideAngular (() {
57
+ outsideZone = Zone .current;
58
+ });
59
+ });
60
+
61
+ expect (outsideZone).toEqual (outerZone);
62
+ expect (ngZone.parent).toEqual ((outerZone));
63
+ }));
64
+
65
+
49
66
it ('should rethrow exceptions from the onTurnDone and call onError when the zone is sync' , () {
50
67
zone.onTurnDone = () {
51
68
throw ["fromOnTurnDone" ];
You can’t perform that action at this time.
0 commit comments