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

Commit 2df2660

Browse files
committed
feat(zone): Allow escaping of auto-digest mechanism.
Closes #557
1 parent d9dfe0f commit 2df2660

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/core/zone.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,20 @@ class LongStackTrace {
2929
* A better zone API which implements onTurnDone.
3030
*/
3131
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(
3439
run: _onRun,
3540
runUnary: _onRunUnary,
3641
scheduleMicrotask: _onScheduleMicrotask,
3742
handleUncaughtError: _uncaughtError
3843
));
3944
}
4045

41-
async.Zone _zone;
4246

4347
List _asyncQueue = [];
4448
bool _errorThrownFromOnRun = false;
@@ -142,6 +146,22 @@ class NgZone {
142146
*/
143147
run(body()) => _zone.run(body);
144148

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+
145165
assertInTurn() {
146166
assert(_runningInTurn > 0 || _inFinishTurn);
147167
}

test/core/zone_spec.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ main() => describe('zone', () {
4646
})));
4747

4848

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+
4966
it('should rethrow exceptions from the onTurnDone and call onError when the zone is sync', () {
5067
zone.onTurnDone = () {
5168
throw ["fromOnTurnDone"];

0 commit comments

Comments
 (0)