refactor(upgrade): allow Closure advanced optimizations in `UpgradeComponent` (#13629)

Get rid of the dynamic invocation style used in `callLifecycleHook()`, which
would break under Closure Compiler's advanced optimizations.
Related to https://github.com/angular/angular/pull/13020#discussion_r93492935.

PR Close #13629
This commit is contained in:
Georgios Kalpakas 2016-12-22 12:17:49 +02:00 committed by Miško Hevery
parent a277e97dd7
commit 00979838ef
1 changed files with 13 additions and 11 deletions

View File

@ -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)) { if (this.controllerInstance && isFunction(this.controllerInstance.$doCheck)) {
const callDoCheck = () => this.callLifecycleHook('$doCheck', this.controllerInstance); const callDoCheck = () => this.controllerInstance.$doCheck();
this.$componentScope.$parent.$watch(callDoCheck); this.$componentScope.$parent.$watch(callDoCheck);
callDoCheck(); callDoCheck();
@ -200,7 +202,9 @@ export class UpgradeComponent implements OnInit, OnChanges, DoCheck, OnDestroy {
postLink(this.$componentScope, this.$element, attrs, requiredControllers, transcludeFn); 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) { ngOnChanges(changes: SimpleChanges) {
@ -208,7 +212,9 @@ export class UpgradeComponent implements OnInit, OnChanges, DoCheck, OnDestroy {
Object.keys(changes).forEach( Object.keys(changes).forEach(
propName => this.bindingDestination[propName] = changes[propName].currentValue); propName => this.bindingDestination[propName] = changes[propName].currentValue);
this.callLifecycleHook('$onChanges', this.bindingDestination, changes); if (isFunction(this.bindingDestination.$onChanges)) {
this.bindingDestination.$onChanges(changes);
}
} }
ngDoCheck() { ngDoCheck() {
@ -231,14 +237,10 @@ export class UpgradeComponent implements OnInit, OnChanges, DoCheck, OnDestroy {
} }
ngOnDestroy() { ngOnDestroy() {
this.callLifecycleHook('$onDestroy', this.controllerInstance); if (this.controllerInstance && isFunction(this.controllerInstance.$onDestroy)) {
this.$componentScope.$destroy(); this.controllerInstance.$onDestroy();
}
private callLifecycleHook(method: LifecycleHook, context: IBindingDestination, arg?: any) {
if (context && isFunction(context[method])) {
context[method](arg);
} }
this.$componentScope.$destroy();
} }
private getDirective(name: string): angular.IDirective { private getDirective(name: string): angular.IDirective {