Skip to content

Commit 98e152f

Browse files
author
Dave Conway-Jones
committed
in line with latest thinking
1 parent 358042c commit 98e152f

3 files changed

Lines changed: 54 additions & 36 deletions

File tree

nodes/core/logic/17-split.html

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
<!--
2+
Copyright JS Foundation and other contributors, http://js.foundation
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
116

217
<script type="text/x-red" data-template-name="split">
318
<div class="form-row">
@@ -73,19 +88,12 @@
7388
<option value="array">an Array</option>
7489
<option value="object">a key/value Object</option>
7590
<option value="merged">a merged Object</option>
76-
<option value="accus">an accumlated key/value Object</option>
77-
<option value="accum">an accumlated Object</option>
7891
</select>
7992
</div>
8093
<div class="form-row node-row-key">
8194
<label style="vertical-align:top; margin-top:7px;">using</label>
8295
<div style="display:inline-block">
83-
<input type="text" id="node-input-key" style="width:300px;">
84-
<div style="line-height:32px;">
85-
as the property key, and set the overall<br/>
86-
<code>msg.<span id="node-newtopic">topic</span></code>
87-
to <input type="text" id="node-input-topic" style="width:55%">
88-
</div>
96+
<input type="text" id="node-input-key" style="width:252px;"> as the key
8997
</div>
9098
</div>
9199
<div class="form-row node-row-joiner">
@@ -95,13 +103,16 @@
95103
<div class="form-row node-row-trigger" id="trigger-row">
96104
<label style="width:auto;">Send the message:</label>
97105
<ul>
98-
<li style="height:40px;">
99-
<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;">
106+
<li>
107+
<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;">
100108
</li>
101-
<li style="height:40px;">
102-
<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;">
109+
<li style="list-style-type:none;">
110+
<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.
103111
</li>
104-
<li style="height:40px;">
112+
<li>
113+
<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;">
114+
</li>
115+
<li>
105116
<label style="width:auto; padding-top:6px;">After a message with the <code>msg.complete</code> property set</label>
106117
</li>
107118
</ul>
@@ -129,12 +140,6 @@
129140
<li>a <b>key/value object</b> - created by using a property of each message to determine the key under which
130141
the required value is stored.</li>
131142
<li>a <b>merged object</b> - created by merging the property of each message under a single object.</li>
132-
<li>an <b>accumulated object</b> - created by merging payload objects into a single object
133-
continuously. Outputs every message once the required number has been reached. <code>msg.complete</code> will
134-
send the msg and then clear the accumlation.</li>
135-
<li>an <b>accumulated key/value object</b> - created by merging payloads of each message under a single object
136-
using the specified property as a key. Outputs every message once the required number has been reached.
137-
<code>msg.complete</code> will send the msg and then clear the accumlation.</li>
138143
</ul>
139144
The other properties of the output message are taken from the last message received before the result is sent.</p>
140145
<p>A <i>count</i> can be set for how many messages should be received before generating the output message</p>
@@ -164,7 +169,8 @@
164169
propertyType: { value:"msg"},
165170
key: {value:"topic"},
166171
joiner: { value:"\\n"},
167-
topic: { value:""},
172+
accumulate: { value:"false" },
173+
//topic: { value:""},
168174
timeout: {value:""},
169175
count: {value:""}
170176
},
@@ -198,10 +204,6 @@
198204
// else { $("#trigger-row").show(); }
199205
});
200206

201-
$("#node-input-key").change(function(e) {
202-
$("#node-newtopic").text($("#node-input-key").val());
203-
});
204-
205207
$("#node-input-property").typedInput({
206208
typeField: $("#node-input-propertyType"),
207209
types:['msg', {value:"full", label:"complete message", hasValue:false}]

nodes/core/logic/17-split.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright JS Foundation and other contributors, http://js.foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
116

217
module.exports = function(RED) {
318
"use strict";
@@ -92,14 +107,15 @@ module.exports = function(RED) {
92107
this.count = Number(n.count || 0);
93108
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");
94109
this.build = n.build || "array";
95-
this.topic = n.topic;
110+
this.accumulate = n.accumulate || "false";
111+
//this.topic = n.topic;
96112
var node = this;
97113
var inflight = {};
98114

99115
var completeSend = function(partId) {
100116
var group = inflight[partId];
101117
clearTimeout(group.timeout);
102-
if ((node.build !== "accum" && node.build !== "accus") || group.msg.hasOwnProperty("complete")) { delete inflight[partId]; }
118+
if ((node.accumulate !== true) || group.msg.hasOwnProperty("complete")) { delete inflight[partId]; }
103119
if (group.type === 'string') {
104120
RED.util.setMessageProperty(group.msg,node.property,group.payload.join(group.joinChar));
105121
} else {
@@ -157,11 +173,11 @@ module.exports = function(RED) {
157173
if (targetCount === 0 && msg.hasOwnProperty('parts')) {
158174
targetCount = msg.parts.count || 0;
159175
}
160-
if ((node.build === 'object') || (node.build === 'accus')) {
176+
if (node.build === 'object') {
161177
propertyKey = RED.util.getMessageProperty(msg,node.key);
162178
}
163179
}
164-
if (((payloadType === 'object') || (payloadType === 'accus')) && (propertyKey === null || propertyKey === undefined || propertyKey === "")) {
180+
if ((payloadType === 'object') && (propertyKey === null || propertyKey === undefined || propertyKey === "")) {
165181
if (node.mode === "auto") {
166182
node.warn("Message missing 'msg.parts.key' property - cannot add to object");
167183
} else {
@@ -179,7 +195,7 @@ module.exports = function(RED) {
179195
msg:msg
180196
};
181197
}
182-
else if (payloadType === 'accum' || payloadType === 'accus') {
198+
else if (node.accumulate === true) {
183199
if (msg.hasOwnProperty("reset")) { delete inflight[partId]; }
184200
inflight[partId] = inflight[partId] || {
185201
currentCount:0,
@@ -210,12 +226,12 @@ module.exports = function(RED) {
210226
}
211227

212228
var group = inflight[partId];
213-
if ((payloadType === 'object') || (payloadType === 'accus')) {
229+
if (payloadType === 'object') {
214230
group.payload[propertyKey] = property;
215231
group.currentCount = Object.keys(group.payload).length;
216-
msg.topic = node.topic || msg.topic;
232+
//msg.topic = node.topic || msg.topic;
217233
}
218-
else if ((payloadType === 'merged') || (payloadType === 'accum')) {
234+
else if (payloadType === 'merged') {
219235
if (Array.isArray(property) || typeof property !== 'object') {
220236
if (!msg.hasOwnProperty("complete")) {
221237
node.warn("Cannot merge non-object types");

test/nodes/core/logic/17-split_spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ describe('JOIN node', function() {
225225
});
226226

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

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

294294
it('should accumulate a key/value object', function(done) {
295-
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"accus", mode:"custom", topic:"bar", key:"foo", count:4},
295+
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"object", accumulate:true, mode:"custom", topic:"bar", key:"foo", count:4},
296296
{id:"n2", type:"helper"}];
297297
helper.load(joinNode, flow, function() {
298298
var n1 = helper.getNode("n1");
299299
var n2 = helper.getNode("n2");
300300
var c = 0;
301301
n2.on("input", function(msg) {
302302
try {
303-
msg.should.have.property("topic","bar");
303+
//msg.should.have.property("topic","bar");
304304
msg.should.have.property("payload");
305305
msg.payload.should.have.property("a",1);
306306
msg.payload.should.have.property("b",2);

0 commit comments

Comments
 (0)