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

Commit

Permalink
feat(zone): Add Zone.getZone api
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Aug 15, 2016
1 parent 2d02e39 commit 0621014
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,18 @@ interface Zone {
* [ZoneSpec.properties] to configure the set of properties associated with the current zone.
*
* @param key The key to retrieve.
* @returns {any} Tha value for the key, or `undefined` if not found.
* @returns {any} The value for the key, or `undefined` if not found.
*/
get(key: string): any;
/**
* Returns a Zone which defines a `key`.
*
* Recursively search the parent Zone until a Zone which has a property `key` is found.
*
* @param key The key to use for identification of the returned zone.
* @returns {Zone} The Zone which defines the `key`, `null` if not found.
*/
getZoneWith(key: string): Zone;
/**
* Used to create a child zone.
*
Expand Down Expand Up @@ -520,13 +529,19 @@ const Zone: ZoneType = (function(global: any) {
}

public get(key: string): any {
const zone: Zone = this.getZoneWith(key) as Zone;
if (zone) return zone._properties[key];
}

public getZoneWith(key: string): AmbientZone {
let current: Zone = this;
while (current) {
if (current._properties.hasOwnProperty(key)) {
return current._properties[key];
return current;
}
current = current._parent;
}
return null;
}

public fork(zoneSpec: ZoneSpec): AmbientZone {
Expand Down
3 changes: 3 additions & 0 deletions test/common/zone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ describe('Zone', function () {
it('should store properties', function () {
var testZone = Zone.current.fork({name: 'A', properties: { key: 'value' }});
expect(testZone.get('key')).toEqual('value');
expect(testZone.getZoneWith('key')).toEqual(testZone);
var childZone = testZone.fork({name: 'B', properties: { key: 'override' }});
expect(testZone.get('key')).toEqual('value');
expect(testZone.getZoneWith('key')).toEqual(testZone);
expect(childZone.get('key')).toEqual('override');
expect(childZone.getZoneWith('key')).toEqual(childZone);
});
});

Expand Down

0 comments on commit 0621014

Please sign in to comment.