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
|
||||
var RED = require("../../red/red");
|
||||
var idList = [];
|
||||
|
||||
// main node definition
|
||||
function DelayNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.timeout = n.timeout * 1000;
|
||||
this.name = n.name
|
||||
this.name = n.name;
|
||||
this.idList = [];
|
||||
|
||||
this.on("input", function(msg) {
|
||||
var node= this;
|
||||
var id;
|
||||
id = setTimeout(function(){
|
||||
idList.splice(idList.indexOf(id),1);
|
||||
node.idList.splice(node.idList.indexOf(id),1);
|
||||
node.send(msg);
|
||||
}, node.timeout);
|
||||
idList.push(id);
|
||||
this.idList.push(id);
|
||||
});
|
||||
}
|
||||
|
||||
// register node
|
||||
RED.nodes.registerType("pause",DelayNode);
|
||||
RED.nodes.registerType("delay",DelayNode);
|
||||
|
||||
DelayNode.prototype.close = function() {
|
||||
for (var i=0; i<idList.length; i++ ) {
|
||||
clearTimeout(idList[i]);
|
||||
for (var i=0; i<this.idList.length; i++ ) {
|
||||
clearTimeout(this.idList[i]);
|
||||
}
|
||||
this.idList = [];
|
||||
}
|
||||
|
@ -15,10 +15,10 @@
|
||||
-->
|
||||
|
||||
<!-- 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">
|
||||
<label for="node-input-topic"><i class="icon-tasks"></i> Pause</label>
|
||||
<input type="text" id="node-input-timeout" placeholder="Time">
|
||||
<label for="node-input-topic"><i class="icon-tasks"></i> Rate</label>
|
||||
<input type="text" id="node-input-timeout" placeholder="msg/second">
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
@ -28,25 +28,25 @@
|
||||
</script>
|
||||
|
||||
<!-- Next, some simple help text is provided for the node. -->
|
||||
<script type="text/x-red" data-help-name="pause">
|
||||
<p>Introduces a pause into a flow</p>
|
||||
<p>Default pause is 5 seconds but can be configured</p>
|
||||
<script type="text/x-red" data-help-name="rateLimit">
|
||||
<p>Rate limts msgs</p>
|
||||
<p>Default is 1 msg per seconds but can be configured</p>
|
||||
</script>
|
||||
|
||||
<!-- Finally, the node type is registered along with all of its properties -->
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('pause',{
|
||||
RED.nodes.registerType('rateLimit',{
|
||||
category: 'function', // the palette category
|
||||
color:"#E6E0F8",
|
||||
defaults: { // defines the editable properties of the node
|
||||
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
|
||||
outputs:1, // set the number of outputs - 0 to n
|
||||
icon: "arrow-in.png", // set the icon (held in public/icons)
|
||||
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
|
||||
return this.name?"node_label_italic":"";
|
@ -18,31 +18,33 @@
|
||||
|
||||
// Require main module
|
||||
var RED = require("../../red/red");
|
||||
var idList = [];
|
||||
|
||||
|
||||
// main node definition
|
||||
function PauseNode(n) {
|
||||
function RateLimitNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.timeout = n.timeout * 1000;
|
||||
this.buffer = [];
|
||||
this.timeout = 1000/n.rate;
|
||||
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) {
|
||||
var node= this;
|
||||
var id;
|
||||
id = setTimeout(function(){
|
||||
idList.splice(idList.indexOf(id),1);
|
||||
node.send(msg);
|
||||
}, node.timeout);
|
||||
idList.push(id);
|
||||
this.buffer.push(msg);
|
||||
});
|
||||
}
|
||||
|
||||
// register node
|
||||
RED.nodes.registerType("pause",PauseNode);
|
||||
RED.nodes.registerType("rateLimit",RateLimitNode);
|
||||
|
||||
PauseNode.prototype.close = function() {
|
||||
for (var i=0; i<idList.length; i++ ) {
|
||||
clearTimeout(idList[i]);
|
||||
}
|
||||
RateLimitNode.prototype.close = function() {
|
||||
clearInterval(this.intervalID);
|
||||
this.buffer = [];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user