Skip to content
Open
134 changes: 83 additions & 51 deletions src/composables/graph/useGraphNodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
import { useNodeDefStore } from '@/stores/nodeDefStore'
import type { WidgetValue } from '@/types/simplifiedWidget'

import type { LGraph, LGraphNode } from '../../lib/litegraph/src/litegraph'
import type {
LGraph,
LGraphNode,
LGraphTriggerAction,
LGraphTriggerEvent,
LGraphTriggerParam
} from '../../lib/litegraph/src/litegraph'
import { NodeSlotType } from '../../lib/litegraph/src/types/globalEnums'

export interface WidgetSlotMetadata {
index: number
linked: boolean
}

export interface SafeWidgetData {
name: string
Expand All @@ -23,6 +35,7 @@ export interface SafeWidgetData {
options?: Record<string, unknown>
callback?: ((value: unknown) => void) | undefined
spec?: InputSpec
slotMetadata?: WidgetSlotMetadata
}

export interface VueNodeData {
Expand Down Expand Up @@ -66,6 +79,22 @@ export function useGraphNodeManager(graph: LGraph): GraphNodeManager {
// Non-reactive storage for original LiteGraph nodes
const nodeRefs = new Map<string, LGraphNode>()

const refreshNodeSlots = (nodeId: string) => {
const nodeRef = nodeRefs.get(nodeId)
const currentData = vueNodeData.get(nodeId)

if (!nodeRef || !currentData) return

const refreshedData = extractVueNodeData(nodeRef)

vueNodeData.set(nodeId, {
...currentData,
widgets: refreshedData.widgets,
inputs: refreshedData.inputs,
outputs: refreshedData.outputs
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const refreshedData = extractVueNodeData(nodeRef)
vueNodeData.set(nodeId, {
...currentData,
widgets: refreshedData.widgets,
inputs: refreshedData.inputs,
outputs: refreshedData.outputs
})
const { widgets, inputs, outputs } = extractVueNodeData(nodeRef)
vueNodeData.set(nodeId, { ...currentData, widgets, inputs, outputs })

This is a sidegrade at best, but... I wanted to see if it would fit. After I checked, the "work" was already done, so please take / discard as you like.

}

// Extract safe data from LiteGraph node for Vue consumption
const extractVueNodeData = (node: LGraphNode): VueNodeData => {
// Determine subgraph ID - null for root graph, string for subgraphs
Expand All @@ -74,6 +103,16 @@ export function useGraphNodeManager(graph: LGraph): GraphNodeManager {
? String(node.graph.id)
: null
// Extract safe widget data
const slotMetadata = new Map<string, WidgetSlotMetadata>()

node.inputs?.forEach((input, index) => {
if (!input?.widget?.name) return
slotMetadata.set(input.widget.name, {
index,
linked: input.link != null
})
})

const safeWidgets = node.widgets?.map((widget) => {
try {
// TODO: Use widget.getReactiveData() once TypeScript types are updated
Expand All @@ -90,6 +129,7 @@ export function useGraphNodeManager(graph: LGraph): GraphNodeManager {
value = widget.options.values[0]
}
const spec = nodeDefStore.getInputSpecForWidget(node, widget.name)
const slotInfo = slotMetadata.get(widget.name)

return {
name: widget.name,
Expand All @@ -98,7 +138,8 @@ export function useGraphNodeManager(graph: LGraph): GraphNodeManager {
label: widget.label,
options: widget.options ? { ...widget.options } : undefined,
callback: widget.callback,
spec
spec,
slotMetadata: slotInfo
}
} catch (error) {
return {
Expand Down Expand Up @@ -373,7 +414,7 @@ export function useGraphNodeManager(graph: LGraph): GraphNodeManager {
const createCleanupFunction = (
originalOnNodeAdded: ((node: LGraphNode) => void) | undefined,
originalOnNodeRemoved: ((node: LGraphNode) => void) | undefined,
originalOnTrigger: ((action: string, param: unknown) => void) | undefined
originalOnTrigger: ((event: LGraphTriggerEvent) => void) | undefined
) => {
return () => {
// Restore original callbacks
Expand Down Expand Up @@ -405,37 +446,27 @@ export function useGraphNodeManager(graph: LGraph): GraphNodeManager {
handleNodeRemoved(node, originalOnNodeRemoved)
}

// Listen for property change events from instrumented nodes
graph.onTrigger = (action: string, param: unknown) => {
if (
action === 'node:property:changed' &&
param &&
typeof param === 'object'
) {
const event = param as {
nodeId: string | number
property: string
oldValue: unknown
newValue: unknown
}

const nodeId = String(event.nodeId)
const triggerHandlers: {
[K in LGraphTriggerAction]: (event: LGraphTriggerParam<K>) => void
} = {
'node:property:changed': (propertyEvent) => {
const nodeId = String(propertyEvent.nodeId)
const currentData = vueNodeData.get(nodeId)

if (currentData) {
switch (event.property) {
switch (propertyEvent.property) {
case 'title':
vueNodeData.set(nodeId, {
...currentData,
title: String(event.newValue)
title: String(propertyEvent.newValue)
})
break
case 'flags.collapsed':
vueNodeData.set(nodeId, {
...currentData,
flags: {
...currentData.flags,
collapsed: Boolean(event.newValue)
collapsed: Boolean(propertyEvent.newValue)
}
})
break
Expand All @@ -444,63 +475,64 @@ export function useGraphNodeManager(graph: LGraph): GraphNodeManager {
...currentData,
flags: {
...currentData.flags,
pinned: Boolean(event.newValue)
pinned: Boolean(propertyEvent.newValue)
}
})
break
case 'mode':
vueNodeData.set(nodeId, {
...currentData,
mode: typeof event.newValue === 'number' ? event.newValue : 0
mode:
typeof propertyEvent.newValue === 'number'
? propertyEvent.newValue
: 0
})
break
case 'color':
vueNodeData.set(nodeId, {
...currentData,
color:
typeof event.newValue === 'string'
? event.newValue
typeof propertyEvent.newValue === 'string'
? propertyEvent.newValue
: undefined
})
break
case 'bgcolor':
vueNodeData.set(nodeId, {
...currentData,
bgcolor:
typeof event.newValue === 'string'
? event.newValue
typeof propertyEvent.newValue === 'string'
? propertyEvent.newValue
: undefined
})
}
}
} else if (
action === 'node:slot-errors:changed' &&
param &&
typeof param === 'object'
) {
const event = param as { nodeId: string | number }
const nodeId = String(event.nodeId)
const litegraphNode = nodeRefs.get(nodeId)
const currentData = vueNodeData.get(nodeId)

if (litegraphNode && currentData) {
// Re-extract slot data with updated hasErrors properties
vueNodeData.set(nodeId, {
...currentData,
inputs: litegraphNode.inputs
? [...litegraphNode.inputs]
: undefined,
outputs: litegraphNode.outputs
? [...litegraphNode.outputs]
: undefined
})
},
'node:slot-errors:changed': (slotErrorsEvent) => {
refreshNodeSlots(String(slotErrorsEvent.nodeId))
},
'node:slot-links:changed': (slotLinksEvent) => {
if (slotLinksEvent.slotType === NodeSlotType.INPUT) {
refreshNodeSlots(String(slotLinksEvent.nodeId))
}
}
}

// Call original trigger handler if it exists
if (originalOnTrigger) {
originalOnTrigger(action, param)
graph.onTrigger = (event: LGraphTriggerEvent) => {
switch (event.type) {
case 'node:property:changed':
triggerHandlers['node:property:changed'](event)
break
case 'node:slot-errors:changed':
triggerHandlers['node:slot-errors:changed'](event)
break
case 'node:slot-links:changed':
triggerHandlers['node:slot-links:changed'](event)
break
}

// Chain to original handler
originalOnTrigger?.(event)
}

// Initialize state
Expand Down
40 changes: 38 additions & 2 deletions src/lib/litegraph/src/LGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ import {
splitPositionables
} from './subgraph/subgraphUtils'
import { Alignment, LGraphEventMode } from './types/globalEnums'
import type {
LGraphTriggerAction,
LGraphTriggerEvent,
LGraphTriggerHandler,
LGraphTriggerParam
} from './types/graphTriggers'
import type {
ExportedSubgraph,
ExposedWidget,
Expand All @@ -65,6 +71,11 @@ import type {
} from './types/serialisation'
import { getAllNestedItems } from './utils/collections'

export type {
LGraphTriggerAction,
LGraphTriggerParam
} from './types/graphTriggers'

export interface LGraphState {
lastGroupId: number
lastNodeId: number
Expand Down Expand Up @@ -254,7 +265,7 @@ export class LGraph
onExecuteStep?(): void
onNodeAdded?(node: LGraphNode): void
onNodeRemoved?(node: LGraphNode): void
onTrigger?(action: string, param: unknown): void
onTrigger?: LGraphTriggerHandler
onBeforeChange?(graph: LGraph, info?: LGraphNode): void
onAfterChange?(graph: LGraph, info?: LGraphNode | null): void
onConnectionChange?(node: LGraphNode): void
Expand Down Expand Up @@ -1180,8 +1191,33 @@ export class LGraph
}

// ********** GLOBALS *****************
trigger<A extends LGraphTriggerAction>(
action: A,
param: LGraphTriggerParam<A>
): void
trigger(action: string, param: unknown): void
trigger(action: string, param: unknown) {
this.onTrigger?.(action, param)
// Convert to discriminated union format for typed handlers
if (
action === 'node:slot-links:changed' &&
param &&
typeof param === 'object'
) {
this.onTrigger?.({ type: action, ...param } as LGraphTriggerEvent)
} else if (
action === 'node:slot-errors:changed' &&
param &&
typeof param === 'object'
) {
this.onTrigger?.({ type: action, ...param } as LGraphTriggerEvent)
} else if (
action === 'node:property:changed' &&
param &&
typeof param === 'object'
) {
this.onTrigger?.({ type: action, ...param } as LGraphTriggerEvent)
}
// Don't handle unknown events - just ignore them
}

/** @todo Clean up - never implemented. */
Expand Down
39 changes: 38 additions & 1 deletion src/lib/litegraph/src/LGraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2852,7 +2852,17 @@ export class LGraphNode
output.links ??= []
output.links.push(link.id)
// connect in input
inputNode.inputs[inputIndex].link = link.id
const targetInput = inputNode.inputs[inputIndex]
targetInput.link = link.id
if (targetInput.widget) {
graph.trigger('node:slot-links:changed', {
nodeId: inputNode.id,
slotType: NodeSlotType.INPUT,
slotIndex: inputIndex,
connected: true,
linkId: link.id
})
}

// Reroutes
const reroutes = LLink.getReroutes(graph, link)
Expand Down Expand Up @@ -3009,6 +3019,15 @@ export class LGraphNode
const input = target.inputs[link_info.target_slot]
// remove there
input.link = null
if (input.widget) {
graph.trigger('node:slot-links:changed', {
nodeId: target.id,
slotType: NodeSlotType.INPUT,
slotIndex: link_info.target_slot,
connected: false,
linkId: link_info.id
})
Comment on lines +3023 to +3029
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the same in all three locations, how would you feel about extracting something like maybeTriggerLinkChange(...)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would only be simplified if it were to be made a class instance property of some sort, which would start to go backwards a bit. If it's moved to a more pure helper, then it becomes even more code, I think. Don't see a way to simplify/refactor cleanly.

}

// remove the link from the links pool
link_info.disconnect(graph, 'input')
Expand Down Expand Up @@ -3045,6 +3064,15 @@ export class LGraphNode
const input = target.inputs[link_info.target_slot]
// remove other side link
input.link = null
if (input.widget) {
graph.trigger('node:slot-links:changed', {
nodeId: target.id,
slotType: NodeSlotType.INPUT,
slotIndex: link_info.target_slot,
connected: false,
linkId: link_info.id
})
}

// link_info hasn't been modified so its ok
target.onConnectionsChange?.(
Expand Down Expand Up @@ -3114,6 +3142,15 @@ export class LGraphNode
const link_id = this.inputs[slot].link
if (link_id != null) {
this.inputs[slot].link = null
if (input.widget) {
graph.trigger('node:slot-links:changed', {
nodeId: this.id,
slotType: NodeSlotType.INPUT,
slotIndex: slot,
connected: false,
linkId: link_id
})
}

// remove other side
const link_info = graph._links.get(link_id)
Expand Down
7 changes: 6 additions & 1 deletion src/lib/litegraph/src/litegraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ export type {
Positionable,
Size
} from './interfaces'
export { LGraph } from './LGraph'
export {
LGraph,
type LGraphTriggerAction,
type LGraphTriggerParam
} from './LGraph'
export type { LGraphTriggerEvent } from './types/graphTriggers'
export { BadgePosition, LGraphBadge } from './LGraphBadge'
export { LGraphCanvas } from './LGraphCanvas'
export { LGraphGroup } from './LGraphGroup'
Expand Down
Loading