Skip to content

Commit 6411450

Browse files
committed
defer node improvements: invert, timeout via msg/context/env (#12)
1 parent 4d9484e commit 6411450

File tree

4 files changed

+76
-74
lines changed

4 files changed

+76
-74
lines changed

nodes/combine-defer.html

+20-34
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
defaults: {
55
topic: {value: ''},
66
name: {value: ''},
7-
timeout: {value: 1}
7+
invert: {value: false, type: 'bool'},
8+
timeout: {value: 1},
9+
timeoutType: {value: 'num'}
810
},
911
inputs: 1,
1012
outputs: 1,
@@ -13,61 +15,45 @@
1315
color: '#D8BFD8',
1416
paletteLabel: 'defer',
1517
label() {
16-
return this.name || ('defer ' + this.defer + ' s');
18+
return this.name || ('defer ' + this.timeout + ' s');
1719
},
1820
oneditprepare() {
19-
/*
20-
$('#node-input-tPayload').typedInput({
21-
default: 'bool',
22-
types: ['bool', 'num', 'string'],
23-
typeField: '#node-input-tPayloadType'
21+
$('#node-input-timeout').typedInput({
22+
default: 'num',
23+
types: ['num', 'msg', 'flow', 'global', 'env'],
24+
typeField: '#node-input-timeoutType'
2425
});
25-
*/
2626
}
2727
});
2828
</script>
2929

3030
<script type="text/x-red" data-template-name="combine-defer">
3131
<div class="form-row">
32-
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
33-
<input type="text" id="node-input-topic">
34-
32+
<label for="node-input-name"><i class="icon-bookmark"></i> Name</label>
33+
<input type="text" id="node-input-name">
3534
</div>
36-
<!--
3735
<div class="form-row">
38-
<label for="node-input-condition"><i class="icon-tasks"></i> Payload</label>
39-
<select id="node-input-condition">
40-
<option value="eq">==</option>
41-
<option value="neq">!=</option>
42-
<option value="lt">&lt;</option>
43-
<option value="gt">&gt;</option>
44-
<option value="lte">&lt;=</option>
45-
<option value="gte">&gt;=</option>
46-
<option value="btwn">is between</option>
47-
</select>
48-
<input id="node-input-tPayload">
49-
<input type="hidden" id="node-input-tPayloadType">
50-
<input type="number" id="node-input-payload" style="display:none">
51-
<input type="number" id="node-input-lower" style="display:none">
52-
<input type="number" id="node-input-upper" style="display:none">
36+
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
37+
<input type="text" id="node-input-topic">
5338
</div>
54-
-->
5539
<div class="form-row">
5640
<label for="node-input-timeout"><i class="fa fa-history"></i> Timeout</label>
57-
<input type="number" id="node-input-timeout">
41+
<input type="text" id="node-input-timeout">
42+
<input type="hidden" id="node-input-timeoutType">
5843
</div>
5944
<div class="form-row">
60-
<label for="node-input-name"><i class="icon-bookmark"></i> Name</label>
61-
<input type="text" id="node-input-name">
45+
<label for="node-input-invert"><i class="icon-tasks"></i> Invert</label>
46+
<select id="node-input-invert">
47+
<option>false</option>
48+
<option>true</option>
49+
</select>
6250
</div>
51+
6352
<style>
6453
#node-input-condition {
6554
width: 120px;
6655
}
67-
68-
6956
</style>
70-
7157
</script>
7258

7359
<script type="text/x-red" data-help-name="combine-defer">

nodes/combine-defer.js

+52-37
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,67 @@ module.exports = function (RED) {
33
constructor(config) {
44
RED.nodes.createNode(this, config);
55

6+
if (config.invert === 'false') {
7+
config.invert = false;
8+
} else {
9+
config.invert = Boolean(config.invert);
10+
}
11+
12+
this.config = config;
13+
614
this.state = {};
715
this.timer = {};
16+
817
this.on('input', msg => {
9-
/*
10-
let match;
11-
switch (config.condition) {
12-
case 'eq':
13-
match = msg.payload === config.tPayload;
14-
break;
15-
case 'neq':
16-
match = msg.payload !== config.tPayload;
17-
break;
18-
case 'gt':
19-
match = msg.payload > config.payload;
20-
break;
21-
case 'lt':
22-
match = msg.payload < config.payload;
23-
break;
24-
case 'gte':
25-
match = msg.payload <= config.payload;
26-
break;
27-
case 'lte':
28-
match = msg.payload >= config.payload;
29-
break;
30-
case 'btwn':
31-
match = (msg.payload >= config.lower) && (msg.payload <= config.upper);
32-
break;
33-
default:
34-
}
35-
*/
36-
if (msg.payload) {
37-
/* istanbul ignore else */
38-
if (!this.timer[msg.topic] && !this.state[msg.topic]) {
39-
this.state[msg.topic] = true;
18+
if (Boolean(msg.payload) === config.invert) {
19+
clearTimeout(this.timer[msg.topic]);
20+
this.timer[msg.topic] = null;
21+
this.state[msg.topic] = false;
22+
this.status({});
23+
} else if (!this.timer[msg.topic] && !this.state[msg.topic]) {
24+
this.state[msg.topic] = true;
25+
26+
this.getTimeout(msg).then(timeout => {
4027
this.timer[msg.topic] = setTimeout(() => {
4128
this.timer[msg.topic] = null;
4229
this.send(msg);
4330
this.status({fill: 'blue', shape: 'dot'});
44-
}, config.timeout * 1000);
31+
}, timeout * 1000);
4532
this.status({fill: 'blue', shape: 'ring'});
33+
});
34+
}
35+
});
36+
}
37+
38+
getTimeout(msg) {
39+
return new Promise((resolve, reject) => {
40+
const type = this.config.timeoutType;
41+
const val = this.config.timeout;
42+
43+
switch (type) {
44+
case 'msg':
45+
resolve(RED.util.getMessageProperty(msg, val));
46+
break;
47+
48+
case 'flow':
49+
case 'global': {
50+
const contextKey = RED.util.parseContextStore(val);
51+
this.context()[type].get(contextKey.key, contextKey.store, (err, res) => {
52+
if (err) {
53+
reject(err);
54+
} else {
55+
resolve(res);
56+
}
57+
});
58+
break;
4659
}
47-
} else {
48-
clearTimeout(this.timer[msg.topic]);
49-
this.timer[msg.topic] = null;
50-
this.state[msg.topic] = false;
51-
this.status({});
60+
61+
case 'env':
62+
resolve(RED.util.evaluateNodeProperty(val, 'env', this));
63+
break;
64+
65+
default:
66+
resolve(val);
5267
}
5368
});
5469
}

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-contrib-combine",
3-
"version": "1.7.1",
3+
"version": "1.8.0",
44
"description": "Node-RED Nodes that output combinations of consecutive incoming messages",
55
"keywords": [
66
"node-red",
@@ -75,7 +75,8 @@
7575
"html"
7676
],
7777
"rules": {
78-
"capitalized-comments": "off"
78+
"capitalized-comments": "off",
79+
"promise/prefer-await-to-then": "off"
7980
}
8081
}
8182
}

0 commit comments

Comments
 (0)