Skip to content

Commit f7db066

Browse files
vicbIgorMinar
authored andcommitted
refactor(core): simplify & cleanup reflection
1 parent 27d7677 commit f7db066

5 files changed

Lines changed: 99 additions & 346 deletions

File tree

modules/@angular/compiler/src/lifecycle_reflector.ts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,51 @@
88

99
import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit, Type} from '@angular/core';
1010

11-
import {MapWrapper} from './facade/collection';
1211
import {LifecycleHooks, reflector} from './private_import_core';
1312

14-
const LIFECYCLE_INTERFACES: Map<any, Type<any>> = MapWrapper.createFromPairs([
15-
[LifecycleHooks.OnInit, OnInit],
16-
[LifecycleHooks.OnDestroy, OnDestroy],
17-
[LifecycleHooks.DoCheck, DoCheck],
18-
[LifecycleHooks.OnChanges, OnChanges],
19-
[LifecycleHooks.AfterContentInit, AfterContentInit],
20-
[LifecycleHooks.AfterContentChecked, AfterContentChecked],
21-
[LifecycleHooks.AfterViewInit, AfterViewInit],
22-
[LifecycleHooks.AfterViewChecked, AfterViewChecked],
23-
]);
24-
25-
const LIFECYCLE_PROPS: Map<any, string> = MapWrapper.createFromPairs([
26-
[LifecycleHooks.OnInit, 'ngOnInit'],
27-
[LifecycleHooks.OnDestroy, 'ngOnDestroy'],
28-
[LifecycleHooks.DoCheck, 'ngDoCheck'],
29-
[LifecycleHooks.OnChanges, 'ngOnChanges'],
30-
[LifecycleHooks.AfterContentInit, 'ngAfterContentInit'],
31-
[LifecycleHooks.AfterContentChecked, 'ngAfterContentChecked'],
32-
[LifecycleHooks.AfterViewInit, 'ngAfterViewInit'],
33-
[LifecycleHooks.AfterViewChecked, 'ngAfterViewChecked'],
34-
]);
3513

3614
export function hasLifecycleHook(hook: LifecycleHooks, token: any): boolean {
37-
var lcInterface = LIFECYCLE_INTERFACES.get(hook);
38-
var lcProp = LIFECYCLE_PROPS.get(hook);
39-
return reflector.hasLifecycleHook(token, lcInterface, lcProp);
15+
return reflector.hasLifecycleHook(token, getInterface(hook), getHookName(hook));
16+
}
17+
18+
function getHookName(hook: LifecycleHooks): string {
19+
switch (hook) {
20+
case LifecycleHooks.OnInit:
21+
return 'ngOnInit';
22+
case LifecycleHooks.OnDestroy:
23+
return 'ngOnDestroy';
24+
case LifecycleHooks.DoCheck:
25+
return 'ngDoCheck';
26+
case LifecycleHooks.OnChanges:
27+
return 'ngOnChanges';
28+
case LifecycleHooks.AfterContentInit:
29+
return 'ngAfterContentInit';
30+
case LifecycleHooks.AfterContentChecked:
31+
return 'ngAfterContentChecked';
32+
case LifecycleHooks.AfterViewInit:
33+
return 'ngAfterViewInit';
34+
case LifecycleHooks.AfterViewChecked:
35+
return 'ngAfterViewChecked';
36+
}
37+
}
38+
39+
function getInterface(hook: LifecycleHooks): any {
40+
switch (hook) {
41+
case LifecycleHooks.OnInit:
42+
return OnInit;
43+
case LifecycleHooks.OnDestroy:
44+
return OnDestroy;
45+
case LifecycleHooks.DoCheck:
46+
return DoCheck;
47+
case LifecycleHooks.OnChanges:
48+
return OnChanges;
49+
case LifecycleHooks.AfterContentInit:
50+
return AfterContentInit;
51+
case LifecycleHooks.AfterContentChecked:
52+
return AfterContentChecked;
53+
case LifecycleHooks.AfterViewInit:
54+
return AfterViewInit;
55+
case LifecycleHooks.AfterViewChecked:
56+
return AfterViewChecked;
57+
}
4058
}

modules/@angular/core/src/reflection/reflection_capabilities.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
134134
interfaces(type: Type<any>): any[] { return []; }
135135

136136
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean {
137-
if (!(type instanceof Type)) return false;
138-
139-
const proto = (<any>type).prototype;
140-
return !!proto[lcProperty];
137+
return type instanceof Type && lcProperty in type.prototype;
141138
}
142139

143140
getter(name: string): GetterFn { return <GetterFn>new Function('o', 'return o.' + name + ';'); }

modules/@angular/core/src/reflection/reflector.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ import {GetterFn, MethodFn, SetterFn} from './types';
1515
export {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
1616
export {GetterFn, MethodFn, SetterFn} from './types';
1717

18-
1918
/**
2019
* Reflective information about a symbol, including annotations, interfaces, and other metadata.
2120
*/
2221
export class ReflectionInfo {
2322
constructor(
2423
public annotations?: any[], public parameters?: any[][], public factory?: Function,
25-
public interfaces?: any[], public propMetadata?: {[key: string]: any[]}) {}
24+
public interfaces?: any[], public propMetadata?: {[name: string]: any[]}) {}
2625
}
2726

2827
/**
@@ -45,8 +44,6 @@ export class Reflector extends ReflectorReader {
4544

4645
updateCapabilities(caps: PlatformReflectionCapabilities) { this.reflectionCapabilities = caps; }
4746

48-
isReflectionEnabled(): boolean { return this.reflectionCapabilities.isReflectionEnabled(); }
49-
5047
/**
5148
* Causes `this` reflector to track keys used to access
5249
* {@link ReflectionInfo} objects.
@@ -66,10 +63,6 @@ export class Reflector extends ReflectorReader {
6663
return allTypes.filter(key => !this._usedKeys.has(key));
6764
}
6865

69-
registerFunction(func: Function, funcInfo: ReflectionInfo): void {
70-
this._injectableInfo.set(func, funcInfo);
71-
}
72-
7366
registerType(type: Type<any>, typeInfo: ReflectionInfo): void {
7467
this._injectableInfo.set(type, typeInfo);
7568
}
@@ -160,6 +153,7 @@ export class Reflector extends ReflectorReader {
160153
resolveIdentifier(name: string, moduleUrl: string, runtime: any): any {
161154
return this.reflectionCapabilities.resolveIdentifier(name, moduleUrl, runtime);
162155
}
156+
163157
resolveEnum(identifier: any, name: string): any {
164158
return this.reflectionCapabilities.resolveEnum(identifier, name);
165159
}

modules/@angular/core/test/reflection/reflector_common.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)