Skip to content

Commit e3fa40a

Browse files
fix(sockets): redrawing edges should not lead to socket ops (#2804)
* fix(sockets): redrawing edges should not lead to socket ops * consolidate
1 parent 6e0055f commit e3fa40a

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { useUndoRedoStore } from '@/stores/undo-redo'
2222
import { useWorkflowDiffStore } from '@/stores/workflow-diff/store'
2323
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
2424
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
25-
import { mergeSubblockState, normalizeName } from '@/stores/workflows/utils'
25+
import { filterNewEdges, mergeSubblockState, normalizeName } from '@/stores/workflows/utils'
2626
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
2727
import type { BlockState, Loop, Parallel, Position } from '@/stores/workflows/workflow/types'
2828

@@ -242,7 +242,10 @@ 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 newEdges = filterNewEdges(edges, workflowStore.edges)
246+
if (newEdges.length > 0) {
247+
workflowStore.batchAddEdges(newEdges)
248+
}
246249
}
247250
break
248251
}
@@ -976,23 +979,26 @@ export function useCollaborativeWorkflow() {
976979

977980
if (edges.length === 0) return false
978981

982+
const newEdges = filterNewEdges(edges, workflowStore.edges)
983+
if (newEdges.length === 0) return false
984+
979985
const operationId = crypto.randomUUID()
980986

981987
addToQueue({
982988
id: operationId,
983989
operation: {
984990
operation: EDGES_OPERATIONS.BATCH_ADD_EDGES,
985991
target: OPERATION_TARGETS.EDGES,
986-
payload: { edges },
992+
payload: { edges: newEdges },
987993
},
988994
workflowId: activeWorkflowId || '',
989995
userId: session?.user?.id || 'unknown',
990996
})
991997

992-
workflowStore.batchAddEdges(edges)
998+
workflowStore.batchAddEdges(newEdges)
993999

9941000
if (!options?.skipUndoRedo) {
995-
edges.forEach((edge) => undoRedo.recordAddEdge(edge.id))
1001+
newEdges.forEach((edge) => undoRedo.recordAddEdge(edge.id))
9961002
}
9971003

9981004
return true

apps/sim/stores/workflows/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import type { Edge } from 'reactflow'
22
import { v4 as uuidv4 } from 'uuid'
3+
4+
export function filterNewEdges(edgesToAdd: Edge[], currentEdges: Edge[]): Edge[] {
5+
return edgesToAdd.filter((edge) => {
6+
if (edge.source === edge.target) return false
7+
return !currentEdges.some(
8+
(e) =>
9+
e.source === edge.source &&
10+
e.sourceHandle === edge.sourceHandle &&
11+
e.target === edge.target &&
12+
e.targetHandle === edge.targetHandle
13+
)
14+
})
15+
}
16+
317
import { getBlockOutputs } from '@/lib/workflows/blocks/block-outputs'
418
import { getBlock } from '@/blocks'
519
import { normalizeName } from '@/executor/constants'

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: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { getBlock } from '@/blocks'
99
import type { SubBlockConfig } from '@/blocks/types'
1010
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
1111
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
12-
import { getUniqueBlockName, mergeSubblockState, normalizeName } from '@/stores/workflows/utils'
12+
import {
13+
filterNewEdges,
14+
getUniqueBlockName,
15+
mergeSubblockState,
16+
normalizeName,
17+
} from '@/stores/workflows/utils'
1318
import type {
1419
Position,
1520
SubBlockState,
@@ -496,29 +501,11 @@ export const useWorkflowStore = create<WorkflowStore>()(
496501

497502
batchAddEdges: (edges: Edge[]) => {
498503
const currentEdges = get().edges
504+
const filtered = filterNewEdges(edges, currentEdges)
499505
const newEdges = [...currentEdges]
500-
const existingEdgeIds = new Set(currentEdges.map((e) => e.id))
501-
502-
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)
510-
const connectionExists = newEdges.some(
511-
(e) =>
512-
e.source === edge.source &&
513-
e.sourceHandle === edge.sourceHandle &&
514-
e.target === edge.target &&
515-
e.targetHandle === edge.targetHandle
516-
)
517-
if (connectionExists) continue
518506

519-
// Skip if would create a cycle
507+
for (const edge of filtered) {
520508
if (wouldCreateCycle([...newEdges], edge.source, edge.target)) continue
521-
522509
newEdges.push({
523510
id: edge.id || crypto.randomUUID(),
524511
source: edge.source,
@@ -528,7 +515,6 @@ export const useWorkflowStore = create<WorkflowStore>()(
528515
type: edge.type || 'default',
529516
data: edge.data || {},
530517
})
531-
existingEdgeIds.add(edge.id)
532518
}
533519

534520
const blocks = get().blocks

0 commit comments

Comments
 (0)