Skip to content

Commit

Permalink
refactor: emit graph destroy event (#5680)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca authored Apr 25, 2024
1 parent 5028413 commit 6326125
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"AABB",
"afteranimate",
"aftercanvasinit",
"afterdestroy",
"afterdraw",
"afterelementcreate",
"afterelementdestroy",
Expand All @@ -20,6 +21,7 @@
"bbox",
"beforeanimate",
"beforecanvasinit",
"beforedestroy",
"beforedraw",
"beforeelementcreate",
"beforeelementdestroy",
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/src/constants/events/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ export enum GraphEvent {
BATCH_START = 'batchstart',
/** <zh/> 批处理结束 | <en/> Batch processing ends */
BATCH_END = 'batchend',
/** <zh/> 销毁开始之前 | <en/> Before destruction */
BEFORE_DESTROY = 'beforedestroy',
/** <zh/> 销毁结束之后 | <en/> After destruction */
AFTER_DESTROY = 'afterdestroy',
}
4 changes: 4 additions & 0 deletions packages/g6/src/runtime/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -944,6 +946,8 @@ export class Graph extends EventEmitter {
window.removeEventListener('resize', this.onResize);

this.destroyed = true;

emit(this, new GraphLifeCycleEvent(GraphEvent.AFTER_DESTROY));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/g6/src/utils/event/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6326125

Please sign in to comment.