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.appElement = o.THIS_EXPR.prop(fieldName);
this.instances.set(resolveIdentifierToken(Identifiers.AppElement).reference, this.appElement);
if (this.hasViewContainer) {
this.view.viewContainerAppElements.push(this.appElement);
}
}
private _createComponentFactoryResolver() {

View File

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

View File

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

View File

@ -46,6 +46,22 @@ export class AppElement {
get parentInjector(): Injector { return this.parentView.injector(this.parentIndex); }
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[] {
var result: any[] /** TODO #9100 */ = [];
if (isPresent(this.nestedViews)) {

View File

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