From 44572f114fe41d0a287d8db19ba84669fc3f7bf3 Mon Sep 17 00:00:00 2001 From: Sammy Jelin Date: Fri, 18 Nov 2016 14:30:47 -0800 Subject: [PATCH] refactor(ngUpgrade): Small cleanup with Testability API and resumeBootstrap (#12926) * With non-static ngUpgrade apps, callbacks to `whenStable` were being invoked with the wrong context * With non-static ngUpgrade apps, `resumeBootstrap` was being run outside the NgZone * Remove redundent `whenStableContext` variable Neither of the first two problems were actually causing bugs (as far as I know), but they *might* have caused problems in the future. Inspired by https://github.com/angular/angular/pull/12910, but for non-static apps. --- modules/@angular/upgrade/src/aot/upgrade_module.ts | 6 ++---- modules/@angular/upgrade/src/upgrade_adapter.ts | 9 +++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/modules/@angular/upgrade/src/aot/upgrade_module.ts b/modules/@angular/upgrade/src/aot/upgrade_module.ts index 46cef6c605..f7f6814f76 100644 --- a/modules/@angular/upgrade/src/aot/upgrade_module.ts +++ b/modules/@angular/upgrade/src/aot/upgrade_module.ts @@ -166,16 +166,14 @@ export class UpgradeModule { (testabilityDelegate: angular.ITestabilityService) => { const originalWhenStable: Function = testabilityDelegate.whenStable; const injector = this.injector; - // Cannot use arrow function below because we need to grab the context + // Cannot use arrow function below because we need the context const newWhenStable = function(callback: Function) { - const whenStableContext: any = this; originalWhenStable.call(this, function() { const ng2Testability: Testability = injector.get(Testability); if (ng2Testability.isStable()) { callback.apply(this, arguments); } else { - ng2Testability.whenStable( - newWhenStable.bind(whenStableContext, callback)); + ng2Testability.whenStable(newWhenStable.bind(this, callback)); } }); }; diff --git a/modules/@angular/upgrade/src/upgrade_adapter.ts b/modules/@angular/upgrade/src/upgrade_adapter.ts index e1984eaad0..a488d9ca28 100644 --- a/modules/@angular/upgrade/src/upgrade_adapter.ts +++ b/modules/@angular/upgrade/src/upgrade_adapter.ts @@ -355,14 +355,14 @@ export class UpgradeAdapter { function(testabilityDelegate: angular.ITestabilityService) { const originalWhenStable: Function = testabilityDelegate.whenStable; - const newWhenStable = (callback: Function): void => { - const whenStableContext: any = this; + // Cannot use arrow function below because we need the context + const newWhenStable = function(callback: Function) { originalWhenStable.call(this, function() { const ng2Testability: Testability = moduleRef.injector.get(Testability); if (ng2Testability.isStable()) { callback.apply(this, arguments); } else { - ng2Testability.whenStable(newWhenStable.bind(whenStableContext, callback)); + ng2Testability.whenStable(newWhenStable.bind(this, callback)); } }); }; @@ -433,8 +433,9 @@ export class UpgradeAdapter { if (windowAngular.resumeBootstrap) { const originalResumeBootstrap: () => void = windowAngular.resumeBootstrap; windowAngular.resumeBootstrap = function() { + let args = arguments; windowAngular.resumeBootstrap = originalResumeBootstrap; - windowAngular.resumeBootstrap.apply(this, arguments); + ngZone.run(() => { windowAngular.resumeBootstrap.apply(this, args); }); resolve(); }; } else {