From d029bd89244c403ab768c0f12c8bbe498d10946e Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Sun, 9 Feb 2025 23:35:40 -0500 Subject: [PATCH] Revert "Convert i/o slot to class in LGraphNode.configure (#506)" (#511) This reverts commit c75157e86e832c8eeeea0ae6bf9066a767512a71. --- src/LGraphNode.ts | 52 ++++++++++++++++++++++------------------- test/LGraphNode.test.ts | 36 ---------------------------- 2 files changed, 28 insertions(+), 60 deletions(-) diff --git a/src/LGraphNode.ts b/src/LGraphNode.ts index 1908bdbf..b1f7e640 100644 --- a/src/LGraphNode.ts +++ b/src/LGraphNode.ts @@ -596,40 +596,44 @@ export class LGraphNode implements Positionable, IPinnable { this.title = this.constructor.title } - this.inputs ??= [] - this.inputs = this.inputs.map(input => toClass(NodeInputSlot, input)) - for (const [i, input] of this.inputs.entries()) { - const link = this.graph ? this.graph._links.get(input.link) : null - this.onConnectionsChange?.(NodeSlotType.INPUT, i, true, link, input) - this.onInputAdded?.(input) - } - - this.outputs ??= [] - this.outputs = this.outputs.map(output => toClass(NodeOutputSlot, output)) - for (const [i, output] of this.outputs.entries()) { - if (!output.links) { - continue + if (this.inputs) { + for (let i = 0; i < this.inputs.length; ++i) { + const input = this.inputs[i] + const link = this.graph ? this.graph._links.get(input.link) : null + this.onConnectionsChange?.(NodeSlotType.INPUT, i, true, link, input) + this.onInputAdded?.(input) } - for (const linkId of output.links) { - const link = this.graph - ? this.graph._links.get(linkId) - : null - this.onConnectionsChange?.(NodeSlotType.OUTPUT, i, true, link, output) + } + + if (this.outputs) { + for (let i = 0; i < this.outputs.length; ++i) { + const output = this.outputs[i] + if (!output.links) { + continue + } + for (let j = 0; j < output.links.length; ++j) { + const link = this.graph + ? this.graph._links.get(output.links[j]) + : null + this.onConnectionsChange?.(NodeSlotType.OUTPUT, i, true, link, output) + } + this.onOutputAdded?.(output) } - this.onOutputAdded?.(output) } if (this.widgets) { - for (const w of this.widgets) { + for (let i = 0; i < this.widgets.length; ++i) { + const w = this.widgets[i] if (!w) continue if (w.options?.property && this.properties[w.options.property] != undefined) w.value = JSON.parse(JSON.stringify(this.properties[w.options.property])) } - - for (const [i, value] of (info.widgets_values ?? []).entries()) { - if (this.widgets[i]) { - this.widgets[i].value = value + if (info.widgets_values) { + for (let i = 0; i < info.widgets_values.length; ++i) { + if (this.widgets[i]) { + this.widgets[i].value = info.widgets_values[i] + } } } } diff --git a/test/LGraphNode.test.ts b/test/LGraphNode.test.ts index 3bc249b6..f1634a09 100644 --- a/test/LGraphNode.test.ts +++ b/test/LGraphNode.test.ts @@ -1,6 +1,5 @@ import { describe, expect } from "vitest" import { LGraphNode } from "@/litegraph" -import { NodeInputSlot, NodeOutputSlot } from "@/NodeSlot" import { lgTest } from "./lgTest" describe("LGraphNode", () => { @@ -14,39 +13,4 @@ describe("LGraphNode", () => { expect(node.size).toEqual(new Float32Array([100, 100])) expect(node.serialize().size).toEqual([100, 100]) }) - - lgTest("should configure inputs correctly", () => { - const node = new LGraphNode("TestNode") - node.configure({ - id: 0, - inputs: [{ name: "TestInput", type: "number", link: null }], - }) - expect(node.inputs.length).toEqual(1) - expect(node.inputs[0].name).toEqual("TestInput") - expect(node.inputs[0].link).toEqual(null) - expect(node.inputs[0]).instanceOf(NodeInputSlot) - - // Should not override existing inputs - node.configure({ id: 1 }) - expect(node.id).toEqual(1) - expect(node.inputs.length).toEqual(1) - }) - - lgTest("should configure outputs correctly", () => { - const node = new LGraphNode("TestNode") - node.configure({ - id: 0, - outputs: [{ name: "TestOutput", type: "number", links: [] }], - }) - expect(node.outputs.length).toEqual(1) - expect(node.outputs[0].name).toEqual("TestOutput") - expect(node.outputs[0].type).toEqual("number") - expect(node.outputs[0].links).toEqual([]) - expect(node.outputs[0]).instanceOf(NodeOutputSlot) - - // Should not override existing outputs - node.configure({ id: 1 }) - expect(node.id).toEqual(1) - expect(node.outputs.length).toEqual(1) - }) })