Skip to content

Commit 8ccf454

Browse files
committed
fix(sockets): redrawing edges should not lead to socket ops
1 parent ebbe67a commit 8ccf454

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

apps/sim/hooks/use-collaborative-workflow.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,20 @@ export function useCollaborativeWorkflow() {
242242
case EDGES_OPERATIONS.BATCH_ADD_EDGES: {
243243
const { edges } = payload
244244
if (Array.isArray(edges) && edges.length > 0) {
245-
workflowStore.batchAddEdges(edges)
245+
const currentEdges = workflowStore.edges
246+
const newEdges = edges.filter((edge: Edge) => {
247+
if (edge.source === edge.target) return false
248+
return !currentEdges.some(
249+
(e) =>
250+
e.source === edge.source &&
251+
e.sourceHandle === edge.sourceHandle &&
252+
e.target === edge.target &&
253+
e.targetHandle === edge.targetHandle
254+
)
255+
})
256+
if (newEdges.length > 0) {
257+
workflowStore.batchAddEdges(newEdges)
258+
}
246259
}
247260
break
248261
}
@@ -976,23 +989,40 @@ export function useCollaborativeWorkflow() {
976989

977990
if (edges.length === 0) return false
978991

992+
const currentEdges = workflowStore.edges
993+
const newEdges = edges.filter((edge) => {
994+
if (edge.source === edge.target) return false
995+
return !currentEdges.some(
996+
(e) =>
997+
e.source === edge.source &&
998+
e.sourceHandle === edge.sourceHandle &&
999+
e.target === edge.target &&
1000+
e.targetHandle === edge.targetHandle
1001+
)
1002+
})
1003+
1004+
if (newEdges.length === 0) {
1005+
logger.debug('Skipping batch add edges - all edges already exist')
1006+
return false
1007+
}
1008+
9791009
const operationId = crypto.randomUUID()
9801010

9811011
addToQueue({
9821012
id: operationId,
9831013
operation: {
9841014
operation: EDGES_OPERATIONS.BATCH_ADD_EDGES,
9851015
target: OPERATION_TARGETS.EDGES,
986-
payload: { edges },
1016+
payload: { edges: newEdges },
9871017
},
9881018
workflowId: activeWorkflowId || '',
9891019
userId: session?.user?.id || 'unknown',
9901020
})
9911021

992-
workflowStore.batchAddEdges(edges)
1022+
workflowStore.batchAddEdges(newEdges)
9931023

9941024
if (!options?.skipUndoRedo) {
995-
edges.forEach((edge) => undoRedo.recordAddEdge(edge.id))
1025+
newEdges.forEach((edge) => undoRedo.recordAddEdge(edge.id))
9961026
}
9971027

9981028
return true

apps/sim/stores/workflows/workflow/store.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ describe('workflow store', () => {
297297
expectEdgeConnects(edges, 'block-1', 'block-2')
298298
})
299299

300-
it('should not add duplicate edges', () => {
300+
it('should not add duplicate connections', () => {
301301
const { addBlock, batchAddEdges } = useWorkflowStore.getState()
302302

303303
addBlock('block-1', 'starter', 'Start', { x: 0, y: 0 })
@@ -309,17 +309,6 @@ describe('workflow store', () => {
309309
const state = useWorkflowStore.getState()
310310
expectEdgeCount(state, 1)
311311
})
312-
313-
it('should prevent self-referencing edges', () => {
314-
const { addBlock, batchAddEdges } = useWorkflowStore.getState()
315-
316-
addBlock('block-1', 'function', 'Self', { x: 0, y: 0 })
317-
318-
batchAddEdges([{ id: 'e1', source: 'block-1', target: 'block-1' }])
319-
320-
const state = useWorkflowStore.getState()
321-
expectEdgeCount(state, 0)
322-
})
323312
})
324313

325314
describe('batchRemoveEdges', () => {

apps/sim/stores/workflows/workflow/store.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,16 +497,8 @@ export const useWorkflowStore = create<WorkflowStore>()(
497497
batchAddEdges: (edges: Edge[]) => {
498498
const currentEdges = get().edges
499499
const newEdges = [...currentEdges]
500-
const existingEdgeIds = new Set(currentEdges.map((e) => e.id))
501500

502501
for (const edge of edges) {
503-
// Skip if edge ID already exists
504-
if (existingEdgeIds.has(edge.id)) continue
505-
506-
// Skip self-referencing edges
507-
if (edge.source === edge.target) continue
508-
509-
// Skip if identical connection already exists (same ports)
510502
const connectionExists = newEdges.some(
511503
(e) =>
512504
e.source === edge.source &&
@@ -515,8 +507,6 @@ export const useWorkflowStore = create<WorkflowStore>()(
515507
e.targetHandle === edge.targetHandle
516508
)
517509
if (connectionExists) continue
518-
519-
// Skip if would create a cycle
520510
if (wouldCreateCycle([...newEdges], edge.source, edge.target)) continue
521511

522512
newEdges.push({
@@ -528,7 +518,6 @@ export const useWorkflowStore = create<WorkflowStore>()(
528518
type: edge.type || 'default',
529519
data: edge.data || {},
530520
})
531-
existingEdgeIds.add(edge.id)
532521
}
533522

534523
const blocks = get().blocks

0 commit comments

Comments
 (0)