From 3f2a92e8016b6ed6e5a4b8055cbae7913ecb6c36 Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Mon, 29 Jan 2018 22:30:47 +0900 Subject: [PATCH] evaluate init value on each reduction --- nodes/core/logic/17-split.js | 7 ++--- test/nodes/core/logic/17-split_spec.js | 39 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/nodes/core/logic/17-split.js b/nodes/core/logic/17-split.js index 8cabbb5d0..92b527d01 100644 --- a/nodes/core/logic/17-split.js +++ b/nodes/core/logic/17-split.js @@ -273,7 +273,7 @@ module.exports = function(RED) { var is_right = node.reduce_right; var flag = is_right ? -1 : 1; 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_fixup = node.reduce_fixup; var count = group.count; @@ -389,13 +389,12 @@ module.exports = function(RED) { this.reduce = (this.mode === "reduce"); if (this.reduce) { - var exp_init = n.reduceInit; - var exp_init_type = n.reduceInitType; + this.exp_init = n.reduceInit; + this.exp_init_type = n.reduceInitType; var exp_reduce = n.reduceExp; var exp_fixup = exp_or_undefined(n.reduceFixup); this.reduce_right = n.reduceRight; try { - this.reduce_init = eval_exp(this, exp_init, exp_init_type); this.reduce_exp = RED.util.prepareJSONataExpression(exp_reduce, this); this.reduce_fixup = (exp_fixup !== undefined) ? RED.util.prepareJSONataExpression(exp_fixup, this) : undefined; } catch(e) { diff --git a/test/nodes/core/logic/17-split_spec.js b/test/nodes/core/logic/17-split_spec.js index 273b03374..296e942d5 100644 --- a/test/nodes/core/logic/17-split_spec.js +++ b/test/nodes/core/logic/17-split_spec.js @@ -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) { var flow = [{id:"n1", type:"join", mode:"reduce", reduceRight:false,