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

Commit dc12d8e

Browse files
JiaLiPassionmhevery
authored andcommittedDec 18, 2016
fix: correct currentZone passed into delegate methods
Fix #587, currentzone in all hook methods will not be corrent if current zone has no hook method (#539)
1 parent 486010b commit dc12d8e

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed
 

‎lib/zone.ts

+30-7
Original file line numberDiff line numberDiff line change
@@ -721,67 +721,90 @@ const Zone: ZoneType = (function(global: any) {
721721

722722
private _forkDlgt: ZoneDelegate;
723723
private _forkZS: ZoneSpec;
724+
private _forkCurrZone: Zone;
724725

725726
private _interceptDlgt: ZoneDelegate;
726727
private _interceptZS: ZoneSpec;
728+
private _interceptCurrZone: Zone;
727729

728730
private _invokeDlgt: ZoneDelegate;
729731
private _invokeZS: ZoneSpec;
732+
private _invokeCurrZone: Zone;
730733

731734
private _handleErrorDlgt: ZoneDelegate;
732735
private _handleErrorZS: ZoneSpec;
736+
private _handleErrorCurrZone: Zone;
733737

734738
private _scheduleTaskDlgt: ZoneDelegate;
735739
private _scheduleTaskZS: ZoneSpec;
740+
private _scheduleTaskCurrZone: Zone;
736741

737742
private _invokeTaskDlgt: ZoneDelegate;
738743
private _invokeTaskZS: ZoneSpec;
744+
private _invokeTaskCurrZone: Zone;
739745

740746
private _cancelTaskDlgt: ZoneDelegate;
741747
private _cancelTaskZS: ZoneSpec;
748+
private _cancelTaskCurrZone: Zone;
742749

743750
private _hasTaskDlgt: ZoneDelegate;
744751
private _hasTaskZS: ZoneSpec;
752+
private _hasTaskCurrZone: Zone;
745753

746754
constructor(zone: Zone, parentDelegate: ZoneDelegate, zoneSpec: ZoneSpec) {
747755
this.zone = zone;
748756
this._parentDelegate = parentDelegate;
749757

750758
this._forkZS = zoneSpec && (zoneSpec && zoneSpec.onFork ? zoneSpec : parentDelegate._forkZS);
751759
this._forkDlgt = zoneSpec && (zoneSpec.onFork ? parentDelegate : parentDelegate._forkDlgt);
760+
this._forkCurrZone = zoneSpec && (zoneSpec.onFork ? this.zone : parentDelegate.zone);
752761

753762
this._interceptZS =
754763
zoneSpec && (zoneSpec.onIntercept ? zoneSpec : parentDelegate._interceptZS);
755764
this._interceptDlgt =
756765
zoneSpec && (zoneSpec.onIntercept ? parentDelegate : parentDelegate._interceptDlgt);
766+
this._interceptCurrZone =
767+
zoneSpec && (zoneSpec.onIntercept ? this.zone : parentDelegate.zone);
757768

758769
this._invokeZS = zoneSpec && (zoneSpec.onInvoke ? zoneSpec : parentDelegate._invokeZS);
759770
this._invokeDlgt =
760771
zoneSpec && (zoneSpec.onInvoke ? parentDelegate : parentDelegate._invokeDlgt);
772+
this._invokeCurrZone =
773+
zoneSpec && (zoneSpec.onInvoke ? this.zone : parentDelegate.zone);
761774

762775
this._handleErrorZS =
763776
zoneSpec && (zoneSpec.onHandleError ? zoneSpec : parentDelegate._handleErrorZS);
764777
this._handleErrorDlgt =
765778
zoneSpec && (zoneSpec.onHandleError ? parentDelegate : parentDelegate._handleErrorDlgt);
779+
this._handleErrorCurrZone =
780+
zoneSpec && (zoneSpec.onHandleError ? this.zone : parentDelegate.zone);
766781

767782
this._scheduleTaskZS =
768783
zoneSpec && (zoneSpec.onScheduleTask ? zoneSpec : parentDelegate._scheduleTaskZS);
769784
this._scheduleTaskDlgt =
770785
zoneSpec && (zoneSpec.onScheduleTask ? parentDelegate : parentDelegate._scheduleTaskDlgt);
786+
this._scheduleTaskCurrZone =
787+
zoneSpec && (zoneSpec.onScheduleTask ? this.zone : parentDelegate.zone);
771788

772789
this._invokeTaskZS =
773790
zoneSpec && (zoneSpec.onInvokeTask ? zoneSpec : parentDelegate._invokeTaskZS);
774791
this._invokeTaskDlgt =
775792
zoneSpec && (zoneSpec.onInvokeTask ? parentDelegate : parentDelegate._invokeTaskDlgt);
793+
this._invokeTaskCurrZone =
794+
zoneSpec && (zoneSpec.onInvokeTask ? this.zone : parentDelegate.zone);
776795

777796
this._cancelTaskZS =
778797
zoneSpec && (zoneSpec.onCancelTask ? zoneSpec : parentDelegate._cancelTaskZS);
779798
this._cancelTaskDlgt =
780799
zoneSpec && (zoneSpec.onCancelTask ? parentDelegate : parentDelegate._cancelTaskDlgt);
800+
this._cancelTaskCurrZone =
801+
zoneSpec && (zoneSpec.onCancelTask ? this.zone : parentDelegate.zone);
781802

782803
this._hasTaskZS = zoneSpec && (zoneSpec.onHasTask ? zoneSpec : parentDelegate._hasTaskZS);
783804
this._hasTaskDlgt =
784805
zoneSpec && (zoneSpec.onHasTask ? parentDelegate : parentDelegate._hasTaskDlgt);
806+
this._hasTaskCurrZone =
807+
zoneSpec && (zoneSpec.onHasTask ? this.zone : parentDelegate.zone);
785808
}
786809

787810
fork(targetZone: Zone, zoneSpec: ZoneSpec): AmbientZone {
@@ -792,29 +815,29 @@ const Zone: ZoneType = (function(global: any) {
792815
intercept(targetZone: Zone, callback: Function, source: string): Function {
793816
return this._interceptZS ?
794817
this._interceptZS.onIntercept(
795-
this._interceptDlgt, this.zone, targetZone, callback, source) :
818+
this._interceptDlgt, this._interceptCurrZone, targetZone, callback, source) :
796819
callback;
797820
}
798821

799822
invoke(targetZone: Zone, callback: Function, applyThis: any, applyArgs: any[], source: string):
800823
any {
801824
return this._invokeZS ?
802825
this._invokeZS.onInvoke(
803-
this._invokeDlgt, this.zone, targetZone, callback, applyThis, applyArgs, source) :
826+
this._invokeDlgt, this._invokeCurrZone, targetZone, callback, applyThis, applyArgs, source) :
804827
callback.apply(applyThis, applyArgs);
805828
}
806829

807830
handleError(targetZone: Zone, error: any): boolean {
808831
return this._handleErrorZS ?
809-
this._handleErrorZS.onHandleError(this._handleErrorDlgt, this.zone, targetZone, error) :
832+
this._handleErrorZS.onHandleError(this._handleErrorDlgt, this._handleErrorCurrZone, targetZone, error) :
810833
true;
811834
}
812835

813836
scheduleTask(targetZone: Zone, task: Task): Task {
814837
try {
815838
if (this._scheduleTaskZS) {
816839
return this._scheduleTaskZS.onScheduleTask(
817-
this._scheduleTaskDlgt, this.zone, targetZone, task);
840+
this._scheduleTaskDlgt, this._scheduleTaskCurrZone, targetZone, task);
818841
} else if (task.scheduleFn) {
819842
task.scheduleFn(task);
820843
} else if (task.type == 'microTask') {
@@ -834,7 +857,7 @@ const Zone: ZoneType = (function(global: any) {
834857
try {
835858
return this._invokeTaskZS ?
836859
this._invokeTaskZS.onInvokeTask(
837-
this._invokeTaskDlgt, this.zone, targetZone, task, applyThis, applyArgs) :
860+
this._invokeTaskDlgt, this._invokeTaskCurrZone, targetZone, task, applyThis, applyArgs) :
838861
task.callback.apply(applyThis, applyArgs);
839862
} finally {
840863
if (targetZone == this.zone && (task.type != 'eventTask') &&
@@ -847,7 +870,7 @@ const Zone: ZoneType = (function(global: any) {
847870
cancelTask(targetZone: Zone, task: Task): any {
848871
let value;
849872
if (this._cancelTaskZS) {
850-
value = this._cancelTaskZS.onCancelTask(this._cancelTaskDlgt, this.zone, targetZone, task);
873+
value = this._cancelTaskZS.onCancelTask(this._cancelTaskDlgt, this._cancelTaskCurrZone, targetZone, task);
851874
} else if (!task.cancelFn) {
852875
throw new Error('Task does not support cancellation, or is already canceled.');
853876
} else {
@@ -862,7 +885,7 @@ const Zone: ZoneType = (function(global: any) {
862885

863886
hasTask(targetZone: Zone, isEmpty: HasTaskState) {
864887
return this._hasTaskZS &&
865-
this._hasTaskZS.onHasTask(this._hasTaskDlgt, this.zone, targetZone, isEmpty);
888+
this._hasTaskZS.onHasTask(this._hasTaskDlgt, this._hasTaskCurrZone, targetZone, isEmpty);
866889
}
867890

868891
private _updateTaskCount(type: TaskType, count: number) {

‎test/common/zone.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ describe('Zone', function() {
3636

3737
expect(errorSpy).toHaveBeenCalled();
3838
});
39+
40+
it('should send correct currentZone in hook method when in nested zone', function() {
41+
var zone = Zone.current;
42+
var zoneA = zone.fork({
43+
name: 'A',
44+
onInvoke: function(parentDelegate, currentZone, targetZone, callback, applyThis, applyArgs, source) {
45+
expect(currentZone.name).toEqual('A');
46+
return parentDelegate.invoke(targetZone, callback, applyThis, applyArgs, source);
47+
}
48+
});
49+
var zoneB = zoneA.fork({
50+
name: 'B',
51+
onInvoke: function(parentDelegate, currentZone, targetZone, callback, applyThis, applyArgs, source) {
52+
expect(currentZone.name).toEqual('B');
53+
return parentDelegate.invoke(targetZone, callback, applyThis, applyArgs, source);
54+
}
55+
});
56+
var zoneC = zoneB.fork({
57+
name: 'C'
58+
});
59+
zoneC.run(function() {
60+
});
61+
});
3962
});
4063

4164
it('should allow zones to be run from within another zone', function() {

0 commit comments

Comments
 (0)
This repository has been archived.