-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathsocket-provider.tsx
More file actions
808 lines (704 loc) · 26.7 KB
/
socket-provider.tsx
File metadata and controls
808 lines (704 loc) · 26.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
'use client'
import {
createContext,
type ReactNode,
useCallback,
useContext,
useEffect,
useRef,
useState,
} from 'react'
import { createLogger } from '@sim/logger'
import { useParams } from 'next/navigation'
import { io, type Socket } from 'socket.io-client'
import { getEnv } from '@/lib/core/config/env'
const logger = createLogger('SocketContext')
interface User {
id: string
name?: string
email?: string
}
interface PresenceUser {
socketId: string
userId: string
userName: string
avatarUrl?: string | null
cursor?: { x: number; y: number } | null
selection?: { type: 'block' | 'edge' | 'none'; id?: string }
}
interface SocketContextType {
socket: Socket | null
isConnected: boolean
isConnecting: boolean
currentWorkflowId: string | null
presenceUsers: PresenceUser[]
joinWorkflow: (workflowId: string) => void
leaveWorkflow: () => void
emitWorkflowOperation: (
operation: string,
target: string,
payload: any,
operationId?: string
) => void
emitSubblockUpdate: (
blockId: string,
subblockId: string,
value: any,
operationId?: string
) => void
emitVariableUpdate: (variableId: string, field: string, value: any, operationId?: string) => void
emitCursorUpdate: (cursor: { x: number; y: number } | null) => void
emitSelectionUpdate: (selection: { type: 'block' | 'edge' | 'none'; id?: string }) => void
// Event handlers for receiving real-time updates
onWorkflowOperation: (handler: (data: any) => void) => void
onSubblockUpdate: (handler: (data: any) => void) => void
onVariableUpdate: (handler: (data: any) => void) => void
onCursorUpdate: (handler: (data: any) => void) => void
onSelectionUpdate: (handler: (data: any) => void) => void
onUserJoined: (handler: (data: any) => void) => void
onUserLeft: (handler: (data: any) => void) => void
onWorkflowDeleted: (handler: (data: any) => void) => void
onWorkflowReverted: (handler: (data: any) => void) => void
onOperationConfirmed: (handler: (data: any) => void) => void
onOperationFailed: (handler: (data: any) => void) => void
}
const SocketContext = createContext<SocketContextType>({
socket: null,
isConnected: false,
isConnecting: false,
currentWorkflowId: null,
presenceUsers: [],
joinWorkflow: () => {},
leaveWorkflow: () => {},
emitWorkflowOperation: () => {},
emitSubblockUpdate: () => {},
emitVariableUpdate: () => {},
emitCursorUpdate: () => {},
emitSelectionUpdate: () => {},
onWorkflowOperation: () => {},
onSubblockUpdate: () => {},
onVariableUpdate: () => {},
onCursorUpdate: () => {},
onSelectionUpdate: () => {},
onUserJoined: () => {},
onUserLeft: () => {},
onWorkflowDeleted: () => {},
onWorkflowReverted: () => {},
onOperationConfirmed: () => {},
onOperationFailed: () => {},
})
export const useSocket = () => useContext(SocketContext)
interface SocketProviderProps {
children: ReactNode
user?: User
}
export function SocketProvider({ children, user }: SocketProviderProps) {
const [socket, setSocket] = useState<Socket | null>(null)
const [isConnected, setIsConnected] = useState(false)
const [isConnecting, setIsConnecting] = useState(false)
const [currentWorkflowId, setCurrentWorkflowId] = useState<string | null>(null)
const [presenceUsers, setPresenceUsers] = useState<PresenceUser[]>([])
const initializedRef = useRef(false)
// Get current workflow ID from URL params
const params = useParams()
const urlWorkflowId = params?.workflowId as string | undefined
// Use refs to store event handlers to avoid stale closures
const eventHandlers = useRef<{
workflowOperation?: (data: any) => void
subblockUpdate?: (data: any) => void
variableUpdate?: (data: any) => void
cursorUpdate?: (data: any) => void
selectionUpdate?: (data: any) => void
userJoined?: (data: any) => void
userLeft?: (data: any) => void
workflowDeleted?: (data: any) => void
workflowReverted?: (data: any) => void
operationConfirmed?: (data: any) => void
operationFailed?: (data: any) => void
}>({})
// Helper function to generate a fresh socket token
const generateSocketToken = async (): Promise<string> => {
// Avoid overlapping token requests
const res = await fetch('/api/auth/socket-token', {
method: 'POST',
credentials: 'include',
headers: { 'cache-control': 'no-store' },
})
if (!res.ok) throw new Error('Failed to generate socket token')
const body = await res.json().catch(() => ({}))
const token = body?.token
if (!token || typeof token !== 'string') throw new Error('Invalid socket token')
return token
}
// Initialize socket when user is available - only once per session
useEffect(() => {
if (!user?.id) return
// Only initialize if we don't have a socket and aren't already connecting
if (initializedRef.current || socket || isConnecting) {
logger.info('Socket already exists or is connecting, skipping initialization')
return
}
logger.info('Initializing socket connection for user:', user.id)
initializedRef.current = true
setIsConnecting(true)
const initializeSocket = () => {
try {
const socketUrl = getEnv('NEXT_PUBLIC_SOCKET_URL') || 'http://localhost:3002'
logger.info('Attempting to connect to Socket.IO server', {
url: socketUrl,
userId: user?.id || 'no-user',
timestamp: new Date().toISOString(),
})
const socketInstance = io(socketUrl, {
transports: ['websocket', 'polling'], // Keep polling fallback for reliability
withCredentials: true,
reconnectionAttempts: Number.POSITIVE_INFINITY, // Socket.IO handles base reconnection
reconnectionDelay: 1000, // Start with 1 second delay
reconnectionDelayMax: 30000, // Max 30 second delay
timeout: 10000, // Back to original timeout
auth: async (cb) => {
try {
const freshToken = await generateSocketToken()
cb({ token: freshToken })
} catch (error) {
logger.error('Failed to generate fresh token for connection:', error)
cb({ token: null })
}
},
})
// Connection events
socketInstance.on('connect', () => {
setIsConnected(true)
setIsConnecting(false)
logger.info('Socket connected successfully', {
socketId: socketInstance.id,
connected: socketInstance.connected,
transport: socketInstance.io.engine?.transport?.name,
})
// Automatically join the current workflow room based on URL
// This handles both initial connections and reconnections
if (urlWorkflowId) {
logger.info(`Joining workflow room after connection: ${urlWorkflowId}`)
socketInstance.emit('join-workflow', {
workflowId: urlWorkflowId,
})
// Update our internal state to match the URL
setCurrentWorkflowId(urlWorkflowId)
}
})
socketInstance.on('disconnect', (reason) => {
setIsConnected(false)
setIsConnecting(false)
logger.info('Socket disconnected', {
reason,
})
// Clear presence when disconnected
setPresenceUsers([])
})
socketInstance.on('connect_error', (error: any) => {
setIsConnecting(false)
logger.error('Socket connection error:', {
message: error.message,
stack: error.stack,
description: error.description,
type: error.type,
transport: error.transport,
})
// Authentication errors now indicate either session expiry or token generation issues
if (
error.message?.includes('Token validation failed') ||
error.message?.includes('Authentication failed') ||
error.message?.includes('Authentication required')
) {
logger.warn(
'Authentication failed - this could indicate session expiry or token generation issues'
)
// The fresh token generation on each attempt should handle most cases automatically
// If this persists, user may need to refresh page or re-login
}
})
// Socket.IO provides reconnection logging with attempt numbers
socketInstance.on('reconnect', (attemptNumber) => {
logger.info('Socket reconnected successfully', {
attemptNumber,
socketId: socketInstance.id,
transport: socketInstance.io.engine?.transport?.name,
})
// Note: Workflow rejoining is handled by the 'connect' event which fires for both initial connections and reconnections
})
socketInstance.on('reconnect_attempt', (attemptNumber) => {
logger.info('Socket reconnection attempt (fresh token will be generated)', {
attemptNumber,
timestamp: new Date().toISOString(),
})
})
socketInstance.on('reconnect_error', (error: any) => {
logger.error('Socket reconnection error:', {
message: error.message,
attemptNumber: error.attemptNumber,
type: error.type,
})
})
socketInstance.on('reconnect_failed', () => {
logger.error('Socket reconnection failed - all attempts exhausted')
setIsConnecting(false)
})
// Presence events
socketInstance.on('presence-update', (users: PresenceUser[]) => {
setPresenceUsers(users)
})
// Note: user-joined and user-left events removed in favor of authoritative presence-update
// Workflow operation events
socketInstance.on('workflow-operation', (data) => {
eventHandlers.current.workflowOperation?.(data)
})
// Subblock update events
socketInstance.on('subblock-update', (data) => {
eventHandlers.current.subblockUpdate?.(data)
})
// Variable update events
socketInstance.on('variable-update', (data) => {
eventHandlers.current.variableUpdate?.(data)
})
// Workflow deletion events
socketInstance.on('workflow-deleted', (data) => {
logger.warn(`Workflow ${data.workflowId} has been deleted`)
// Clear current workflow ID if it matches the deleted workflow
if (currentWorkflowId === data.workflowId) {
setCurrentWorkflowId(null)
setPresenceUsers([])
}
eventHandlers.current.workflowDeleted?.(data)
})
// Workflow revert events
socketInstance.on('workflow-reverted', (data) => {
logger.info(`Workflow ${data.workflowId} has been reverted to deployed state`)
eventHandlers.current.workflowReverted?.(data)
})
// Shared function to rehydrate workflow stores
const rehydrateWorkflowStores = async (
workflowId: string,
workflowState: any,
source: 'copilot' | 'workflow-state'
) => {
// Import stores dynamically
const [
{ useOperationQueueStore },
{ useWorkflowRegistry },
{ useWorkflowStore },
{ useSubBlockStore },
{ useWorkflowDiffStore },
] = await Promise.all([
import('@/stores/operation-queue/store'),
import('@/stores/workflows/registry/store'),
import('@/stores/workflows/workflow/store'),
import('@/stores/workflows/subblock/store'),
import('@/stores/workflow-diff/store'),
])
// Only proceed if this is the active workflow
const { activeWorkflowId } = useWorkflowRegistry.getState()
if (activeWorkflowId !== workflowId) {
logger.info(`Skipping rehydration - workflow ${workflowId} is not active`)
return false
}
// Check for pending operations
const hasPending = useOperationQueueStore
.getState()
.operations.some((op: any) => op.workflowId === workflowId && op.status !== 'confirmed')
if (hasPending) {
logger.info(`Skipping ${source} rehydration due to pending operations in queue`)
return false
}
// Extract subblock values from blocks
const subblockValues: Record<string, Record<string, any>> = {}
Object.entries(workflowState.blocks || {}).forEach(([blockId, block]) => {
const blockState = block as any
subblockValues[blockId] = {}
Object.entries(blockState.subBlocks || {}).forEach(([subblockId, subblock]) => {
subblockValues[blockId][subblockId] = (subblock as any).value
})
})
// Replace local workflow store with authoritative server state
useWorkflowStore.setState({
blocks: workflowState.blocks || {},
edges: workflowState.edges || [],
loops: workflowState.loops || {},
parallels: workflowState.parallels || {},
lastSaved: workflowState.lastSaved || Date.now(),
deploymentStatuses: workflowState.deploymentStatuses || {},
})
// Replace subblock store values for this workflow
useSubBlockStore.setState((state: any) => ({
workflowValues: {
...state.workflowValues,
[workflowId]: subblockValues,
},
}))
logger.info(`Successfully rehydrated stores from ${source}`)
return true
}
// Copilot workflow edit events (database has been updated, rehydrate stores)
socketInstance.on('copilot-workflow-edit', async (data) => {
logger.info(
`Copilot edited workflow ${data.workflowId} - rehydrating stores from database`
)
try {
// Fetch fresh workflow state directly from API
const response = await fetch(`/api/workflows/${data.workflowId}`)
if (response.ok) {
const responseData = await response.json()
const workflowData = responseData.data
if (workflowData?.state) {
await rehydrateWorkflowStores(data.workflowId, workflowData.state, 'copilot')
}
} else {
logger.error('Failed to fetch fresh workflow state:', response.statusText)
}
} catch (error) {
logger.error('Failed to rehydrate stores after copilot edit:', error)
}
})
// Operation confirmation events
socketInstance.on('operation-confirmed', (data) => {
logger.debug('Operation confirmed', { operationId: data.operationId })
eventHandlers.current.operationConfirmed?.(data)
})
// Operation failure events
socketInstance.on('operation-failed', (data) => {
logger.warn('Operation failed', { operationId: data.operationId, error: data.error })
eventHandlers.current.operationFailed?.(data)
})
// Cursor update events
socketInstance.on('cursor-update', (data) => {
setPresenceUsers((prev) =>
prev.map((user) =>
user.socketId === data.socketId ? { ...user, cursor: data.cursor } : user
)
)
eventHandlers.current.cursorUpdate?.(data)
})
// Selection update events
socketInstance.on('selection-update', (data) => {
setPresenceUsers((prev) =>
prev.map((user) =>
user.socketId === data.socketId ? { ...user, selection: data.selection } : user
)
)
eventHandlers.current.selectionUpdate?.(data)
})
// Enhanced error handling for new server events
socketInstance.on('error', (error) => {
logger.error('Socket error:', error)
})
socketInstance.on('operation-error', (error) => {
logger.error('Operation error:', error)
})
socketInstance.on('operation-forbidden', (error) => {
logger.warn('Operation forbidden:', error)
// Could show a toast notification to user
})
socketInstance.on('operation-confirmed', (data) => {
logger.debug('Operation confirmed:', data)
})
socketInstance.on('workflow-state', async (workflowData) => {
logger.info('Received workflow state from server')
if (workflowData?.state) {
await rehydrateWorkflowStores(workflowData.id, workflowData.state, 'workflow-state')
}
})
setSocket(socketInstance)
return () => {
socketInstance.close()
}
} catch (error) {
logger.error('Failed to initialize socket with token:', error)
setIsConnecting(false)
}
}
// Start the socket initialization
initializeSocket()
// Cleanup on unmount only (not on user change since socket is session-level)
return () => {
positionUpdateTimeouts.current.forEach((timeoutId) => {
clearTimeout(timeoutId)
})
positionUpdateTimeouts.current.clear()
pendingPositionUpdates.current.clear()
}
}, [user?.id])
// Handle workflow room switching when URL changes (for navigation between workflows)
useEffect(() => {
if (!socket || !isConnected || !urlWorkflowId) return
// If we're already in the correct workflow room, no need to switch
if (currentWorkflowId === urlWorkflowId) return
logger.info(
`URL workflow changed from ${currentWorkflowId} to ${urlWorkflowId}, switching rooms`
)
// Leave current workflow first if we're in one
if (currentWorkflowId) {
logger.info(`Leaving current workflow ${currentWorkflowId} before joining ${urlWorkflowId}`)
socket.emit('leave-workflow')
}
// Join the new workflow room
logger.info(`Joining workflow room: ${urlWorkflowId}`)
socket.emit('join-workflow', {
workflowId: urlWorkflowId,
})
setCurrentWorkflowId(urlWorkflowId)
}, [socket, isConnected, urlWorkflowId, currentWorkflowId])
// Cleanup socket on component unmount
useEffect(() => {
return () => {
if (socket) {
logger.info('Cleaning up socket connection on unmount')
socket.disconnect()
}
}
}, [])
// Join workflow room
const joinWorkflow = useCallback(
(workflowId: string) => {
if (!socket || !user?.id) {
logger.warn('Cannot join workflow: socket or user not available')
return
}
// Prevent duplicate joins to the same workflow
if (currentWorkflowId === workflowId) {
logger.info(`Already in workflow ${workflowId}, skipping join`)
return
}
// Leave current workflow first if we're in one
if (currentWorkflowId) {
logger.info(`Leaving current workflow ${currentWorkflowId} before joining ${workflowId}`)
socket.emit('leave-workflow')
}
logger.info(`Joining workflow: ${workflowId}`)
socket.emit('join-workflow', {
workflowId, // Server gets user info from authenticated session
})
setCurrentWorkflowId(workflowId)
},
[socket, user, currentWorkflowId]
)
// Leave current workflow room
const leaveWorkflow = useCallback(() => {
if (socket && currentWorkflowId) {
logger.info(`Leaving workflow: ${currentWorkflowId}`)
try {
const { useOperationQueueStore } = require('@/stores/operation-queue/store')
useOperationQueueStore.getState().cancelOperationsForWorkflow(currentWorkflowId)
} catch {}
socket.emit('leave-workflow')
setCurrentWorkflowId(null)
setPresenceUsers([])
// Clean up any pending position updates
positionUpdateTimeouts.current.forEach((timeoutId) => {
clearTimeout(timeoutId)
})
positionUpdateTimeouts.current.clear()
pendingPositionUpdates.current.clear()
}
}, [socket, currentWorkflowId])
// Light throttling for position updates to ensure smooth collaborative movement
const positionUpdateTimeouts = useRef<Map<string, number>>(new Map())
const pendingPositionUpdates = useRef<Map<string, any>>(new Map())
// Emit workflow operations (blocks, edges, subflows)
const emitWorkflowOperation = useCallback(
(operation: string, target: string, payload: any, operationId?: string) => {
if (!socket || !currentWorkflowId) {
return
}
// Apply light throttling only to position updates for smooth collaborative experience
const isPositionUpdate = operation === 'update-position' && target === 'block'
const { commit = true } = payload || {}
if (isPositionUpdate && payload.id) {
const blockId = payload.id
if (commit) {
socket.emit('workflow-operation', {
operation,
target,
payload,
timestamp: Date.now(),
operationId,
})
pendingPositionUpdates.current.delete(blockId)
const timeoutId = positionUpdateTimeouts.current.get(blockId)
if (timeoutId) {
clearTimeout(timeoutId)
positionUpdateTimeouts.current.delete(blockId)
}
return
}
pendingPositionUpdates.current.set(blockId, {
operation,
target,
payload,
timestamp: Date.now(),
operationId,
})
if (!positionUpdateTimeouts.current.has(blockId)) {
const timeoutId = window.setTimeout(() => {
const latestUpdate = pendingPositionUpdates.current.get(blockId)
if (latestUpdate) {
socket.emit('workflow-operation', latestUpdate)
pendingPositionUpdates.current.delete(blockId)
}
positionUpdateTimeouts.current.delete(blockId)
}, 33)
positionUpdateTimeouts.current.set(blockId, timeoutId)
}
} else {
// For all non-position updates, emit immediately
socket.emit('workflow-operation', {
operation,
target,
payload,
timestamp: Date.now(),
operationId, // Include operation ID for queue tracking
})
}
},
[socket, currentWorkflowId]
)
// Emit subblock value updates
const emitSubblockUpdate = useCallback(
(blockId: string, subblockId: string, value: any, operationId?: string) => {
// Only emit if socket is connected and we're in a valid workflow room
if (socket && currentWorkflowId) {
socket.emit('subblock-update', {
blockId,
subblockId,
value,
timestamp: Date.now(),
operationId, // Include operation ID for queue tracking
})
} else {
logger.warn('Cannot emit subblock update: no socket connection or workflow room', {
hasSocket: !!socket,
currentWorkflowId,
blockId,
subblockId,
})
}
},
[socket, currentWorkflowId]
)
// Emit variable value updates
const emitVariableUpdate = useCallback(
(variableId: string, field: string, value: any, operationId?: string) => {
// Only emit if socket is connected and we're in a valid workflow room
if (socket && currentWorkflowId) {
socket.emit('variable-update', {
variableId,
field,
value,
timestamp: Date.now(),
operationId, // Include operation ID for queue tracking
})
} else {
logger.warn('Cannot emit variable update: no socket connection or workflow room', {
hasSocket: !!socket,
currentWorkflowId,
variableId,
field,
})
}
},
[socket, currentWorkflowId]
)
// Cursor throttling optimized for database connection health
const lastCursorEmit = useRef(0)
const emitCursorUpdate = useCallback(
(cursor: { x: number; y: number } | null) => {
if (!socket || !currentWorkflowId) {
return
}
const now = performance.now()
if (cursor === null) {
socket.emit('cursor-update', { cursor: null })
lastCursorEmit.current = now
return
}
// Reduced to 30fps (33ms) to reduce database load while maintaining smooth UX
if (now - lastCursorEmit.current >= 33) {
socket.emit('cursor-update', { cursor })
lastCursorEmit.current = now
}
},
[socket, currentWorkflowId]
)
// Emit selection updates
const emitSelectionUpdate = useCallback(
(selection: { type: 'block' | 'edge' | 'none'; id?: string }) => {
if (socket && currentWorkflowId) {
socket.emit('selection-update', { selection })
}
},
[socket, currentWorkflowId]
)
// Event handler registration functions
const onWorkflowOperation = useCallback((handler: (data: any) => void) => {
eventHandlers.current.workflowOperation = handler
}, [])
const onSubblockUpdate = useCallback((handler: (data: any) => void) => {
eventHandlers.current.subblockUpdate = handler
}, [])
const onVariableUpdate = useCallback((handler: (data: any) => void) => {
eventHandlers.current.variableUpdate = handler
}, [])
const onCursorUpdate = useCallback((handler: (data: any) => void) => {
eventHandlers.current.cursorUpdate = handler
}, [])
const onSelectionUpdate = useCallback((handler: (data: any) => void) => {
eventHandlers.current.selectionUpdate = handler
}, [])
const onUserJoined = useCallback((handler: (data: any) => void) => {
eventHandlers.current.userJoined = handler
}, [])
const onUserLeft = useCallback((handler: (data: any) => void) => {
eventHandlers.current.userLeft = handler
}, [])
const onWorkflowDeleted = useCallback((handler: (data: any) => void) => {
eventHandlers.current.workflowDeleted = handler
}, [])
const onWorkflowReverted = useCallback((handler: (data: any) => void) => {
eventHandlers.current.workflowReverted = handler
}, [])
const onOperationConfirmed = useCallback((handler: (data: any) => void) => {
eventHandlers.current.operationConfirmed = handler
}, [])
const onOperationFailed = useCallback((handler: (data: any) => void) => {
eventHandlers.current.operationFailed = handler
}, [])
return (
<SocketContext.Provider
value={{
socket,
isConnected,
isConnecting,
currentWorkflowId,
presenceUsers,
joinWorkflow,
leaveWorkflow,
emitWorkflowOperation,
emitSubblockUpdate,
emitVariableUpdate,
emitCursorUpdate,
emitSelectionUpdate,
onWorkflowOperation,
onSubblockUpdate,
onVariableUpdate,
onCursorUpdate,
onSelectionUpdate,
onUserJoined,
onUserLeft,
onWorkflowDeleted,
onWorkflowReverted,
onOperationConfirmed,
onOperationFailed,
}}
>
{children}
</SocketContext.Provider>
)
}