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

Support nodeMessageBufferMaxLength in delay node

This commit is contained in:
Kazuhito Yokoi 2019-09-25 18:10:33 +09:00
parent 8bc81c0d32
commit f81e0403ee
3 changed files with 58 additions and 16 deletions

View File

@ -20,6 +20,20 @@ module.exports = function(RED) {
var MILLIS_TO_NANOS = 1000000; var MILLIS_TO_NANOS = 1000000;
var SECONDS_TO_NANOS = 1000000000; var SECONDS_TO_NANOS = 1000000000;
var _maxKeptMsgsCount;
function maxKeptMsgsCount(node) {
if (_maxKeptMsgsCount === undefined) {
var name = "nodeMessageBufferMaxLength";
if (RED.settings.hasOwnProperty(name)) {
_maxKeptMsgsCount = RED.settings[name];
}
else {
_maxKeptMsgsCount = 0;
}
}
return _maxKeptMsgsCount;
}
function DelayNode(n) { function DelayNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -172,10 +186,16 @@ module.exports = function(RED) {
node.intervalID = setInterval(sendMsgFromBuffer, node.rate); node.intervalID = setInterval(sendMsgFromBuffer, node.rate);
} }
if (!msg.hasOwnProperty("flush")) { if (!msg.hasOwnProperty("flush")) {
var max_msgs = maxKeptMsgsCount(node);
if ((max_msgs > 0) && (node.buffer.length >= max_msgs)) {
node.buffer = [];
node.error(RED._("delay.errors.too-many"), msg);
} else {
node.buffer.push(msg); node.buffer.push(msg);
node.reportDepth(); node.reportDepth();
} }
} }
}
else { else {
if (msg.hasOwnProperty("rate")) { if (msg.hasOwnProperty("rate")) {
node.rate = msg.rate; node.rate = msg.rate;
@ -186,6 +206,21 @@ module.exports = function(RED) {
} }
} }
else { else {
if (maxKeptMsgsCount(node) > 0) {
if (node.intervalID === -1) {
node.send(msg);
node.intervalID = setInterval(sendMsgFromBuffer, node.rate);
} else {
if (msg.hasOwnProperty("rate") && node.rate !== msg.rate) {
node.rate = msg.rate;
clearInterval(node.intervalID);
node.intervalID = setInterval(sendMsgFromBuffer, node.rate);
}
if (node.buffer.length < _maxKeptMsgsCount) {
node.buffer.push(msg);
}
}
} else {
if (msg.hasOwnProperty("rate")) { if (msg.hasOwnProperty("rate")) {
node.rate = msg.rate; node.rate = msg.rate;
} }
@ -202,6 +237,7 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
} }
} }
}
if (msg.hasOwnProperty("reset")) { if (msg.hasOwnProperty("reset")) {
clearInterval(node.intervalID); clearInterval(node.intervalID);
node.intervalID = -1; node.intervalID = -1;

View File

@ -280,6 +280,9 @@
"singular": "Day" "singular": "Day"
} }
} }
},
"errors": {
"too-many" : "too many pending messages in delay node"
} }
}, },
"trigger": { "trigger": {

View File

@ -280,6 +280,9 @@
"singular": "日" "singular": "日"
} }
} }
},
"errors": {
"too-many": "delayード内で保持しているメッセージが多すぎます"
} }
}, },
"trigger": { "trigger": {