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

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {AnimationEntryMetadata, BaseException, Injectable, Type, ViewMetadata, resolveForwardRef} from '@angular/core';
import {AnimationEntryMetadata, BaseException, Compiler, Injectable, Injector, Type, ViewMetadata, resolveForwardRef} from '@angular/core';
import {ViewResolver} from '../index';
import {Map} from '../src/facade/collection';
@ -21,38 +21,38 @@ export class MockViewResolver extends ViewResolver {
/** @internal */
_animations = new Map<Type, AnimationEntryMetadata[]>();
/** @internal */
_viewCache = new Map<Type, ViewMetadata>();
/** @internal */
_directiveOverrides = new Map<Type, Map<Type, Type>>();
constructor() { super(); }
constructor(private _injector: Injector) { super(); }
private get _compiler(): Compiler { return this._injector.get(Compiler); }
private _clearCacheFor(component: Type) { this._compiler.clearCacheFor(component); }
/**
* Overrides the {@link ViewMetadata} for a component.
*/
setView(component: Type, view: ViewMetadata): void {
this._checkOverrideable(component);
this._views.set(component, view);
this._clearCacheFor(component);
}
/**
* Overrides the inline template for a component - other configuration remains unchanged.
*/
setInlineTemplate(component: Type, template: string): void {
this._checkOverrideable(component);
this._inlineTemplates.set(component, template);
this._clearCacheFor(component);
}
setAnimations(component: Type, animations: AnimationEntryMetadata[]): void {
this._checkOverrideable(component);
this._animations.set(component, animations);
this._clearCacheFor(component);
}
/**
* Overrides a directive from the component {@link ViewMetadata}.
*/
overrideViewDirective(component: Type, from: Type, to: Type): void {
this._checkOverrideable(component);
var overrides = this._directiveOverrides.get(component);
if (isBlank(overrides)) {
@ -61,6 +61,7 @@ export class MockViewResolver extends ViewResolver {
}
overrides.set(from, to);
this._clearCacheFor(component);
}
/**
@ -72,10 +73,7 @@ export class MockViewResolver extends ViewResolver {
* - Override the @View definition, see `setInlineTemplate`.
*/
resolve(component: Type): ViewMetadata {
var view = this._viewCache.get(component);
if (isPresent(view)) return view;
view = this._views.get(component);
var view = this._views.get(component);
if (isBlank(view)) {
view = super.resolve(component);
}
@ -123,26 +121,8 @@ export class MockViewResolver extends ViewResolver {
interpolation: view.interpolation
});
this._viewCache.set(component, view);
return view;
}
/**
* @internal
*
* Once a component has been compiled, the AppProtoView is stored in the compiler cache.
*
* Then it should not be possible to override the component configuration after the component
* has been compiled.
*/
_checkOverrideable(component: Type): void {
var cached = this._viewCache.get(component);
if (isPresent(cached)) {
throw new BaseException(
`The component ${stringify(component)} has already been compiled, its configuration can not be changed`);
}
}
}
function flattenArray(tree: any[], out: Array<Type|any[]>): void {