mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Apply linting suggestions to the registry
enclosure
This commit is contained in:
parent
e4098d3991
commit
5c69599e78
@ -30,22 +30,22 @@ module.exports = function(RED) {
|
||||
const crypto = require("crypto");
|
||||
const targetCache = (function () {
|
||||
let registry = { id: {}, name: {} }
|
||||
function getIndex(/** @type {[LinkTarget]}*/ targets, id) {
|
||||
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
|
||||
* @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,
|
||||
@ -58,72 +58,72 @@ module.exports = function(RED) {
|
||||
/**
|
||||
* Get a list of targets registerd to this name
|
||||
* @param {string} name Name of the target
|
||||
* @param {boolean} [excludeSubflows] set `true` to exclude
|
||||
* @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.
|
||||
* To restrict to a single flow, include the `flowId`
|
||||
* If there is no targets OR more than one target, null is returned
|
||||
* @param {string} name Name of the node
|
||||
* @param {string} [flowId]
|
||||
* @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[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);
|
||||
|
Loading…
Reference in New Issue
Block a user