refactor(core): simplify & cleanup reflection

This commit is contained in:
Victor Berchet
2016-10-17 23:17:34 -07:00
committed by Igor Minar
parent 27d76776b8
commit f7db0668d1
5 changed files with 99 additions and 346 deletions
@@ -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;
}
}
@@ -134,10 +134,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
interfaces(type: Type<any>): any[] { return []; }
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean {
if (!(type instanceof Type)) return false;
const proto = (<any>type).prototype;
return !!proto[lcProperty];
return type instanceof Type && lcProperty in type.prototype;
}
getter(name: string): GetterFn { return <GetterFn>new Function('o', 'return o.' + name + ';'); }
@@ -15,14 +15,13 @@ import {GetterFn, MethodFn, SetterFn} from './types';
export {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
export {GetterFn, MethodFn, SetterFn} from './types';
/**
* Reflective information about a symbol, including annotations, interfaces, and other metadata.
*/
export class ReflectionInfo {
constructor(
public annotations?: any[], public parameters?: any[][], public factory?: Function,
public interfaces?: any[], public propMetadata?: {[key: string]: any[]}) {}
public interfaces?: any[], public propMetadata?: {[name: string]: any[]}) {}
}
/**
@@ -45,8 +44,6 @@ export class Reflector extends ReflectorReader {
updateCapabilities(caps: PlatformReflectionCapabilities) { this.reflectionCapabilities = caps; }
isReflectionEnabled(): boolean { return this.reflectionCapabilities.isReflectionEnabled(); }
/**
* Causes `this` reflector to track keys used to access
* {@link ReflectionInfo} objects.
@@ -66,10 +63,6 @@ export class Reflector extends ReflectorReader {
return allTypes.filter(key => !this._usedKeys.has(key));
}
registerFunction(func: Function, funcInfo: ReflectionInfo): void {
this._injectableInfo.set(func, funcInfo);
}
registerType(type: Type<any>, typeInfo: ReflectionInfo): void {
this._injectableInfo.set(type, typeInfo);
}
@@ -160,6 +153,7 @@ export class Reflector extends ReflectorReader {
resolveIdentifier(name: string, moduleUrl: string, runtime: any): any {
return this.reflectionCapabilities.resolveIdentifier(name, moduleUrl, runtime);
}
resolveEnum(identifier: any, name: string): any {
return this.reflectionCapabilities.resolveEnum(identifier, name);
}
@@ -1,42 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {makeDecorator, makeParamDecorator, makePropDecorator} from '@angular/core/src/util/decorators';
export interface ClassDecoratorFactory {
(data: ClassDecorator): any;
new (data: ClassDecorator): ClassDecorator;
}
export interface ClassDecorator { value: any; }
export interface ParamDecorator { value: any; }
export interface PropDecorator { value: any; }
export function classDecorator(value: any /** TODO #9100 */) {
return new ClassDecorator({value: value});
}
export function paramDecorator(value: any /** TODO #9100 */) {
return new ParamDecorator(value);
}
export function propDecorator(value: any /** TODO #9100 */) {
return new PropDecorator(value);
}
/** @Annotation */ export const ClassDecorator =
<ClassDecoratorFactory>makeDecorator('ClassDecorator', {value: undefined});
/** @Annotation */ export const ParamDecorator =
makeParamDecorator('ParamDecorator', [['value', undefined]]);
/** @Annotation */ export const PropDecorator =
makePropDecorator('PropDecorator', [['value', undefined]]);
// used only in Dart
export class HasGetterAndSetterDecorators {}
File diff suppressed because it is too large Load Diff