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

Commit 0621014

Browse files
committed
feat(zone): Add Zone.getZone api
1 parent 2d02e39 commit 0621014

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Diff for: lib/zone.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,18 @@ interface Zone {
137137
* [ZoneSpec.properties] to configure the set of properties associated with the current zone.
138138
*
139139
* @param key The key to retrieve.
140-
* @returns {any} Tha value for the key, or `undefined` if not found.
140+
* @returns {any} The value for the key, or `undefined` if not found.
141141
*/
142142
get(key: string): any;
143+
/**
144+
* Returns a Zone which defines a `key`.
145+
*
146+
* Recursively search the parent Zone until a Zone which has a property `key` is found.
147+
*
148+
* @param key The key to use for identification of the returned zone.
149+
* @returns {Zone} The Zone which defines the `key`, `null` if not found.
150+
*/
151+
getZoneWith(key: string): Zone;
143152
/**
144153
* Used to create a child zone.
145154
*
@@ -520,13 +529,19 @@ const Zone: ZoneType = (function(global: any) {
520529
}
521530

522531
public get(key: string): any {
532+
const zone: Zone = this.getZoneWith(key) as Zone;
533+
if (zone) return zone._properties[key];
534+
}
535+
536+
public getZoneWith(key: string): AmbientZone {
523537
let current: Zone = this;
524538
while (current) {
525539
if (current._properties.hasOwnProperty(key)) {
526-
return current._properties[key];
540+
return current;
527541
}
528542
current = current._parent;
529543
}
544+
return null;
530545
}
531546

532547
public fork(zoneSpec: ZoneSpec): AmbientZone {

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

+3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ describe('Zone', function () {
6060
it('should store properties', function () {
6161
var testZone = Zone.current.fork({name: 'A', properties: { key: 'value' }});
6262
expect(testZone.get('key')).toEqual('value');
63+
expect(testZone.getZoneWith('key')).toEqual(testZone);
6364
var childZone = testZone.fork({name: 'B', properties: { key: 'override' }});
6465
expect(testZone.get('key')).toEqual('value');
66+
expect(testZone.getZoneWith('key')).toEqual(testZone);
6567
expect(childZone.get('key')).toEqual('override');
68+
expect(childZone.getZoneWith('key')).toEqual(childZone);
6669
});
6770
});
6871

0 commit comments

Comments
 (0)