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

add Zone.root to get root zone directly #601

Merged
merged 1 commit into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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