@@ -44,14 +44,8 @@ export function useCollaborativeWorkflow() {
4444 const lastPositionTimestamps = useRef < Map < string , number > > ( new Map ( ) )
4545
4646 // Operation queue
47- const {
48- queue,
49- hasOperationError,
50- addToQueue,
51- confirmOperation,
52- failOperation,
53- handleSocketReconnection,
54- } = useOperationQueue ( )
47+ const { queue, hasOperationError, addToQueue, confirmOperation, failOperation } =
48+ useOperationQueue ( )
5549
5650 // Clear position timestamps when switching workflows
5751 // Note: Workflow joining is now handled automatically by socket connect event based on URL
@@ -74,30 +68,6 @@ export function useCollaborativeWorkflow() {
7468 registerEmitFunctions ( emitWorkflowOperation , emitSubblockUpdate , currentWorkflowId )
7569 } , [ emitWorkflowOperation , emitSubblockUpdate , currentWorkflowId ] )
7670
77- // Log connection status changes and handle reconnection
78- useEffect ( ( ) => {
79- logger . info ( 'Collaborative workflow connection status changed' , {
80- isConnected,
81- currentWorkflowId,
82- activeWorkflowId,
83- presenceUsers : presenceUsers . length ,
84- } )
85-
86- // Clear operation queue when socket reconnects AND has joined workflow
87- // We need both isConnected=true AND currentWorkflowId to match activeWorkflowId
88- // This ensures the socket has actually joined the workflow room before we allow retries
89- if ( isConnected && currentWorkflowId && currentWorkflowId === activeWorkflowId ) {
90- logger . info ( 'Socket reconnected and joined workflow - clearing operation queue' )
91- handleSocketReconnection ( )
92- }
93- } , [
94- isConnected ,
95- currentWorkflowId ,
96- activeWorkflowId ,
97- presenceUsers . length ,
98- handleSocketReconnection ,
99- ] )
100-
10171 useEffect ( ( ) => {
10272 const handleWorkflowOperation = ( data : any ) => {
10373 const { operation, target, payload, userId } = data
@@ -416,10 +386,8 @@ export function useCollaborativeWorkflow() {
416386 } )
417387
418388 localAction ( )
419-
420- emitWorkflowOperation ( operation , target , payload , operationId )
421389 } ,
422- [ addToQueue , emitWorkflowOperation , session ?. user ?. id ]
390+ [ addToQueue , session ?. user ?. id ]
423391 )
424392
425393 const executeQueuedDebouncedOperation = useCallback (
@@ -496,8 +464,6 @@ export function useCollaborativeWorkflow() {
496464 workflowStore . addEdge ( autoConnectEdge )
497465 }
498466
499- // Emit to server with operation ID for tracking
500- emitWorkflowOperation ( 'add' , 'block' , completeBlockData , operationId )
501467 return
502468 }
503469
@@ -562,9 +528,6 @@ export function useCollaborativeWorkflow() {
562528 if ( autoConnectEdge ) {
563529 workflowStore . addEdge ( autoConnectEdge )
564530 }
565-
566- // Emit to server with operation ID
567- emitWorkflowOperation ( 'add' , 'block' , completeBlockData , operationId )
568531 } ,
569532 [ workflowStore , emitWorkflowOperation , addToQueue , session ?. user ?. id ]
570533 )
@@ -594,17 +557,37 @@ export function useCollaborativeWorkflow() {
594557 const globalWindow = window as any
595558 const pendingUpdates = globalWindow . __pendingSubblockUpdates
596559 if ( pendingUpdates && Array . isArray ( pendingUpdates ) ) {
597- // Emit collaborative subblock updates for each changed subblock
560+ // Queue each subblock update individually
598561 for ( const update of pendingUpdates ) {
599562 const { blockId, subBlockId, newValue } = update
600- emitSubblockUpdate ( blockId , subBlockId , newValue )
563+ const operationId = crypto . randomUUID ( )
564+
565+ addToQueue ( {
566+ id : operationId ,
567+ operation : {
568+ operation : 'subblock-update' ,
569+ target : 'subblock' ,
570+ payload : { blockId, subblockId : subBlockId , value : newValue } ,
571+ } ,
572+ workflowId : activeWorkflowId || '' ,
573+ userId : session ?. user ?. id || 'unknown' ,
574+ } )
575+
576+ subBlockStore . setValue ( blockId , subBlockId , newValue )
601577 }
602578 // Clear the pending updates
603579 globalWindow . __pendingSubblockUpdates = undefined
604580 }
605581 } )
606582 } ,
607- [ executeQueuedOperation , workflowStore , emitSubblockUpdate ]
583+ [
584+ executeQueuedOperation ,
585+ workflowStore ,
586+ addToQueue ,
587+ subBlockStore ,
588+ activeWorkflowId ,
589+ session ?. user ?. id ,
590+ ]
608591 )
609592
610593 const collaborativeToggleBlockEnabled = useCallback (
@@ -802,9 +785,6 @@ export function useCollaborativeWorkflow() {
802785
803786 // Apply locally first (immediate UI feedback)
804787 subBlockStore . setValue ( blockId , subblockId , value )
805-
806- // Emit to server with operation ID for tracking
807- emitSubblockUpdate ( blockId , subblockId , value , operationId )
808788 } ,
809789 [
810790 subBlockStore ,
0 commit comments