diff --git a/src/core/graph/nodeShell/slotLabelReactivity.regression.test.ts b/src/core/graph/nodeShell/slotLabelReactivity.regression.test.ts new file mode 100644 index 00000000000..64f479230b1 --- /dev/null +++ b/src/core/graph/nodeShell/slotLabelReactivity.regression.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, test } from 'vitest' +import { computed, isReactive } from 'vue' + +import type { INodeOutputSlot } from '@/lib/litegraph/src/interfaces' +import { LGraphNode } from '@/lib/litegraph/src/litegraph' + +describe('slot label reactivity (regression #16642)', () => { + test('plain-object output written by index is upgraded and reactive', () => { + const node = new LGraphNode('Plain') + node.addOutput('out', 'STRING') + + const outLabel = computed(() => node.outputs[0].label) + expect(outLabel.value).toBeUndefined() + + const output: INodeOutputSlot = { + name: 'out', + type: 'STRING', + links: [], + boundingRect: new Float64Array(4) + } + node.outputs[0] = output + expect(outLabel.value).toBeUndefined() + + node.outputs[0].label = 'c' + + expect(outLabel.value).toBe('c') + expect(isReactive(node.outputs[0])).toBe(true) + }) +}) diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 3fb1eec569a..fb82b414c89 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -63,6 +63,7 @@ import { } from './node/slotLinks' import { createInputSlotView, + createOutputSlotView, resolveInputSlotView } from './node/slotDescriptorView' import { initializeWidgetsView } from './node/widgetsView' @@ -1047,6 +1048,7 @@ export class LGraphNode type, this.title_mode ) + this._state.outputs = createOutputSlotView(this, this._state.outputs) this._inputs = this._state.inputs this._outputs = this._state.outputs for (const property of [ diff --git a/src/lib/litegraph/src/node/slotDescriptorView.test.ts b/src/lib/litegraph/src/node/slotDescriptorView.test.ts index dc9a66ccace..b911eb7cf55 100644 --- a/src/lib/litegraph/src/node/slotDescriptorView.test.ts +++ b/src/lib/litegraph/src/node/slotDescriptorView.test.ts @@ -60,6 +60,22 @@ describe('slot identity', () => { expect(node._state.inputs[0]).toBe(node.inputs[0]) }) + it('upgrades extension-assigned outputs when they are written', () => { + const node = new LGraphNode('Node') + const output = { + name: 'output', + type: 'INT', + links: [], + boundingRect: new Float64Array(4) + } + + node.outputs = [output] + + expect(node._state.outputs).toBe(node.outputs) + expect(node.outputs[0]).toBeInstanceOf(NodeOutputSlot) + expect(node._state.outputs[0]).toBe(node.outputs[0]) + }) + it('preserves native indexOf behavior for arbitrary values', () => { const node = new LGraphNode('Node') node.addInput('slot', 'INT') diff --git a/src/lib/litegraph/src/node/slotDescriptorView.ts b/src/lib/litegraph/src/node/slotDescriptorView.ts index af519cd9f37..41f83671b6c 100644 --- a/src/lib/litegraph/src/node/slotDescriptorView.ts +++ b/src/lib/litegraph/src/node/slotDescriptorView.ts @@ -1,6 +1,10 @@ import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { INodeInputSlot } from '@/lib/litegraph/src/interfaces' +import type { + INodeInputSlot, + INodeOutputSlot +} from '@/lib/litegraph/src/interfaces' import { NodeInputSlot } from '@/lib/litegraph/src/node/NodeInputSlot' +import { NodeOutputSlot } from '@/lib/litegraph/src/node/NodeOutputSlot' import { toClass } from '@/lib/litegraph/src/utils/type' const assignedInputViews = new WeakMap< @@ -28,6 +32,21 @@ export function createInputSlotView( return view } +export function createOutputSlotView( + node: LGraphNode, + outputs: INodeOutputSlot[] +): INodeOutputSlot[] { + return new Proxy(outputs, { + set(target, property, value: unknown, receiver) { + const output = + isArrayIndex(property) && isOutputSlot(value) + ? toClass(NodeOutputSlot, value, node) + : value + return Reflect.set(target, property, output, receiver) + } + }) +} + export function resolveInputSlotView( inputs: INodeInputSlot[], input: INodeInputSlot @@ -54,3 +73,12 @@ function isInputSlot(value: unknown): value is INodeInputSlot { 'type' in value ) } + +function isOutputSlot(value: unknown): value is INodeOutputSlot { + return ( + value !== null && + typeof value === 'object' && + 'name' in value && + 'type' in value + ) +}