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

View File

@ -16,6 +16,10 @@ import {
} from 'angular2/src/core/metadata'; } from 'angular2/src/core/metadata';
import {reflector} from 'angular2/src/core/reflection/reflection'; import {reflector} from 'angular2/src/core/reflection/reflection';
function _isDirectiveMetadata(type: any): boolean {
return type instanceof DirectiveMetadata;
}
/* /*
* Resolve a `Type` for {@link DirectiveMetadata}. * Resolve a `Type` for {@link DirectiveMetadata}.
* *
@ -31,14 +35,13 @@ export class DirectiveResolver {
resolve(type: Type): DirectiveMetadata { resolve(type: Type): DirectiveMetadata {
var typeMetadata = reflector.annotations(resolveForwardRef(type)); var typeMetadata = reflector.annotations(resolveForwardRef(type));
if (isPresent(typeMetadata)) { if (isPresent(typeMetadata)) {
for (var i = 0; i < typeMetadata.length; i++) { var metadata = ListWrapper.find(typeMetadata, _isDirectiveMetadata);
var metadata = typeMetadata[i]; if (isPresent(metadata)) {
if (metadata instanceof DirectiveMetadata) { var propertyMetadata = reflector.propMetadata(type);
var propertyMetadata = reflector.propMetadata(type); return this._mergeWithPropertyMetadata(metadata, propertyMetadata);
return this._mergeWithPropertyMetadata(metadata, propertyMetadata);
}
} }
} }
throw new BaseException(`No Directive annotation found on ${stringify(type)}`); 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 {resolveForwardRef, Injectable} from 'angular2/src/core/di';
import {Type, isPresent, stringify} from 'angular2/src/core/facade/lang'; 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 {BaseException} from 'angular2/src/core/facade/exceptions';
import {PipeMetadata} from 'angular2/src/core/metadata'; import {PipeMetadata} from 'angular2/src/core/metadata';
import {reflector} from 'angular2/src/core/reflection/reflection'; import {reflector} from 'angular2/src/core/reflection/reflection';
function _isPipeMetadata(type: any): boolean {
return type instanceof PipeMetadata;
}
/** /**
* Resolve a `Type` for {@link PipeMetadata}. * Resolve a `Type` for {@link PipeMetadata}.
* *
@ -19,11 +24,9 @@ export class PipeResolver {
resolve(type: Type): PipeMetadata { resolve(type: Type): PipeMetadata {
var metas = reflector.annotations(resolveForwardRef(type)); var metas = reflector.annotations(resolveForwardRef(type));
if (isPresent(metas)) { if (isPresent(metas)) {
for (var i = 0; i < metas.length; i++) { var annotation = ListWrapper.find(metas, _isPipeMetadata);
var annotation = metas[i]; if (isPresent(annotation)) {
if (annotation instanceof PipeMetadata) { return annotation;
return annotation;
}
} }
} }
throw new BaseException(`No Pipe decorator found on ${stringify(type)}`); throw new BaseException(`No Pipe decorator found on ${stringify(type)}`);