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

Add timeout option to link-call node

This commit is contained in:
Nick O'Leary 2021-09-29 14:28:12 +01:00
parent 1931395fdb
commit dfd9364061
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 28 additions and 2 deletions

View File

@ -28,6 +28,10 @@
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"> <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div> </div>
<div class="form-row">
<label for="node-input-timeout"><span data-i18n="exec.label.timeout"></span></label>
<input type="text" id="node-input-timeout" placeholder="30" style="width: 70px; margin-right: 5px;"><span data-i18n="inject.seconds"></span>
</div>
<div style="position:relative; height: 30px; text-align: right;"><div style="display:inline-block"><input type="text" id="node-input-link-target-filter"></div></div> <div style="position:relative; height: 30px; text-align: right;"><div style="display:inline-block"><input type="text" id="node-input-link-target-filter"></div></div>
<div class="form-row node-input-link-row"></div> <div class="form-row node-input-link-row"></div>
</script> </script>
@ -240,7 +244,8 @@
color:"#ddd",//"#87D8CF", color:"#ddd",//"#87D8CF",
defaults: { defaults: {
name: {value:""}, name: {value:""},
links: { value: [], type:"link in[]"} links: { value: [], type:"link in[]"},
timeout: { value: "30", validate:RED.validators.number(true) }
}, },
inputs: 1, inputs: 1,
outputs: 1, outputs: 1,

View File

@ -76,6 +76,10 @@ module.exports = function(RED) {
const node = this; const node = this;
const target = n.links[0]; const target = n.links[0];
const messageEvents = {}; const messageEvents = {};
let timeout = parseFloat(n.timeout || "30")*1000;
if (isNaN(timeout)) {
timeout = 30000;
}
this.on("input", function(msg, send, done) { this.on("input", function(msg, send, done) {
msg._linkSource = msg._linkSource || []; msg._linkSource = msg._linkSource || [];
@ -83,7 +87,14 @@ module.exports = function(RED) {
id: crypto.randomBytes(14).toString('hex'), id: crypto.randomBytes(14).toString('hex'),
node: node.id, node: node.id,
} }
messageEvents[messageEvent.id] = { send, done }; messageEvents[messageEvent.id] = {
msg: RED.util.cloneMessage(msg),
send,
done,
ts: setTimeout(function() {
timeoutMessage(messageEvent.id)
}, timeout )
};
msg._linkSource.push(messageEvent); msg._linkSource.push(messageEvent);
var targetNode = RED.nodes.getNode(target); var targetNode = RED.nodes.getNode(target);
if (targetNode) { if (targetNode) {
@ -97,6 +108,7 @@ module.exports = function(RED) {
} }
const messageEvent = messageEvents[eventId]; const messageEvent = messageEvents[eventId];
if (messageEvent) { if (messageEvent) {
clearTimeout(messageEvent.ts);
delete messageEvents[eventId]; delete messageEvents[eventId];
messageEvent.send(msg); messageEvent.send(msg);
messageEvent.done(); messageEvent.done();
@ -104,6 +116,15 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
} }
} }
function timeoutMessage(eventId) {
const messageEvent = messageEvents[eventId];
if (messageEvent) {
delete messageEvents[eventId];
node.error("timeout",messageEvent.msg);
}
}
} }
RED.nodes.registerType("link call",LinkCallNode); RED.nodes.registerType("link call",LinkCallNode);