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

Add async context support to Change node

This commit is contained in:
Nick O'Leary 2018-07-07 22:23:26 +01:00
parent afb566b6b4
commit 1b693eed37
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 163 additions and 123 deletions

View File

@ -99,6 +99,7 @@ module.exports = function(RED) {
} }
function applyRule(msg,rule) { function applyRule(msg,rule) {
return new Promise(function(resolve, reject){
try { try {
var property = rule.p; var property = rule.p;
var value = rule.to; var value = rule.to;
@ -150,11 +151,11 @@ module.exports = function(RED) {
fromRE = new RegExp(fromRE, "g"); fromRE = new RegExp(fromRE, "g");
} catch (e) { } catch (e) {
valid = false; valid = false;
node.error(RED._("change.errors.invalid-from",{error:e.message}),msg); reject(RED._("change.errors.invalid-from",{error:e.message}));
return; return;
} }
} else { } else {
node.error(RED._("change.errors.invalid-from",{error:"unsupported type: "+(typeof fromValue)}),msg); reject(RED._("change.errors.invalid-from",{error:"unsupported type: "+(typeof fromValue)}));
return return
} }
} else { } else {
@ -226,31 +227,47 @@ module.exports = function(RED) {
} }
} }
} catch(err) {/*console.log(err.stack)*/} } catch(err) {/*console.log(err.stack)*/}
resolve(msg);
});
}
function applyRules(msg, currentRule) {
var r = node.rules[currentRule];
var rulePromise;
if (r.t === "move") {
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(
msg => applyRule(msg,{t:"delete", p:r.p, pt:r.pt})
);
}
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(
msg => applyRule(msg,{t:"delete", p:r.p, pt:r.pt})
).then(
msg => applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:"_temp_move", tot:r.pt})
).then(
msg => applyRule(msg,{t:"delete", p:"_temp_move", pt:r.pt})
)
}
} else {
rulePromise = applyRule(msg,r);
}
return rulePromise.then(
msg => {
if (!msg) {
return
} else if (currentRule === node.rules.length - 1) {
return msg; return msg;
} else {
return applyRules(msg, currentRule+1);
}
}
);
} }
if (valid) { if (valid) {
this.on('input', function(msg) { this.on('input', function(msg) {
for (var i=0; i<this.rules.length; i++) { applyRules(msg, 0)
if (this.rules[i].t === "move") { .then( msg => { if (msg) { node.send(msg) }} )
var r = this.rules[i]; .catch( err => node.error(err, msg))
if ((r.tot !== r.pt) || (r.p.indexOf(r.to) !== -1)) {
msg = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:r.p, tot:r.pt});
applyRule(msg,{t:"delete", p:r.p, pt:r.pt});
}
else { // 2 step move if we are moving from a child
msg = applyRule(msg,{t:"set", p:"_temp_move", pt:r.tot, to:r.p, tot:r.pt});
applyRule(msg,{t:"delete", p:r.p, pt:r.pt});
msg = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:"_temp_move", tot:r.pt});
applyRule(msg,{t:"delete", p:"_temp_move", pt:r.pt});
}
} else {
msg = applyRule(msg,this.rules[i]);
}
if (msg === null) {
return;
}
}
node.send(msg);
}); });
} }
} }

View File

@ -15,6 +15,7 @@
**/ **/
var should = require("should"); var should = require("should");
var sinon = require("sinon");
var changeNode = require("../../../../nodes/core/logic/15-change.js"); var changeNode = require("../../../../nodes/core/logic/15-change.js");
var helper = require("node-red-node-test-helper"); var helper = require("node-red-node-test-helper");
@ -454,6 +455,28 @@ describe('change Node', function() {
}); });
}); });
it('reports invalid jsonata expression', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"$invalid(payload)","tot":"jsonata"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
sinon.spy(changeNode1,"error");
helperNode1.on("input", function(msg) {
done("Invalid jsonata expression passed message through");
});
changeNode1.receive({payload:"Hello World!"});
setTimeout(function() {
try {
changeNode1.error.called.should.be.true();
done();
} catch(err) {
done(err);
}
},50);
});
});
}); });
describe('#change', function() { describe('#change', function() {
it('changes the value of the message property', function(done) { it('changes the value of the message property', function(done) {