mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Update Function node to add node.done
This commit is contained in:
@@ -19,7 +19,7 @@ module.exports = function(RED) {
|
||||
var util = require("util");
|
||||
var vm = require("vm");
|
||||
|
||||
function sendResults(node,_msgid,msgs) {
|
||||
function sendResults(node,send,_msgid,msgs) {
|
||||
if (msgs == null) {
|
||||
return;
|
||||
} else if (!util.isArray(msgs)) {
|
||||
@@ -49,7 +49,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
}
|
||||
if (msgCount>0) {
|
||||
node.send(msgs);
|
||||
send(msgs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,8 +58,17 @@ module.exports = function(RED) {
|
||||
var node = this;
|
||||
this.name = n.name;
|
||||
this.func = n.func;
|
||||
|
||||
var handleNodeDoneCall = true;
|
||||
// Check to see if the Function appears to call `node.done()`. If so,
|
||||
// we will assume it is well written and does actually call node.done().
|
||||
// Otherwise, we will call node.done() after the function returns regardless.
|
||||
if (/node\.done\s*\(\s*\)/.test(this.func)) {
|
||||
handleNodeDoneCall = false;
|
||||
}
|
||||
|
||||
var functionText = "var results = null;"+
|
||||
"results = (function(msg){ "+
|
||||
"results = (function(msg,__send__,__done__){ "+
|
||||
"var __msgid__ = msg._msgid;"+
|
||||
"var node = {"+
|
||||
"id:__node__.id,"+
|
||||
@@ -71,10 +80,11 @@ module.exports = function(RED) {
|
||||
"trace:__node__.trace,"+
|
||||
"on:__node__.on,"+
|
||||
"status:__node__.status,"+
|
||||
"send:function(msgs){ __node__.send(__msgid__,msgs);}"+
|
||||
"send:function(msgs){ __node__.send(__send__,__msgid__,msgs);},"+
|
||||
"done:__done__"+
|
||||
"};\n"+
|
||||
this.func+"\n"+
|
||||
"})(msg);";
|
||||
"})(msg,send,done);";
|
||||
this.topic = n.topic;
|
||||
this.outstandingTimers = [];
|
||||
this.outstandingIntervals = [];
|
||||
@@ -104,8 +114,8 @@ module.exports = function(RED) {
|
||||
trace: function() {
|
||||
node.trace.apply(node, arguments);
|
||||
},
|
||||
send: function(id, msgs) {
|
||||
sendResults(node, id, msgs);
|
||||
send: function(send, id, msgs) {
|
||||
sendResults(node, send, id, msgs);
|
||||
},
|
||||
on: function() {
|
||||
if (arguments[0] === "input") {
|
||||
@@ -223,12 +233,18 @@ module.exports = function(RED) {
|
||||
// lineOffset: -11, // line number offset to be used for stack traces
|
||||
// columnOffset: 0, // column number offset to be used for stack traces
|
||||
});
|
||||
this.on("input", function(msg) {
|
||||
this.on("input", function(msg,send,done) {
|
||||
try {
|
||||
var start = process.hrtime();
|
||||
context.msg = msg;
|
||||
context.send = send;
|
||||
context.done = done;
|
||||
|
||||
this.script.runInContext(context);
|
||||
sendResults(this,msg._msgid,context.results);
|
||||
sendResults(this,send,msg._msgid,context.results);
|
||||
if (handleNodeDoneCall) {
|
||||
done();
|
||||
}
|
||||
|
||||
var duration = process.hrtime(start);
|
||||
var converted = Math.floor((duration[0] * 1e9 + duration[1])/10000)/100;
|
||||
|
Reference in New Issue
Block a user