refactor(core): support importUri in StaticReflector

Closes #8195
This commit is contained in:
Alex Eagle 2016-02-18 10:53:21 -08:00 committed by Alex Eagle
parent 6103aa0a46
commit 3e114227f8
3 changed files with 21 additions and 8 deletions

View File

@ -39,6 +39,7 @@ import {
SkipSelfMetadata SkipSelfMetadata
} from 'angular2/src/core/di/metadata'; } from 'angular2/src/core/di/metadata';
import {AttributeMetadata} from 'angular2/src/core/metadata/di'; import {AttributeMetadata} from 'angular2/src/core/metadata/di';
import {ReflectorReader} from 'angular2/src/core/reflection/reflector_reader';
@Injectable() @Injectable()
export class RuntimeMetadataResolver { export class RuntimeMetadataResolver {
@ -46,11 +47,19 @@ export class RuntimeMetadataResolver {
private _pipeCache = new Map<Type, cpl.CompilePipeMetadata>(); private _pipeCache = new Map<Type, cpl.CompilePipeMetadata>();
private _anonymousTypes = new Map<Object, number>(); private _anonymousTypes = new Map<Object, number>();
private _anonymousTypeIndex = 0; private _anonymousTypeIndex = 0;
private _reflector: ReflectorReader;
constructor(private _directiveResolver: DirectiveResolver, private _pipeResolver: PipeResolver, constructor(private _directiveResolver: DirectiveResolver, private _pipeResolver: PipeResolver,
private _viewResolver: ViewResolver, private _viewResolver: ViewResolver,
@Optional() @Inject(PLATFORM_DIRECTIVES) private _platformDirectives: Type[], @Optional() @Inject(PLATFORM_DIRECTIVES) private _platformDirectives: Type[],
@Optional() @Inject(PLATFORM_PIPES) private _platformPipes: Type[]) {} @Optional() @Inject(PLATFORM_PIPES) private _platformPipes: Type[],
_reflector?: ReflectorReader) {
if (isPresent(_reflector)) {
this._reflector = _reflector;
} else {
this._reflector = reflector;
}
}
private sanitizeTokenName(token: any): string { private sanitizeTokenName(token: any): string {
let identifier = stringify(token); let identifier = stringify(token);
@ -78,7 +87,7 @@ export class RuntimeMetadataResolver {
if (dirMeta instanceof md.ComponentMetadata) { if (dirMeta instanceof md.ComponentMetadata) {
assertArrayOfStrings('styles', dirMeta.styles); assertArrayOfStrings('styles', dirMeta.styles);
var cmpMeta = <md.ComponentMetadata>dirMeta; var cmpMeta = <md.ComponentMetadata>dirMeta;
moduleUrl = calcModuleUrl(directiveType, cmpMeta); moduleUrl = calcModuleUrl(this._reflector, directiveType, cmpMeta);
var viewMeta = this._viewResolver.resolve(directiveType); var viewMeta = this._viewResolver.resolve(directiveType);
assertArrayOfStrings('styles', viewMeta.styles); assertArrayOfStrings('styles', viewMeta.styles);
templateMeta = new cpl.CompileTemplateMetadata({ templateMeta = new cpl.CompileTemplateMetadata({
@ -148,7 +157,7 @@ export class RuntimeMetadataResolver {
var meta = this._pipeCache.get(pipeType); var meta = this._pipeCache.get(pipeType);
if (isBlank(meta)) { if (isBlank(meta)) {
var pipeMeta = this._pipeResolver.resolve(pipeType); var pipeMeta = this._pipeResolver.resolve(pipeType);
var moduleUrl = reflector.importUri(pipeType); var moduleUrl = this._reflector.importUri(pipeType);
meta = new cpl.CompilePipeMetadata({ meta = new cpl.CompilePipeMetadata({
type: this.getTypeMetadata(pipeType, moduleUrl), type: this.getTypeMetadata(pipeType, moduleUrl),
name: pipeMeta.name, name: pipeMeta.name,
@ -341,7 +350,8 @@ function isValidType(value: Type): boolean {
return isPresent(value) && (value instanceof Type); return isPresent(value) && (value instanceof Type);
} }
function calcModuleUrl(type: Type, cmpMetadata: md.ComponentMetadata): string { function calcModuleUrl(reflector: ReflectorReader, type: Type,
cmpMetadata: md.ComponentMetadata): string {
var moduleId = cmpMetadata.moduleId; var moduleId = cmpMetadata.moduleId;
if (isPresent(moduleId)) { if (isPresent(moduleId)) {
var scheme = getUrlScheme(moduleId); var scheme = getUrlScheme(moduleId);

View File

@ -25,6 +25,7 @@ import {
ViewQueryMetadata, ViewQueryMetadata,
QueryMetadata, QueryMetadata,
} from 'angular2/src/core/metadata'; } from 'angular2/src/core/metadata';
import {ReflectorReader} from 'angular2/src/core/reflection/reflector_reader';
/** /**
* The host of the static resolver is expected to be able to provide module metadata in the form of * The host of the static resolver is expected to be able to provide module metadata in the form of
@ -56,15 +57,16 @@ export class StaticType {
* A static reflector implements enough of the Reflector API that is necessary to compile * A static reflector implements enough of the Reflector API that is necessary to compile
* templates statically. * templates statically.
*/ */
export class StaticReflector { export class StaticReflector implements ReflectorReader {
private typeCache = new Map<string, StaticType>(); private typeCache = new Map<string, StaticType>();
private annotationCache = new Map<StaticType, any[]>(); private annotationCache = new Map<StaticType, any[]>();
private propertyCache = new Map<StaticType, {[key: string]: any}>(); private propertyCache = new Map<StaticType, {[key: string]: any}>();
private parameterCache = new Map<StaticType, any[]>(); private parameterCache = new Map<StaticType, any[]>();
private metadataCache = new Map<string, {[key: string]: any}>(); private metadataCache = new Map<string, {[key: string]: any}>();
constructor(private host: StaticReflectorHost) { this.initializeConversionMap(); } constructor(private host: StaticReflectorHost) { this.initializeConversionMap(); }
importUri(typeOrFunc: any): string { return (<StaticType>typeOrFunc).moduleId; }
/** /**
* getStatictype produces a Type whose metadata is known but whose implementation is not loaded. * getStatictype produces a Type whose metadata is known but whose implementation is not loaded.
* All types passed to the StaticResolver should be pseudo-types returned by this method. * All types passed to the StaticResolver should be pseudo-types returned by this method.
@ -438,7 +440,7 @@ export class StaticReflector {
return simplify(value); return simplify(value);
} }
private getModuleMetadata(module: string): {[key: string]: any} { public getModuleMetadata(module: string): {[key: string]: any} {
let moduleMetadata = this.metadataCache.get(module); let moduleMetadata = this.metadataCache.get(module);
if (!isPresent(moduleMetadata)) { if (!isPresent(moduleMetadata)) {
moduleMetadata = this.host.getMetadataFor(module); moduleMetadata = this.host.getMetadataFor(module);

View File

@ -6,4 +6,5 @@ export abstract class ReflectorReader {
abstract parameters(typeOrFunc: /*Type*/ any): any[][]; abstract parameters(typeOrFunc: /*Type*/ any): any[][];
abstract annotations(typeOrFunc: /*Type*/ any): any[]; abstract annotations(typeOrFunc: /*Type*/ any): any[];
abstract propMetadata(typeOrFunc: /*Type*/ any): {[key: string]: any[]}; abstract propMetadata(typeOrFunc: /*Type*/ any): {[key: string]: any[]};
} abstract importUri(typeOrFunc: /*Type*/ any): string;
}