diff --git a/packages/core/package.json b/packages/core/package.json index 15d52591..fc43a5ae 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@litegraph-ts/core", - "version": "0.2.19", + "version": "0.2.20", "description": "A graph node editor similar to PD or UDK Blueprints. It works in an HTML5 Canvas and allows to export graphs to be included in applications.", "source": "src/index.ts", "types": "src/index.ts", diff --git a/packages/core/src/LGraph.ts b/packages/core/src/LGraph.ts index a3c1c7f3..e7a09f10 100644 --- a/packages/core/src/LGraph.ts +++ b/packages/core/src/LGraph.ts @@ -22,8 +22,6 @@ import { } from "./types"; import { LayoutDirection, NodeMode } from "./types"; import { v4 as uuidv4 } from "uuid"; -import { UUID } from "./types"; -import { Disposed } from "./misc/Disposed"; import { EventEmitter } from "./misc/EventEmitter"; export type LGraphAddNodeMode = @@ -100,7 +98,6 @@ export default class LGraph { static DEFAULT_SUPPORTED_TYPES: string[] = ["number", "string", "boolean"]; supported_types: string[] | null = null; - disposed = new Disposed(); events = new EventEmitter<{ play: () => void; stop: () => void; @@ -136,9 +133,7 @@ export default class LGraph { change: (graph: LGraph) => void; serialize: (data: SerializedLGraph) => void; configure: (data: SerializedLGraph) => void; - }>({ - signal: this.disposed.signal, - }); + }>(); constructor(o?: SerializedLGraph) { if (LiteGraph.debug) { diff --git a/packages/core/src/LGraphCanvas.ts b/packages/core/src/LGraphCanvas.ts index 7cb37899..b3d85bb0 100644 --- a/packages/core/src/LGraphCanvas.ts +++ b/packages/core/src/LGraphCanvas.ts @@ -42,8 +42,6 @@ import { type Vector4, } from "./types"; import { clamp } from "./utils"; -import { UUID } from "./types"; -import { Disposed } from "./misc/Disposed"; import { EventEmitter } from "./misc/EventEmitter"; export interface IGraphPanel extends HTMLDivElement { @@ -508,7 +506,6 @@ export default class LGraphCanvas search_box: IGraphDialog | null = null; prompt_box: IGraphDialog | null = null; - disposed = new Disposed(); events = new EventEmitter<{ clear: () => void; dropItem: (e: DragEvent) => void; diff --git a/packages/core/src/LGraphNode.ts b/packages/core/src/LGraphNode.ts index 00563a2a..203e3a79 100644 --- a/packages/core/src/LGraphNode.ts +++ b/packages/core/src/LGraphNode.ts @@ -206,6 +206,13 @@ export default class LGraphNode { this.properties_info = []; //for the info this.flags = {}; + + this.events.once("removed", () => { + this.disposed.dispose(); + this.widgets?.forEach((w) => { + w.onNodeRemoved?.(this); + }); + }); } title: string; @@ -336,7 +343,9 @@ export default class LGraphNode { nodeOptionalOutputAdd: (slot: INodeOutputSlot) => void; resize: (size: Vector2) => void; propertyChanged: (k: string, v: any, prev_v: any) => void; - }>(); + }>({ + signal: this.disposed.signal, + }); // sync position with the node dom_anchors: { @@ -3561,11 +3570,7 @@ export default class LGraphNode { * when removed from graph * Called by `LGraph.remove` `LGraph.clear` */ - onRemoved(options?: LGraphRemoveNodeOptions): void { - this.widgets?.forEach((w) => { - w.onNodeRemoved?.(this); - }); - } + onRemoved?(options?: LGraphRemoveNodeOptions): void; /** * if returns false the incoming connection will be canceled diff --git a/packages/core/src/misc/EventEmitter.ts b/packages/core/src/misc/EventEmitter.ts index aaaa008a..8df02d25 100644 --- a/packages/core/src/misc/EventEmitter.ts +++ b/packages/core/src/misc/EventEmitter.ts @@ -150,13 +150,17 @@ export class EventEmitter { } listeners.push(listener); - if (options?.signal) { - options?.signal.addEventListener("abort", () => { - this.removeListener(eventName, listener); - }); - } - if (this.signal) { - this.signal.addEventListener("abort", () => { + const signals = [options?.signal, this.signal].filter( + Boolean, + ) as AbortSignal[]; + + if (signals.length !== 0) { + const mergedSignal = new AbortController(); + signals.forEach((signal) => + signal.addEventListener("abort", () => mergedSignal.abort()), + ); + + mergedSignal.signal.addEventListener("abort", () => { this.removeListener(eventName, listener); }); }