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

Commit 9818139

Browse files
JiaLiPassionmhevery
authored andcommitted
feat: add Zone.root api (#601)
1 parent 6d31734 commit 9818139

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Diff for: lib/zone.ts

+12
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ interface ZoneType {
257257
* Verify that Zone has been correctly patched. Specifically that Promise is zone aware.
258258
*/
259259
assertZonePatched();
260+
261+
/**
262+
* Return the root zone.
263+
*/
264+
root: Zone;
260265
}
261266

262267
/**
@@ -564,6 +569,13 @@ const Zone: ZoneType = (function(global: any) {
564569
}
565570
}
566571

572+
static get root(): AmbientZone {
573+
let zone = Zone.current;
574+
while (zone.parent) {
575+
zone = zone.parent;
576+
}
577+
return zone;
578+
}
567579

568580
static get current(): AmbientZone {
569581
return _currentZoneFrame.zone;

Diff for: test/common/zone.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ describe('Zone', function() {
8383
});
8484
});
8585

86+
describe('run out side of current zone', function() {
87+
it('should be able to get root zone', function() {
88+
Zone.current.fork({name: 'testZone'}).run(function() {
89+
expect(Zone.root.name).toEqual('<root>');
90+
});
91+
});
92+
93+
it('should be able to get run under rootZone', function() {
94+
Zone.current.fork({name: 'testZone'}).run(function() {
95+
Zone.root.run(() => {
96+
expect(Zone.current.name).toEqual('<root>');
97+
});
98+
});
99+
});
100+
101+
it('should be able to get run outside of current zone', function() {
102+
Zone.current.fork({name: 'testZone'}).run(function() {
103+
Zone.root.fork({name: 'newTestZone'}).run(() => {
104+
expect(Zone.current.name).toEqual('newTestZone');
105+
expect(Zone.current.parent.name).toEqual('<root>');
106+
});
107+
});
108+
});
109+
});
86110

87111
describe('get', function() {
88112
it('should store properties', function() {

0 commit comments

Comments
 (0)