mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Use node/tab map to make filterNodes more efficient
This commit is contained in:
		@@ -17,6 +17,8 @@ RED.nodes = (function() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    var node_defs = {};
 | 
					    var node_defs = {};
 | 
				
			||||||
    var nodes = [];
 | 
					    var nodes = [];
 | 
				
			||||||
 | 
					    var nodeTabMap = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var configNodes = {};
 | 
					    var configNodes = {};
 | 
				
			||||||
    var links = [];
 | 
					    var links = [];
 | 
				
			||||||
    var defaultWorkspace;
 | 
					    var defaultWorkspace;
 | 
				
			||||||
@@ -213,6 +215,11 @@ RED.nodes = (function() {
 | 
				
			|||||||
                n.i = nextId+1;
 | 
					                n.i = nextId+1;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            nodes.push(n);
 | 
					            nodes.push(n);
 | 
				
			||||||
 | 
					            if (nodeTabMap[n.z]) {
 | 
				
			||||||
 | 
					                nodeTabMap[n.z][n.id] = n;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                console.warn("Node added to unknown tab/subflow:",n);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        RED.events.emit('nodes:add',n);
 | 
					        RED.events.emit('nodes:add',n);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -246,6 +253,9 @@ RED.nodes = (function() {
 | 
				
			|||||||
            node = getNode(id);
 | 
					            node = getNode(id);
 | 
				
			||||||
            if (node) {
 | 
					            if (node) {
 | 
				
			||||||
                nodes.splice(nodes.indexOf(node),1);
 | 
					                nodes.splice(nodes.indexOf(node),1);
 | 
				
			||||||
 | 
					                if (nodeTabMap[node.z]) {
 | 
				
			||||||
 | 
					                    delete nodeTabMap[node.z][node.id];
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                removedLinks = links.filter(function(l) { return (l.source === node) || (l.target === node); });
 | 
					                removedLinks = links.filter(function(l) { return (l.source === node) || (l.target === node); });
 | 
				
			||||||
                removedLinks.forEach(function(l) {links.splice(links.indexOf(l), 1); });
 | 
					                removedLinks.forEach(function(l) {links.splice(links.indexOf(l), 1); });
 | 
				
			||||||
                var updatedConfigNode = false;
 | 
					                var updatedConfigNode = false;
 | 
				
			||||||
@@ -300,6 +310,8 @@ RED.nodes = (function() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    function addWorkspace(ws,targetIndex) {
 | 
					    function addWorkspace(ws,targetIndex) {
 | 
				
			||||||
        workspaces[ws.id] = ws;
 | 
					        workspaces[ws.id] = ws;
 | 
				
			||||||
 | 
					        nodeTabMap[ws.id] = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ws._def = RED.nodes.getType('tab');
 | 
					        ws._def = RED.nodes.getType('tab');
 | 
				
			||||||
        if (targetIndex === undefined) {
 | 
					        if (targetIndex === undefined) {
 | 
				
			||||||
            workspacesOrder.push(ws.id);
 | 
					            workspacesOrder.push(ws.id);
 | 
				
			||||||
@@ -312,6 +324,7 @@ RED.nodes = (function() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    function removeWorkspace(id) {
 | 
					    function removeWorkspace(id) {
 | 
				
			||||||
        delete workspaces[id];
 | 
					        delete workspaces[id];
 | 
				
			||||||
 | 
					        delete nodeTabMap[ws.id];
 | 
				
			||||||
        workspacesOrder.splice(workspacesOrder.indexOf(id),1);
 | 
					        workspacesOrder.splice(workspacesOrder.indexOf(id),1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var removedNodes = [];
 | 
					        var removedNodes = [];
 | 
				
			||||||
@@ -357,6 +370,8 @@ RED.nodes = (function() {
 | 
				
			|||||||
            sf.name = subflowName;
 | 
					            sf.name = subflowName;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        subflows[sf.id] = sf;
 | 
					        subflows[sf.id] = sf;
 | 
				
			||||||
 | 
					        nodeTabMap[sf.id] = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        RED.nodes.registerType("subflow:"+sf.id, {
 | 
					        RED.nodes.registerType("subflow:"+sf.id, {
 | 
				
			||||||
            defaults:{
 | 
					            defaults:{
 | 
				
			||||||
                name:{value:""},
 | 
					                name:{value:""},
 | 
				
			||||||
@@ -393,6 +408,7 @@ RED.nodes = (function() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    function removeSubflow(sf) {
 | 
					    function removeSubflow(sf) {
 | 
				
			||||||
        delete subflows[sf.id];
 | 
					        delete subflows[sf.id];
 | 
				
			||||||
 | 
					        delete nodeTabMap[sf.id];
 | 
				
			||||||
        registry.removeNodeType("subflow:"+sf.id);
 | 
					        registry.removeNodeType("subflow:"+sf.id);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1266,12 +1282,13 @@ RED.nodes = (function() {
 | 
				
			|||||||
    // TODO: supports filter.z|type
 | 
					    // TODO: supports filter.z|type
 | 
				
			||||||
    function filterNodes(filter) {
 | 
					    function filterNodes(filter) {
 | 
				
			||||||
        var result = [];
 | 
					        var result = [];
 | 
				
			||||||
 | 
					        var searchSet = nodes;
 | 
				
			||||||
 | 
					        if (filter.hasOwnProperty("z") && Object.hasOwnProperty("values") && nodeTabMap.hasOwnProperty(filter.z) ) {
 | 
				
			||||||
 | 
					            searchSet = Object.values(nodeTabMap[filter.z]);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for (var n=0;n<nodes.length;n++) {
 | 
					        for (var n=0;n<searchSet.length;n++) {
 | 
				
			||||||
            var node = nodes[n];
 | 
					            var node = searchSet[n];
 | 
				
			||||||
            if (filter.hasOwnProperty("z") && node.z !== filter.z) {
 | 
					 | 
				
			||||||
                continue;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            if (filter.hasOwnProperty("type") && node.type !== filter.type) {
 | 
					            if (filter.hasOwnProperty("type") && node.type !== filter.type) {
 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -1339,6 +1356,7 @@ RED.nodes = (function() {
 | 
				
			|||||||
    function clear() {
 | 
					    function clear() {
 | 
				
			||||||
        nodes = [];
 | 
					        nodes = [];
 | 
				
			||||||
        links = [];
 | 
					        links = [];
 | 
				
			||||||
 | 
					        nodeTabMap = {};
 | 
				
			||||||
        configNodes = {};
 | 
					        configNodes = {};
 | 
				
			||||||
        workspacesOrder = [];
 | 
					        workspacesOrder = [];
 | 
				
			||||||
        var subflowIds = Object.keys(subflows);
 | 
					        var subflowIds = Object.keys(subflows);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user