1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

let default apply if msg.delay not set in override mode. (#1397)

* let default apply if msg.delay not set in override mode.

* Update tests to match

* allow msg.delay to be 0 if wanted

and test for that
This commit is contained in:
Dave Conway-Jones 2017-10-10 21:40:09 +01:00 committed by Nick O'Leary
parent 53bfe12ac1
commit ae7c298b1a
4 changed files with 16 additions and 9 deletions

View File

@ -102,7 +102,7 @@
<dt class="optional">delay <span class="property-type">number</span></dt> <dt class="optional">delay <span class="property-type">number</span></dt>
<dd>Sets the delay, in milliseconds, to be applied to the message. This <dd>Sets the delay, in milliseconds, to be applied to the message. This
option only applies if the node is configured to allow the message to option only applies if the node is configured to allow the message to
provide the delay interval.</dd> override the configured default delay interval.</dd>
<dt class="optional">reset</dt> <dt class="optional">reset</dt>
<dd>If the received message has this property set to any value, all <dd>If the received message has this property set to any value, all
outstanding messages held by the node are cleared without being sent.</dd> outstanding messages held by the node are cleared without being sent.</dd>
@ -260,7 +260,7 @@
$("#delay-details-for").show(); $("#delay-details-for").show();
$("#random-details").hide(); $("#random-details").hide();
} else if (this.value === "delayv") { } else if (this.value === "delayv") {
$("#delay-details-for").hide(); $("#delay-details-for").show();
$("#random-details").hide(); $("#random-details").hide();
} else if (this.value === "random") { } else if (this.value === "random") {
$("#delay-details-for").hide(); $("#delay-details-for").hide();

View File

@ -105,7 +105,10 @@ module.exports = function(RED) {
} }
else if (node.pauseType === "delayv") { else if (node.pauseType === "delayv") {
node.on("input", function(msg) { node.on("input", function(msg) {
var delayvar = Number(msg.delay || 0); var delayvar = Number(node.timeout);
if (msg.hasOwnProperty("delay") && !isNaN(parseFloat(msg.delay))) {
delayvar = parseFloat(msg.delay);
}
if (delayvar < 0) { delayvar = 0; } if (delayvar < 0) { delayvar = 0; }
var id = setTimeout(function() { var id = setTimeout(function() {
node.idList.splice(node.idList.indexOf(id),1); node.idList.splice(node.idList.indexOf(id),1);
@ -113,7 +116,7 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
}, delayvar); }, delayvar);
node.idList.push(id); node.idList.push(id);
if ((delayvar >= 1) && (node.idList.length !== 0)) { if ((delayvar >= 0) && (node.idList.length !== 0)) {
node.status({fill:"blue",shape:"dot",text:delayvar/1000+"s"}); node.status({fill:"blue",shape:"dot",text:delayvar/1000+"s"});
} }
if (msg.hasOwnProperty("reset")) { clearDelayList(); } if (msg.hasOwnProperty("reset")) { clearDelayList(); }

View File

@ -204,7 +204,7 @@
"for": "For", "for": "For",
"delaymsg": "Delay each message", "delaymsg": "Delay each message",
"delayfixed": "Fixed delay", "delayfixed": "Fixed delay",
"delayvarmsg": "Set delay with msg.delay", "delayvarmsg": "Override delay with msg.delay",
"randomdelay": "Random delay", "randomdelay": "Random delay",
"limitrate": "Rate Limit", "limitrate": "Rate Limit",
"limitall": "All messages", "limitall": "All messages",

View File

@ -347,7 +347,7 @@ describe('delay Node', function() {
* @param delay - the variable delay: milliseconds * @param delay - the variable delay: milliseconds
*/ */
function variableDelayTest(aTimeoutFrom, aTimeoutTo, aTimeoutUnit, delay, done) { function variableDelayTest(aTimeoutFrom, aTimeoutTo, aTimeoutUnit, delay, done) {
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delayv","timeout":5,"timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":aTimeoutFrom,"randomLast":aTimeoutTo,"randomUnits":aTimeoutUnit,"drop":false,"wires":[["helperNode1"]]}, var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delayv","timeout":0.5,"timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":aTimeoutFrom,"randomLast":aTimeoutTo,"randomUnits":aTimeoutUnit,"drop":false,"wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(delayNode, flow, function() { helper.load(delayNode, flow, function() {
var delayNode1 = helper.getNode("delayNode1"); var delayNode1 = helper.getNode("delayNode1");
@ -400,12 +400,16 @@ describe('delay Node', function() {
variableDelayTest("200", "300", "milliseconds", 250, done); variableDelayTest("200", "300", "milliseconds", 250, done);
}); });
it('variable delay is zero if msg.delay not specified', function(done) { it('variable delay is the default if msg.delay not specified', function(done) {
variableDelayTest("0", "50", "milliseconds", null, done); variableDelayTest("450", "550", "milliseconds", null, done);
});
it('variable delay is zero if msg.delay is zero', function(done) {
variableDelayTest("0", "20", "milliseconds", 0, done);
}); });
it('variable delay is zero if msg.delay is negative', function(done) { it('variable delay is zero if msg.delay is negative', function(done) {
variableDelayTest("0", "50", "milliseconds", -250, done); variableDelayTest("0", "20", "milliseconds", -250, done);
}); });
/** /**