refactor(compiler): make `view.disposable` array null if empty

This commit is contained in:
Tobias Bosch 2016-10-31 15:38:15 -07:00 committed by vsavkin
parent bda1909ede
commit f6710fefeb
3 changed files with 11 additions and 4 deletions

View File

@ -573,7 +573,7 @@ function generateCreateMethod(view: CompileView): o.Statement[] {
[
view.lastRenderNode,
o.literalArr(view.nodes.map(node => node.renderNode)),
o.literalArr(view.disposables),
view.disposables.length ? o.literalArr(view.disposables) : o.NULL_EXPR,
])
.toStmt(),
new o.ReturnStatement(resultExpr)

View File

@ -120,8 +120,10 @@ export abstract class AppView<T> {
}
var hostElement =
this.type === ViewType.COMPONENT ? this.declarationAppElement.nativeElement : null;
for (var i = 0; i < this.disposables.length; i++) {
this.disposables[i]();
if (this.disposables) {
for (var i = 0; i < this.disposables.length; i++) {
this.disposables[i]();
}
}
this.destroyInternal();
this.dirtyParentQueriesInternal();

View File

@ -117,7 +117,12 @@ export class ViewRef_<C> implements EmbeddedViewRef<C>, ChangeDetectorRef {
this.markForCheck();
}
onDestroy(callback: Function) { this._view.disposables.push(callback); }
onDestroy(callback: Function) {
if (!this._view.disposables) {
this._view.disposables = [];
}
this._view.disposables.push(callback);
}
destroy() { this._view.detachAndDestroy(); }
}