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

Commit

Permalink
feat(zone): Allow escaping of auto-digest mechanism.
Browse files Browse the repository at this point in the history
Closes #557
  • Loading branch information
mhevery committed Feb 20, 2014
1 parent d9dfe0f commit 2df2660
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/core/zone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@ class LongStackTrace {
* A better zone API which implements onTurnDone.
*/
class NgZone {
NgZone() {
_zone = async.Zone.current.fork(specification: new async.ZoneSpecification(
final async.Zone _outerZone;
async.Zone _zone;

NgZone()
: _outerZone = async.Zone.current
{
_zone = _outerZone.fork(specification: new async.ZoneSpecification(
run: _onRun,
runUnary: _onRunUnary,
scheduleMicrotask: _onScheduleMicrotask,
handleUncaughtError: _uncaughtError
));
}

async.Zone _zone;

List _asyncQueue = [];
bool _errorThrownFromOnRun = false;
Expand Down Expand Up @@ -142,6 +146,22 @@ class NgZone {
*/
run(body()) => _zone.run(body);

/**
* Allows one to escape the auto-digest mechanism of Angular.
*
* myFunction(NgZone zone, Element element) {
* element.onClick.listen(() {
* // auto-digest will run after element click.
* });
* zone.runOutsideAngular(() {
* element.onMouseMove.listen(() {
* // auto-digest will NOT run after mouse move
* });
* });
* }
*/
runOutsideAngular(body()) => _outerZone.run(body);

assertInTurn() {
assert(_runningInTurn > 0 || _inFinishTurn);
}
Expand Down
17 changes: 17 additions & 0 deletions test/core/zone_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ main() => describe('zone', () {
})));


it('should allow executing code outside the zone', inject(() {
var zone = new NgZone();
var outerZone = Zone.current;
var ngZone;
var outsideZone;
zone.run(() {
ngZone = Zone.current;
zone.runOutsideAngular(() {
outsideZone = Zone.current;
});
});

expect(outsideZone).toEqual(outerZone);
expect(ngZone.parent).toEqual((outerZone));
}));


it('should rethrow exceptions from the onTurnDone and call onError when the zone is sync', () {
zone.onTurnDone = () {
throw ["fromOnTurnDone"];
Expand Down

0 comments on commit 2df2660

Please sign in to comment.