refactor(compiler): inline `view.contentChildren`

This commit is contained in:
Tobias Bosch 2016-10-31 10:31:48 -07:00 committed by vsavkin
parent e5fdf4c70a
commit b3e3cd3add
8 changed files with 38 additions and 36 deletions

View File

@ -98,6 +98,9 @@ export class CompileElement extends CompileNode {
this.view.createMethod.addStmt(statement); this.view.createMethod.addStmt(statement);
this.appElement = o.THIS_EXPR.prop(fieldName); this.appElement = o.THIS_EXPR.prop(fieldName);
this.instances.set(resolveIdentifierToken(Identifiers.AppElement).reference, this.appElement); this.instances.set(resolveIdentifierToken(Identifiers.AppElement).reference, this.appElement);
if (this.hasViewContainer) {
this.view.viewContainerAppElements.push(this.appElement);
}
} }
private _createComponentFactoryResolver() { private _createComponentFactoryResolver() {

View File

@ -32,6 +32,7 @@ export class CompileView implements NameResolver {
public nodes: CompileNode[] = []; public nodes: CompileNode[] = [];
// root nodes or AppElements for ViewContainers // root nodes or AppElements for ViewContainers
public rootNodesOrAppElements: o.Expression[] = []; public rootNodesOrAppElements: o.Expression[] = [];
public viewContainerAppElements: o.Expression[] = [];
public createMethod: CompileMethod; public createMethod: CompileMethod;
public animationBindingsMethod: CompileMethod; public animationBindingsMethod: CompileMethod;

View File

@ -473,6 +473,8 @@ function createViewClass(
function generateDestroyMethod(view: CompileView): o.Statement[] { function generateDestroyMethod(view: CompileView): o.Statement[] {
const stmts: o.Statement[] = []; const stmts: o.Statement[] = [];
view.viewContainerAppElements.forEach(
(appElement) => { stmts.push(appElement.callMethod('destroyNestedViews', []).toStmt()); });
view.viewChildren.forEach( view.viewChildren.forEach(
(viewChild) => { stmts.push(viewChild.callMethod('destroy', []).toStmt()); }); (viewChild) => { stmts.push(viewChild.callMethod('destroy', []).toStmt()); });
stmts.push(...view.destroyMethod.finish()); stmts.push(...view.destroyMethod.finish());
@ -572,9 +574,11 @@ function generateDetectChangesMethod(view: CompileView): o.Statement[] {
} }
stmts.push(...view.animationBindingsMethod.finish()); stmts.push(...view.animationBindingsMethod.finish());
stmts.push(...view.detectChangesInInputsMethod.finish()); stmts.push(...view.detectChangesInInputsMethod.finish());
stmts.push( view.viewContainerAppElements.forEach((appElement) => {
o.THIS_EXPR.callMethod('detectContentChildrenChanges', [DetectChangesVars.throwOnChange]) stmts.push(
.toStmt()); appElement.callMethod('detectChangesInNestedViews', [DetectChangesVars.throwOnChange])
.toStmt());
});
var afterContentStmts = view.updateContentQueriesMethod.finish().concat( var afterContentStmts = view.updateContentQueriesMethod.finish().concat(
view.afterContentLifecycleCallbacksMethod.finish()); view.afterContentLifecycleCallbacksMethod.finish());
if (afterContentStmts.length > 0) { if (afterContentStmts.length > 0) {

View File

@ -75,7 +75,7 @@ export class ComponentRef_<C> extends ComponentRef<C> {
get changeDetectorRef(): ChangeDetectorRef { return this._hostElement.parentView.ref; }; get changeDetectorRef(): ChangeDetectorRef { return this._hostElement.parentView.ref; };
get componentType(): Type<any> { return this._componentType; } get componentType(): Type<any> { return this._componentType; }
destroy(): void { this._hostElement.parentView.destroy(); } destroy(): void { this._hostElement.parentView.detachAndDestroy(); }
onDestroy(callback: Function): void { this.hostView.onDestroy(callback); } onDestroy(callback: Function): void { this.hostView.onDestroy(callback); }
} }

View File

@ -46,6 +46,22 @@ export class AppElement {
get parentInjector(): Injector { return this.parentView.injector(this.parentIndex); } get parentInjector(): Injector { return this.parentView.injector(this.parentIndex); }
get injector(): Injector { return this.parentView.injector(this.index); } get injector(): Injector { return this.parentView.injector(this.index); }
detectChangesInNestedViews(throwOnChange: boolean): void {
if (this.nestedViews) {
for (var i = 0; i < this.nestedViews.length; i++) {
this.nestedViews[i].detectChanges(throwOnChange);
}
}
}
destroyNestedViews(): void {
if (this.nestedViews) {
for (var i = 0; i < this.nestedViews.length; i++) {
this.nestedViews[i].destroy();
}
}
}
mapNestedViews(nestedViewClass: any, callback: Function): any[] { mapNestedViews(nestedViewClass: any, callback: Function): any[] {
var result: any[] /** TODO #9100 */ = []; var result: any[] /** TODO #9100 */ = [];
if (isPresent(this.nestedViews)) { if (isPresent(this.nestedViews)) {

View File

@ -33,7 +33,6 @@ export abstract class AppView<T> {
rootNodesOrAppElements: any[]; rootNodesOrAppElements: any[];
allNodes: any[]; allNodes: any[];
disposables: Function[]; disposables: Function[];
contentChildren: AppView<any>[] = [];
viewContainerElement: AppElement = null; viewContainerElement: AppElement = null;
numberOfChecks: number = 0; numberOfChecks: number = 0;
@ -124,29 +123,19 @@ export abstract class AppView<T> {
} }
} }
destroy() { detachAndDestroy() {
if (this._hasExternalHostElement) { if (this._hasExternalHostElement) {
this.renderer.detachView(this.flatRootNodes); this.renderer.detachView(this.flatRootNodes);
} else if (isPresent(this.viewContainerElement)) { } else if (isPresent(this.viewContainerElement)) {
this.viewContainerElement.detachView(this.viewContainerElement.nestedViews.indexOf(this)); this.viewContainerElement.detachView(this.viewContainerElement.nestedViews.indexOf(this));
} }
this._destroyRecurse(); this.destroy();
} }
private _destroyRecurse() { destroy() {
if (this.cdMode === ChangeDetectorStatus.Destroyed) { if (this.cdMode === ChangeDetectorStatus.Destroyed) {
return; return;
} }
var children = this.contentChildren;
for (var i = 0; i < children.length; i++) {
children[i]._destroyRecurse();
}
this.destroyLocal();
this.cdMode = ChangeDetectorStatus.Destroyed;
}
destroyLocal() {
var hostElement = var hostElement =
this.type === ViewType.COMPONENT ? this.declarationAppElement.nativeElement : null; this.type === ViewType.COMPONENT ? this.declarationAppElement.nativeElement : null;
for (var i = 0; i < this.disposables.length; i++) { for (var i = 0; i < this.disposables.length; i++) {
@ -161,6 +150,8 @@ export abstract class AppView<T> {
} else { } else {
this.renderer.destroyView(hostElement, this.allNodes); this.renderer.destroyView(hostElement, this.allNodes);
} }
this.cdMode = ChangeDetectorStatus.Destroyed;
} }
/** /**
@ -222,28 +213,16 @@ export abstract class AppView<T> {
/** /**
* Overwritten by implementations * Overwritten by implementations
*/ */
detectChangesInternal(throwOnChange: boolean): void { detectChangesInternal(throwOnChange: boolean): void {}
this.detectContentChildrenChanges(throwOnChange);
}
detectContentChildrenChanges(throwOnChange: boolean) {
for (var i = 0; i < this.contentChildren.length; ++i) {
var child = this.contentChildren[i];
if (child.cdMode === ChangeDetectorStatus.Detached) continue;
child.detectChanges(throwOnChange);
}
}
markContentChildAsMoved(renderAppElement: AppElement): void { this.dirtyParentQueriesInternal(); } markContentChildAsMoved(renderAppElement: AppElement): void { this.dirtyParentQueriesInternal(); }
addToContentChildren(renderAppElement: AppElement): void { addToContentChildren(renderAppElement: AppElement): void {
renderAppElement.parentView.contentChildren.push(this);
this.viewContainerElement = renderAppElement; this.viewContainerElement = renderAppElement;
this.dirtyParentQueriesInternal(); this.dirtyParentQueriesInternal();
} }
removeFromContentChildren(renderAppElement: AppElement): void { removeFromContentChildren(renderAppElement: AppElement): void {
ListWrapper.remove(renderAppElement.parentView.contentChildren, this);
this.dirtyParentQueriesInternal(); this.dirtyParentQueriesInternal();
this.viewContainerElement = null; this.viewContainerElement = null;
} }
@ -310,10 +289,10 @@ export class DebugAppView<T> extends AppView<T> {
} }
} }
destroyLocal() { destroy() {
this._resetDebug(); this._resetDebug();
try { try {
super.destroyLocal(); super.destroy();
} catch (e) { } catch (e) {
this._rethrowWithContext(e); this._rethrowWithContext(e);
throw e; throw e;

View File

@ -119,5 +119,5 @@ export class ViewRef_<C> implements EmbeddedViewRef<C>, ChangeDetectorRef {
onDestroy(callback: Function) { this._view.disposables.push(callback); } onDestroy(callback: Function) { this._view.disposables.push(callback); }
destroy() { this._view.destroy(); } destroy() { this._view.detachAndDestroy(); }
} }

View File

@ -113,7 +113,7 @@ class _View_TreeRootComponent0 extends import1.AppView<import3.TreeRootComponent
this._NgIf_0_6.ngIf = currVal_0; this._NgIf_0_6.ngIf = currVal_0;
this._expr_0 = currVal_0; this._expr_0 = currVal_0;
} }
this.detectContentChildrenChanges(throwOnChange); this._appEl_0.detectChangesInNestedViews(throwOnChange);
} }
} }
export function viewFactory_TreeRootComponent0( export function viewFactory_TreeRootComponent0(
@ -145,7 +145,6 @@ class _View_TreeRootComponent1 extends import1.AppView<any> {
destroyInternal() { this._TreeComponent0_0_4View.destroyInternal(); } destroyInternal() { this._TreeComponent0_0_4View.destroyInternal(); }
detectChangesInternal(throwOnChange: boolean): void { detectChangesInternal(throwOnChange: boolean): void {
this._TreeComponent0_0_4View.updateData(this.parent.context.data); this._TreeComponent0_0_4View.updateData(this.parent.context.data);
this.detectContentChildrenChanges(throwOnChange);
this._TreeComponent0_0_4View.detectChangesInternal(throwOnChange); this._TreeComponent0_0_4View.detectChangesInternal(throwOnChange);
} }
} }