Skip to content

Commit

Permalink
in line with latest thinking
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Conway-Jones committed Jun 5, 2017
1 parent 358042c commit 98e152f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
50 changes: 26 additions & 24 deletions nodes/core/logic/17-split.html
@@ -1,3 +1,18 @@
<!--
Copyright JS Foundation and other contributors, http://js.foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<script type="text/x-red" data-template-name="split">
<div class="form-row">
Expand Down Expand Up @@ -73,19 +88,12 @@
<option value="array">an Array</option>
<option value="object">a key/value Object</option>
<option value="merged">a merged Object</option>
<option value="accus">an accumlated key/value Object</option>
<option value="accum">an accumlated Object</option>
</select>
</div>
<div class="form-row node-row-key">
<label style="vertical-align:top; margin-top:7px;">using</label>
<div style="display:inline-block">
<input type="text" id="node-input-key" style="width:300px;">
<div style="line-height:32px;">
as the property key, and set the overall<br/>
<code>msg.<span id="node-newtopic">topic</span></code>
to <input type="text" id="node-input-topic" style="width:55%">
</div>
<input type="text" id="node-input-key" style="width:252px;"> as the key
</div>
</div>
<div class="form-row node-row-joiner">
Expand All @@ -95,13 +103,16 @@
<div class="form-row node-row-trigger" id="trigger-row">
<label style="width:auto;">Send the message:</label>
<ul>
<li style="height:40px;">
<label style="width:280px;" for="node-input-count">After a number of message parts:</label> <input id="node-input-count" placeholder="count" type="text" style="width: 75px;">
<li>
<label style="width:280px;" for="node-input-count">After a number of message parts</label> <input id="node-input-count" placeholder="count" type="text" style="width:75px;">
</li>
<li style="height:40px;">
<label style="width:280px;" for="node-input-timeout">After a timeout following the first message:</label> <input id="node-input-timeout" placeholder="seconds" type="text" style="width: 75px;">
<li style="list-style-type:none;">
<input type="checkbox" id="node-input-accumulate" style="display:inline-block; width:20px; list-style-type:none; margin-left:20px; vertical-align:top;"> and every subsequent message.
</li>
<li style="height:40px;">
<li>
<label style="width:280px;" for="node-input-timeout">After a timeout following the first message</label> <input id="node-input-timeout" placeholder="seconds" type="text" style="width:75px;">
</li>
<li>
<label style="width:auto; padding-top:6px;">After a message with the <code>msg.complete</code> property set</label>
</li>
</ul>
Expand Down Expand Up @@ -129,12 +140,6 @@
<li>a <b>key/value object</b> - created by using a property of each message to determine the key under which
the required value is stored.</li>
<li>a <b>merged object</b> - created by merging the property of each message under a single object.</li>
<li>an <b>accumulated object</b> - created by merging payload objects into a single object
continuously. Outputs every message once the required number has been reached. <code>msg.complete</code> will
send the msg and then clear the accumlation.</li>
<li>an <b>accumulated key/value object</b> - created by merging payloads of each message under a single object
using the specified property as a key. Outputs every message once the required number has been reached.
<code>msg.complete</code> will send the msg and then clear the accumlation.</li>
</ul>
The other properties of the output message are taken from the last message received before the result is sent.</p>
<p>A <i>count</i> can be set for how many messages should be received before generating the output message</p>
Expand Down Expand Up @@ -164,7 +169,8 @@
propertyType: { value:"msg"},
key: {value:"topic"},
joiner: { value:"\\n"},
topic: { value:""},
accumulate: { value:"false" },
//topic: { value:""},
timeout: {value:""},
count: {value:""}
},
Expand Down Expand Up @@ -198,10 +204,6 @@
// else { $("#trigger-row").show(); }
});

$("#node-input-key").change(function(e) {
$("#node-newtopic").text($("#node-input-key").val());
});

$("#node-input-property").typedInput({
typeField: $("#node-input-propertyType"),
types:['msg', {value:"full", label:"complete message", hasValue:false}]
Expand Down
32 changes: 24 additions & 8 deletions nodes/core/logic/17-split.js
@@ -1,3 +1,18 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

module.exports = function(RED) {
"use strict";
Expand Down Expand Up @@ -92,14 +107,15 @@ module.exports = function(RED) {
this.count = Number(n.count || 0);
this.joiner = (n.joiner||"").replace(/\\n/g,"\n").replace(/\\r/g,"\r").replace(/\\t/g,"\t").replace(/\\e/g,"\e").replace(/\\f/g,"\f").replace(/\\0/g,"\0");
this.build = n.build || "array";
this.topic = n.topic;
this.accumulate = n.accumulate || "false";
//this.topic = n.topic;
var node = this;
var inflight = {};

var completeSend = function(partId) {
var group = inflight[partId];
clearTimeout(group.timeout);
if ((node.build !== "accum" && node.build !== "accus") || group.msg.hasOwnProperty("complete")) { delete inflight[partId]; }
if ((node.accumulate !== true) || group.msg.hasOwnProperty("complete")) { delete inflight[partId]; }
if (group.type === 'string') {
RED.util.setMessageProperty(group.msg,node.property,group.payload.join(group.joinChar));
} else {
Expand Down Expand Up @@ -157,11 +173,11 @@ module.exports = function(RED) {
if (targetCount === 0 && msg.hasOwnProperty('parts')) {
targetCount = msg.parts.count || 0;
}
if ((node.build === 'object') || (node.build === 'accus')) {
if (node.build === 'object') {
propertyKey = RED.util.getMessageProperty(msg,node.key);
}
}
if (((payloadType === 'object') || (payloadType === 'accus')) && (propertyKey === null || propertyKey === undefined || propertyKey === "")) {
if ((payloadType === 'object') && (propertyKey === null || propertyKey === undefined || propertyKey === "")) {
if (node.mode === "auto") {
node.warn("Message missing 'msg.parts.key' property - cannot add to object");
} else {
Expand All @@ -179,7 +195,7 @@ module.exports = function(RED) {
msg:msg
};
}
else if (payloadType === 'accum' || payloadType === 'accus') {
else if (node.accumulate === true) {
if (msg.hasOwnProperty("reset")) { delete inflight[partId]; }
inflight[partId] = inflight[partId] || {
currentCount:0,
Expand Down Expand Up @@ -210,12 +226,12 @@ module.exports = function(RED) {
}

var group = inflight[partId];
if ((payloadType === 'object') || (payloadType === 'accus')) {
if (payloadType === 'object') {
group.payload[propertyKey] = property;
group.currentCount = Object.keys(group.payload).length;
msg.topic = node.topic || msg.topic;
//msg.topic = node.topic || msg.topic;
}
else if ((payloadType === 'merged') || (payloadType === 'accum')) {
else if (payloadType === 'merged') {
if (Array.isArray(property) || typeof property !== 'object') {
if (!msg.hasOwnProperty("complete")) {
node.warn("Cannot merge non-object types");
Expand Down
8 changes: 4 additions & 4 deletions test/nodes/core/logic/17-split_spec.js
Expand Up @@ -225,7 +225,7 @@ describe('JOIN node', function() {
});

it('should accumulate a merged object', function(done) {
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"accum",mode:"custom"},
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"merged",mode:"custom",accumulate:true},
{id:"n2", type:"helper"}];
helper.load(joinNode, flow, function() {
var n1 = helper.getNode("n1");
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('JOIN node', function() {
});

it('should be able to reset an accumulation', function(done) {
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"accum",mode:"custom"},
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"merged",accumulate:true,mode:"custom"},
{id:"n2", type:"helper"}];
helper.load(joinNode, flow, function() {
var n1 = helper.getNode("n1");
Expand Down Expand Up @@ -292,15 +292,15 @@ describe('JOIN node', function() {
});

it('should accumulate a key/value object', function(done) {
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"accus", mode:"custom", topic:"bar", key:"foo", count:4},
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"object", accumulate:true, mode:"custom", topic:"bar", key:"foo", count:4},
{id:"n2", type:"helper"}];
helper.load(joinNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
try {
msg.should.have.property("topic","bar");
//msg.should.have.property("topic","bar");
msg.should.have.property("payload");
msg.payload.should.have.property("a",1);
msg.payload.should.have.property("b",2);
Expand Down

0 comments on commit 98e152f

Please sign in to comment.