fix session reconnect

This commit is contained in:
Chris Scott committed 2025-11-25 16:38:07 -05:00
1 parent a4b6222577
commit 45b8d7d117
1 file changed
+28
+28
View File
@@ -8,6 +8,7 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string)
const queryClient = useQueryClient()
const eventSourceRef = useRef<EventSource | null>(null)
const urlRef = useRef<string | null>(null)
const reconnectTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const [isConnected, setIsConnected] = useState(false)
const [error, setError] = useState<string | null>(null)
@@ -35,6 +36,16 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string)
urlRef.current = eventSourceUrl
const connectSSE = () => {
if (reconnectTimeoutRef.current) {
clearTimeout(reconnectTimeoutRef.current)
reconnectTimeoutRef.current = null
}
if (eventSourceRef.current) {
eventSourceRef.current.close()
eventSourceRef.current = null
}
try {
const eventSource = new EventSource(eventSourceUrl)
eventSourceRef.current = eventSource
@@ -42,6 +53,8 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string)
eventSource.onopen = () => {
setIsConnected(true)
setError(null)
queryClient.invalidateQueries({ queryKey: ['opencode', 'sessions', opcodeUrl, directory] })
queryClient.invalidateQueries({ queryKey: ['opencode', 'messages', opcodeUrl] })
}
eventSource.onerror = (e) => {
@@ -69,6 +82,12 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string)
}
}
const handleReconnect = () => {
if (!eventSourceRef.current || eventSourceRef.current.readyState === EventSource.CLOSED) {
connectSSE()
}
}
const handleSSEEvent = (event: SSEEvent) => {
switch (event.type) {
case 'session.updated':
@@ -230,7 +249,16 @@ case 'message.updated':
connectSSE()
window.addEventListener('focus', handleReconnect)
window.addEventListener('online', handleReconnect)
return () => {
window.removeEventListener('focus', handleReconnect)
window.removeEventListener('online', handleReconnect)
if (reconnectTimeoutRef.current) {
clearTimeout(reconnectTimeoutRef.current)
reconnectTimeoutRef.current = null
}
if (eventSourceRef.current) {
eventSourceRef.current.close()
eventSourceRef.current = null