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

new-style callback function (yaml node)

This commit is contained in:
Kunihiko Toumura 2019-11-29 16:59:58 +09:00
parent 0eda0a4935
commit 1177aa8aca

View File

@ -6,33 +6,35 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.property = n.property||"payload"; this.property = n.property||"payload";
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg,send,done) {
var value = RED.util.getMessageProperty(msg,node.property); var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) { if (value !== undefined) {
if (typeof value === "string") { if (typeof value === "string") {
try { try {
value = yaml.load(value); value = yaml.load(value);
RED.util.setMessageProperty(msg,node.property,value); RED.util.setMessageProperty(msg,node.property,value);
node.send(msg); send(msg);
done();
} }
catch(e) { node.error(e.message,msg); } catch(e) { done(e.message); }
} }
else if (typeof value === "object") { else if (typeof value === "object") {
if (!Buffer.isBuffer(value)) { if (!Buffer.isBuffer(value)) {
try { try {
value = yaml.dump(value); value = yaml.dump(value);
RED.util.setMessageProperty(msg,node.property,value); RED.util.setMessageProperty(msg,node.property,value);
node.send(msg); send(msg);
done();
} }
catch(e) { catch(e) {
node.error(RED._("yaml.errors.dropped-error")); done(RED._("yaml.errors.dropped-error"));
} }
} }
else { node.warn(RED._("yaml.errors.dropped-object")); } else { node.warn(RED._("yaml.errors.dropped-object")); done(); }
} }
else { node.warn(RED._("yaml.errors.dropped")); } else { node.warn(RED._("yaml.errors.dropped")); done(); }
} }
else { node.send(msg); } // If no payload - just pass it on. else { send(msg); done(); } // If no payload - just pass it on.
}); });
} }
RED.nodes.registerType("yaml",YAMLNode); RED.nodes.registerType("yaml",YAMLNode);