Skip to content

Commit 77360a6

Browse files
committed
Option to use UUIDs instead of integers for node IDs
1 parent 0f2a905 commit 77360a6

1 file changed

Lines changed: 45 additions & 7 deletions

File tree

src/litegraph.js

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@
142142

143143
ctrl_shift_v_paste_connect_unselected_outputs: false, //[true!] allows ctrl + shift + v to paste nodes with the outputs of the unselected nodes connected with the inputs of the newly pasted nodes
144144

145+
// if true, all newly created nodes/links will use string UUIDs for their id fields instead of integers.
146+
// use this if you must have node IDs that are unique across all graphs and subgraphs.
147+
use_uuids: false,
148+
145149
/**
146150
* Register a node class so it can be listed when the user wants to create a new one
147151
* @method registerNodeType
@@ -601,6 +605,13 @@
601605
return target;
602606
},
603607

608+
/*
609+
* https://gist.github.com/jed/982883?permalink_comment_id=852670#gistcomment-852670
610+
*/
611+
uuidv4: function() {
612+
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,a=>(a^Math.random()*16>>a/4).toString(16));
613+
},
614+
604615
/**
605616
* Returns if the types of two slots are compatible (taking into account wildcards, etc)
606617
* @method isValidConnection
@@ -1405,18 +1416,29 @@
14051416
console.warn(
14061417
"LiteGraph: there is already a node with this ID, changing it"
14071418
);
1408-
node.id = ++this.last_node_id;
1419+
if (LiteGraph.use_uuids) {
1420+
node.id = LiteGraph.uuidv4();
1421+
}
1422+
else {
1423+
node.id = ++this.last_node_id;
1424+
}
14091425
}
14101426

14111427
if (this._nodes.length >= LiteGraph.MAX_NUMBER_OF_NODES) {
14121428
throw "LiteGraph: max number of nodes in a graph reached";
14131429
}
14141430

14151431
//give him an id
1416-
if (node.id == null || node.id == -1) {
1417-
node.id = ++this.last_node_id;
1418-
} else if (this.last_node_id < node.id) {
1419-
this.last_node_id = node.id;
1432+
if (LiteGraph.use_uuids) {
1433+
if (node.id == null || node.id == -1)
1434+
node.id = LiteGraph.uuidv4();
1435+
}
1436+
else {
1437+
if (node.id == null || node.id == -1) {
1438+
node.id = ++this.last_node_id;
1439+
} else if (this.last_node_id < node.id) {
1440+
this.last_node_id = node.id;
1441+
}
14201442
}
14211443

14221444
node.graph = this;
@@ -2412,7 +2434,12 @@
24122434
enumerable: true
24132435
});
24142436

2415-
this.id = -1; //not know till not added
2437+
if (LiteGraph.use_uuids) {
2438+
this.id = LiteGraph.uuidv4();
2439+
}
2440+
else {
2441+
this.id = -1; //not know till not added
2442+
}
24162443
this.type = null;
24172444

24182445
//inputs available: array of inputs
@@ -2626,6 +2653,11 @@
26262653
}
26272654

26282655
delete data["id"];
2656+
2657+
if (LiteGraph.use_uuids) {
2658+
data["id"] = LiteGraph.uuidv4()
2659+
}
2660+
26292661
//remove links
26302662
node.configure(data);
26312663

@@ -4264,10 +4296,16 @@
42644296
break;
42654297
}
42664298
}
4299+
4300+
var nextId
4301+
if (LiteGraph.use_uuids)
4302+
nextId = LiteGraph.uuidv4();
4303+
else
4304+
nextId = ++this.graph.last_link_id;
42674305

42684306
//create link class
42694307
link_info = new LLink(
4270-
++this.graph.last_link_id,
4308+
nextId,
42714309
input.type || output.type,
42724310
this.id,
42734311
slot,

0 commit comments

Comments
 (0)