Remove redundant msg != null checks.

This commit is contained in:
Mark Hindess
2014-09-08 21:10:06 +01:00
parent 7b63680be1
commit 5d9c16ffbf
7 changed files with 163 additions and 182 deletions

View File

@@ -27,60 +27,56 @@ module.exports = function(RED) {
var node = this;
this.on("input", function(msg) {
if (msg != null) {
node.status({fill:"blue",shape:"dot"});
if (this.useSpawn === true) {
// make the extra args into an array
// then prepend with the msg.payload
if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); }
var arg = [];
if (node.append.length > 0) { arg = node.append.split(","); }
if (msg.payload.trim() !== "") { arg.unshift(msg.payload); }
node.log(node.cmd+" ["+arg+"]");
if (node.cmd.indexOf(" ") == -1) {
var ex = spawn(node.cmd,arg);
ex.stdout.on('data', function (data) {
//console.log('[exec] stdout: ' + data);
msg.payload = data.toString();
node.send([msg,null,null]);
});
ex.stderr.on('data', function (data) {
//console.log('[exec] stderr: ' + data);
msg.payload = data.toString();
node.send([null,msg,null]);
});
ex.on('close', function (code) {
//console.log('[exec] result: ' + code);
msg.payload = code;
node.status({});
node.send([null,null,msg]);
});
ex.on('error', function (code) {
node.warn(code);
});
}
else { node.error("Spawn command must be just the command - no spaces or extra parameters"); }
}
else {
var cl = node.cmd+" "+msg.payload+" "+node.append;
node.log(cl);
var child = exec(cl, function (error, stdout, stderr) {
msg.payload = stdout;
var msg2 = {payload:stderr};
var msg3 = null;
//console.log('[exec] stdout: ' + stdout);
//console.log('[exec] stderr: ' + stderr);
if (error !== null) {
msg3 = {payload:error};
//console.log('[exec] error: ' + error);
}
node.status({fill:"blue",shape:"dot"});
if (this.useSpawn === true) {
// make the extra args into an array
// then prepend with the msg.payload
if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); }
var arg = [];
if (node.append.length > 0) { arg = node.append.split(","); }
if (msg.payload.trim() !== "") { arg.unshift(msg.payload); }
node.log(node.cmd+" ["+arg+"]");
if (node.cmd.indexOf(" ") == -1) {
var ex = spawn(node.cmd,arg);
ex.stdout.on('data', function (data) {
//console.log('[exec] stdout: ' + data);
msg.payload = data.toString();
node.send([msg,null,null]);
});
ex.stderr.on('data', function (data) {
//console.log('[exec] stderr: ' + data);
msg.payload = data.toString();
node.send([null,msg,null]);
});
ex.on('close', function (code) {
//console.log('[exec] result: ' + code);
msg.payload = code;
node.status({});
node.send([msg,msg2,msg3]);
node.send([null,null,msg]);
});
ex.on('error', function (code) {
node.warn(code);
});
}
else { node.error("Spawn command must be just the command - no spaces or extra parameters"); }
}
else {
var cl = node.cmd+" "+msg.payload+" "+node.append;
node.log(cl);
var child = exec(cl, function (error, stdout, stderr) {
msg.payload = stdout;
var msg2 = {payload:stderr};
var msg3 = null;
//console.log('[exec] stdout: ' + stdout);
//console.log('[exec] stderr: ' + stderr);
if (error !== null) {
msg3 = {payload:error};
//console.log('[exec] error: ' + error);
}
node.status({});
node.send([msg,msg2,msg3]);
});
}
});
}

View File

@@ -39,38 +39,36 @@ module.exports = function(RED) {
try {
this.script = vm.createScript(functionText);
this.on("input", function(msg) {
if (msg != null) {
try {
var start = process.hrtime();
context.msg = msg;
this.script.runInContext(context);
var results = context.results;
if (results == null) {
results = [];
} else if (results.length == null) {
results = [results];
}
if (msg._topic) {
for (var m in results) {
if (results[m]) {
if (util.isArray(results[m])) {
for (var n=0; n < results[m].length; n++) {
results[m][n]._topic = msg._topic;
}
} else {
results[m]._topic = msg._topic;
try {
var start = process.hrtime();
context.msg = msg;
this.script.runInContext(context);
var results = context.results;
if (results == null) {
results = [];
} else if (results.length == null) {
results = [results];
}
if (msg._topic) {
for (var m in results) {
if (results[m]) {
if (util.isArray(results[m])) {
for (var n=0; n < results[m].length; n++) {
results[m][n]._topic = msg._topic;
}
} else {
results[m]._topic = msg._topic;
}
}
}
this.send(results);
var duration = process.hrtime(start);
if (process.env.NODE_RED_FUNCTION_TIME) {
this.status({fill:"yellow",shape:"dot",text:""+Math.floor((duration[0]* 1e9 + duration[1])/10000)/100});
}
} catch(err) {
this.error(err.toString());
}
this.send(results);
var duration = process.hrtime(start);
if (process.env.NODE_RED_FUNCTION_TIME) {
this.status({fill:"yellow",shape:"dot",text:""+Math.floor((duration[0]* 1e9 + duration[1])/10000)/100});
}
} catch(err) {
this.error(err.toString());
}
});
} catch(err) {

View File

@@ -47,14 +47,12 @@ module.exports = function(RED) {
}
node.on("input", function(msg) {
if (msg != null) {
try {
m = msg;
i = 0;
rec(msg);
} catch(err) {
node.error(err.message);
}
try {
m = msg;
i = 0;
rec(msg);
} catch(err) {
node.error(err.message);
}
});
}