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

Move all event emitting into runtime side, not api side

This commit is contained in:
Nick O'Leary 2018-02-28 11:23:25 +00:00
parent e123e7b0b0
commit 06a5e4273b
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 50 additions and 39 deletions

View File

@ -18,17 +18,13 @@ var when = require("when");
var apiUtils = require("../util");
var redNodes;
var log;
var i18n;
var settings;
var events;
module.exports = {
init: function(runtime) {
redNodes = runtime.nodes;
log = runtime.log;
i18n = runtime.i18n;
settings = runtime.settings;
events = runtime.events;
},
getAll: function(req,res) {
if (req.get("accept") == "application/json") {
@ -72,11 +68,6 @@ module.exports = {
return;
}
promise.then(function(info) {
if (isUpgrade) {
events.emit("runtime-event",{id:"node/upgraded",retain:false,payload:{module:node.module,version:node.version}});
} else {
events.emit("runtime-event",{id:"node/added",retain:false,payload:info.nodes});
}
if (node.module) {
log.audit({event: "nodes.install",module:node.module,version:node.version},req);
res.json(info);
@ -114,7 +105,6 @@ module.exports = {
}
promise.then(function(list) {
events.emit("runtime-event",{id:"node/removed",retain:false,payload:list});
log.audit({event: "nodes.remove",module:mod},req);
res.status(204).end();
}).catch(function(err) {
@ -248,21 +238,6 @@ function putNode(node, enabled) {
} else {
promise = redNodes.disableNode(node.id);
}
return promise.then(function(info) {
if (info.enabled === enabled && !info.err) {
events.emit("runtime-event",{id:"node/"+(enabled?"enabled":"disabled"),retain:false,payload:info});
log.info(" "+log._("api.nodes."+(enabled?"enabled":"disabled")));
for (var i=0;i<info.types.length;i++) {
log.info(" - "+info.types[i]);
}
} else if (enabled && info.err) {
log.warn(log._("api.nodes.error-enable"));
log.warn(" - "+info.name+" : "+info.err);
}
return info;
});
}
return promise;
}

View File

@ -24,8 +24,6 @@ var cors = require('cors');
var auth = require("./auth");
var apiUtil = require("./util");
var i18n;
var log;
var adminApp;
var server;
var runtime;
@ -35,8 +33,6 @@ function init(_server,_runtime) {
server = _server;
runtime = _runtime;
var settings = runtime.settings;
i18n = runtime.i18n;
log = runtime.log;
if (settings.httpAdminRoot !== false) {
apiUtil.init(runtime);
adminApp = express();

View File

@ -104,7 +104,43 @@ function init(runtime) {
function disableNode(id) {
flows.checkTypeInUse(id);
return registry.disableNode(id);
return registry.disableNode(id).then(function(info) {
reportNodeStateChange(info,false);
return info;
});
}
function enableNode(id) {
return registry.enableNode(id).then(function(info) {
reportNodeStateChange(info,true);
return info;
});
}
function reportNodeStateChange(info,enabled) {
if (info.enabled === enabled && !info.err) {
events.emit("runtime-event",{id:"node/"+(enabled?"enabled":"disabled"),retain:false,payload:info});
log.info(" "+log._("api.nodes."+(enabled?"enabled":"disabled")));
for (var i=0;i<info.types.length;i++) {
log.info(" - "+info.types[i]);
}
} else if (enabled && info.err) {
log.warn(log._("api.nodes.error-enable"));
log.warn(" - "+info.name+" : "+info.err);
}
}
function installModule(module,version) {
var module = registry.getModuleInfo(module);
var isUpgrade = !!module;
return registry.installModule(module,version).then(function(info) {
if (isUpgrade) {
events.emit("runtime-event",{id:"node/upgraded",retain:false,payload:{module:module,version:version}});
} else {
events.emit("runtime-event",{id:"node/added",retain:false,payload:info.nodes});
}
return info;
});
}
function uninstallModule(module) {
@ -115,7 +151,10 @@ function uninstallModule(module) {
for (var i=0;i<info.nodes.length;i++) {
flows.checkTypeInUse(module+"/"+info.nodes[i].name);
}
return registry.uninstallModule(module);
return registry.uninstallModule(module).then(function(list) {
events.emit("runtime-event",{id:"node/removed",retain:false,payload:list});
return list;
});
}
}
@ -130,10 +169,10 @@ module.exports = {
eachNode: flows.eachNode,
paletteEditorEnabled: registry.paletteEditorEnabled,
installModule: registry.installModule,
installModule: installModule,
uninstallModule: uninstallModule,
enableNode: registry.enableNode,
enableNode: enableNode,
disableNode: disableNode,
// Node type registry

View File

@ -240,7 +240,7 @@ describe("red/nodes/index", function() {
}
});
sinon.stub(registry,"disableNode",function(id) {
return randomNodeInfo;
return when.resolve(randomNodeInfo);
});
});
afterEach(function() {
@ -252,11 +252,12 @@ describe("red/nodes/index", function() {
index.init(runtime);
index.registerType('test-node-set','test', TestNode);
index.loadFlows().then(function() {
var info = index.disableNode("5678");
registry.disableNode.calledOnce.should.be.true();
registry.disableNode.calledWith("5678").should.be.true();
info.should.eql(randomNodeInfo);
done();
return index.disableNode("5678").then(function(info) {
registry.disableNode.calledOnce.should.be.true();
registry.disableNode.calledWith("5678").should.be.true();
info.should.eql(randomNodeInfo);
done();
});
}).otherwise(function(err) {
done(err);
});