Skip to content

Commit b34b746

Browse files
committed
fix(workflow-code): skip the Code tab refresh for resizes and handle toggles
A container resize and a handle toggle copy the edge array without changing an edge, and the draft subscription compared edges by identity, so each read the source again; the handle-toggle operations were also missing from the geometry operations. Edges are now compared by element and both toggle operations count as geometry.
1 parent b5ad3c5 commit b34b746

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.test.tsx‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useOperationQueueStore } from '@/stores/operation-queue/store'
1818
import { useVariablesStore } from '@/stores/variables/store'
1919
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
2020
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
21+
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
2122

2223
const WORKSPACE_ID = 'ws-1'
2324
const WORKFLOW_ID = 'wf-1'
@@ -68,6 +69,32 @@ describe('Code tab refresh', () => {
6869
useWorkflowRegistry.setState({ activeWorkflowId: WORKFLOW_ID })
6970
useSubBlockStore.setState({ workflowValues: {} })
7071
useVariablesStore.setState({ variables: {} })
72+
useWorkflowStore.setState({
73+
blocks: {
74+
group: {
75+
id: 'group',
76+
type: 'loop',
77+
name: 'Group',
78+
position: { x: 0, y: 0 },
79+
subBlocks: {},
80+
outputs: {},
81+
enabled: true,
82+
data: { width: 400, height: 200 },
83+
},
84+
score: {
85+
id: 'score',
86+
type: 'function',
87+
name: 'Score',
88+
position: { x: 0, y: 0 },
89+
subBlocks: {},
90+
outputs: {},
91+
enabled: true,
92+
},
93+
},
94+
edges: [{ id: 'e1', source: 'group', target: 'score' }],
95+
loops: {},
96+
parallels: {},
97+
})
7198
useOperationQueueStore.getState().reset()
7299
const container = document.createElement('div')
73100
root = createRoot(container)
@@ -138,6 +165,46 @@ describe('Code tab refresh', () => {
138165
await vi.waitFor(() => expect(sourceReads).toHaveLength(2))
139166
})
140167

168+
it('ignores a container resize, which copies the edge list without changing an edge', async () => {
169+
await vi.waitFor(() => expect(sourceReads).toHaveLength(1))
170+
act(() =>
171+
useWorkflowStore.getState().updateNodeDimensions('group', { width: 600, height: 300 })
172+
)
173+
await settle()
174+
expect(sourceReads).toHaveLength(1)
175+
})
176+
177+
it('ignores a handle toggle, from the store write and from its queued operation', async () => {
178+
await vi.waitFor(() => expect(sourceReads).toHaveLength(1))
179+
act(() => {
180+
useWorkflowStore.getState().batchToggleHandles(['score'])
181+
useOperationQueueStore.getState().addToQueue({
182+
id: 'op-handles',
183+
operation: {
184+
operation: 'batch-toggle-handles',
185+
target: 'blocks',
186+
payload: { ids: ['score'] },
187+
},
188+
workflowId: WORKFLOW_ID,
189+
userId: 'user-1',
190+
})
191+
useOperationQueueStore.getState().confirmOperation('op-handles')
192+
})
193+
await settle()
194+
expect(sourceReads).toHaveLength(1)
195+
})
196+
197+
it('re-reads the source after an edge is added', async () => {
198+
await vi.waitFor(() => expect(sourceReads).toHaveLength(1))
199+
act(() =>
200+
useWorkflowStore.setState((state) => ({
201+
edges: [...state.edges, { id: 'e2', source: 'score', target: 'group' }],
202+
}))
203+
)
204+
await settle()
205+
await vi.waitFor(() => expect(sourceReads).toHaveLength(2))
206+
})
207+
141208
it('ignores a variable change in another workflow', async () => {
142209
await vi.waitFor(() => expect(sourceReads).toHaveLength(1))
143210
setVariable('wf-other', '3')

‎apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ const REFRESH_DEBOUNCE_MS = 750
1919

2020
const CODE_VIEW_OPTIONS = { ...TEXT_EDITOR_OPTIONS, readOnly: true, domReadOnly: true }
2121

22-
/** Canvas geometry operations, which the printed source never shows. */
22+
/** Canvas geometry operations (moves and handle orientation), which the printed source never shows. */
2323
const GEOMETRY_OPERATIONS: ReadonlySet<string> = new Set([
2424
BLOCK_OPERATIONS.UPDATE_POSITION,
2525
BLOCKS_OPERATIONS.BATCH_UPDATE_POSITIONS,
26+
BLOCK_OPERATIONS.TOGGLE_HANDLES,
27+
BLOCKS_OPERATIONS.BATCH_TOGGLE_HANDLES,
2628
])
2729

2830
/**

‎apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/deploy/hooks/use-draft-workflow-state.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useMemo } from 'react'
22
import { BLOCK_LAYOUT_DATA_FIELDS, BLOCK_LAYOUT_FIELDS } from '@sim/workflow-code/layout'
33
import { mergeSubblockStateWithValues } from '@sim/workflow-persistence/subblocks'
44
import type { BlockState } from '@sim/workflow-types/workflow'
5+
import type { Edge } from '@xyflow/react'
56
import { useVariablesStore } from '@/stores/variables/store'
67
import type { Variable } from '@/stores/variables/types'
78
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
@@ -90,6 +91,15 @@ function blocksChangedBeyondGeometry(
9091
})
9192
}
9293

94+
/**
95+
* Whether the edge list gained, lost or replaced an edge. Block-only writes (a container resize,
96+
* a handle toggle) copy the array without touching an edge, so identity alone over-reports.
97+
*/
98+
function edgesChanged(next: readonly Edge[], previous: readonly Edge[]): boolean {
99+
if (next === previous) return false
100+
return next.length !== previous.length || next.some((edge, index) => edge !== previous[index])
101+
}
102+
93103
/** Whether a variable of `workflowId` was added, removed or replaced. */
94104
function workflowVariablesChanged(
95105
next: Record<string, Variable>,
@@ -120,7 +130,7 @@ export function subscribeToDraftChanges(workflowId: string, onChange: () => void
120130
const unsubscribers = [
121131
useWorkflowStore.subscribe((state, previous) => {
122132
if (
123-
state.edges !== previous.edges ||
133+
edgesChanged(state.edges, previous.edges) ||
124134
state.loops !== previous.loops ||
125135
state.parallels !== previous.parallels ||
126136
(state.blocks !== previous.blocks &&

0 commit comments

Comments
 (0)