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

Method #findSlotByType

src/LGraphNode.ts:2293–2335  ·  view source on GitHub ↗

* Finds a matching slot from those provided, returning the slot itself or its index in slots. * @param slots Slots to search (this.inputs or this.outputs) * @param type Type of slot to look for * @param returnObj If true, returns the slot itself. Otherwise, the index. * @param p

(
    slots: TSlot[],
    type: ISlotType,
    returnObj?: boolean,
    preferFreeSlot?: boolean,
    doNotUseOccupied?: boolean,
  )

Source from the content-addressed store, hash-verified

2291 * @returns If a match is found, the slot if returnObj is true, otherwise the index. If no matches are found, -1
2292 */
2293 #findSlotByType<TSlot extends INodeInputSlot | INodeOutputSlot>(
2294 slots: TSlot[],
2295 type: ISlotType,
2296 returnObj?: boolean,
2297 preferFreeSlot?: boolean,
2298 doNotUseOccupied?: boolean,
2299 ): TSlot | number {
2300 const length = slots?.length
2301 if (!length) return -1
2302
2303 // Empty string and * match anything (type: 0)
2304 if (type == "" || type == "*") type = 0
2305 const sourceTypes = String(type).toLowerCase().split(",")
2306
2307 // Run the search
2308 let occupiedSlot: number | TSlot | null = null
2309 for (let i = 0; i < length; ++i) {
2310 const slot: TSlot & IGenericLinkOrLinks = slots[i]
2311 const destTypes = slot.type == "0" || slot.type == "*"
2312 ? ["0"]
2313 : String(slot.type).toLowerCase().split(",")
2314
2315 for (const sourceType of sourceTypes) {
2316 // TODO: Remove _event_ entirely.
2317 const source = sourceType == "_event_" ? LiteGraph.EVENT : sourceType
2318
2319 for (const destType of destTypes) {
2320 const dest = destType == "_event_" ? LiteGraph.EVENT : destType
2321
2322 if (source == dest || source === "*" || dest === "*") {
2323 if (preferFreeSlot && (slot.links?.length || slot.link != null)) {
2324 // In case we can't find a free slot.
2325 occupiedSlot ??= returnObj ? slot : i
2326 continue
2327 }
2328 return returnObj ? slot : i
2329 }
2330 }
2331 }
2332 }
2333
2334 return doNotUseOccupied ? -1 : occupiedSlot ?? -1
2335 }
2336
2337 /**
2338 * Determines the slot index to connect to when attempting to connect by type.

Callers 3

findInputSlotByTypeMethod · 0.95
findOutputSlotByTypeMethod · 0.95
findSlotByTypeMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected