Skip to content

Commit

Permalink
defer node improvements: invert, timeout via msg/context/env (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbyquaker committed May 12, 2019
1 parent 4d9484e commit 6411450
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 74 deletions.
54 changes: 20 additions & 34 deletions nodes/combine-defer.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
defaults: {
topic: {value: ''},
name: {value: ''},
timeout: {value: 1}
invert: {value: false, type: 'bool'},
timeout: {value: 1},
timeoutType: {value: 'num'}
},
inputs: 1,
outputs: 1,
Expand All @@ -13,61 +15,45 @@
color: '#D8BFD8',
paletteLabel: 'defer',
label() {
return this.name || ('defer ' + this.defer + ' s');
return this.name || ('defer ' + this.timeout + ' s');
},
oneditprepare() {
/*
$('#node-input-tPayload').typedInput({
default: 'bool',
types: ['bool', 'num', 'string'],
typeField: '#node-input-tPayloadType'
$('#node-input-timeout').typedInput({
default: 'num',
types: ['num', 'msg', 'flow', 'global', 'env'],
typeField: '#node-input-timeoutType'
});
*/
}
});
</script>

<script type="text/x-red" data-template-name="combine-defer">
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
<input type="text" id="node-input-topic">

<label for="node-input-name"><i class="icon-bookmark"></i> Name</label>
<input type="text" id="node-input-name">
</div>
<!--
<div class="form-row">
<label for="node-input-condition"><i class="icon-tasks"></i> Payload</label>
<select id="node-input-condition">
<option value="eq">==</option>
<option value="neq">!=</option>
<option value="lt">&lt;</option>
<option value="gt">&gt;</option>
<option value="lte">&lt;=</option>
<option value="gte">&gt;=</option>
<option value="btwn">is between</option>
</select>
<input id="node-input-tPayload">
<input type="hidden" id="node-input-tPayloadType">
<input type="number" id="node-input-payload" style="display:none">
<input type="number" id="node-input-lower" style="display:none">
<input type="number" id="node-input-upper" style="display:none">
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
<input type="text" id="node-input-topic">
</div>
-->
<div class="form-row">
<label for="node-input-timeout"><i class="fa fa-history"></i> Timeout</label>
<input type="number" id="node-input-timeout">
<input type="text" id="node-input-timeout">
<input type="hidden" id="node-input-timeoutType">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-bookmark"></i> Name</label>
<input type="text" id="node-input-name">
<label for="node-input-invert"><i class="icon-tasks"></i> Invert</label>
<select id="node-input-invert">
<option>false</option>
<option>true</option>
</select>
</div>

<style>
#node-input-condition {
width: 120px;
}


</style>

</script>

<script type="text/x-red" data-help-name="combine-defer">
Expand Down
89 changes: 52 additions & 37 deletions nodes/combine-defer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,67 @@ module.exports = function (RED) {
constructor(config) {
RED.nodes.createNode(this, config);

if (config.invert === 'false') {
config.invert = false;
} else {
config.invert = Boolean(config.invert);
}

this.config = config;

this.state = {};
this.timer = {};

this.on('input', msg => {
/*
let match;
switch (config.condition) {
case 'eq':
match = msg.payload === config.tPayload;
break;
case 'neq':
match = msg.payload !== config.tPayload;
break;
case 'gt':
match = msg.payload > config.payload;
break;
case 'lt':
match = msg.payload < config.payload;
break;
case 'gte':
match = msg.payload <= config.payload;
break;
case 'lte':
match = msg.payload >= config.payload;
break;
case 'btwn':
match = (msg.payload >= config.lower) && (msg.payload <= config.upper);
break;
default:
}
*/
if (msg.payload) {
/* istanbul ignore else */
if (!this.timer[msg.topic] && !this.state[msg.topic]) {
this.state[msg.topic] = true;
if (Boolean(msg.payload) === config.invert) {
clearTimeout(this.timer[msg.topic]);
this.timer[msg.topic] = null;
this.state[msg.topic] = false;
this.status({});
} else if (!this.timer[msg.topic] && !this.state[msg.topic]) {
this.state[msg.topic] = true;

this.getTimeout(msg).then(timeout => {
this.timer[msg.topic] = setTimeout(() => {
this.timer[msg.topic] = null;
this.send(msg);
this.status({fill: 'blue', shape: 'dot'});
}, config.timeout * 1000);
}, timeout * 1000);
this.status({fill: 'blue', shape: 'ring'});
});
}
});
}

getTimeout(msg) {
return new Promise((resolve, reject) => {
const type = this.config.timeoutType;
const val = this.config.timeout;

switch (type) {
case 'msg':
resolve(RED.util.getMessageProperty(msg, val));
break;

case 'flow':
case 'global': {
const contextKey = RED.util.parseContextStore(val);
this.context()[type].get(contextKey.key, contextKey.store, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
break;
}
} else {
clearTimeout(this.timer[msg.topic]);
this.timer[msg.topic] = null;
this.state[msg.topic] = false;
this.status({});

case 'env':
resolve(RED.util.evaluateNodeProperty(val, 'env', this));
break;

default:
resolve(val);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-combine",
"version": "1.7.1",
"version": "1.8.0",
"description": "Node-RED Nodes that output combinations of consecutive incoming messages",
"keywords": [
"node-red",
Expand Down Expand Up @@ -75,7 +75,8 @@
"html"
],
"rules": {
"capitalized-comments": "off"
"capitalized-comments": "off",
"promise/prefer-await-to-then": "off"
}
}
}

0 comments on commit 6411450

Please sign in to comment.