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

Store node list as module list

This commit is contained in:
Anna Thomas 2014-11-26 15:25:15 +00:00
parent 04ffaeb2b8
commit bb6e27f662
2 changed files with 128 additions and 69 deletions

View File

@ -46,31 +46,58 @@ function filterNodeInfo(n) {
return r; return r;
} }
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
return false;
}
}
return true;
}
function getModule(id) {
return id.split("/")[0];
}
function getNode(id) {
return id.split("/")[1];
}
var registry = (function() { var registry = (function() {
var nodeConfigCache = null; var nodeConfigCache = null;
var nodeConfigs = {}; var moduleConfigs = {};
var nodeList = []; var nodeList = [];
var nodeConstructors = {}; var nodeConstructors = {};
var nodeTypeToId = {}; var nodeTypeToId = {};
var moduleNodes = {}; var moduleNodes = {};
function saveNodeList() { function saveNodeList() {
var nodeList = {}; var moduleList = {};
for (var i in nodeConfigs) { for (var mod in moduleConfigs) {
if (nodeConfigs.hasOwnProperty(i)) { if (moduleConfigs.hasOwnProperty(mod)) {
var nodeConfig = nodeConfigs[i]; var module = mod;
var n = filterNodeInfo(nodeConfig); if (!moduleList[module]) {
n.file = nodeConfig.file; moduleList[module] = {};
delete n.loaded; moduleList[module].name = module;
delete n.err; moduleList[module].nodes = {};
delete n.file; }
delete n.id; var nodes = moduleConfigs[mod].nodes;
nodeList[i] = n; for(var node in nodes) {
if (nodes.hasOwnProperty(node)) {
var config = nodes[node];
var n = filterNodeInfo(config);
delete n.loaded;
delete n.err;
delete n.file;
delete n.id;
moduleList[module].nodes[node] = n;
}
}
} }
} }
if (settings.available()) { if (settings.available()) {
return settings.set("nodes",nodeList); return settings.set("modules",moduleList);
} else { } else {
return when.reject("Settings unavailable"); return when.reject("Settings unavailable");
} }
@ -79,15 +106,9 @@ var registry = (function() {
return { return {
init: function() { init: function() {
if (settings.available()) { if (settings.available()) {
nodeConfigs = settings.get("nodes")||{}; moduleConfigs = settings.get("modules")||{};
// Restore the node id property to individual entries
for (var id in nodeConfigs) {
if (nodeConfigs.hasOwnProperty(id)) {
nodeConfigs[id].id = id;
}
}
} else { } else {
nodeConfigs = {}; moduleConfigs = {};
} }
moduleNodes = {}; moduleNodes = {};
nodeTypeToId = {}; nodeTypeToId = {};
@ -105,19 +126,27 @@ var registry = (function() {
moduleNodes[set.module] = moduleNodes[set.module]||[]; moduleNodes[set.module] = moduleNodes[set.module]||[];
moduleNodes[set.module].push(set.name); moduleNodes[set.module].push(set.name);
if (version) { if (!moduleConfigs[set.module]) {
moduleConfigs[set.module] = {
name: set.module,
nodes: {}
};
} }
nodeConfigs[id] = set; if (version) {
moduleConfigs[set.module].version = version;
}
moduleConfigs[set.module].nodes[set.name] = set;
nodeList.push(id); nodeList.push(id);
nodeConfigCache = null; nodeConfigCache = null;
}, },
removeNode: function(id) { removeNode: function(id) {
var config = nodeConfigs[id]; var config = moduleConfigs[getModule(id)].nodes[getNode(id)];
if (!config) { if (!config) {
throw new Error("Unrecognised id: "+id); throw new Error("Unrecognised id: "+id);
} }
delete nodeConfigs[id]; delete moduleConfigs[getModule(id)].nodes[getNode(id)];
var i = nodeList.indexOf(id); var i = nodeList.indexOf(id);
if (i > -1) { if (i > -1) {
nodeList.splice(i,1); nodeList.splice(i,1);
@ -148,19 +177,31 @@ var registry = (function() {
return infoList; return infoList;
}, },
getNodeInfo: function(typeOrId) { getNodeInfo: function(typeOrId) {
var id = typeOrId;
if (nodeTypeToId[typeOrId]) { if (nodeTypeToId[typeOrId]) {
return filterNodeInfo(nodeConfigs[nodeTypeToId[typeOrId]]); id = nodeTypeToId[typeOrId];
} else if (nodeConfigs[typeOrId]) {
return filterNodeInfo(nodeConfigs[typeOrId]);
} else {
return null;
} }
if (id) {
var module = moduleConfigs[getModule(id)];
if (module) {
var config = module.nodes[getNode(id)];
if (config) {
return filterNodeInfo(config);
}
}
}
return null;
}, },
getNodeList: function() { getNodeList: function() {
var list = []; var list = [];
for (var id in nodeConfigs) { for (var module in moduleConfigs) {
if (nodeConfigs.hasOwnProperty(id)) { if (moduleConfigs.hasOwnProperty(module)) {
list.push(filterNodeInfo(nodeConfigs[id])); var nodes = moduleConfigs[module].nodes;
for (var node in nodes) {
if (nodes.hasOwnProperty(node)) {
list.push(filterNodeInfo(nodes[node]));
}
}
} }
} }
return list; return list;
@ -172,10 +213,11 @@ var registry = (function() {
var nodes = moduleNodes[module]; var nodes = moduleNodes[module];
var m = { var m = {
name: module, name: module,
version: moduleConfigs[module].version,
nodes: [] nodes: []
}; };
for (var i = 0; i < nodes.length; ++i) { for (var i = 0; i < nodes.length; ++i) {
m.nodes.push(filterNodeInfo(nodeConfigs[module+"/"+nodes[i]])); m.nodes.push(filterNodeInfo(moduleConfigs[module].nodes[nodes[i]]));
} }
list.push(m); list.push(m);
} }
@ -184,14 +226,14 @@ var registry = (function() {
}, },
getModuleInfo: function(module) { getModuleInfo: function(module) {
if (moduleNodes[module]) { if (moduleNodes[module]) {
console.log(moduleNodes[module]);
var nodes = moduleNodes[module]; var nodes = moduleNodes[module];
var m = { var m = {
name: module, name: module,
version: moduleConfigs[module].version,
nodes: [] nodes: []
}; };
for (var i = 0; i < nodes.length; ++i) { for (var i = 0; i < nodes.length; ++i) {
m.nodes.push(filterNodeInfo(nodeConfigs[module+"/"+nodes[i]])); m.nodes.push(filterNodeInfo(moduleConfigs[module].nodes[nodes[i]]));
} }
return m; return m;
} else { } else {
@ -220,7 +262,8 @@ var registry = (function() {
var result = ""; var result = "";
var script = ""; var script = "";
for (var i=0;i<nodeList.length;i++) { for (var i=0;i<nodeList.length;i++) {
var config = nodeConfigs[nodeList[i]]; var id = nodeList[i];
var config = moduleConfigs[getModule(id)].nodes[getNode(id)];
if (config.enabled && !config.err) { if (config.enabled && !config.err) {
result += config.config; result += config.config;
script += config.script; script += config.script;
@ -237,7 +280,7 @@ var registry = (function() {
}, },
getNodeConfig: function(id) { getNodeConfig: function(id) {
var config = nodeConfigs[id]; var config = moduleConfigs[getModule(id)].nodes[getNode(id)];
if (config) { if (config) {
var result = config.config; var result = config.config;
if (config.script) { if (config.script) {
@ -250,7 +293,15 @@ var registry = (function() {
}, },
getNodeConstructor: function(type) { getNodeConstructor: function(type) {
var config = nodeConfigs[nodeTypeToId[type]]; var id = nodeTypeToId[type];
var config;
if (typeof id === "undefined") {
config = undefined;
} else {
config = moduleConfigs[getModule(id)].nodes[getNode(id)];
}
if (!config || (config.enabled && !config.err)) { if (!config || (config.enabled && !config.err)) {
return nodeConstructors[type]; return nodeConstructors[type];
} }
@ -259,7 +310,7 @@ var registry = (function() {
clear: function() { clear: function() {
nodeConfigCache = null; nodeConfigCache = null;
nodeConfigs = {}; moduleConfigs = {};
nodeList = []; nodeList = [];
nodeConstructors = {}; nodeConstructors = {};
nodeTypeToId = {}; nodeTypeToId = {};
@ -273,16 +324,16 @@ var registry = (function() {
return moduleNodes[module]; return moduleNodes[module];
}, },
enableNodeSet: function(id) { enableNodeSet: function(typeOrId) {
if (!settings.available()) { if (!settings.available()) {
throw new Error("Settings unavailable"); throw new Error("Settings unavailable");
} }
var config; var id = typeOrId;
if (nodeTypeToId[id]) { if (nodeTypeToId[typeOrId]) {
config = nodeConfigs[nodeTypeToId[id]]; id = nodeTypeToId[typeOrId];
} else {
config = nodeConfigs[id];
} }
var config = moduleConfigs[getModule(id)].nodes[getNode(id)];
if (config) { if (config) {
delete config.err; delete config.err;
config.enabled = true; config.enabled = true;
@ -293,21 +344,21 @@ var registry = (function() {
nodeConfigCache = null; nodeConfigCache = null;
saveNodeList(); saveNodeList();
} else { } else {
throw new Error("Unrecognised id: "+id); throw new Error("Unrecognised id: "+typeOrId);
} }
return filterNodeInfo(config); return filterNodeInfo(config);
}, },
disableNodeSet: function(id) { disableNodeSet: function(typeOrId) {
if (!settings.available()) { if (!settings.available()) {
throw new Error("Settings unavailable"); throw new Error("Settings unavailable");
} }
var config; var id = typeOrId;
if (nodeTypeToId[id]) { if (nodeTypeToId[typeOrId]) {
config = nodeConfigs[nodeTypeToId[id]]; id = nodeTypeToId[typeOrId];
} else {
config = nodeConfigs[id];
} }
var config = moduleConfigs[getModule(id)].nodes[getNode(id)];
if (config) { if (config) {
// TODO: persist setting // TODO: persist setting
config.enabled = false; config.enabled = false;
@ -323,9 +374,9 @@ var registry = (function() {
cleanNodeList: function() { cleanNodeList: function() {
var removed = false; var removed = false;
for (var id in nodeConfigs) { for (var mod in moduleConfigs) {
if (nodeConfigs.hasOwnProperty(id)) { if (moduleConfigs.hasOwnProperty(mod)) {
if (nodeConfigs[id].module && !moduleNodes[nodeConfigs[id].module]) { if (moduleConfigs[mod] && !moduleNodes[mod]) {
registry.removeNode(id); registry.removeNode(id);
removed = true; removed = true;
} }

View File

@ -289,26 +289,30 @@ describe('NodeRegistry', function() {
var settings = { var settings = {
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "TestNode2",resourcesDir + "TestNode3"], nodesDir:[resourcesDir + "TestNode1",resourcesDir + "TestNode2",resourcesDir + "TestNode3"],
available: function() { return true; }, available: function() { return true; },
set: function(s,v) {return when.resolve();}, set: function(s,v) { return when.resolve(); },
get: function(s) { return null;} get: function(s) { return null; }
} };
var settingsSave = sinon.spy(settings,"set"); var settingsSave = sinon.spy(settings,"set");
typeRegistry.init(settings); typeRegistry.init(settings);
typeRegistry.load("wontexist",true).then(function() { typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList(); var nodeList = typeRegistry.getNodeList();
list.should.be.Array.and.have.length(3); var moduleList = typeRegistry.getModuleList();
nodeList.should.be.Array.and.have.length(3);
moduleList.should.be.Array.and.have.length(1);
settingsSave.callCount.should.equal(1); settingsSave.callCount.should.equal(1);
settingsSave.firstCall.args[0].should.be.equal("nodes"); settingsSave.firstCall.args[0].should.be.equal("modules");
var savedList = settingsSave.firstCall.args[1]; var savedList = settingsSave.firstCall.args[1];
savedList[list[0].id].name == list[0].name; savedList[moduleList[0].name].name.should.equal(moduleList[0].name);
savedList[list[1].id].name == list[1].name;
savedList[list[2].id].name == list[2].name;
savedList[list[0].id].should.not.have.property("err"); savedList[moduleList[0].name].nodes[moduleList[0].nodes[0].name].name.should.equal(moduleList[0].nodes[0].name);
savedList[list[1].id].should.not.have.property("err"); savedList[moduleList[0].name].nodes[moduleList[0].nodes[1].name].name.should.equal(moduleList[0].nodes[1].name);
savedList[list[2].id].should.not.have.property("err"); savedList[moduleList[0].name].nodes[moduleList[0].nodes[2].name].name.should.equal(moduleList[0].nodes[2].name);
savedList[moduleList[0].name].nodes[moduleList[0].nodes[0].name].should.not.have.property("err");
savedList[moduleList[0].name].nodes[moduleList[0].nodes[1].name].should.not.have.property("err");
savedList[moduleList[0].name].nodes[moduleList[0].nodes[2].name].should.not.have.property("err");
done(); done();
}).catch(function(e) { }).catch(function(e) {
@ -550,7 +554,6 @@ describe('NodeRegistry', function() {
var nodeConstructor = typeRegistry.get("test-node-1"); var nodeConstructor = typeRegistry.get("test-node-1");
(typeof nodeConstructor).should.be.equal("undefined"); (typeof nodeConstructor).should.be.equal("undefined");
done(); done();
}).catch(function(e) { }).catch(function(e) {
done(e); done(e);
@ -747,6 +750,11 @@ describe('NodeRegistry', function() {
module.should.have.property("name","TestNodeModule"); module.should.have.property("name","TestNodeModule");
module.should.have.property("version","0.0.1"); module.should.have.property("version","0.0.1");
var modules = typeRegistry.getModuleList();
modules[0].should.have.property("name","TestNodeModule");
modules[0].should.have.property("version","0.0.1");
done(); done();
}).catch(function(e) { }).catch(function(e) {
done(e); done(e);