refactor: use ListWrapper.find()

This commit is contained in:
Victor Berchet 2015-10-22 23:35:53 -07:00
parent b90d899408
commit dd2598ccd8
3 changed files with 24 additions and 19 deletions

View File

@ -5,6 +5,7 @@ import {Injectable} from 'angular2/src/core/di';
import {Type, isBlank, stringify} from 'angular2/src/core/facade/lang';
import {BaseException} from 'angular2/src/core/facade/exceptions';
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
import {ListWrapper} from 'angular2/src/core/facade/collection';
import {reflector} from 'angular2/src/core/reflection/reflection';
import {CompiledHostTemplate} from 'angular2/src/core/linker/template_commands';
@ -20,20 +21,18 @@ export abstract class Compiler {
abstract clearCache();
}
function _isCompiledHostTemplate(type: any): boolean {
return type instanceof CompiledHostTemplate;
}
@Injectable()
export class Compiler_ extends Compiler {
constructor(private _protoViewFactory: ProtoViewFactory) { super(); }
compileInHost(componentType: Type): Promise<ProtoViewRef> {
var metadatas = reflector.annotations(componentType);
var compiledHostTemplate = null;
for (var i = 0; i < metadatas.length; i++) {
var metadata = metadatas[i];
if (metadata instanceof CompiledHostTemplate) {
compiledHostTemplate = metadata;
break;
}
}
var compiledHostTemplate = ListWrapper.find(metadatas, _isCompiledHostTemplate);
if (isBlank(compiledHostTemplate)) {
throw new BaseException(
`No precompiled template for component ${stringify(componentType)} found`);

View File

@ -16,6 +16,10 @@ import {
} from 'angular2/src/core/metadata';
import {reflector} from 'angular2/src/core/reflection/reflection';
function _isDirectiveMetadata(type: any): boolean {
return type instanceof DirectiveMetadata;
}
/*
* Resolve a `Type` for {@link DirectiveMetadata}.
*
@ -31,14 +35,13 @@ export class DirectiveResolver {
resolve(type: Type): DirectiveMetadata {
var typeMetadata = reflector.annotations(resolveForwardRef(type));
if (isPresent(typeMetadata)) {
for (var i = 0; i < typeMetadata.length; i++) {
var metadata = typeMetadata[i];
if (metadata instanceof DirectiveMetadata) {
var propertyMetadata = reflector.propMetadata(type);
return this._mergeWithPropertyMetadata(metadata, propertyMetadata);
}
var metadata = ListWrapper.find(typeMetadata, _isDirectiveMetadata);
if (isPresent(metadata)) {
var propertyMetadata = reflector.propMetadata(type);
return this._mergeWithPropertyMetadata(metadata, propertyMetadata);
}
}
throw new BaseException(`No Directive annotation found on ${stringify(type)}`);
}

View File

@ -1,9 +1,14 @@
import {resolveForwardRef, Injectable} from 'angular2/src/core/di';
import {Type, isPresent, stringify} from 'angular2/src/core/facade/lang';
import {ListWrapper} from 'angular2/src/core/facade/collection';
import {BaseException} from 'angular2/src/core/facade/exceptions';
import {PipeMetadata} from 'angular2/src/core/metadata';
import {reflector} from 'angular2/src/core/reflection/reflection';
function _isPipeMetadata(type: any): boolean {
return type instanceof PipeMetadata;
}
/**
* Resolve a `Type` for {@link PipeMetadata}.
*
@ -19,11 +24,9 @@ export class PipeResolver {
resolve(type: Type): PipeMetadata {
var metas = reflector.annotations(resolveForwardRef(type));
if (isPresent(metas)) {
for (var i = 0; i < metas.length; i++) {
var annotation = metas[i];
if (annotation instanceof PipeMetadata) {
return annotation;
}
var annotation = ListWrapper.find(metas, _isPipeMetadata);
if (isPresent(annotation)) {
return annotation;
}
}
throw new BaseException(`No Pipe decorator found on ${stringify(type)}`);