mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix session reconnect
This commit is contained in:
1 parent
a4b6222577
commit
45b8d7d117
1 file changed
+28
@@ -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
|
||||
|
||||
Reference in new issue
Block a user