* disconnect one output to an specific 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 to which this slot is connected [Optional, * if not target_node is specified all nodes will be disconnected] * @returns
(slot: string | number, target_node?: LGraphNode)
| 2755 | * @returns if it was disconnected successfully |
| 2756 | */ |
| 2757 | disconnectOutput(slot: string | number, target_node?: LGraphNode): boolean { |
| 2758 | if (typeof slot === "string") { |
| 2759 | slot = this.findOutputSlot(slot) |
| 2760 | if (slot == -1) { |
| 2761 | if (LiteGraph.debug) console.log(`Connect: Error, no slot of name ${slot}`) |
| 2762 | return false |
| 2763 | } |
| 2764 | } else if (!this.outputs || slot >= this.outputs.length) { |
| 2765 | if (LiteGraph.debug) console.log("Connect: Error, slot number not found") |
| 2766 | return false |
| 2767 | } |
| 2768 | |
| 2769 | // get output slot |
| 2770 | const output = this.outputs[slot] |
| 2771 | if (!output) return false |
| 2772 | |
| 2773 | if (output._floatingLinks) { |
| 2774 | for (const link of output._floatingLinks) { |
| 2775 | if (link.hasOrigin(this.id, slot)) { |
| 2776 | this.graph?.removeFloatingLink(link) |
| 2777 | } |
| 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | if (!output.links || output.links.length == 0) return false |
| 2782 | const { links } = output |
| 2783 | |
| 2784 | // one of the output links in this slot |
| 2785 | const graph = this.graph |
| 2786 | if (!graph) throw new NullGraphError() |
| 2787 | |
| 2788 | if (target_node) { |
| 2789 | const target = typeof target_node === "number" |
| 2790 | ? graph.getNodeById(target_node) |
| 2791 | : target_node |
| 2792 | if (!target) throw "Target Node not found" |
| 2793 | |
| 2794 | for (const [i, link_id] of links.entries()) { |
| 2795 | const link_info = graph._links.get(link_id) |
| 2796 | if (link_info?.target_id != target.id) continue |
| 2797 | |
| 2798 | // is the link we are searching for... |
| 2799 | // remove here |
| 2800 | links.splice(i, 1) |
| 2801 | const input = target.inputs[link_info.target_slot] |
| 2802 | // remove there |
| 2803 | input.link = null |
| 2804 | |
| 2805 | // remove the link from the links pool |
| 2806 | link_info.disconnect(graph, "input") |
| 2807 | graph._version++ |
| 2808 | |
| 2809 | // link_info hasn't been modified so its ok |
| 2810 | target.onConnectionsChange?.( |
| 2811 | NodeSlotType.INPUT, |
| 2812 | link_info.target_slot, |
| 2813 | false, |
| 2814 | link_info, |
no test coverage detected