Skip to content

Commit 2178c65

Browse files
committed
:fix: node disposed
1 parent 903cb2c commit 2178c65

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@litegraph-ts/core",
3-
"version": "0.2.19",
3+
"version": "0.2.20",
44
"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.",
55
"source": "src/index.ts",
66
"types": "src/index.ts",

packages/core/src/LGraph.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import {
2222
} from "./types";
2323
import { LayoutDirection, NodeMode } from "./types";
2424
import { v4 as uuidv4 } from "uuid";
25-
import { UUID } from "./types";
26-
import { Disposed } from "./misc/Disposed";
2725
import { EventEmitter } from "./misc/EventEmitter";
2826

2927
export type LGraphAddNodeMode =
@@ -100,7 +98,6 @@ export default class LGraph {
10098
static DEFAULT_SUPPORTED_TYPES: string[] = ["number", "string", "boolean"];
10199
supported_types: string[] | null = null;
102100

103-
disposed = new Disposed();
104101
events = new EventEmitter<{
105102
play: () => void;
106103
stop: () => void;
@@ -136,9 +133,7 @@ export default class LGraph {
136133
change: (graph: LGraph) => void;
137134
serialize: (data: SerializedLGraph) => void;
138135
configure: (data: SerializedLGraph) => void;
139-
}>({
140-
signal: this.disposed.signal,
141-
});
136+
}>();
142137

143138
constructor(o?: SerializedLGraph) {
144139
if (LiteGraph.debug) {

packages/core/src/LGraphCanvas.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ import {
4242
type Vector4,
4343
} from "./types";
4444
import { clamp } from "./utils";
45-
import { UUID } from "./types";
46-
import { Disposed } from "./misc/Disposed";
4745
import { EventEmitter } from "./misc/EventEmitter";
4846

4947
export interface IGraphPanel extends HTMLDivElement {
@@ -508,7 +506,6 @@ export default class LGraphCanvas
508506
search_box: IGraphDialog | null = null;
509507
prompt_box: IGraphDialog | null = null;
510508

511-
disposed = new Disposed();
512509
events = new EventEmitter<{
513510
clear: () => void;
514511
dropItem: (e: DragEvent) => void;

packages/core/src/LGraphNode.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ export default class LGraphNode {
206206
this.properties_info = []; //for the info
207207

208208
this.flags = {};
209+
210+
this.events.once("removed", () => {
211+
this.disposed.dispose();
212+
this.widgets?.forEach((w) => {
213+
w.onNodeRemoved?.(this);
214+
});
215+
});
209216
}
210217

211218
title: string;
@@ -336,7 +343,9 @@ export default class LGraphNode {
336343
nodeOptionalOutputAdd: (slot: INodeOutputSlot) => void;
337344
resize: (size: Vector2) => void;
338345
propertyChanged: (k: string, v: any, prev_v: any) => void;
339-
}>();
346+
}>({
347+
signal: this.disposed.signal,
348+
});
340349

341350
// sync position with the node
342351
dom_anchors: {
@@ -3561,11 +3570,7 @@ export default class LGraphNode {
35613570
* when removed from graph
35623571
* Called by `LGraph.remove` `LGraph.clear`
35633572
*/
3564-
onRemoved(options?: LGraphRemoveNodeOptions): void {
3565-
this.widgets?.forEach((w) => {
3566-
w.onNodeRemoved?.(this);
3567-
});
3568-
}
3573+
onRemoved?(options?: LGraphRemoveNodeOptions): void;
35693574

35703575
/**
35713576
* if returns false the incoming connection will be canceled

packages/core/src/misc/EventEmitter.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,17 @@ export class EventEmitter<Events extends EventMap> {
150150
}
151151
listeners.push(listener);
152152

153-
if (options?.signal) {
154-
options?.signal.addEventListener("abort", () => {
155-
this.removeListener(eventName, listener);
156-
});
157-
}
158-
if (this.signal) {
159-
this.signal.addEventListener("abort", () => {
153+
const signals = [options?.signal, this.signal].filter(
154+
Boolean,
155+
) as AbortSignal[];
156+
157+
if (signals.length !== 0) {
158+
const mergedSignal = new AbortController();
159+
signals.forEach((signal) =>
160+
signal.addEventListener("abort", () => mergedSignal.abort()),
161+
);
162+
163+
mergedSignal.signal.addEventListener("abort", () => {
160164
this.removeListener(eventName, listener);
161165
});
162166
}

0 commit comments

Comments
 (0)