({ children, user }: SocketProviderProps)
| 150 | } |
| 151 | |
| 152 | export function SocketProvider({ children, user }: SocketProviderProps) { |
| 153 | const [socket, setSocket] = useState<Socket | null>(null) |
| 154 | const [isConnected, setIsConnected] = useState(false) |
| 155 | const [isConnecting, setIsConnecting] = useState(false) |
| 156 | const [isReconnecting, setIsReconnecting] = useState(false) |
| 157 | const [isRetryingWorkflowJoin, setIsRetryingWorkflowJoin] = useState(false) |
| 158 | const [currentWorkflowId, setCurrentWorkflowId] = useState<string | null>(null) |
| 159 | const [currentSocketId, setCurrentSocketId] = useState<string | null>(null) |
| 160 | const [authFailed, setAuthFailed] = useState(false) |
| 161 | const [blockedJoinWorkflowId, setBlockedJoinWorkflowId] = useState<string | null>(null) |
| 162 | const [explicitWorkflowId, setExplicitWorkflowId] = useState<string | null>(null) |
| 163 | const initializedRef = useRef(false) |
| 164 | const socketRef = useRef<Socket | null>(null) |
| 165 | const currentWorkflowIdRef = useRef<string | null>(null) |
| 166 | const explicitWorkflowIdRef = useRef<string | null>(explicitWorkflowId) |
| 167 | const joinControllerRef = useRef(new SocketJoinController()) |
| 168 | const joinRetryTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null) |
| 169 | const authRetryAttemptsRef = useRef(0) |
| 170 | const authRetryTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null) |
| 171 | |
| 172 | const params = useParams() |
| 173 | const urlWorkflowId = params?.workflowId as string | undefined |
| 174 | const urlWorkflowIdRef = useRef(urlWorkflowId) |
| 175 | urlWorkflowIdRef.current = urlWorkflowId |
| 176 | explicitWorkflowIdRef.current = explicitWorkflowId |
| 177 | |
| 178 | const eventHandlers = useRef<{ |
| 179 | workflowOperation?: (data: WorkflowOperationBroadcast) => void |
| 180 | subblockUpdate?: (data: SubblockUpdateBroadcast) => void |
| 181 | variableUpdate?: (data: VariableUpdateBroadcast) => void |
| 182 | cursorUpdate?: (data: CursorUpdateBroadcast) => void |
| 183 | selectionUpdate?: (data: SelectionUpdateBroadcast) => void |
| 184 | workflowDeleted?: (data: WorkflowDeletedBroadcast) => void |
| 185 | workflowReverted?: (data: WorkflowRevertedBroadcast) => void |
| 186 | workflowUpdated?: (data: WorkflowUpdatedBroadcast) => void |
| 187 | workflowDeployed?: (data: WorkflowDeployedBroadcast) => void |
| 188 | operationConfirmed?: (data: OperationConfirmedBroadcast) => void |
| 189 | operationFailed?: (data: OperationFailedBroadcast) => void |
| 190 | }>({}) |
| 191 | |
| 192 | const positionUpdateTimeouts = useRef<Map<string, number>>(new Map()) |
| 193 | const pendingPositionUpdates = useRef<Map<string, any>>(new Map()) |
| 194 | |
| 195 | /** |
| 196 | * Presence is high-frequency (cursor frames many times per second) so it lives |
| 197 | * in {@link usePresenceStore}, not the broad socket context — writing it here no |
| 198 | * longer mints a new context value, so emitter-only `useSocket()` consumers stop |
| 199 | * re-rendering on every cursor frame. These thin wrappers delegate to the store's |
| 200 | * stable actions read via `getState()`. |
| 201 | */ |
| 202 | const setPresenceUsers = useCallback((users: PresenceUser[]) => { |
| 203 | usePresenceStore.getState().setPresenceUsers(users) |
| 204 | }, []) |
| 205 | |
| 206 | const updatePresenceUsers = useCallback((updater: (prev: PresenceUser[]) => PresenceUser[]) => { |
| 207 | usePresenceStore.getState().updatePresenceUsers(updater) |
| 208 | }, []) |
| 209 |
nothing calls this directly
no test coverage detected