1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add RED.nodes.getNodeLinkCount to provide O(1) lookup of link count on node port

This commit is contained in:
Nick O'Leary 2021-12-29 19:01:05 +00:00
parent f86e743cce
commit 510a09ecba
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -594,7 +594,9 @@ RED.nodes = (function() {
}
allNodes.addNode(n);
if (!nodeLinks[n.id]) {
nodeLinks[n.id] = {in:[],out:[]};
nodeLinks[n.id] = {
inCount:[],outCount:[],in:[],out:[]
};
}
}
RED.events.emit('nodes:add',n);
@ -604,15 +606,19 @@ RED.nodes = (function() {
if (l.source) {
// Possible the node hasn't been added yet
if (!nodeLinks[l.source.id]) {
nodeLinks[l.source.id] = {in:[],out:[]};
nodeLinks[l.source.id] = {inCount:[],outCount:[],in:[],out:[]};
}
nodeLinks[l.source.id].out.push(l);
nodeLinks[l.source.id].outCount[l.sourcePort] = (nodeLinks[l.source.id].outCount[l.sourcePort] || 0) + 1
l.source.dirty = true;
}
if (l.target) {
if (!nodeLinks[l.target.id]) {
nodeLinks[l.target.id] = {in:[],out:[]};
nodeLinks[l.target.id] = {inCount:[],outCount:[],in:[],out:[]};
}
nodeLinks[l.target.id].in.push(l);
nodeLinks[l.target.id].inCount[0] = (nodeLinks[l.target.id].inCount[0] || 0) + 1
l.target.dirty = true;
}
if (l.source.z === l.target.z && linkTabMap[l.source.z]) {
linkTabMap[l.source.z].push(l);
@ -761,15 +767,19 @@ RED.nodes = (function() {
if (index != -1) {
links.splice(index,1);
if (l.source && nodeLinks[l.source.id]) {
l.source.dirty = true;
var sIndex = nodeLinks[l.source.id].out.indexOf(l)
if (sIndex !== -1) {
nodeLinks[l.source.id].out.splice(sIndex,1)
nodeLinks[l.source.id].outCount[l.sourcePort]--
}
}
if (l.target && nodeLinks[l.target.id]) {
l.target.dirty = true;
var tIndex = nodeLinks[l.target.id].in.indexOf(l)
if (tIndex !== -1) {
nodeLinks[l.target.id].in.splice(tIndex,1)
nodeLinks[l.target.id].inCount[0]--
}
}
if (l.source.z === l.target.z && linkTabMap[l.source.z]) {
@ -2706,6 +2716,16 @@ RED.nodes = (function() {
}
return [];
},
getNodeLinkCount: function(id,portType,index) {
if (nodeLinks[id]) {
if (portType === 1) {
return nodeLinks[id].inCount[index] || 0
} else {
return nodeLinks[id].outCount[index] || 0
}
}
return 0;
},
addWorkspace: addWorkspace,
removeWorkspace: removeWorkspace,
getWorkspaceOrder: function() { return workspacesOrder },