Miško Hevery b96869afd2 refactor(Type): merge Type and ConcreType<?> into Type<?> (#10616)
Closes #9729

BREAKING CHANGE:

`Type` is now `Type<T>` which means that in most cases you have to
use `Type<any>` in place of `Type`.

We don't expect that any user applications use the `Type` type.
2016-08-10 18:21:28 -07:00

137 lines
3.7 KiB
TypeScript

/**
* @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 {OpaqueToken} from '../di';
import {BaseException} from '../facade/exceptions';
import {stringify} from '../facade/lang';
import {ViewEncapsulation} from '../metadata';
import {Type} from '../type';
import {ComponentFactory} from './component_factory';
import {NgModuleFactory} from './ng_module_factory';
/**
* Indicates that a component is still being loaded in a synchronous compile.
*
* @stable
*/
export class ComponentStillLoadingError extends BaseException {
constructor(public compType: Type<any>) {
super(`Can't compile synchronously as ${stringify(compType)} is still being loaded!`);
}
}
/**
* Combination of NgModuleFactory and ComponentFactorys.
*
* @experimental
*/
export class ModuleWithComponentFactories<T> {
constructor(
public ngModuleFactory: NgModuleFactory<T>,
public componentFactories: ComponentFactory<any>[]) {}
}
function _throwError() {
throw new BaseException(`Runtime compiler is not loaded`);
}
/**
* 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.
*
* Each `@NgModule` provides an own `Compiler` to its injector,
* that will use the directives/pipes of the ng module for compilation
* of components.
* @stable
*/
export class Compiler {
/**
* Loads the template and styles of a component and returns the associated `ComponentFactory`.
*/
compileComponentAsync<T>(component: Type<T>, ngModule: Type<any> = null):
Promise<ComponentFactory<T>> {
throw _throwError();
}
/**
* Compiles the given component. All templates have to be either inline or compiled via
* `compileComponentAsync` before. Otherwise throws a {@link ComponentStillLoadingError}.
*/
compileComponentSync<T>(component: Type<T>, ngModule: Type<any> = null): ComponentFactory<T> {
throw _throwError();
}
/**
* Compiles the given NgModule and all of its components. All templates of the components listed
* in `entryComponents`
* have to be inlined. Otherwise throws a {@link ComponentStillLoadingError}.
*/
compileModuleSync<T>(moduleType: Type<T>): NgModuleFactory<T> { throw _throwError(); }
/**
* Compiles the given NgModule and all of its components
*/
compileModuleAsync<T>(moduleType: Type<T>): Promise<NgModuleFactory<T>> { throw _throwError(); }
/**
* Same as {@link compileModuleSync} put also creates ComponentFactories for all components.
*/
compileModuleAndAllComponentsSync<T>(moduleType: Type<T>): ModuleWithComponentFactories<T> {
throw _throwError();
}
/**
* Same as {@link compileModuleAsync} put also creates ComponentFactories for all components.
*/
compileModuleAndAllComponentsAsync<T>(moduleType: Type<T>):
Promise<ModuleWithComponentFactories<T>> {
throw _throwError();
}
/**
* Clears all caches
*/
clearCache(): void {}
/**
* Clears the cache for the given component/ngModule.
*/
clearCacheFor(type: Type<any>) {}
}
/**
* Options for creating a compiler
*
* @experimental
*/
export type CompilerOptions = {
useDebug?: boolean,
useJit?: boolean,
defaultEncapsulation?: ViewEncapsulation,
providers?: any[],
};
/**
* Token to provide CompilerOptions in the platform injector.
*
* @experimental
*/
export const COMPILER_OPTIONS = new OpaqueToken('compilerOptions');
/**
* A factory for creating a Compiler
*
* @experimental
*/
export abstract class CompilerFactory {
abstract createCompiler(options?: CompilerOptions[]): Compiler;
}