Link Call should not call into subflow...

- includes missing jsdoc
- improves speed (no searching, only lookups)
- code formatting consistency
- improve tests
This commit is contained in:
Steve-Mcl
2022-02-28 13:57:22 +00:00
parent e653a933f1
commit 249f7e45fb
2 changed files with 175 additions and 85 deletions

View File

@@ -21,14 +21,15 @@
* @property {string} name - Name of target Node
* @property {number} flowId - ID of flow where the target node exists
* @property {string} flowName - Name of flow where the target node exists
* @property {boolean} isSubFlow - True if the link-in node exists in a subflow instance
*/
module.exports = function(RED) {
"use strict";
const crypto = require("crypto");
const targetCache = (function() {
const registry = { ids: {}, named: {}};
const targetCache = (function () {
const registry = { id: {}, name: {} };
function getIndex(/** @type {[LinkTarget]}*/ targets, id) {
for (let index = 0; index < (targets || []).length; index++) {
const element = targets[index];
@@ -46,21 +47,26 @@ module.exports = function(RED) {
function generateTarget(node) {
const isSubFlow = node._flow.TYPE === "subflow";
return {
id: node.id,
name: node.name || node.id,
id: node.id,
name: node.name || node.id,
flowId: node._flow.flow.id,
flowName: isSubFlow ? node._flow.subflowDef.name : node._flow.flow.label,
flowName: isSubFlow ? node._flow.subflowDef.name : node._flow.flow.label,
isSubFlow: isSubFlow
}
}
return {
/**
* Get a list of targets registerd to this name
* @param {string} name
* @param {string} name Name of the target
* @param {boolean} [excludeSubflows] set `true` to exclude
* @returns {[LinkTarget]} Targets registerd to this name.
*/
getTargets(name) {
return registry.named[name] || [];
getTargets(name, excludeSubflows) {
const targets = registry.name[name] || [];
if (excludeSubflows) {
return targets.filter(e => e.isSubFlow != true);
}
return targets;
},
/**
* Get a single target by registered name.
@@ -70,17 +76,17 @@ module.exports = function(RED) {
* @param {string} [flowId]
* @returns {LinkTarget} target
*/
getTarget(name, flowId) {
getTarget(name, flowId) {
/** @type {[LinkTarget]}*/
let possibleTargets = this.getTargets(name);
/** @type {LinkTarget}*/ let target;
if(possibleTargets.length) {
if(flowId) {
possibleTargets = possibleTargets.filter(e => e.flowId == flowId);
}
/** @type {LinkTarget}*/
let target;
if (possibleTargets.length && flowId) {
possibleTargets = possibleTargets.filter(e => e.flowId == flowId);
}
if(possibleTargets.length === 1) {
if (possibleTargets.length === 1) {
target = possibleTargets[0];
}
}
return target;
},
/**
@@ -89,35 +95,35 @@ module.exports = function(RED) {
* @returns {LinkTarget} target
*/
getTargetById(nodeId) {
return registry.ids[nodeId];
},
return registry.id[nodeId];
},
register(/** @type {LinkInNode} */ node) {
const target = generateTarget(node);
const tByName = this.getTarget(target.name, target.flowId);
if(!tByName) {
registry.named[target.name] = registry.named[target.name] || [];
registry.named[target.name].push(target)
if (!tByName || tByName.id !== target.id) {
registry.name[target.name] = registry.name[target.name] || [];
registry.name[target.name].push(target)
}
registry.ids[target.id] = target;
registry.id[target.id] = target;
return target;
},
remove(node) {
const target = generateTarget(node);
const tn = this.getTarget(target.name, target.flowId);
if(tn) {
if (tn) {
const targs = this.getTargets(tn.name);
const idx = getIndex(targs, tn.id);
if(idx > -1) {
targs.splice(idx,1);
if (idx > -1) {
targs.splice(idx, 1);
}
if(targs.length === 0) {
delete registry.named[tn.name];
if (targs.length === 0) {
delete registry.name[tn.name];
}
}
delete registry.ids[target.id];
delete registry.id[target.id];
},
clear() {
registry = { ids: {}, named: {}};
registry = { id: {}, name: {} };
}
}
})();
@@ -187,17 +193,27 @@ module.exports = function(RED) {
if (isNaN(timeout)) {
timeout = 30000;
}
function findNode(target) {
let foundNode = RED.nodes.getNode(target); //1st see if the target is a direct node id
if(!foundNode) {
//first look in this flow for the node
function getTargetNode(msg) {
const dynamicMode = linkType === "dynamic";
const target = dynamicMode ? msg.target : staticTarget
////1st see if the target is a direct node id
let foundNode;
if (targetCache.getTargetById(target)) {
foundNode = RED.nodes.getNode(target)
}
if (target && !foundNode && dynamicMode) {
//next, look in **this flow only** for the node
let cachedTarget = targetCache.getTarget(target, node._flow.flow.id);
if(!cachedTarget) {
//single target node not found in registry! get all possible targets
const possibleTargets = targetCache.getTargets(target);
if(possibleTargets.length === 1) {
if (!cachedTarget) {
//single target node not found in registry!
//get all possible targets from regular flows (exclude subflow instances)
const possibleTargets = targetCache.getTargets(target, true);
if (possibleTargets.length === 1) {
//only 1 link-in found with this name - good, lets use it
cachedTarget = possibleTargets[0];
} else if (possibleTargets.length > 1) {
//more than 1 link-in has this name, raise an error
throw new Error(`Multiple link-in nodes named '${target}' found`);
}
}
@@ -205,31 +221,31 @@ module.exports = function(RED) {
foundNode = RED.nodes.getNode(cachedTarget.id);
}
}
if(foundNode instanceof LinkInNode) {
if (foundNode instanceof LinkInNode) {
return foundNode;
}
throw new Error(`link-in node '${target}' not found`);
throw new Error(`target link-in node '${target || ""}' not found`);
}
this.on("input", function(msg, send, done) {
this.on("input", function (msg, send, done) {
try {
let targetNode = linkType == "dynamic" ? findNode(msg.target) : RED.nodes.getNode(staticTarget);
if (targetNode && targetNode instanceof LinkInNode) {
const targetNode = getTargetNode(msg);
if (targetNode instanceof LinkInNode) {
msg._linkSource = msg._linkSource || [];
const messageEvent = {
id: crypto.randomBytes(14).toString('hex'),
node: node.id,
}
}
messageEvents[messageEvent.id] = {
msg: RED.util.cloneMessage(msg),
send,
done,
ts: setTimeout(function() {
ts: setTimeout(function () {
timeoutMessage(messageEvent.id)
}, timeout )
}, timeout)
};
msg._linkSource.push(messageEvent);
targetNode.receive(msg);
}
}
} catch (error) {
node.error(error, msg);
}