Skip to content

Commit

Permalink
refactor(upgrade): allow Closure advanced optimizations in `UpgradeCo…
Browse files Browse the repository at this point in the history
…mponent`

Get rid of the dynamic invocation style used in `callLifecycleHook()`, which
would break under Closure Compiler's advanced optimizations.
Related to angular#13020 (comment).
  • Loading branch information
gkalpak committed Dec 22, 2016
1 parent f114e40 commit c36f496
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions modules/@angular/upgrade/src/aot/upgrade_component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ export class UpgradeComponent implements OnInit, OnChanges, DoCheck, OnDestroy {
});
}

this.callLifecycleHook('$onInit', this.controllerInstance);
if (this.controllerInstance && isFunction(this.controllerInstance.$onInit)) {
this.controllerInstance.$onInit();
}

if (this.controllerInstance && isFunction(this.controllerInstance.$doCheck)) {
const callDoCheck = () => this.callLifecycleHook('$doCheck', this.controllerInstance);
const callDoCheck = () => this.controllerInstance.$doCheck();

this.$componentScope.$parent.$watch(callDoCheck);
callDoCheck();
Expand Down Expand Up @@ -200,15 +202,19 @@ export class UpgradeComponent implements OnInit, OnChanges, DoCheck, OnDestroy {
postLink(this.$componentScope, this.$element, attrs, requiredControllers, transcludeFn);
}

this.callLifecycleHook('$postLink', this.controllerInstance);
if (this.controllerInstance && isFunction(this.controllerInstance.$postLink)) {
this.controllerInstance.$postLink();
}
}

ngOnChanges(changes: SimpleChanges) {
// Forward input changes to `bindingDestination`
Object.keys(changes).forEach(
propName => this.bindingDestination[propName] = changes[propName].currentValue);

this.callLifecycleHook('$onChanges', this.bindingDestination, changes);
if (isFunction(this.bindingDestination.$onChanges)) {
this.bindingDestination.$onChanges(changes);
}
}

ngDoCheck() {
Expand All @@ -231,14 +237,10 @@ export class UpgradeComponent implements OnInit, OnChanges, DoCheck, OnDestroy {
}

ngOnDestroy() {
this.callLifecycleHook('$onDestroy', this.controllerInstance);
this.$componentScope.$destroy();
}

private callLifecycleHook(method: LifecycleHook, context: IBindingDestination, arg?: any) {
if (context && isFunction(context[method])) {
context[method](arg);
if (this.controllerInstance && isFunction(this.controllerInstance.$onDestroy)) {
this.controllerInstance.$onDestroy();
}
this.$componentScope.$destroy();
}

private getDirective(name: string): angular.IDirective {
Expand Down

0 comments on commit c36f496

Please sign in to comment.