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

@ -207,7 +207,7 @@ export declare abstract class ChangeDetectorRef {
}
/** @stable */
export declare function Class(clsDef: ClassDefinition): ConcreteType;
export declare function Class(clsDef: ClassDefinition): ConcreteType<any>;
/** @stable */
export interface ClassDefinition {
@ -226,6 +226,14 @@ export declare class CollectionChangeRecord {
toString(): string;
}
/** @stable */
export declare class Compiler {
clearCache(): void;
clearCacheFor(compType: Type): void;
compileComponentAsync<T>(component: ConcreteType<T>): Promise<ComponentFactory<T>>;
compileComponentSync<T>(component: ConcreteType<T>): ComponentFactory<T>;
}
/** @stable */
export declare var Component: ComponentMetadataFactory;
@ -254,7 +262,7 @@ export declare class ComponentFactory<C> {
/** @stable */
export declare abstract class ComponentFactoryResolver {
abstract resolveComponentFactory<T>(component: ClassWithConstructor<T>): ComponentFactory<T>;
abstract resolveComponentFactory<T>(component: ConcreteType<T>): ComponentFactory<T>;
static NULL: ComponentFactoryResolver;
}
@ -1267,7 +1275,7 @@ export interface TypeDecorator {
annotations: any[];
(target: Object, propertyKey?: string | symbol, parameterIndex?: number): void;
<T extends Type>(type: T): T;
Class(obj: ClassDefinition): ConcreteType;
Class(obj: ClassDefinition): ConcreteType<any>;
}
/** @stable */

View File

@ -89,10 +89,10 @@ export declare function setBaseTestProviders(platformProviders: Array<Type | Pro
export declare class TestComponentBuilder {
protected _injector: Injector;
constructor(_injector: Injector);
createAsync(rootComponentType: Type): Promise<ComponentFixture<any>>;
createFakeAsync(rootComponentType: Type): ComponentFixture<any>;
createAsync<T>(rootComponentType: ConcreteType<T>): Promise<ComponentFixture<T>>;
createFakeAsync<T>(rootComponentType: ConcreteType<T>): ComponentFixture<T>;
protected createFromFactory<C>(ngZone: NgZone, componentFactory: ComponentFactory<C>): ComponentFixture<C>;
/** @deprecated */ createSync<C>(componentFactory: ComponentFactory<C>): ComponentFixture<C>;
createSync<T>(rootComponentType: ConcreteType<T>): ComponentFixture<T>;
overrideAnimations(componentType: Type, animations: AnimationEntryMetadata[]): TestComponentBuilder;
overrideDirective(componentType: Type, from: Type, to: Type): TestComponentBuilder;
overrideProviders(type: Type, providers: any[]): TestComponentBuilder;