Skip to content

Commit b217dbf

Browse files
committed
Always use unique names when adding slots
1 parent 6867ff2 commit b217dbf

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/strings.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,19 @@ export function stringOrEmpty(value: unknown): string {
2121
export function parseSlotTypes(type: ISlotType): string[] {
2222
return type == "" || type == "0" ? ["*"] : String(type).toLowerCase().split(",")
2323
}
24+
25+
/**
26+
* Creates a unique name by appending an underscore and a number to the end of the name
27+
* if it already exists.
28+
* @param name The name to make unique
29+
* @param existingNames The names that already exist. Default: an empty array
30+
* @returns The name, or a unique name if it already exists.
31+
*/
32+
export function nextUniqueName(name: string, existingNames: string[] = []): string {
33+
let i = 1
34+
const baseName = name
35+
while (existingNames.includes(name)) {
36+
name = `${baseName}_${i++}`
37+
}
38+
return name
39+
}

src/subgraph/EmptySubgraphInput.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { LGraphNode } from "@/LGraphNode"
44
import type { RerouteId } from "@/Reroute"
55

66
import { LLink } from "@/LLink"
7+
import { nextUniqueName } from "@/strings"
78
import { zeroUuid } from "@/utils/uuid"
89

910
import { SubgraphInput } from "./SubgraphInput"
@@ -23,7 +24,11 @@ export class EmptySubgraphInput extends SubgraphInput {
2324
}
2425

2526
override connect(slot: INodeInputSlot, node: LGraphNode, afterRerouteId?: RerouteId): LLink | undefined {
26-
const input = this.parent.subgraph.addInput(slot.name, String(slot.type))
27+
const { subgraph } = this.parent
28+
const existingNames = subgraph.inputs.map(x => x.name)
29+
30+
const name = nextUniqueName(slot.name, existingNames)
31+
const input = subgraph.addInput(name, String(slot.type))
2732
return input.connect(slot, node, afterRerouteId)
2833
}
2934

src/subgraph/EmptySubgraphOutput.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { LGraphNode } from "@/LGraphNode"
44
import type { RerouteId } from "@/Reroute"
55

66
import { LLink } from "@/LLink"
7+
import { nextUniqueName } from "@/strings"
78
import { zeroUuid } from "@/utils/uuid"
89

910
import { SubgraphOutput } from "./SubgraphOutput"
@@ -23,7 +24,11 @@ export class EmptySubgraphOutput extends SubgraphOutput {
2324
}
2425

2526
override connect(slot: INodeOutputSlot, node: LGraphNode, afterRerouteId?: RerouteId): LLink | undefined {
26-
const output = this.parent.subgraph.addOutput(slot.name, String(slot.type))
27+
const { subgraph } = this.parent
28+
const existingNames = subgraph.outputs.map(x => x.name)
29+
30+
const name = nextUniqueName(slot.name, existingNames)
31+
const output = subgraph.addOutput(name, String(slot.type))
2732
return output.connect(slot, node, afterRerouteId)
2833
}
2934

0 commit comments

Comments
 (0)