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

Merge pull request #3751 from Steve-Mcl/fix-linkcall-registry

Fix linkcall registry bugs
This commit is contained in:
Nick O'Leary 2022-07-11 20:27:08 +01:00 committed by GitHub
commit 542e9cacc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,23 +29,23 @@ module.exports = function(RED) {
"use strict";
const crypto = require("crypto");
const targetCache = (function () {
const registry = { id: {}, name: {} };
function getIndex(/** @type {[LinkTarget]}*/ targets, id) {
let registry = { id: {}, name: {} }
function getIndex (/** @type {[LinkTarget]} */ targets, id) {
for (let index = 0; index < (targets || []).length; index++) {
const element = targets[index];
const element = targets[index]
if (element.id === id) {
return index;
return index
}
}
return -1;
return -1
}
/**
* Generate a target object from a node
* @param {LinkInNode} node
* @returns {LinkTarget} a link target object
*/
function generateTarget(node) {
const isSubFlow = node._flow.TYPE === "subflow";
function generateTarget (node) {
const isSubFlow = node._flow.TYPE === 'subflow'
return {
id: node.id,
name: node.name || node.id,
@ -61,12 +61,12 @@ module.exports = function(RED) {
* @param {boolean} [excludeSubflows] set `true` to exclude
* @returns {[LinkTarget]} Targets registerd to this name.
*/
getTargets(name, excludeSubflows) {
const targets = registry.name[name] || [];
getTargets (name, excludeSubflows) {
const targets = registry.name[name] || []
if (excludeSubflows) {
return targets.filter(e => e.isSubFlow != true);
return targets.filter(e => e.isSubFlow !== true)
}
return targets;
return targets
},
/**
* Get a single target by registered name.
@ -76,54 +76,54 @@ module.exports = function(RED) {
* @param {string} [flowId]
* @returns {LinkTarget} target
*/
getTarget(name, flowId) {
/** @type {[LinkTarget]}*/
let possibleTargets = this.getTargets(name);
/** @type {LinkTarget}*/
let target;
getTarget (name, flowId) {
/** @type {[LinkTarget]} */
let possibleTargets = this.getTargets(name)
/** @type {LinkTarget} */
let target
if (possibleTargets.length && flowId) {
possibleTargets = possibleTargets.filter(e => e.flowId == flowId);
possibleTargets = possibleTargets.filter(e => e.flowId === flowId)
}
if (possibleTargets.length === 1) {
target = possibleTargets[0];
target = possibleTargets[0]
}
return target;
return target
},
/**
* Get a target by node ID
* @param {string} nodeId ID of the node
* @returns {LinkTarget} target
*/
getTargetById(nodeId) {
return registry.id[nodeId];
getTargetById (nodeId) {
return registry.id[nodeId]
},
register(/** @type {LinkInNode} */ node) {
const target = generateTarget(node);
const tByName = this.getTarget(target.name, target.flowId);
register (/** @type {LinkInNode} */ node) {
const target = generateTarget(node)
const tByName = this.getTarget(target.name, target.flowId)
if (!tByName || tByName.id !== target.id) {
registry.name[target.name] = registry.name[target.name] || [];
registry.name[target.name] = registry.name[target.name] || []
registry.name[target.name].push(target)
}
registry.id[target.id] = target;
return target;
registry.id[target.id] = target
return target
},
remove(node) {
const target = generateTarget(node);
const targs = this.getTargets(target.name);
const idx = getIndex(targs, target.id);
remove (node) {
const target = generateTarget(node)
const targs = this.getTargets(target.name)
const idx = getIndex(targs, target.id)
if (idx > -1) {
targs.splice(idx, 1);
targs.splice(idx, 1)
}
if (targs.length === 0) {
delete registry.name[tn.name];
delete registry.name[target.name]
}
delete registry.id[target.id];
delete registry.id[target.id]
},
clear() {
registry = { id: {}, name: {} };
clear () {
registry = { id: {}, name: {} }
}
}
})();
})()
function LinkInNode(n) {
RED.nodes.createNode(this,n);