Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If & else handles can connect to items in invalid ways. #132

Open
1 task done
itsWindows11 opened this issue Oct 14, 2024 · 1 comment
Open
1 task done

If & else handles can connect to items in invalid ways. #132

itsWindows11 opened this issue Oct 14, 2024 · 1 comment
Labels
bug Something isn't working complexity: challenging help wanted Extra attention is needed status: ready to pickup
Milestone

Comments

@itsWindows11
Copy link
Collaborator

⚠️ Search for existing issues first ⚠️

  • I have searched the existing issues, and there is no existing issue for my problem

Which Operating System are you using?

Windows

Which version of data-river are you using?

0.0.1

Describe your issue.

As shown in the video below, if/else handles can be connected to a block directly despite an indirect connection to that same block existing. Also, if a block is connected to the "if handle", it can get connected to the "else handle", and vice versa.

Screen.Recording.2024-10-14.201613.mp4

Paste Error from Terminal/Console

No response

@itsWindows11 itsWindows11 added bug Something isn't working status: needs triage help wanted Extra attention is needed labels Oct 14, 2024
@github-project-automation github-project-automation bot moved this to Backlog in data-river Oct 14, 2024
@c0rtexR c0rtexR added this to the 0.1.0 milestone Oct 14, 2024
@c0rtexR c0rtexR moved this from Backlog to Ready in data-river Oct 14, 2024
@c0rtexR
Copy link
Contributor

c0rtexR commented Oct 14, 2024

Desired Behavior:

  • Allow if or else outputs to connect to multiple blocks.
  • Prevent connections where both if and else outputs connect to the same block.
  • Prevent cycles in the graph to avoid infinite loops.

Proposed Solution:

Implement connection validation logic to prevent invalid configurations:

  1. Use isValidConnection on Handles:

    Update the SourceHandle component to include an isValidConnection function that checks for invalid connections. This function should:

    • Check if the target node is already connected to the other output (if or else) of the same logic block.
    • Prevent the connection if it would result in both if and else outputs connecting to the same node.
    • Check for potential cycles that could create infinite loops.
  2. Pre-calculate Invalid Connections:

    Perform validation during the connection attempt (onConnect event) rather than during dragging to avoid performance issues with animation frames.

  3. Update Connectable State:

    Dynamically set the connectable prop on the Handle components based on the validation results to prevent users from creating invalid connections.

Action Items:

  • Modify the SourceHandle component to include the isValidConnection prop with the validation function.
  • Implement the validation logic in the isValidConnection function.
  • Ensure that the validation checks are efficient to prevent any performance degradation.

Example Code Snippet:

// SourceHandle.ts
import React from "react";
import { Handle, Position } from "reactflow";
import { useSelector } from "react-redux";

const SourceHandle = ({ isVisible, nodeId, handleId }) => {
  const edges = useSelector((state) => state.reactFlow.edges);

  const isValidConnection = (connection) => {
    // Prevent connecting both 'if' and 'else' to the same target
    const existingConnections = edges.filter(
      (edge) =>
        edge.source === nodeId &&
        edge.target === connection.target &&
        edge.sourceHandle !== connection.sourceHandle
    );
    return existingConnections.length === 0;
  };

  return (
    <Handle
      type="source"
      position={Position.Right}
      id={handleId} // e.g., 'if' or 'else'
      isValidConnection={isValidConnection}
      style={{ visibility: isVisible ? "visible" : "hidden" }}
    />
  );
};

export default SourceHandle;

Notes:

  • Ensure that each handle (if and else) has a unique id to differentiate them.
  • The isValidConnection function accesses the current edges from the Redux store to check for existing connections.
  • Consider implementing cycle detection if necessary to prevent infinite loops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working complexity: challenging help wanted Extra attention is needed status: ready to pickup
Projects
Status: Ready
Development

No branches or pull requests

2 participants