From 3e35ee6fe2458025d55b4af4a045c98da4e98516 Mon Sep 17 00:00:00 2001 From: cluezhang Date: Fri, 22 Dec 2023 10:48:10 +0800 Subject: [PATCH] fix: memory leak of graph view dispose(introduce by #4023) --- packages/x6/src/graph/view.ts | 5 +++++ packages/x6/src/view/view.ts | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/x6/src/graph/view.ts b/packages/x6/src/graph/view.ts index 165e233fb0b..1923f1942a5 100644 --- a/packages/x6/src/graph/view.ts +++ b/packages/x6/src/graph/view.ts @@ -18,6 +18,11 @@ export class GraphView extends View { private restore: () => void + /** Graph's `this.container` is from outer, should not dispose */ + protected get disposeContainer(): boolean { + return false + } + protected get options() { return this.graph.options } diff --git a/packages/x6/src/view/view.ts b/packages/x6/src/view/view.ts index 9e942243a7f..d6d255884de 100644 --- a/packages/x6/src/view/view.ts +++ b/packages/x6/src/view/view.ts @@ -13,6 +13,11 @@ export abstract class View extends Basecoat { return 2 } + /** If need remove `this.container` DOM */ + protected get disposeContainer() { + return true + } + constructor() { super() this.cid = Private.uniqueId() @@ -39,8 +44,12 @@ export abstract class View extends Basecoat { this.removeEventListeners(document) this.onRemove() delete View.views[this.cid] + if (this.disposeContainer) { + this.unmount(elem) + } + } else { + this.unmount(elem) } - this.unmount(elem) return this } @@ -363,10 +372,10 @@ export abstract class View extends Basecoat { return View.normalizeEvent(evt) } - // @View.dispose() - // dispose() { - // this.remove() - // } + @View.dispose() + dispose() { + this.remove() + } } export namespace View {