Merge pull request #4021 from node-red/inject-async-context

Allow Inject node to work with async context stores
This commit is contained in:
Nick O'Leary 2023-02-02 10:22:59 +00:00 committed by GitHub
commit 055d0081e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 29 deletions

View File

@ -95,45 +95,64 @@ module.exports = function(RED) {
} }
this.on("input", function(msg, send, done) { this.on("input", function(msg, send, done) {
var errors = []; const errors = [];
var props = this.props; let props = this.props;
if (msg.__user_inject_props__ && Array.isArray(msg.__user_inject_props__)) { if (msg.__user_inject_props__ && Array.isArray(msg.__user_inject_props__)) {
props = msg.__user_inject_props__; props = msg.__user_inject_props__;
} }
delete msg.__user_inject_props__; delete msg.__user_inject_props__;
props.forEach(p => { props = [...props]
var property = p.p; function evaluateProperty(doneEvaluating) {
var value = p.v ? p.v : ''; if (props.length === 0) {
var valueType = p.vt ? p.vt : 'str'; doneEvaluating()
return
}
const p = props.shift()
const property = p.p;
const value = p.v ? p.v : '';
const valueType = p.vt ? p.vt : 'str';
if (!property) return; if (property) {
if (valueType === "jsonata") {
if (valueType === "jsonata") { if (p.v) {
if (p.v) { try {
try { var exp = RED.util.prepareJSONataExpression(p.v, node);
var exp = RED.util.prepareJSONataExpression(p.v, node); var val = RED.util.evaluateJSONataExpression(exp, msg);
var val = RED.util.evaluateJSONataExpression(exp, msg); RED.util.setMessageProperty(msg, property, val, true);
RED.util.setMessageProperty(msg, property, val, true); }
catch (err) {
errors.push(err.message);
}
} }
catch (err) { evaluateProperty(doneEvaluating)
errors.push(err.message); } else {
try {
RED.util.evaluateNodeProperty(value, valueType, node, msg, (err, newValue) => {
if (err) {
errors.push(err.toString())
} else {
RED.util.setMessageProperty(msg,property,newValue,true);
}
evaluateProperty(doneEvaluating)
})
} catch (err) {
errors.push(err.toString());
evaluateProperty(doneEvaluating)
} }
} }
return; } else {
evaluateProperty(doneEvaluating)
} }
try {
RED.util.setMessageProperty(msg,property,RED.util.evaluateNodeProperty(value, valueType, this, msg),true);
} catch (err) {
errors.push(err.toString());
}
});
if (errors.length) {
done(errors.join('; '));
} else {
send(msg);
done();
} }
evaluateProperty(() => {
if (errors.length) {
done(errors.join('; '));
} else {
send(msg);
done();
}
})
}); });
} }