From 0e035e47df58cbdb769d83d2a8dfda384b538f5a Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 17 Jan 2019 22:35:58 +0000 Subject: [PATCH] Avoid duplicate links when missing node type installed Fixes #2032 --- .../@node-red/editor-client/src/js/nodes.js | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/nodes.js b/packages/node_modules/@node-red/editor-client/src/js/nodes.js index c24d95bc0..d9ef6ab44 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/nodes.js +++ b/packages/node_modules/@node-red/editor-client/src/js/nodes.js @@ -1306,21 +1306,23 @@ RED.nodes = (function() { RED.events.on("registry:node-type-added",function(type) { var def = registry.getNodeType(type); var replaced = false; - var replaceNodes = []; + var replaceNodes = {}; RED.nodes.eachNode(function(n) { if (n.type === "unknown" && n.name === type) { - replaceNodes.push(n); + replaceNodes[n.id] = n; } }); RED.nodes.eachConfig(function(n) { if (n.type === "unknown" && n.name === type) { - replaceNodes.push(n); + replaceNodes[n.id] = n; } }); - if (replaceNodes.length > 0) { + var replaceNodeIds = Object.keys(replaceNodes); + if (replaceNodeIds.length > 0) { var reimportList = []; - replaceNodes.forEach(function(n) { + replaceNodeIds.forEach(function(id) { + var n = replaceNodes[id]; if (configNodes.hasOwnProperty(n.id)) { delete configNodes[n.id]; } else { @@ -1328,6 +1330,18 @@ RED.nodes = (function() { } reimportList.push(convertNode(n)); }); + + // Remove any links between nodes that are going to be reimported. + // This prevents a duplicate link from being added. + var removeLinks = []; + RED.nodes.eachLink(function(l) { + if (replaceNodes.hasOwnProperty(l.source.id) && replaceNodes.hasOwnProperty(l.target.id)) { + removeLinks.push(l); + } + }); + removeLinks.forEach(removeLink); + + RED.view.redraw(true); var result = importNodes(reimportList,false); var newNodeMap = {};