* Connect an output of this node to an input of another node * @param slot (could be the number of the slot or the string with the name of the slot) * @param target_node the target node * @param target_slot the input slot of the target node (could be the number of the slot or the string wit
(
slot: number | string,
target_node: LGraphNode,
target_slot: ISlotType,
afterRerouteId?: RerouteId,
)
| 2498 | * @returns the link_info is created, otherwise null |
| 2499 | */ |
| 2500 | connect( |
| 2501 | slot: number | string, |
| 2502 | target_node: LGraphNode, |
| 2503 | target_slot: ISlotType, |
| 2504 | afterRerouteId?: RerouteId, |
| 2505 | ): LLink | null { |
| 2506 | // Allow legacy API support for searching target_slot by string, without mutating the input variables |
| 2507 | let targetIndex: number | null |
| 2508 | |
| 2509 | const { graph, outputs } = this |
| 2510 | if (!graph) { |
| 2511 | // could be connected before adding it to a graph |
| 2512 | // due to link ids being associated with graphs |
| 2513 | console.log("Connect: Error, node doesn't belong to any graph. Nodes must be added first to a graph before connecting them.") |
| 2514 | return null |
| 2515 | } |
| 2516 | |
| 2517 | // seek for the output slot |
| 2518 | if (typeof slot === "string") { |
| 2519 | slot = this.findOutputSlot(slot) |
| 2520 | if (slot == -1) { |
| 2521 | if (LiteGraph.debug) console.log(`Connect: Error, no slot of name ${slot}`) |
| 2522 | return null |
| 2523 | } |
| 2524 | } else if (!outputs || slot >= outputs.length) { |
| 2525 | if (LiteGraph.debug) console.log("Connect: Error, slot number not found") |
| 2526 | return null |
| 2527 | } |
| 2528 | |
| 2529 | if (target_node && typeof target_node === "number") { |
| 2530 | const nodeById = graph.getNodeById(target_node) |
| 2531 | if (!nodeById) throw "target node is null" |
| 2532 | |
| 2533 | target_node = nodeById |
| 2534 | } |
| 2535 | if (!target_node) throw "target node is null" |
| 2536 | |
| 2537 | // avoid loopback |
| 2538 | if (target_node == this) return null |
| 2539 | |
| 2540 | // you can specify the slot by name |
| 2541 | if (typeof target_slot === "string") { |
| 2542 | targetIndex = target_node.findInputSlot(target_slot) |
| 2543 | if (targetIndex == -1) { |
| 2544 | if (LiteGraph.debug) console.log(`Connect: Error, no slot of name ${targetIndex}`) |
| 2545 | return null |
| 2546 | } |
| 2547 | } else if (target_slot === LiteGraph.EVENT) { |
| 2548 | // TODO: Events |
| 2549 | if (LiteGraph.do_add_triggers_slots) { |
| 2550 | target_node.changeMode(LGraphEventMode.ON_TRIGGER) |
| 2551 | targetIndex = target_node.findInputSlot("onTrigger") |
| 2552 | } else { |
| 2553 | return null |
| 2554 | } |
| 2555 | } else if (typeof target_slot === "number") { |
| 2556 | targetIndex = target_slot |
| 2557 | } else { |