mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Fix some scope issues in delay node and add rate limiting node
This commit is contained in:
parent
a4fb33ee73
commit
dd293da6fb
@ -18,31 +18,32 @@
|
|||||||
|
|
||||||
// Require main module
|
// Require main module
|
||||||
var RED = require("../../red/red");
|
var RED = require("../../red/red");
|
||||||
var idList = [];
|
|
||||||
|
|
||||||
// main node definition
|
// main node definition
|
||||||
function DelayNode(n) {
|
function DelayNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
|
|
||||||
this.timeout = n.timeout * 1000;
|
this.timeout = n.timeout * 1000;
|
||||||
this.name = n.name
|
this.name = n.name;
|
||||||
|
this.idList = [];
|
||||||
|
|
||||||
this.on("input", function(msg) {
|
this.on("input", function(msg) {
|
||||||
var node= this;
|
var node= this;
|
||||||
var id;
|
var id;
|
||||||
id = setTimeout(function(){
|
id = setTimeout(function(){
|
||||||
idList.splice(idList.indexOf(id),1);
|
node.idList.splice(node.idList.indexOf(id),1);
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
}, node.timeout);
|
}, node.timeout);
|
||||||
idList.push(id);
|
this.idList.push(id);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// register node
|
// register node
|
||||||
RED.nodes.registerType("pause",DelayNode);
|
RED.nodes.registerType("delay",DelayNode);
|
||||||
|
|
||||||
DelayNode.prototype.close = function() {
|
DelayNode.prototype.close = function() {
|
||||||
for (var i=0; i<idList.length; i++ ) {
|
for (var i=0; i<this.idList.length; i++ ) {
|
||||||
clearTimeout(idList[i]);
|
clearTimeout(this.idList[i]);
|
||||||
}
|
}
|
||||||
|
this.idList = [];
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,10 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<!-- First, the content of the edit dialog is defined. -->
|
<!-- First, the content of the edit dialog is defined. -->
|
||||||
<script type="text/x-red" data-template-name="pause">
|
<script type="text/x-red" data-template-name="rateLimit">
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-topic"><i class="icon-tasks"></i> Pause</label>
|
<label for="node-input-topic"><i class="icon-tasks"></i> Rate</label>
|
||||||
<input type="text" id="node-input-timeout" placeholder="Time">
|
<input type="text" id="node-input-timeout" placeholder="msg/second">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
@ -28,25 +28,25 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Next, some simple help text is provided for the node. -->
|
<!-- Next, some simple help text is provided for the node. -->
|
||||||
<script type="text/x-red" data-help-name="pause">
|
<script type="text/x-red" data-help-name="rateLimit">
|
||||||
<p>Introduces a pause into a flow</p>
|
<p>Rate limts msgs</p>
|
||||||
<p>Default pause is 5 seconds but can be configured</p>
|
<p>Default is 1 msg per seconds but can be configured</p>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Finally, the node type is registered along with all of its properties -->
|
<!-- Finally, the node type is registered along with all of its properties -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
RED.nodes.registerType('pause',{
|
RED.nodes.registerType('rateLimit',{
|
||||||
category: 'function', // the palette category
|
category: 'function', // the palette category
|
||||||
color:"#E6E0F8",
|
color:"#E6E0F8",
|
||||||
defaults: { // defines the editable properties of the node
|
defaults: { // defines the editable properties of the node
|
||||||
name: {value:""}, // along with default values.
|
name: {value:""}, // along with default values.
|
||||||
timeout: {value:"5", required:true, validate:RED.validators.number()}
|
rate: {value:"1", required:true, validate:RED.validators.number()}
|
||||||
},
|
},
|
||||||
inputs:1, // set the number of inputs - only 0 or 1
|
inputs:1, // set the number of inputs - only 0 or 1
|
||||||
outputs:1, // set the number of outputs - 0 to n
|
outputs:1, // set the number of outputs - 0 to n
|
||||||
icon: "arrow-in.png", // set the icon (held in public/icons)
|
icon: "arrow-in.png", // set the icon (held in public/icons)
|
||||||
label: function() { // sets the default label contents
|
label: function() { // sets the default label contents
|
||||||
return this.name||"pause "+this.timeout+" s";
|
return this.name||this.topic||"limit "+this.rate+" msg/s";
|
||||||
},
|
},
|
||||||
labelStyle: function() { // sets the class to apply to the label
|
labelStyle: function() { // sets the class to apply to the label
|
||||||
return this.name?"node_label_italic":"";
|
return this.name?"node_label_italic":"";
|
@ -18,31 +18,33 @@
|
|||||||
|
|
||||||
// Require main module
|
// Require main module
|
||||||
var RED = require("../../red/red");
|
var RED = require("../../red/red");
|
||||||
var idList = [];
|
|
||||||
|
|
||||||
// main node definition
|
// main node definition
|
||||||
function PauseNode(n) {
|
function RateLimitNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
|
this.buffer = [];
|
||||||
this.timeout = n.timeout * 1000;
|
this.timeout = 1000/n.rate;
|
||||||
this.name = n.name
|
this.name = n.name
|
||||||
|
|
||||||
|
var node= this
|
||||||
|
|
||||||
|
this.intervalID = setInterval(function() {
|
||||||
|
if (node.buffer.length > 0) {
|
||||||
|
node.send(node.buffer.shift());
|
||||||
|
}
|
||||||
|
},this.timeout);
|
||||||
|
|
||||||
this.on("input", function(msg) {
|
this.on("input", function(msg) {
|
||||||
var node= this;
|
this.buffer.push(msg);
|
||||||
var id;
|
|
||||||
id = setTimeout(function(){
|
|
||||||
idList.splice(idList.indexOf(id),1);
|
|
||||||
node.send(msg);
|
|
||||||
}, node.timeout);
|
|
||||||
idList.push(id);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// register node
|
// register node
|
||||||
RED.nodes.registerType("pause",PauseNode);
|
RED.nodes.registerType("rateLimit",RateLimitNode);
|
||||||
|
|
||||||
PauseNode.prototype.close = function() {
|
RateLimitNode.prototype.close = function() {
|
||||||
for (var i=0; i<idList.length; i++ ) {
|
clearInterval(this.intervalID);
|
||||||
clearTimeout(idList[i]);
|
this.buffer = [];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user