53 lines
2.1 KiB
TypeScript
Raw Normal View History

import {ProtoViewRef} from 'angular2/src/core/linker/view_ref';
import {ProtoViewFactory} from 'angular2/src/core/linker/proto_view_factory';
import {Injectable} from 'angular2/src/core/di';
import {Type, isBlank, stringify} from 'angular2/src/facade/lang';
import {BaseException} from 'angular2/src/facade/exceptions';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {reflector} from 'angular2/src/core/reflection/reflection';
import {CompiledHostTemplate} from 'angular2/src/core/linker/template_commands';
2015-05-20 09:48:15 -07:00
/**
* Low-level service for compiling {@link Component}s into {@link ProtoViewRef ProtoViews}s, which
* can later be used to create and render a Component instance.
*
* Most applications should instead use higher-level {@link DynamicComponentLoader} service, which
* both compiles and instantiates a Component.
*/
export abstract class Compiler {
abstract compileInHost(componentType: Type): Promise<ProtoViewRef>;
abstract clearCache();
}
2015-10-22 23:35:53 -07:00
function _isCompiledHostTemplate(type: any): boolean {
return type instanceof CompiledHostTemplate;
}
2015-05-20 09:48:15 -07:00
@Injectable()
export class Compiler_ extends Compiler {
constructor(private _protoViewFactory: ProtoViewFactory) { super(); }
2015-05-20 09:48:15 -07:00
compileInHost(componentType: Type): Promise<ProtoViewRef> {
var metadatas = reflector.annotations(componentType);
var compiledHostTemplate = metadatas.find(_isCompiledHostTemplate);
2015-10-22 23:35:53 -07:00
if (isBlank(compiledHostTemplate)) {
throw new BaseException(
`No precompiled template for component ${stringify(componentType)} found`);
2015-05-20 09:48:15 -07:00
}
return PromiseWrapper.resolve(this._createProtoView(compiledHostTemplate));
2015-05-20 09:48:15 -07:00
}
private _createProtoView(compiledHostTemplate: CompiledHostTemplate): ProtoViewRef {
return this._protoViewFactory.createHost(compiledHostTemplate).ref;
}
2015-05-20 09:48:15 -07:00
clearCache() { this._protoViewFactory.clearCache(); }
2015-05-20 09:48:15 -07:00
}
export function internalCreateProtoView(compiler: Compiler,
compiledHostTemplate: CompiledHostTemplate): ProtoViewRef {
return (<any>compiler)._createProtoView(compiledHostTemplate);
}