refactor(compiler): remove view factories, use view classes directly

This commit is contained in:
Tobias Bosch
2016-11-02 08:36:23 -07:00
committed by Vikram Subramanian
parent 7c5cc9bc41
commit 0e3d655220
12 changed files with 64 additions and 112 deletions

View File

@ -21,7 +21,7 @@ import {CompileMethod} from './compile_method';
import {CompileQuery, addQueryToTokenMap, createQueryList} from './compile_query';
import {CompileView, CompileViewRootNode} from './compile_view';
import {InjectMethodVars, ViewProperties} from './constants';
import {ComponentFactoryDependency, DirectiveWrapperDependency, ViewFactoryDependency} from './deps';
import {ComponentFactoryDependency, DirectiveWrapperDependency, ViewClassDependency} from './deps';
import {getPropertyInView, injectFromViewParentInjector} from './util';
export class CompileNode {
@ -60,7 +60,7 @@ export class CompileElement extends CompileNode {
private _resolvedProvidersArray: ProviderAst[], public hasViewContainer: boolean,
public hasEmbeddedView: boolean, references: ReferenceAst[],
private _targetDependencies:
Array<ViewFactoryDependency|ComponentFactoryDependency|DirectiveWrapperDependency>) {
Array<ViewClassDependency|ComponentFactoryDependency|DirectiveWrapperDependency>) {
super(parent, view, nodeIndex, renderNode, sourceAst);
this.referenceTokens = {};
references.forEach(ref => this.referenceTokens[ref.name] = ref.value);

View File

@ -21,7 +21,7 @@ import {CompileElement, CompileNode} from './compile_element';
import {CompileMethod} from './compile_method';
import {CompilePipe} from './compile_pipe';
import {CompileQuery, addQueryToTokenMap, createQueryList} from './compile_query';
import {getPropertyInView, getViewFactoryName} from './util';
import {getPropertyInView, getViewClassName} from './util';
export enum CompileViewRootNodeType {
Node,
@ -73,7 +73,7 @@ export class CompileView implements NameResolver {
public locals = new Map<string, o.Expression>();
public className: string;
public classType: o.Type;
public viewFactory: o.ReadVarExpr;
public classExpr: o.ReadVarExpr;
public literalArrayCount = 0;
public literalMapCount = 0;
@ -101,9 +101,9 @@ export class CompileView implements NameResolver {
this.detachMethod = new CompileMethod(this);
this.viewType = getViewType(component, viewIndex);
this.className = `_View_${component.type.name}${viewIndex}`;
this.className = getViewClassName(component, viewIndex);
this.classType = o.importType(new CompileIdentifierMetadata({name: this.className}));
this.viewFactory = o.variable(getViewFactoryName(component, viewIndex));
this.classExpr = o.variable(this.className);
if (this.viewType === ViewType.COMPONENT || this.viewType === ViewType.HOST) {
this.componentView = this;
} else {

View File

@ -8,7 +8,7 @@
import {CompileIdentifierMetadata} from '../compile_metadata';
export class ViewFactoryDependency {
export class ViewClassDependency {
constructor(
public comp: CompileIdentifierMetadata, public placeholder: CompileIdentifierMetadata) {}
}

View File

@ -71,9 +71,9 @@ export function injectFromViewParentInjector(
return viewExpr.callMethod('injectorGet', args);
}
export function getViewFactoryName(
export function getViewClassName(
component: CompileDirectiveMetadata, embeddedTemplateIndex: number): string {
return `viewFactory_${component.type.name}${embeddedTemplateIndex}`;
return `View_${component.type.name}${embeddedTemplateIndex}`;
}
export function getHandleEventMethodName(elementIndex: number): string {

View File

@ -22,8 +22,8 @@ import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventA
import {CompileElement, CompileNode} from './compile_element';
import {CompileView, CompileViewRootNode, CompileViewRootNodeType} from './compile_view';
import {ChangeDetectorStatusEnum, DetectChangesVars, InjectMethodVars, ViewConstructorVars, ViewEncapsulationEnum, ViewProperties, ViewTypeEnum} from './constants';
import {ComponentFactoryDependency, DirectiveWrapperDependency, ViewFactoryDependency} from './deps';
import {getViewFactoryName} from './util';
import {ComponentFactoryDependency, DirectiveWrapperDependency, ViewClassDependency} from './deps';
import {getViewClassName} from './util';
const IMPLICIT_TEMPLATE_VAR = '\$implicit';
const CLASS_ATTR = 'class';
@ -36,8 +36,7 @@ var rootSelectorVar = o.variable('rootSelector');
export function buildView(
view: CompileView, template: TemplateAst[],
targetDependencies:
Array<ViewFactoryDependency|ComponentFactoryDependency|DirectiveWrapperDependency>):
number {
Array<ViewClassDependency|ComponentFactoryDependency|DirectiveWrapperDependency>): number {
var builderVisitor = new ViewBuilderVisitor(view, targetDependencies);
const parentEl =
view.declarationElement.isNull() ? view.declarationElement : view.declarationElement.parent;
@ -64,7 +63,7 @@ class ViewBuilderVisitor implements TemplateAstVisitor {
constructor(
public view: CompileView,
public targetDependencies:
Array<ViewFactoryDependency|ComponentFactoryDependency|DirectiveWrapperDependency>) {}
Array<ViewClassDependency|ComponentFactoryDependency|DirectiveWrapperDependency>) {}
private _isRootNode(parent: CompileElement): boolean { return parent.view !== this.view; }
@ -222,9 +221,9 @@ class ViewBuilderVisitor implements TemplateAstVisitor {
var compViewExpr: o.ReadPropExpr = null;
if (isPresent(component)) {
let nestedComponentIdentifier =
new CompileIdentifierMetadata({name: getViewFactoryName(component, 0)});
new CompileIdentifierMetadata({name: getViewClassName(component, 0)});
this.targetDependencies.push(
new ViewFactoryDependency(component.type, nestedComponentIdentifier));
new ViewClassDependency(component.type, nestedComponentIdentifier));
compViewExpr = o.THIS_EXPR.prop(`compView_${nodeIndex}`); // fix highlighting: `
this.view.fields.push(new o.ClassField(
@ -234,7 +233,7 @@ class ViewBuilderVisitor implements TemplateAstVisitor {
compileElement.setComponentView(compViewExpr);
this.view.createMethod.addStmt(
compViewExpr
.set(o.importExpr(nestedComponentIdentifier).callFn([
.set(o.importExpr(nestedComponentIdentifier).instantiate([
ViewProperties.viewUtils, o.THIS_EXPR, o.literal(nodeIndex), renderNode
]))
.toStmt());
@ -416,7 +415,6 @@ function createViewTopLevelStmts(view: CompileView, targetStatements: o.Statemen
var viewClass = createViewClass(view, renderCompTypeVar, nodeDebugInfosVar);
targetStatements.push(viewClass);
targetStatements.push(createViewFactory(view, viewClass));
}
function createStaticNodeDebugInfo(node: CompileNode): o.Expression {
@ -514,27 +512,6 @@ function generateDestroyMethod(view: CompileView): o.Statement[] {
return stmts;
}
function createViewFactory(view: CompileView, viewClass: o.ClassStmt): o.Statement {
var viewFactoryArgs = [
new o.FnParam(
ViewConstructorVars.viewUtils.name, o.importType(resolveIdentifier(Identifiers.ViewUtils))),
new o.FnParam(
ViewConstructorVars.parentView.name,
o.importType(resolveIdentifier(Identifiers.AppView), [o.DYNAMIC_TYPE])),
new o.FnParam(ViewConstructorVars.parentIndex.name, o.NUMBER_TYPE),
new o.FnParam(ViewConstructorVars.parentElement.name, o.DYNAMIC_TYPE)
];
return o
.fn(viewFactoryArgs,
[
new o.ReturnStatement(o.variable(viewClass.name)
.instantiate(viewClass.constructorMethod.params.map(
(param) => o.variable(param.name)))),
],
o.importType(resolveIdentifier(Identifiers.AppView), [getContextType(view)]))
.toDeclStmt(view.viewFactory.name, [o.StmtModifier.Final]);
}
function generateCreateMethod(view: CompileView): o.Statement[] {
var parentRenderNodeExpr: o.Expression = o.NULL_EXPR;
var parentRenderNodeStmts: any[] = [];
@ -709,7 +686,7 @@ function generateCreateEmbeddedViewsMethod(view: CompileView) {
const parentNodeIndex = node.isRootElement() ? null : node.parent.nodeIndex;
stmts.push(new o.IfStmt(
nodeIndexVar.equals(o.literal(node.nodeIndex)),
[new o.ReturnStatement(node.embeddedView.viewFactory.callFn([
[new o.ReturnStatement(node.embeddedView.classExpr.instantiate([
ViewProperties.viewUtils, o.THIS_EXPR, o.literal(node.nodeIndex), node.renderNode
]))]));
}

View File

@ -17,17 +17,17 @@ import {TemplateAst} from '../template_parser/template_ast';
import {CompileElement} from './compile_element';
import {CompileView} from './compile_view';
import {ComponentFactoryDependency, DirectiveWrapperDependency, ViewFactoryDependency} from './deps';
import {ComponentFactoryDependency, DirectiveWrapperDependency, ViewClassDependency} from './deps';
import {bindView} from './view_binder';
import {buildView, finishView} from './view_builder';
export {ComponentFactoryDependency, DirectiveWrapperDependency, ViewFactoryDependency} from './deps';
export {ComponentFactoryDependency, DirectiveWrapperDependency, ViewClassDependency} from './deps';
export class ViewCompileResult {
constructor(
public statements: o.Statement[], public viewFactoryVar: string,
public statements: o.Statement[], public viewClassVar: string,
public dependencies:
Array<ViewFactoryDependency|ComponentFactoryDependency|DirectiveWrapperDependency>) {}
Array<ViewClassDependency|ComponentFactoryDependency|DirectiveWrapperDependency>) {}
}
@Injectable()
@ -39,7 +39,7 @@ export class ViewCompiler {
pipes: CompilePipeMetadata[],
compiledAnimations: AnimationEntryCompileResult[]): ViewCompileResult {
const dependencies:
Array<ViewFactoryDependency|ComponentFactoryDependency|DirectiveWrapperDependency> = [];
Array<ViewClassDependency|ComponentFactoryDependency|DirectiveWrapperDependency> = [];
const view = new CompileView(
component, this._genConfig, pipes, styles, compiledAnimations, 0,
CompileElement.createNull(), []);
@ -51,6 +51,6 @@ export class ViewCompiler {
bindView(view, template, this._schemaRegistry);
finishView(view, statements);
return new ViewCompileResult(statements, view.viewFactory.name, dependencies);
return new ViewCompileResult(statements, view.classExpr.name, dependencies);
}
}