Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/src/workflow/nodes/loop-node/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const mountLoopBodyNode = () => {
if (!nodeOutgoingNode.some((item: any) => item.type == loopBodyNode.type)) {
let workflow = { nodes: [loopStartNode], edges: [] }
let x = props.nodeModel.x
let y = props.nodeModel.y + 200
let y = props.nodeModel.y + 350
if (props.nodeModel.properties.node_data.loop_body) {
workflow = props.nodeModel.properties.node_data.loop_body
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code has the following issues:

  1. Variable Scope: The variable let workflow shadows the previously declared function parameter workflow.

  2. Incremental Y-Coordinate Addition: There is an inconsistency in how the y-coordinate is being incremented. The initial addition of 200 was changed to 350.

  3. Type Safety: Although not directly related to runtime errors, using any instead of more specific types can lead to errors if assumptions are incorrect at runtime.

  4. Logic for Loop Body Property: If nodeOutgoingNode already contains a node with the same type as loopBodyNode, setting workflow to props.nodeModel.properties.node_data.loop_body will overwrite the existing nodes without concatenating them.

Optimizations/Lessons Learned

  1. Avoid Variable Shadowing: Declare variables with unique names to avoid shadowing other variables within their scope.

  2. Consistent Code Style: Maintain consistency in incrementing the y-coordinate and ensure that there are no unintended changes in logic due to minor adjustments.

  3. Ensure Type Safety: Use appropriate TypeScript or JavaScript types to help catch potential errors related to data structures used.

  4. Handle Edge Cases: For properties like nodeData.loop_body, consider implementing checks to handle cases where the property might be missing or invalid before processing it.

Here is the corrected version of your code:

const mountLoopBodyNode = () => {
  const { nodeOutgoingNodes } = props;
  
  // Check if the outgoing edge matches the expected type
  if (!nodeOutgoingNodes.some(item => typeof item === 'object' && item.type === loopBodyNode.type)) {
    let workflow = {
      nodes: [loopStartNode],
      edges: []
    };

    const baseYOffset = 200; // Define base offset
    let yOffset = props.nodeModel.y;

    // Adjust based on previous condition (shouldn't run under this scenario, added just in case)
    if (baseYOffset !== 350) {
      yOffset += baseYOffset + 150; // Calculate actual desired offset
    }

    if (properties?.nodeData?.loop_body) {
      workflow.nodes.push(...(Array.isArray(properties?.nodeData?.loop_body) ? properties?.nodeData?.loop_body : [properties?.nodeData?.loop_body]));
    }
  }
};

In this updated version of the code:

  • Added strict typing (typeof item === 'object' &&) to better understand the structure of the items in nodeOutgoingNodes.
  • Removed the commented-out code related to changing the yOffset.
  • Ensured proper handling of array expansion when dealing with the loop_body property, assuming it's an array.
  • Used named objects for better readability and maintainability of the code.

Expand Down
Loading