From 2dfc3e5245f96a3b8486a8e6641c5685ac85e012 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Thu, 5 Dec 2024 23:51:30 -0800 Subject: [PATCH 1/4] Fix memory leak in component renderer --- mesop/version.py | 2 +- .../src/component_renderer/component_renderer.ts | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/mesop/version.py b/mesop/version.py index 73048b9d..e227ea53 100644 --- a/mesop/version.py +++ b/mesop/version.py @@ -1,6 +1,6 @@ """Contains the version string.""" -VERSION = "0.12.9" +VERSION = "0.12.10beta5" if __name__ == "__main__": print(VERSION) diff --git a/mesop/web/src/component_renderer/component_renderer.ts b/mesop/web/src/component_renderer/component_renderer.ts index 503286ff..d7313de4 100644 --- a/mesop/web/src/component_renderer/component_renderer.ts +++ b/mesop/web/src/component_renderer/component_renderer.ts @@ -3,6 +3,7 @@ import { Component, ComponentRef, ElementRef, + EmbeddedViewRef, HostListener, Input, TemplateRef, @@ -51,6 +52,7 @@ export class ComponentRenderer { private _boxType: BoxType | undefined; private _componentRef!: ComponentRef; customElement: HTMLElement | undefined; + projectedViewRef: EmbeddedViewRef | undefined; constructor( private channel: Channel, @@ -65,6 +67,13 @@ export class ComponentRenderer { MESOP_EVENT_NAME, this.dispatchCustomUserEvent, ); + if (this.customElement.parentNode) { + this.customElement.parentNode.removeChild(this.customElement); + } + } + if (this.projectedViewRef) { + this.applicationRef.detachView(this.projectedViewRef); + this.projectedViewRef.destroy(); } } @@ -206,14 +215,14 @@ export class ComponentRenderer { const typeName = this.component.getType()?.getName()!; let options = {}; if (this.component.getChildrenList().length) { - const projectedViewRef = this.childrenTemplate.createEmbeddedView(this); + this.projectedViewRef = this.childrenTemplate.createEmbeddedView(this); // Need to attach view or it doesn't render. // View automatically detaches when it is destroyed. // Template will destroy each ViewRef when it is destroyed. const index = this.component.getType()?.getTypeIndex() ?? 0; - this.applicationRef.attachView(projectedViewRef); + this.applicationRef.attachView(this.projectedViewRef); const projectableNodes = []; - projectableNodes[index] = projectedViewRef.rootNodes; + projectableNodes[index] = this.projectedViewRef.rootNodes; options = { projectableNodes, }; From b08544b7d00cc3b6c6bceb7715c761db862557c8 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Fri, 6 Dec 2024 00:13:10 -0800 Subject: [PATCH 2/4] fix --- mesop/web/src/component_renderer/component_renderer.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/mesop/web/src/component_renderer/component_renderer.ts b/mesop/web/src/component_renderer/component_renderer.ts index d7313de4..9601ace4 100644 --- a/mesop/web/src/component_renderer/component_renderer.ts +++ b/mesop/web/src/component_renderer/component_renderer.ts @@ -67,9 +67,6 @@ export class ComponentRenderer { MESOP_EVENT_NAME, this.dispatchCustomUserEvent, ); - if (this.customElement.parentNode) { - this.customElement.parentNode.removeChild(this.customElement); - } } if (this.projectedViewRef) { this.applicationRef.detachView(this.projectedViewRef); @@ -217,10 +214,10 @@ export class ComponentRenderer { if (this.component.getChildrenList().length) { this.projectedViewRef = this.childrenTemplate.createEmbeddedView(this); // Need to attach view or it doesn't render. - // View automatically detaches when it is destroyed. - // Template will destroy each ViewRef when it is destroyed. - const index = this.component.getType()?.getTypeIndex() ?? 0; + // ApplicationRef will automatically detach the view + // when the view ref is destroyed. this.applicationRef.attachView(this.projectedViewRef); + const index = this.component.getType()?.getTypeIndex() ?? 0; const projectableNodes = []; projectableNodes[index] = this.projectedViewRef.rootNodes; options = { From a7c834c745ffa3559451b9fedf7644b22a0d118c Mon Sep 17 00:00:00 2001 From: Will Chen Date: Fri, 6 Dec 2024 00:15:50 -0800 Subject: [PATCH 3/4] no detach --- mesop/web/src/component_renderer/component_renderer.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/mesop/web/src/component_renderer/component_renderer.ts b/mesop/web/src/component_renderer/component_renderer.ts index 9601ace4..613bc21a 100644 --- a/mesop/web/src/component_renderer/component_renderer.ts +++ b/mesop/web/src/component_renderer/component_renderer.ts @@ -69,7 +69,6 @@ export class ComponentRenderer { ); } if (this.projectedViewRef) { - this.applicationRef.detachView(this.projectedViewRef); this.projectedViewRef.destroy(); } } From 7497fcdd0bd05bab5e95085e6bf64a54a7118cf6 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Fri, 6 Dec 2024 13:11:54 -0800 Subject: [PATCH 4/4] revert --- mesop/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesop/version.py b/mesop/version.py index e227ea53..73048b9d 100644 --- a/mesop/version.py +++ b/mesop/version.py @@ -1,6 +1,6 @@ """Contains the version string.""" -VERSION = "0.12.10beta5" +VERSION = "0.12.9" if __name__ == "__main__": print(VERSION)