fix(compiler): support lifecycle hooks in compiler_cli

This commit is contained in:
Tobias Bosch
2016-05-04 10:00:59 -07:00
parent bdce154282
commit 7150ace7c7
15 changed files with 143 additions and 102 deletions

View File

@ -5,6 +5,7 @@ export interface PlatformReflectionCapabilities {
isReflectionEnabled(): boolean;
factory(type: Type): Function;
interfaces(type: Type): any[];
hasLifecycleHook(type: any, lcInterface: /*Type*/ any, lcProperty: string): boolean;
parameters(type: any): any[][];
annotations(type: any): any[];
propMetadata(typeOrFunc: any): {[key: string]: any[]};

View File

@ -291,6 +291,11 @@ class ReflectionCapabilities implements PlatformReflectionCapabilities {
return name.endsWith("=") ? name.substring(0, name.length - 1) : name;
}
bool hasLifecycleHook(dynamic type, Type lcInterface, String lcProperty) {
if (type is! Type) return false;
return this.interfaces(type).contains(lcInterface);
}
List interfaces(type) {
final clazz = reflectType(type);
_assertDeclaresLifecycleHooks(clazz);

View File

@ -197,8 +197,16 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
return {};
}
interfaces(type: Type): any[] {
throw new BaseException("JavaScript does not support interfaces");
// Note: JavaScript does not support to query for interfaces during runtime.
// However, we can't throw here as the reflector will always call this method
// when asked for a lifecycle interface as this is what we check in Dart.
interfaces(type: Type): any[] { return []; }
hasLifecycleHook(type: any, lcInterface: Type, lcProperty: string): boolean {
if (!(type instanceof Type)) return false;
var proto = (<any>type).prototype;
return !!proto[lcProperty];
}
getter(name: string): GetterFn { return <GetterFn>new Function('o', 'return o.' + name + ';'); }

View File

@ -45,9 +45,7 @@ export class Reflector extends ReflectorReader {
this.reflectionCapabilities = reflectionCapabilities;
}
updateCapabilities(caps: PlatformReflectionCapabilities) {
this.reflectionCapabilities = caps;
}
updateCapabilities(caps: PlatformReflectionCapabilities) { this.reflectionCapabilities = caps; }
isReflectionEnabled(): boolean { return this.reflectionCapabilities.isReflectionEnabled(); }
@ -120,7 +118,7 @@ export class Reflector extends ReflectorReader {
}
}
interfaces(type: Type): any[] {
interfaces(type: /*Type*/ any): any[] {
if (this._injectableInfo.has(type)) {
var res = this._getReflectionInfo(type).interfaces;
return isPresent(res) ? res : [];
@ -129,6 +127,15 @@ export class Reflector extends ReflectorReader {
}
}
hasLifecycleHook(type: any, lcInterface: /*Type*/ any, lcProperty: string): boolean {
var interfaces = this.interfaces(type);
if (interfaces.indexOf(lcInterface) !== -1) {
return true;
} else {
return this.reflectionCapabilities.hasLifecycleHook(type, lcInterface, lcProperty);
}
}
getter(name: string): GetterFn {
if (this._getters.has(name)) {
return this._getters.get(name);