feat(compiler): support sync runtime compile

Adds new abstraction `Compiler` with methods
`compileComponentAsync` and `compileComponentSync`.
This is in preparation of deprecating `ComponentResolver`.

`compileComponentSync` is able to compile components
synchronously given all components either have an inline
template or they have been compiled before.

Also changes `TestComponentBuilder.createSync` to
take a `Type` and use the new `compileComponentSync` method.

Also supports overriding the component metadata even if
the component has already been compiled.

Also fixes #7084 in a better way.

BREAKING CHANGE:
`TestComponentBuilder.createSync` now takes a component type
and throws if not all templates are either inlined
are compiled before via `createAsync`.

Closes #9594
This commit is contained in:
Tobias Bosch
2016-06-24 08:46:43 -07:00
parent 24eb8389d2
commit bf598d6b8b
35 changed files with 1093 additions and 700 deletions

View File

@ -7,6 +7,7 @@
*/
// Public API for compiler
export {Compiler} from './linker/compiler';
export {ComponentFactory, ComponentRef} from './linker/component_factory';
export {ComponentFactoryResolver, NoComponentFactoryError} from './linker/component_factory_resolver';
export {ComponentResolver} from './linker/component_resolver';

View File

@ -0,0 +1,45 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {BaseException} from '../facade/exceptions';
import {ConcreteType, Type, stringify} from '../facade/lang';
import {ComponentFactory} from './component_factory';
/**
* Low-level service for running the angular compiler duirng runtime
* to create {@link ComponentFactory}s, which
* can later be used to create and render a Component instance.
* @stable
*/
export class Compiler {
/**
* Loads the template and styles of a component and returns the associated `ComponentFactory`.
*/
compileComponentAsync<T>(component: ConcreteType<T>): Promise<ComponentFactory<T>> {
throw new BaseException(
`Runtime compiler is not loaded. Tried to compile ${stringify(component)}`);
}
/**
* Compiles the given component. All templates have to be either inline or compiled via
* `compileComponentAsync` before.
*/
compileComponentSync<T>(component: ConcreteType<T>): ComponentFactory<T> {
throw new BaseException(
`Runtime compiler is not loaded. Tried to compile ${stringify(component)}`);
}
/**
* Clears all caches
*/
clearCache(): void {}
/**
* Clears the cache for the given component.
*/
clearCacheFor(compType: Type) {}
}

View File

@ -8,7 +8,7 @@
import {Inject, OpaqueToken, Optional, SkipSelf} from '../di';
import {BaseException} from '../facade/exceptions';
import {ClassWithConstructor, stringify} from '../facade/lang';
import {ConcreteType, stringify} from '../facade/lang';
import {ComponentFactory} from './component_factory';
@ -33,7 +33,7 @@ class _NullComponentFactoryResolver implements ComponentFactoryResolver {
*/
export abstract class ComponentFactoryResolver {
static NULL: ComponentFactoryResolver = new _NullComponentFactoryResolver();
abstract resolveComponentFactory<T>(component: ClassWithConstructor<T>): ComponentFactory<T>;
abstract resolveComponentFactory<T>(component: ConcreteType<T>): ComponentFactory<T>;
}
export class CodegenComponentFactoryResolver implements ComponentFactoryResolver {

View File

@ -19,7 +19,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
isReflectionEnabled(): boolean { return true; }
factory(t: ConcreteType): Function {
factory(t: ConcreteType<any>): Function {
switch (t.length) {
case 0:
return () => new t();

View File

@ -84,7 +84,7 @@ export interface TypeDecorator {
/**
* Generate a class from the definition and annotate it with {@link TypeDecorator#annotations}.
*/
Class(obj: ClassDefinition): ConcreteType;
Class(obj: ClassDefinition): ConcreteType<any>;
}
function extractAnnotation(annotation: any): any {
@ -219,7 +219,7 @@ function applyParams(fnOrArray: (Function | any[]), key: string): Function {
* ```
* @stable
*/
export function Class(clsDef: ClassDefinition): ConcreteType {
export function Class(clsDef: ClassDefinition): ConcreteType<any> {
var constructor = applyParams(
clsDef.hasOwnProperty('constructor') ? clsDef.constructor : undefined, 'constructor');
var proto = constructor.prototype;
@ -246,7 +246,7 @@ export function Class(clsDef: ClassDefinition): ConcreteType {
(constructor as any /** TODO #9100 */)['overriddenName'] = `class${_nextClassId++}`;
}
return <ConcreteType>constructor;
return <ConcreteType<any>>constructor;
}
var Reflect = global.Reflect;