From 63261258cfbe28ee7e664106b23db798cd523c68 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 25 Apr 2024 19:10:18 +0800 Subject: [PATCH] refactor: emit graph destroy event (#5680) --- .vscode/settings.json | 2 ++ packages/g6/src/constants/events/graph.ts | 4 ++++ packages/g6/src/runtime/graph.ts | 4 ++++ packages/g6/src/utils/event/events.ts | 4 +++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index caaff0a3f57..d1db4d757d6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "AABB", "afteranimate", "aftercanvasinit", + "afterdestroy", "afterdraw", "afterelementcreate", "afterelementdestroy", @@ -20,6 +21,7 @@ "bbox", "beforeanimate", "beforecanvasinit", + "beforedestroy", "beforedraw", "beforeelementcreate", "beforeelementdestroy", diff --git a/packages/g6/src/constants/events/graph.ts b/packages/g6/src/constants/events/graph.ts index cb3bad35379..bc633d08a7f 100644 --- a/packages/g6/src/constants/events/graph.ts +++ b/packages/g6/src/constants/events/graph.ts @@ -51,4 +51,8 @@ export enum GraphEvent { BATCH_START = 'batchstart', /** 批处理结束 | Batch processing ends */ BATCH_END = 'batchend', + /** 销毁开始之前 | Before destruction */ + BEFORE_DESTROY = 'beforedestroy', + /** 销毁结束之后 | After destruction */ + AFTER_DESTROY = 'afterdestroy', } diff --git a/packages/g6/src/runtime/graph.ts b/packages/g6/src/runtime/graph.ts index 151315efb5d..67682753f12 100644 --- a/packages/g6/src/runtime/graph.ts +++ b/packages/g6/src/runtime/graph.ts @@ -929,6 +929,8 @@ export class Graph extends EventEmitter { * @apiCategory instance */ public destroy(): void { + emit(this, new GraphLifeCycleEvent(GraphEvent.BEFORE_DESTROY)); + const { layout, element, model, canvas, behavior, plugin } = this.context; plugin?.destroy(); behavior?.destroy(); @@ -944,6 +946,8 @@ export class Graph extends EventEmitter { window.removeEventListener('resize', this.onResize); this.destroyed = true; + + emit(this, new GraphLifeCycleEvent(GraphEvent.AFTER_DESTROY)); } /** diff --git a/packages/g6/src/utils/event/events.ts b/packages/g6/src/utils/event/events.ts index 29361a232d6..7b921581768 100644 --- a/packages/g6/src/utils/event/events.ts +++ b/packages/g6/src/utils/event/events.ts @@ -26,7 +26,9 @@ export class GraphLifeCycleEvent extends BaseEvent implements IGraphLifeCycleEve | GraphEvent.BEFORE_SIZE_CHANGE | GraphEvent.AFTER_SIZE_CHANGE | GraphEvent.BATCH_START - | GraphEvent.BATCH_END, + | GraphEvent.BATCH_END + | GraphEvent.BEFORE_DESTROY + | GraphEvent.AFTER_DESTROY, public data?: any, ) { super(type);