MCPcopy Create free account
hub / github.com/Comfy-Org/litegraph.js / connect

Method connect

src/subgraph/SubgraphOutput.ts:29–101  ·  view source on GitHub ↗
(slot: INodeOutputSlot, node: LGraphNode, afterRerouteId?: RerouteId)

Source from the content-addressed store, hash-verified

27 declare parent: SubgraphOutputNode
28
29 override connect(slot: INodeOutputSlot, node: LGraphNode, afterRerouteId?: RerouteId): LLink | undefined {
30 const { subgraph } = this.parent
31
32 // Validate type compatibility
33 if (!LiteGraph.isValidConnection(slot.type, this.type)) return
34
35 // Allow nodes to block connection
36 const outputIndex = node.outputs.indexOf(slot)
37 if (outputIndex === -1) throw new Error("Slot is not an output of the given node")
38
39 if (node.onConnectOutput?.(outputIndex, this.type, this, this.parent, -1) === false) return
40
41 // Link should not be present, but just in case, disconnect it
42 const existingLink = this.getLinks().at(0)
43 if (existingLink != null) {
44 subgraph.beforeChange()
45
46 existingLink.disconnect(subgraph, "input")
47 const resolved = existingLink.resolve(subgraph)
48 const links = resolved.output?.links
49 if (links) removeFromArray(links, existingLink.id)
50 }
51
52 const link = new LLink(
53 ++subgraph.state.lastLinkId,
54 slot.type,
55 node.id,
56 outputIndex,
57 this.parent.id,
58 this.parent.slots.indexOf(this),
59 afterRerouteId,
60 )
61
62 // Add to graph links list
63 subgraph._links.set(link.id, link)
64
65 // Set link ID in each slot
66 this.linkIds[0] = link.id
67 slot.links ??= []
68 slot.links.push(link.id)
69
70 // Reroutes
71 const reroutes = LLink.getReroutes(subgraph, link)
72 for (const reroute of reroutes) {
73 reroute.linkIds.add(link.id)
74 if (reroute.floating) delete reroute.floating
75 reroute._dragging = undefined
76 }
77
78 // If this is the terminus of a floating link, remove it
79 const lastReroute = reroutes.at(-1)
80 if (lastReroute) {
81 for (const linkId of lastReroute.floatingLinkIds) {
82 const link = subgraph.floatingLinks.get(linkId)
83 if (link?.parentId === lastReroute.id) {
84 subgraph.removeFloatingLink(link)
85 }
86 }

Callers

nothing calls this directly

Calls 11

removeFromArrayFunction · 0.90
isValidConnectionMethod · 0.80
beforeChangeMethod · 0.80
resolveMethod · 0.80
setMethod · 0.80
addMethod · 0.80
getMethod · 0.80
afterChangeMethod · 0.80
removeFloatingLinkMethod · 0.65
disconnectMethod · 0.45
getReroutesMethod · 0.45

Tested by

no test coverage detected