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

evaluate init value on each reduction

This commit is contained in:
Hiroyasu Nishiyama 2018-01-29 22:30:47 +09:00
parent 9353d5c1c4
commit 3f2a92e801
2 changed files with 42 additions and 4 deletions

View File

@ -273,7 +273,7 @@ module.exports = function(RED) {
var is_right = node.reduce_right; var is_right = node.reduce_right;
var flag = is_right ? -1 : 1; var flag = is_right ? -1 : 1;
var msgs = group.msgs; var msgs = group.msgs;
var accum = node.reduce_init; var accum = eval_exp(node, node.exp_init, node.exp_init_type);
var reduce_exp = node.reduce_exp; var reduce_exp = node.reduce_exp;
var reduce_fixup = node.reduce_fixup; var reduce_fixup = node.reduce_fixup;
var count = group.count; var count = group.count;
@ -389,13 +389,12 @@ module.exports = function(RED) {
this.reduce = (this.mode === "reduce"); this.reduce = (this.mode === "reduce");
if (this.reduce) { if (this.reduce) {
var exp_init = n.reduceInit; this.exp_init = n.reduceInit;
var exp_init_type = n.reduceInitType; this.exp_init_type = n.reduceInitType;
var exp_reduce = n.reduceExp; var exp_reduce = n.reduceExp;
var exp_fixup = exp_or_undefined(n.reduceFixup); var exp_fixup = exp_or_undefined(n.reduceFixup);
this.reduce_right = n.reduceRight; this.reduce_right = n.reduceRight;
try { try {
this.reduce_init = eval_exp(this, exp_init, exp_init_type);
this.reduce_exp = RED.util.prepareJSONataExpression(exp_reduce, this); this.reduce_exp = RED.util.prepareJSONataExpression(exp_reduce, this);
this.reduce_fixup = (exp_fixup !== undefined) ? RED.util.prepareJSONataExpression(exp_fixup, this) : undefined; this.reduce_fixup = (exp_fixup !== undefined) ? RED.util.prepareJSONataExpression(exp_fixup, this) : undefined;
} catch(e) { } catch(e) {

View File

@ -874,6 +874,45 @@ describe('JOIN node', function() {
}); });
}); });
it('should redece messages with array result', function(done) {
var flow = [{id:"n1", type:"join", mode:"reduce",
reduceRight:false,
reduceExp:"$append($A,[payload])",
reduceInit:"[]",
reduceInitType:"json",
reduceFixup:undefined,
wires:[["n2"]]},
{id:"n2", type:"helper"}];
helper.load(joinNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var count = 0;
n2.on("input", function(msg) {
try {
msg.should.have.property("payload");
msg.payload.should.be.an.Array();
var payload = msg.payload;
payload.length.should.equal(2);
if (count == 0) {
payload[0].should.equal(1);
payload[1].should.equal(2);
}
else if (count == 1){
payload[0].should.equal(3);
payload[1].should.equal(4);
done();
}
count++;
}
catch(e) { done(e); }
});
n1.receive({payload:1, parts:{index:0, count:2, id:222}});
n1.receive({payload:2, parts:{index:1, count:2, id:222}});
n1.receive({payload:3, parts:{index:2, count:2, id:333}});
n1.receive({payload:4, parts:{index:3, count:2, id:333}});
});
});
it('should handle too many pending messages for reduce mode', function(done) { it('should handle too many pending messages for reduce mode', function(done) {
var flow = [{id:"n1", type:"join", mode:"reduce", var flow = [{id:"n1", type:"join", mode:"reduce",
reduceRight:false, reduceRight:false,