MCPcopy
hub / github.com/simstudioai/sim / SocketProvider

Function SocketProvider

apps/sim/app/workspace/providers/socket-provider.tsx:152–1180  ·  view source on GitHub ↗
({ children, user }: SocketProviderProps)

Source from the content-addressed store, hash-verified

150}
151
152export 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

Callers

nothing calls this directly

Calls 15

isSocketWorkflowVisibleFunction · 0.90
getTabSessionIdFunction · 0.85
initializeSocketFunction · 0.85
infoMethod · 0.80
retryJoinMethod · 0.80
requestWorkflowMethod · 0.80
debugMethod · 0.80
clearMethod · 0.65
warnMethod · 0.65
closeMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected