From 17b32b5fd4c1eb37ea30af8ae41a79843f37c935 Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Fri, 19 Jul 2019 12:20:07 +0900 Subject: [PATCH] fix(zone.js): hook should set correct current zone (#31642) Close #31641 PR Close #31642 --- packages/zone.js/lib/zone.ts | 18 ++++++++++-------- packages/zone.js/test/common/zone.spec.ts | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/packages/zone.js/lib/zone.ts b/packages/zone.js/lib/zone.ts index 8898656c50..3b7c27e068 100644 --- a/packages/zone.js/lib/zone.ts +++ b/packages/zone.js/lib/zone.ts @@ -1021,47 +1021,49 @@ const Zone: ZoneType = (function(global: any) { this._forkZS = zoneSpec && (zoneSpec && zoneSpec.onFork ? zoneSpec : parentDelegate !._forkZS); this._forkDlgt = zoneSpec && (zoneSpec.onFork ? parentDelegate : parentDelegate !._forkDlgt); - this._forkCurrZone = zoneSpec && (zoneSpec.onFork ? this.zone : parentDelegate !.zone); + this._forkCurrZone = + zoneSpec && (zoneSpec.onFork ? this.zone : parentDelegate !._forkCurrZone); this._interceptZS = zoneSpec && (zoneSpec.onIntercept ? zoneSpec : parentDelegate !._interceptZS); this._interceptDlgt = zoneSpec && (zoneSpec.onIntercept ? parentDelegate : parentDelegate !._interceptDlgt); this._interceptCurrZone = - zoneSpec && (zoneSpec.onIntercept ? this.zone : parentDelegate !.zone); + zoneSpec && (zoneSpec.onIntercept ? this.zone : parentDelegate !._interceptCurrZone); this._invokeZS = zoneSpec && (zoneSpec.onInvoke ? zoneSpec : parentDelegate !._invokeZS); this._invokeDlgt = zoneSpec && (zoneSpec.onInvoke ? parentDelegate ! : parentDelegate !._invokeDlgt); - this._invokeCurrZone = zoneSpec && (zoneSpec.onInvoke ? this.zone : parentDelegate !.zone); + this._invokeCurrZone = + zoneSpec && (zoneSpec.onInvoke ? this.zone : parentDelegate !._invokeCurrZone); this._handleErrorZS = zoneSpec && (zoneSpec.onHandleError ? zoneSpec : parentDelegate !._handleErrorZS); this._handleErrorDlgt = zoneSpec && (zoneSpec.onHandleError ? parentDelegate ! : parentDelegate !._handleErrorDlgt); this._handleErrorCurrZone = - zoneSpec && (zoneSpec.onHandleError ? this.zone : parentDelegate !.zone); + zoneSpec && (zoneSpec.onHandleError ? this.zone : parentDelegate !._handleErrorCurrZone); this._scheduleTaskZS = zoneSpec && (zoneSpec.onScheduleTask ? zoneSpec : parentDelegate !._scheduleTaskZS); this._scheduleTaskDlgt = zoneSpec && (zoneSpec.onScheduleTask ? parentDelegate ! : parentDelegate !._scheduleTaskDlgt); - this._scheduleTaskCurrZone = - zoneSpec && (zoneSpec.onScheduleTask ? this.zone : parentDelegate !.zone); + this._scheduleTaskCurrZone = zoneSpec && + (zoneSpec.onScheduleTask ? this.zone : parentDelegate !._scheduleTaskCurrZone); this._invokeTaskZS = zoneSpec && (zoneSpec.onInvokeTask ? zoneSpec : parentDelegate !._invokeTaskZS); this._invokeTaskDlgt = zoneSpec && (zoneSpec.onInvokeTask ? parentDelegate ! : parentDelegate !._invokeTaskDlgt); this._invokeTaskCurrZone = - zoneSpec && (zoneSpec.onInvokeTask ? this.zone : parentDelegate !.zone); + zoneSpec && (zoneSpec.onInvokeTask ? this.zone : parentDelegate !._invokeTaskCurrZone); this._cancelTaskZS = zoneSpec && (zoneSpec.onCancelTask ? zoneSpec : parentDelegate !._cancelTaskZS); this._cancelTaskDlgt = zoneSpec && (zoneSpec.onCancelTask ? parentDelegate ! : parentDelegate !._cancelTaskDlgt); this._cancelTaskCurrZone = - zoneSpec && (zoneSpec.onCancelTask ? this.zone : parentDelegate !.zone); + zoneSpec && (zoneSpec.onCancelTask ? this.zone : parentDelegate !._cancelTaskCurrZone); this._hasTaskZS = null; this._hasTaskDlgt = null; diff --git a/packages/zone.js/test/common/zone.spec.ts b/packages/zone.js/test/common/zone.spec.ts index 2f21e1fcc1..58a235cb4c 100644 --- a/packages/zone.js/test/common/zone.spec.ts +++ b/packages/zone.js/test/common/zone.spec.ts @@ -49,6 +49,22 @@ describe('Zone', function() { const zoneC = zoneB.fork({name: 'C'}); zoneC.run(function() {}); }); + + it('should send correct currentZone in hook method when in nested zone with empty implementation', + function() { + const zone = Zone.current; + const zoneA = zone.fork({ + name: 'A', + onInvoke: function( + parentDelegate, currentZone, targetZone, callback, applyThis, applyArgs, source) { + expect(currentZone.name).toEqual('A'); + return parentDelegate.invoke(targetZone, callback, applyThis, applyArgs, source); + } + }); + const zoneB = zoneA.fork({name: 'B'}); + const zoneC = zoneB.fork({name: 'C'}); + zoneC.run(function() {}); + }); }); it('should allow zones to be run from within another zone', function() {