From 8159c664ecea6c96dd59e1eb6d0222c91eaa979f Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 27 Nov 2024 15:36:00 +0800 Subject: [PATCH] refactor: adjust destroy clean-up logic (#1853) * refactor: adjust destroy clean-up logic * chore: commit changeset --- .changeset/empty-falcons-occur.md | 5 +++++ __tests__/main.ts | 2 +- packages/g-lite/src/Canvas.ts | 12 +++++------- 3 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 .changeset/empty-falcons-occur.md diff --git a/.changeset/empty-falcons-occur.md b/.changeset/empty-falcons-occur.md new file mode 100644 index 000000000..0f8bf3fef --- /dev/null +++ b/.changeset/empty-falcons-occur.md @@ -0,0 +1,5 @@ +--- +'@antv/g-lite': patch +--- + +refactor: adjust destroy clean-up logic diff --git a/__tests__/main.ts b/__tests__/main.ts index a318549c7..01ca9f768 100644 --- a/__tests__/main.ts +++ b/__tests__/main.ts @@ -155,7 +155,7 @@ plot(); async function plot() { if (currentContainer) { currentContainer.remove(); - if (canvas) canvas.destroy(); + if (canvas) canvas.destroy(false); if (prevAfter) prevAfter(); } currentContainer = document.createElement('div'); diff --git a/packages/g-lite/src/Canvas.ts b/packages/g-lite/src/Canvas.ts index 0630c2a47..947378806 100644 --- a/packages/g-lite/src/Canvas.ts +++ b/packages/g-lite/src/Canvas.ts @@ -394,12 +394,10 @@ export class Canvas extends EventTarget implements ICanvas { } /** - * `cleanUp` means clean all the internal services of Canvas which happens when calling `canvas.destroy()`. + * @param cleanUp - whether to clean up all the internal services of Canvas + * @param skipTriggerEvent - whether to skip trigger destroy event */ destroy(cleanUp = true, skipTriggerEvent?: boolean) { - if (skipTriggerEvent === undefined) - skipTriggerEvent = this.getConfig().fastCleanExistingCanvas; - if (!skipTriggerEvent) { this.dispatchEvent(new CustomEvent(CanvasEvent.BEFORE_DESTROY)); } @@ -409,9 +407,9 @@ export class Canvas extends EventTarget implements ICanvas { // unmount all children const root = this.getRoot(); - this.unmountChildren(root); if (cleanUp) { + this.unmountChildren(root); // destroy Document this.document.destroy(); this.getEventService().destroy(); @@ -421,8 +419,8 @@ export class Canvas extends EventTarget implements ICanvas { this.getRenderingService().destroy(); this.getContextService().destroy(); - // clear root after renderservice destroyed - if (cleanUp && this.context.rBushRoot) { + // clear root after render service destroyed + if (this.context.rBushRoot) { // clear rbush this.context.rBushRoot.clear(); this.context.rBushRoot = null;