Skip to content

Commit

Permalink
Fix memory leak in component renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen committed Dec 6, 2024
1 parent f6ce2eb commit fa8e631
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mesop/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Contains the version string."""

VERSION = "0.12.9"
VERSION = "0.12.10beta5"

if __name__ == "__main__":
print(VERSION)
15 changes: 12 additions & 3 deletions mesop/web/src/component_renderer/component_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TemplateRef,
ViewChild,
ViewContainerRef,
ViewRef,
} from '@angular/core';
import {CommonModule} from '@angular/common';
import {
Expand Down Expand Up @@ -51,6 +52,7 @@ export class ComponentRenderer {
private _boxType: BoxType | undefined;
private _componentRef!: ComponentRef<BaseComponent>;
customElement: HTMLElement | undefined;
projectedViewRef: ViewRef | undefined;

constructor(
private channel: Channel,
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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,
};
Expand Down

0 comments on commit fa8e631

Please sign in to comment.