All files / src/pages/VisProgPage/visualProgrammingUI HandleRules.ts

0% Statements 0/12
0% Branches 0/8
0% Functions 0/9
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                                                                                             
import {
  type HandleRule,
  ruleResult
} from "./HandleRuleLogic.ts";
import useFlowStore from "./VisProgStores.tsx";
 
 
/**
 * this specifies what types of nodes can make a connection to a handle that uses this rule
 */
export function allowOnlyConnectionsFromType(nodeTypes: string[]) : HandleRule  {
  return ((_, {source}) => {
    const sourceType = useFlowStore.getState().nodes.find(node => node.id === source.id)!.type!;
    return nodeTypes.find(type => sourceType === type)
      ? ruleResult.satisfied
      : ruleResult.notSatisfied(`the target doesn't allow connections from nodes with type: ${sourceType}`);
  })
}
 
/**
 * similar to allowOnlyConnectionsFromType,
 * this is a more specific variant that allows you to restrict connections to specific handles on each nodeType
 */
//
export function allowOnlyConnectionsFromHandle(handles: {nodeType: string, handleId: string}[]) : HandleRule  {
  return ((_, {source}) => {
    const sourceNode = useFlowStore.getState().nodes.find(node => node.id === source.id)!;
    return handles.find(handle => sourceNode.type === handle.nodeType && source.handleId === handle.handleId)
      ? ruleResult.satisfied
      : ruleResult.notSatisfied(`the target doesn't allow connections from nodes with type: ${sourceNode.type}`);
  })
}
 
 
/**
 * This rule prevents a node from making a connection between its own handles
 */
export const noSelfConnections : HandleRule =
  (connection, _) => {
    return connection.source !== connection.target
      ? ruleResult.satisfied
      : ruleResult.notSatisfied("nodes are not allowed to connect to themselves");
  }