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

Commit

Permalink
add runOutsideOfCurrentZone just like runOutsideOfAngular
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed Jan 15, 2017
1 parent e1d3240 commit 3230f9b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ interface ZoneType {
* Verify that Zone has been correctly patched. Specifically that Promise is zone aware.
*/
assertZonePatched();

/**
* Run function outside current zone, instead it will run under rootZone.
*/
runOutsideOfCurrentZone<T>(
callback: Function, applyThis?: any, applyArgs?: any, source?: string, runGuarded?: boolean,
zoneSpec?: ZoneSpec): T;
}

/**
Expand Down Expand Up @@ -564,6 +571,27 @@ const Zone: ZoneType = (function(global: any) {
}
}

static get rootZone(): AmbientZone {
let zone = Zone.current;
while (zone.parent) {
zone = zone.parent;
}
return zone;
}

static runOutsideOfCurrentZone(
callback: Function, applyThis?: any, applyArgs?: any, source?: string,
runGuarded: boolean = true, zoneSpec?: ZoneSpec) {
let zone = Zone.rootZone;
if (zoneSpec) {
zone = zone.fork(zoneSpec);
}
if (runGuarded) {
return zone.runGuarded(callback, applyThis, applyArgs, source);
} else {
return zone.run(callback, applyThis, applyArgs, source);
}
}

static get current(): AmbientZone {
return _currentZoneFrame.zone;
Expand Down
18 changes: 18 additions & 0 deletions test/common/zone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ describe('Zone', function() {
});
});

describe('run out side of current zone', function() {
it('should be able to get run under rootZone', function() {
Zone.current.fork({name: 'testZone'}).run(function() {
Zone.runOutsideOfCurrentZone(() => {
expect(Zone.current.name).toEqual('<root>');
});
});
});

it('should be able to get run outside of current zone', function() {
Zone.current.fork({name: 'testZone'}).run(function() {
Zone.runOutsideOfCurrentZone(() => {
expect(Zone.current.name).toEqual('newTestZone');
expect(Zone.current.parent.name).toEqual('<root>');
}, null, null, null, true, {name: 'newTestZone'});
});
});
});

describe('get', function() {
it('should store properties', function() {
Expand Down

0 comments on commit 3230f9b

Please sign in to comment.