From 108ec24b456d8045b9d6abbde85239c19f61cf62 Mon Sep 17 00:00:00 2001 From: "JiaLi.Passion" Date: Sun, 15 Jan 2017 14:37:17 +0900 Subject: [PATCH] add Zone.root api --- lib/zone.ts | 12 ++++++++++++ test/common/zone.spec.ts | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/zone.ts b/lib/zone.ts index e80fcb1d3..a0549dd4b 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -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; } /** @@ -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; diff --git a/test/common/zone.spec.ts b/test/common/zone.spec.ts index 258f100ee..3aa3cbe10 100644 --- a/test/common/zone.spec.ts +++ b/test/common/zone.spec.ts @@ -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(''); + }); + }); + + 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(''); + }); + }); + }); + + 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(''); + }); + }); + }); + }); describe('get', function() { it('should store properties', function() {