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

Remove promises from Change node

This commit is contained in:
Nick O'Leary 2018-12-21 14:36:45 +00:00
parent 5fa4d227b8
commit d5ef428edd
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -98,7 +98,7 @@ module.exports = function(RED) {
} }
} }
function getToValue(msg,rule) { function getToValue(msg,rule,done) {
var value = rule.to; var value = rule.to;
if (rule.tot === 'json') { if (rule.tot === 'json') {
value = JSON.parse(rule.to); value = JSON.parse(rule.to);
@ -107,52 +107,33 @@ module.exports = function(RED) {
} }
if (rule.tot === "msg") { if (rule.tot === "msg") {
value = RED.util.getMessageProperty(msg,rule.to); value = RED.util.getMessageProperty(msg,rule.to);
} else if ((rule.tot === 'flow') || } else if ((rule.tot === 'flow') || (rule.tot === 'global')) {
(rule.tot === 'global')) {
return new Promise((resolve,reject) => {
RED.util.evaluateNodeProperty(rule.to, rule.tot, node, msg, (err,value) => { RED.util.evaluateNodeProperty(rule.to, rule.tot, node, msg, (err,value) => {
if (err) { if (err) {
resolve(undefined); done(undefined,undefined);
} else { } else {
resolve(value); done(undefined,value);
} }
}); });
}); return
} else if (rule.tot === 'date') { } else if (rule.tot === 'date') {
value = Date.now(); value = Date.now();
} else if (rule.tot === 'jsonata') { } else if (rule.tot === 'jsonata') {
return new Promise((resolve,reject) => {
RED.util.evaluateJSONataExpression(rule.to,msg, (err, value) => { RED.util.evaluateJSONataExpression(rule.to,msg, (err, value) => {
if (err) { if (err) {
reject(RED._("change.errors.invalid-expr",{error:err.message})) done(RED._("change.errors.invalid-expr",{error:err.message}))
} else { } else {
resolve(value); done(undefined, value);
} }
}); });
}); return;
} }
return Promise.resolve(value); done(undefined,value);
} }
function getFromValue(msg,rule) {
var fromValue; function getFromValueType(fromValue, done) {
var fromType; var fromType;
var fromRE; var fromRE;
if (rule.t === 'change') {
if (rule.fromt === 'msg' || rule.fromt === 'flow' || rule.fromt === 'global') {
return new Promise((resolve,reject) => {
if (rule.fromt === "msg") {
resolve(RED.util.getMessageProperty(msg,rule.from));
} else if (rule.fromt === 'flow' || rule.fromt === 'global') {
var contextKey = RED.util.parseContextStore(rule.from);
node.context()[rule.fromt].get(contextKey.key, contextKey.store, (err,fromValue) => {
if (err) {
reject(err);
} else {
resolve(fromValue);
}
});
}
}).then(fromValue => {
if (typeof fromValue === 'number' || fromValue instanceof Number) { if (typeof fromValue === 'number' || fromValue instanceof Number) {
fromType = 'num'; fromType = 'num';
} else if (typeof fromValue === 'boolean') { } else if (typeof fromValue === 'boolean') {
@ -166,38 +147,66 @@ module.exports = function(RED) {
try { try {
fromRE = new RegExp(fromRE, "g"); fromRE = new RegExp(fromRE, "g");
} catch (e) { } catch (e) {
throw new Error(RED._("change.errors.invalid-from",{error:e.message})); done(new Error(RED._("change.errors.invalid-from",{error:e.message})));
} }
} else { } else {
throw new Error(RED._("change.errors.invalid-from",{error:"unsupported type: "+(typeof fromValue)})); done(new Error(RED._("change.errors.invalid-from",{error:"unsupported type: "+(typeof fromValue)})));
} }
return { done(undefined,{
fromType, fromType,
fromValue, fromValue,
fromRE fromRE
});
}
function getFromValue(msg,rule, done) {
var fromValue;
var fromType;
var fromRE;
if (rule.t === 'change') {
if (rule.fromt === 'msg' || rule.fromt === 'flow' || rule.fromt === 'global') {
if (rule.fromt === "msg") {
return getFromValueType(RED.util.getMessageProperty(msg,rule.from),done);
} else if (rule.fromt === 'flow' || rule.fromt === 'global') {
var contextKey = RED.util.parseContextStore(rule.from);
node.context()[rule.fromt].get(contextKey.key, contextKey.store, (err,fromValue) => {
if (err) {
done(err)
} else {
getFromValueType(fromValue,done);
} }
}); });
return;
}
} else { } else {
fromType = rule.fromt; fromType = rule.fromt;
fromValue = rule.from; fromValue = rule.from;
fromRE = rule.fromRE; fromRE = rule.fromRE;
} }
} }
return Promise.resolve({ done(undefined, {
fromType, fromType,
fromValue, fromValue,
fromRE fromRE
}); });
} }
function applyRule(msg,rule) { function applyRule(msg,rule,done) {
var property = rule.p; var property = rule.p;
var current; var current;
var fromValue; var fromValue;
var fromType; var fromType;
var fromRE; var fromRE;
try { try {
return getToValue(msg,rule).then(value => { getToValue(msg,rule,(err,value) => {
return getFromValue(msg,rule).then(fromParts => { if (err) {
node.error(err, msg);
return done(undefined,null);
} else {
getFromValue(msg,rule,(err,fromParts) => {
if (err) {
node.error(err, msg);
return done(undefined,null);
} else {
fromValue = fromParts.fromValue; fromValue = fromParts.fromValue;
fromType = fromParts.fromType; fromType = fromParts.fromType;
fromRE = fromParts.fromRE; fromRE = fromParts.fromRE;
@ -229,16 +238,16 @@ module.exports = function(RED) {
} }
} }
} catch(err) {} } catch(err) {}
return msg; return done(undefined,msg);
} else if (rule.pt === 'flow' || rule.pt === 'global') { } else if (rule.pt === 'flow' || rule.pt === 'global') {
var contextKey = RED.util.parseContextStore(property); var contextKey = RED.util.parseContextStore(property);
return new Promise((resolve,reject) => {
var target = node.context()[rule.pt]; var target = node.context()[rule.pt];
var callback = err => { var callback = err => {
if (err) { if (err) {
reject(err); node.error(err, msg);
return done(undefined,null);
} else { } else {
resolve(msg); done(undefined,msg);
} }
} }
if (rule.t === 'delete') { if (rule.t === 'delete') {
@ -248,8 +257,8 @@ module.exports = function(RED) {
} else if (rule.t === 'change') { } else if (rule.t === 'change') {
target.get(contextKey.key,contextKey.store,(err,current) => { target.get(contextKey.key,contextKey.store,(err,current) => {
if (err) { if (err) {
reject(err); node.error(err, msg);
return; return done(undefined,null);
} }
if (typeof current === 'string') { if (typeof current === 'string') {
if ((fromType === 'num' || fromType === 'bool' || fromType === 'str') && current === fromValue) { if ((fromType === 'num' || fromType === 'bool' || fromType === 'str') && current === fromValue) {
@ -271,58 +280,63 @@ module.exports = function(RED) {
} }
}); });
} }
});
} }
}); }
}).catch(err => { })
node.error(err, msg); }
return null;
}); });
} catch(err) { } catch(err) {
return Promise.resolve(msg); // This is an okay error
done(undefined,msg);
} }
} }
function applyRules(msg, currentRule) { function completeApplyingRules(msg,currentRule,done) {
if (!msg) {
return done();
} else if (currentRule === node.rules.length - 1) {
return done(undefined, msg);
} else {
applyRules(msg, currentRule+1,done);
}
}
function applyRules(msg, currentRule, done) {
if (currentRule >= node.rules.length) { if (currentRule >= node.rules.length) {
return Promise.resolve(msg); return done(undefined,msg);
} }
var r = node.rules[currentRule]; var r = node.rules[currentRule];
var rulePromise; var rulePromise;
if (r.t === "move") { if (r.t === "move") {
if ((r.tot !== r.pt) || (r.p.indexOf(r.to) !== -1)) { if ((r.tot !== r.pt) || (r.p.indexOf(r.to) !== -1)) {
rulePromise = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:r.p, tot:r.pt}).then( applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:r.p, tot:r.pt},(err,msg) => {
msg => applyRule(msg,{t:"delete", p:r.p, pt:r.pt}) applyRule(msg,{t:"delete", p:r.p, pt:r.pt}, (err,msg) => {
); completeApplyingRules(msg,currentRule,done);
} })
else { // 2 step move if we are moving from a child });
rulePromise = applyRule(msg,{t:"set", p:"_temp_move", pt:r.tot, to:r.p, tot:r.pt}).then( } else { // 2 step move if we are moving from a child
msg => applyRule(msg,{t:"delete", p:r.p, pt:r.pt}) applyRule(msg,{t:"set", p:"_temp_move", pt:r.tot, to:r.p, tot:r.pt},(err,msg)=> {
).then( applyRule(msg,{t:"delete", p:r.p, pt:r.pt},(err,msg)=> {
msg => applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:"_temp_move", tot:r.pt}) applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:"_temp_move", tot:r.pt},(err,msg)=> {
).then( applyRule(msg,{t:"delete", p:"_temp_move", pt:r.pt},(err,msg)=> {
msg => applyRule(msg,{t:"delete", p:"_temp_move", pt:r.pt}) completeApplyingRules(msg,currentRule,done);
) });
});
});
});
} }
} else { } else {
rulePromise = applyRule(msg,r); applyRule(msg,r,(err,msg)=> { completeApplyingRules(msg,currentRule,done); });
}
return rulePromise.then(
msg => {
if (!msg) {
return
} else if (currentRule === node.rules.length - 1) {
return msg;
} else {
return applyRules(msg, currentRule+1);
} }
} }
);
}
if (valid) { if (valid) {
this.on('input', function(msg) { this.on('input', function(msg) {
applyRules(msg, 0) applyRules(msg, 0, (err,msg) => {
.then( msg => { if (msg) { node.send(msg) }} ) if (err) {
.catch( err => node.error(err, msg)) node.error(err,msg);
} else if (msg) {
node.send(msg);
}
})
}); });
} }
} }