Skip to content

Commit 0de6d11

Browse files
authored
feat(GraphComponent): emit event on component unmount (#196)
1 parent fa9c492 commit 0de6d11

File tree

1 file changed

+17
-0
lines changed
  • src/components/canvas/GraphComponent

1 file changed

+17
-0
lines changed

src/components/canvas/GraphComponent/index.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export class GraphComponent<
4343
return this.props.affectsUsableRect ?? this.context.affectsUsableRect ?? true;
4444
}
4545

46+
private mounted = false;
47+
4648
constructor(props: Props, parent: Component) {
4749
super(props, parent);
4850

@@ -165,6 +167,15 @@ export class GraphComponent<
165167
});
166168
}
167169

170+
public isMounted() {
171+
return this.mounted;
172+
}
173+
174+
protected willMount() {
175+
super.willMount();
176+
this.mounted = true;
177+
}
178+
168179
/**
169180
* Subscribes to a graph event and automatically unsubscribes on component unmount.
170181
*
@@ -224,6 +235,10 @@ export class GraphComponent<
224235
this.unsubscribe.push(signal.subscribe(cb));
225236
}
226237

238+
public onUnmounted(cb: () => void) {
239+
return this.addEventListener("graph-component-unmounted", cb);
240+
}
241+
227242
protected unmount() {
228243
super.unmount();
229244
this.unsubscribe.forEach((cb) => cb());
@@ -232,6 +247,8 @@ export class GraphComponent<
232247
});
233248
this.ports.clear();
234249
this.destroyHitBox();
250+
this.mounted = false;
251+
this.dispatchEvent(new CustomEvent("graph-component-unmounted", { detail: { component: this } }));
235252
}
236253

237254
public setHitBox(minX: number, minY: number, maxX: number, maxY: number, force?: boolean) {

0 commit comments

Comments
 (0)