Skip to content

Commit 433d5b3

Browse files
committed
cleanup more code
1 parent 496be57 commit 433d5b3

File tree

3 files changed

+90
-129
lines changed

3 files changed

+90
-129
lines changed

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

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -199,39 +199,6 @@ export function useCollaborativeWorkflow() {
199199
try {
200200
if (target === 'block') {
201201
switch (operation) {
202-
case 'add':
203-
workflowStore.addBlock(
204-
payload.id,
205-
payload.type,
206-
payload.name,
207-
payload.position,
208-
payload.data,
209-
payload.parentId,
210-
payload.extent,
211-
{
212-
enabled: payload.enabled,
213-
horizontalHandles: payload.horizontalHandles,
214-
advancedMode: payload.advancedMode,
215-
triggerMode: payload.triggerMode ?? false,
216-
height: payload.height,
217-
}
218-
)
219-
if (payload.autoConnectEdge) {
220-
workflowStore.addEdge(payload.autoConnectEdge)
221-
}
222-
// Apply subblock values if present in payload
223-
if (payload.subBlocks && typeof payload.subBlocks === 'object') {
224-
Object.entries(payload.subBlocks).forEach(([subblockId, subblock]) => {
225-
if (WEBHOOK_SUBBLOCK_FIELDS.includes(subblockId)) {
226-
return
227-
}
228-
const value = (subblock as any)?.value
229-
if (value !== undefined && value !== null) {
230-
subBlockStore.setValue(payload.id, subblockId, value)
231-
}
232-
})
233-
}
234-
break
235202
case 'update-position': {
236203
const blockId = payload.id
237204

@@ -263,40 +230,6 @@ export function useCollaborativeWorkflow() {
263230
case 'update-name':
264231
workflowStore.updateBlockName(payload.id, payload.name)
265232
break
266-
case 'remove': {
267-
const blockId = payload.id
268-
const blocksToRemove = new Set<string>([blockId])
269-
270-
const findAllDescendants = (parentId: string) => {
271-
Object.entries(workflowStore.blocks).forEach(([id, block]) => {
272-
if (block.data?.parentId === parentId) {
273-
blocksToRemove.add(id)
274-
findAllDescendants(id)
275-
}
276-
})
277-
}
278-
findAllDescendants(blockId)
279-
280-
workflowStore.removeBlock(blockId)
281-
lastPositionTimestamps.current.delete(blockId)
282-
283-
const updatedBlocks = useWorkflowStore.getState().blocks
284-
const updatedEdges = useWorkflowStore.getState().edges
285-
const graph = {
286-
blocksById: updatedBlocks,
287-
edgesById: Object.fromEntries(updatedEdges.map((e) => [e.id, e])),
288-
}
289-
290-
const undoRedoStore = useUndoRedoStore.getState()
291-
const stackKeys = Object.keys(undoRedoStore.stacks)
292-
stackKeys.forEach((key) => {
293-
const [workflowId, userId] = key.split(':')
294-
if (workflowId === activeWorkflowId) {
295-
undoRedoStore.pruneInvalidEntries(workflowId, userId, graph)
296-
}
297-
})
298-
break
299-
}
300233
case 'toggle-enabled':
301234
workflowStore.toggleBlockEnabled(payload.id)
302235
break
@@ -316,40 +249,6 @@ export function useCollaborativeWorkflow() {
316249
}
317250
break
318251
}
319-
case 'duplicate':
320-
workflowStore.addBlock(
321-
payload.id,
322-
payload.type,
323-
payload.name,
324-
payload.position,
325-
payload.data,
326-
payload.parentId,
327-
payload.extent,
328-
{
329-
enabled: payload.enabled,
330-
horizontalHandles: payload.horizontalHandles,
331-
advancedMode: payload.advancedMode,
332-
triggerMode: payload.triggerMode ?? false,
333-
height: payload.height,
334-
}
335-
)
336-
// Handle auto-connect edge if present
337-
if (payload.autoConnectEdge) {
338-
workflowStore.addEdge(payload.autoConnectEdge)
339-
}
340-
// Apply subblock values from duplicate payload so collaborators see content immediately
341-
if (payload.subBlocks && typeof payload.subBlocks === 'object') {
342-
Object.entries(payload.subBlocks).forEach(([subblockId, subblock]) => {
343-
if (WEBHOOK_SUBBLOCK_FIELDS.includes(subblockId)) {
344-
return
345-
}
346-
const value = (subblock as any)?.value
347-
if (value !== undefined) {
348-
subBlockStore.setValue(payload.id, subblockId, value)
349-
}
350-
})
351-
}
352-
break
353252
}
354253
} else if (target === 'blocks') {
355254
switch (operation) {

apps/sim/socket/tests/socket-server.test.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,26 @@ describe('Socket Server Integration Tests', () => {
103103

104104
const operationPromise = new Promise<void>((resolve) => {
105105
client2.once('workflow-operation', (data) => {
106-
expect(data.operation).toBe('add')
107-
expect(data.target).toBe('block')
108-
expect(data.payload.id).toBe('block-123')
106+
expect(data.operation).toBe('batch-add-blocks')
107+
expect(data.target).toBe('blocks')
108+
expect(data.payload.blocks[0].id).toBe('block-123')
109109
resolve()
110110
})
111111
})
112112

113113
clientSocket.emit('workflow-operation', {
114114
workflowId,
115-
operation: 'add',
116-
target: 'block',
117-
payload: { id: 'block-123', type: 'action', name: 'Test Block' },
115+
operation: 'batch-add-blocks',
116+
target: 'blocks',
117+
payload: {
118+
blocks: [
119+
{ id: 'block-123', type: 'action', name: 'Test Block', position: { x: 0, y: 0 } },
120+
],
121+
edges: [],
122+
loops: {},
123+
parallels: {},
124+
subBlockValues: {},
125+
},
118126
timestamp: Date.now(),
119127
})
120128

@@ -170,9 +178,17 @@ describe('Socket Server Integration Tests', () => {
170178

171179
clients[0].emit('workflow-operation', {
172180
workflowId,
173-
operation: 'add',
174-
target: 'block',
175-
payload: { id: 'stress-block', type: 'action' },
181+
operation: 'batch-add-blocks',
182+
target: 'blocks',
183+
payload: {
184+
blocks: [
185+
{ id: 'stress-block', type: 'action', name: 'Stress Block', position: { x: 0, y: 0 } },
186+
],
187+
edges: [],
188+
loops: {},
189+
parallels: {},
190+
subBlockValues: {},
191+
},
176192
timestamp: Date.now(),
177193
})
178194

@@ -211,7 +227,7 @@ describe('Socket Server Integration Tests', () => {
211227
const operationsPromise = new Promise<void>((resolve) => {
212228
client2.on('workflow-operation', (data) => {
213229
receivedCount++
214-
receivedOperations.add(data.payload.id)
230+
receivedOperations.add(data.payload.blocks[0].id)
215231

216232
if (receivedCount === numOperations) {
217233
resolve()
@@ -222,9 +238,22 @@ describe('Socket Server Integration Tests', () => {
222238
for (let i = 0; i < numOperations; i++) {
223239
clientSocket.emit('workflow-operation', {
224240
workflowId,
225-
operation: 'add',
226-
target: 'block',
227-
payload: { id: `rapid-block-${i}`, type: 'action' },
241+
operation: 'batch-add-blocks',
242+
target: 'blocks',
243+
payload: {
244+
blocks: [
245+
{
246+
id: `rapid-block-${i}`,
247+
type: 'action',
248+
name: `Rapid Block ${i}`,
249+
position: { x: 0, y: 0 },
250+
},
251+
],
252+
edges: [],
253+
loops: {},
254+
parallels: {},
255+
subBlockValues: {},
256+
},
228257
timestamp: Date.now(),
229258
})
230259
}

apps/sim/stores/operation-queue/store.ts

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,31 @@ export const useOperationQueueStore = create<OperationQueueState>((set, get) =>
375375
cancelOperationsForBlock: (blockId: string) => {
376376
logger.debug('Canceling all operations for block', { blockId })
377377

378-
// No debounced timeouts to cancel (moved to server-side)
379-
380-
// Find and cancel operation timeouts for operations related to this block
381378
const state = get()
382-
const operationsToCancel = state.operations.filter(
383-
(op) =>
384-
(op.operation.target === 'block' && op.operation.payload?.id === blockId) ||
385-
(op.operation.target === 'subblock' && op.operation.payload?.blockId === blockId)
386-
)
379+
const operationsToCancel = state.operations.filter((op) => {
380+
const { target, payload, operation } = op.operation
381+
382+
// Single block property updates (update-position, toggle-enabled, update-name, etc.)
383+
if (target === 'block' && payload?.id === blockId) return true
384+
385+
// Subblock updates for this block
386+
if (target === 'subblock' && payload?.blockId === blockId) return true
387+
388+
// Batch block operations
389+
if (target === 'blocks') {
390+
if (operation === 'batch-add-blocks' && Array.isArray(payload?.blocks)) {
391+
return payload.blocks.some((b: { id: string }) => b.id === blockId)
392+
}
393+
if (operation === 'batch-remove-blocks' && Array.isArray(payload?.ids)) {
394+
return payload.ids.includes(blockId)
395+
}
396+
if (operation === 'batch-update-positions' && Array.isArray(payload?.updates)) {
397+
return payload.updates.some((u: { id: string }) => u.id === blockId)
398+
}
399+
}
400+
401+
return false
402+
})
387403

388404
// Cancel timeouts for these operations
389405
operationsToCancel.forEach((op) => {
@@ -401,13 +417,30 @@ export const useOperationQueueStore = create<OperationQueueState>((set, get) =>
401417
})
402418

403419
// Remove all operations for this block (both pending and processing)
404-
const newOperations = state.operations.filter(
405-
(op) =>
406-
!(
407-
(op.operation.target === 'block' && op.operation.payload?.id === blockId) ||
408-
(op.operation.target === 'subblock' && op.operation.payload?.blockId === blockId)
409-
)
410-
)
420+
const newOperations = state.operations.filter((op) => {
421+
const { target, payload, operation } = op.operation
422+
423+
// Single block property updates (update-position, toggle-enabled, update-name, etc.)
424+
if (target === 'block' && payload?.id === blockId) return false
425+
426+
// Subblock updates for this block
427+
if (target === 'subblock' && payload?.blockId === blockId) return false
428+
429+
// Batch block operations
430+
if (target === 'blocks') {
431+
if (operation === 'batch-add-blocks' && Array.isArray(payload?.blocks)) {
432+
if (payload.blocks.some((b: { id: string }) => b.id === blockId)) return false
433+
}
434+
if (operation === 'batch-remove-blocks' && Array.isArray(payload?.ids)) {
435+
if (payload.ids.includes(blockId)) return false
436+
}
437+
if (operation === 'batch-update-positions' && Array.isArray(payload?.updates)) {
438+
if (payload.updates.some((u: { id: string }) => u.id === blockId)) return false
439+
}
440+
}
441+
442+
return true
443+
})
411444

412445
set({
413446
operations: newOperations,

0 commit comments

Comments
 (0)