refactor(core): simplify & cleanup reflection

This commit is contained in:
Victor Berchet
2016-10-11 18:42:00 -07:00
committed by Igor Minar
parent 27d76776b8
commit f7db0668d1
5 changed files with 99 additions and 346 deletions

View File

@ -8,33 +8,51 @@
import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit, Type} from '@angular/core';
import {MapWrapper} from './facade/collection';
import {LifecycleHooks, reflector} from './private_import_core';
const LIFECYCLE_INTERFACES: Map<any, Type<any>> = MapWrapper.createFromPairs([
[LifecycleHooks.OnInit, OnInit],
[LifecycleHooks.OnDestroy, OnDestroy],
[LifecycleHooks.DoCheck, DoCheck],
[LifecycleHooks.OnChanges, OnChanges],
[LifecycleHooks.AfterContentInit, AfterContentInit],
[LifecycleHooks.AfterContentChecked, AfterContentChecked],
[LifecycleHooks.AfterViewInit, AfterViewInit],
[LifecycleHooks.AfterViewChecked, AfterViewChecked],
]);
const LIFECYCLE_PROPS: Map<any, string> = MapWrapper.createFromPairs([
[LifecycleHooks.OnInit, 'ngOnInit'],
[LifecycleHooks.OnDestroy, 'ngOnDestroy'],
[LifecycleHooks.DoCheck, 'ngDoCheck'],
[LifecycleHooks.OnChanges, 'ngOnChanges'],
[LifecycleHooks.AfterContentInit, 'ngAfterContentInit'],
[LifecycleHooks.AfterContentChecked, 'ngAfterContentChecked'],
[LifecycleHooks.AfterViewInit, 'ngAfterViewInit'],
[LifecycleHooks.AfterViewChecked, 'ngAfterViewChecked'],
]);
export function hasLifecycleHook(hook: LifecycleHooks, token: any): boolean {
var lcInterface = LIFECYCLE_INTERFACES.get(hook);
var lcProp = LIFECYCLE_PROPS.get(hook);
return reflector.hasLifecycleHook(token, lcInterface, lcProp);
return reflector.hasLifecycleHook(token, getInterface(hook), getHookName(hook));
}
function getHookName(hook: LifecycleHooks): string {
switch (hook) {
case LifecycleHooks.OnInit:
return 'ngOnInit';
case LifecycleHooks.OnDestroy:
return 'ngOnDestroy';
case LifecycleHooks.DoCheck:
return 'ngDoCheck';
case LifecycleHooks.OnChanges:
return 'ngOnChanges';
case LifecycleHooks.AfterContentInit:
return 'ngAfterContentInit';
case LifecycleHooks.AfterContentChecked:
return 'ngAfterContentChecked';
case LifecycleHooks.AfterViewInit:
return 'ngAfterViewInit';
case LifecycleHooks.AfterViewChecked:
return 'ngAfterViewChecked';
}
}
function getInterface(hook: LifecycleHooks): any {
switch (hook) {
case LifecycleHooks.OnInit:
return OnInit;
case LifecycleHooks.OnDestroy:
return OnDestroy;
case LifecycleHooks.DoCheck:
return DoCheck;
case LifecycleHooks.OnChanges:
return OnChanges;
case LifecycleHooks.AfterContentInit:
return AfterContentInit;
case LifecycleHooks.AfterContentChecked:
return AfterContentChecked;
case LifecycleHooks.AfterViewInit:
return AfterViewInit;
case LifecycleHooks.AfterViewChecked:
return AfterViewChecked;
}
}