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.
This commit is contained in:
Sammy Jelin 2016-11-18 14:30:47 -08:00 committed by Chuck Jazdzewski
parent 1ef4696cb7
commit 44572f114f
2 changed files with 7 additions and 8 deletions

View File

@ -166,16 +166,14 @@ export class UpgradeModule {
(testabilityDelegate: angular.ITestabilityService) => { (testabilityDelegate: angular.ITestabilityService) => {
const originalWhenStable: Function = testabilityDelegate.whenStable; const originalWhenStable: Function = testabilityDelegate.whenStable;
const injector = this.injector; 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 newWhenStable = function(callback: Function) {
const whenStableContext: any = this;
originalWhenStable.call(this, function() { originalWhenStable.call(this, function() {
const ng2Testability: Testability = injector.get(Testability); const ng2Testability: Testability = injector.get(Testability);
if (ng2Testability.isStable()) { if (ng2Testability.isStable()) {
callback.apply(this, arguments); callback.apply(this, arguments);
} else { } else {
ng2Testability.whenStable( ng2Testability.whenStable(newWhenStable.bind(this, callback));
newWhenStable.bind(whenStableContext, callback));
} }
}); });
}; };

View File

@ -355,14 +355,14 @@ export class UpgradeAdapter {
function(testabilityDelegate: angular.ITestabilityService) { function(testabilityDelegate: angular.ITestabilityService) {
const originalWhenStable: Function = testabilityDelegate.whenStable; const originalWhenStable: Function = testabilityDelegate.whenStable;
const newWhenStable = (callback: Function): void => { // Cannot use arrow function below because we need the context
const whenStableContext: any = this; const newWhenStable = function(callback: Function) {
originalWhenStable.call(this, function() { originalWhenStable.call(this, function() {
const ng2Testability: Testability = moduleRef.injector.get(Testability); const ng2Testability: Testability = moduleRef.injector.get(Testability);
if (ng2Testability.isStable()) { if (ng2Testability.isStable()) {
callback.apply(this, arguments); callback.apply(this, arguments);
} else { } else {
ng2Testability.whenStable(newWhenStable.bind(whenStableContext, callback)); ng2Testability.whenStable(newWhenStable.bind(this, callback));
} }
}); });
}; };
@ -433,8 +433,9 @@ export class UpgradeAdapter {
if (windowAngular.resumeBootstrap) { if (windowAngular.resumeBootstrap) {
const originalResumeBootstrap: () => void = windowAngular.resumeBootstrap; const originalResumeBootstrap: () => void = windowAngular.resumeBootstrap;
windowAngular.resumeBootstrap = function() { windowAngular.resumeBootstrap = function() {
let args = arguments;
windowAngular.resumeBootstrap = originalResumeBootstrap; windowAngular.resumeBootstrap = originalResumeBootstrap;
windowAngular.resumeBootstrap.apply(this, arguments); ngZone.run(() => { windowAngular.resumeBootstrap.apply(this, args); });
resolve(); resolve();
}; };
} else { } else {