Skip to content

Commit

Permalink
refactor(ngUpgrade): Small cleanup with Testability API and resumeBoo…
Browse files Browse the repository at this point in the history
…tstrap

* 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
* Updated static example to get type mocks for ng1

Neither of the first two problems were actually causing bugs (as far as I know), but they *might*
have caused problems in the future.  The example had to be updated to make the tests pass (no idea
why this change mattered to that example).

Inspired by angular#12910, but for non-static apps.
  • Loading branch information
sjelin committed Nov 16, 2016
1 parent 481c9b3 commit cd7a74d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion modules/@angular/examples/upgrade/static/ts/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Ng2AppModule {
// #docregion Angular 1 Stuff
// #docregion ng1-module
// This Angular 1 module represents the Angular 1 pieces of the application
const ng1AppModule = angular.module('ng1AppModule', []);
const ng1AppModule = (window as any).angular.module('ng1AppModule', []);
// #enddocregion

// #docregion ng1-hero
Expand Down
6 changes: 2 additions & 4 deletions modules/@angular/upgrade/src/aot/upgrade_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
};
Expand Down
9 changes: 5 additions & 4 deletions modules/@angular/upgrade/src/upgrade_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
};
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cd7a74d

Please sign in to comment.