Skip to content

Commit

Permalink
:fix: node disposed
Browse files Browse the repository at this point in the history
  • Loading branch information
zhzLuke96 committed May 27, 2024
1 parent 903cb2c commit 2178c65
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/LGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/LGraphCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/LGraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions packages/core/src/misc/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,17 @@ export class EventEmitter<Events extends EventMap> {
}
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);
});
}
Expand Down

0 comments on commit 2178c65

Please sign in to comment.