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

Commit

Permalink
feat: add Zone.root api (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Feb 7, 2017
1 parent 6d31734 commit 9818139
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ interface ZoneType {
* Verify that Zone has been correctly patched. Specifically that Promise is zone aware.
*/
assertZonePatched();

/**
* Return the root zone.
*/
root: Zone;
}

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

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

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

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

it('should be able to get run under rootZone', function() {
Zone.current.fork({name: 'testZone'}).run(function() {
Zone.root.run(() => {
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.root.fork({name: 'newTestZone'}).run(() => {
expect(Zone.current.name).toEqual('newTestZone');
expect(Zone.current.parent.name).toEqual('<root>');
});
});
});
});

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

0 comments on commit 9818139

Please sign in to comment.