Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/core/graph/nodeShell/slotLabelReactivity.regression.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
2 changes: 2 additions & 0 deletions src/lib/litegraph/src/LGraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
} from './node/slotLinks'
import {
createInputSlotView,
createOutputSlotView,
resolveInputSlotView
} from './node/slotDescriptorView'
import { initializeWidgetsView } from './node/widgetsView'
Expand Down Expand Up @@ -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 [
Expand Down
16 changes: 16 additions & 0 deletions src/lib/litegraph/src/node/slotDescriptorView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
30 changes: 29 additions & 1 deletion src/lib/litegraph/src/node/slotDescriptorView.ts
Original file line number Diff line number Diff line change
@@ -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<
Expand Down Expand Up @@ -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
Expand All @@ -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
)
}
Loading