|
| 1 | +import { ref, onUnmounted, type Ref } from 'vue' |
| 2 | +import type { InstallationStatusData } from '@/types/mcp-installations' |
| 3 | + |
| 4 | +export interface UseStatusStreamOptions { |
| 5 | + reconnectDelay?: number |
| 6 | + withCredentials?: boolean |
| 7 | +} |
| 8 | + |
| 9 | +export interface UseStatusStreamReturn { |
| 10 | + statusData: Ref<InstallationStatusData | null> |
| 11 | + isConnected: Ref<boolean> |
| 12 | + isLoading: Ref<boolean> |
| 13 | + error: Ref<string | null> |
| 14 | + connect: (url: string) => void |
| 15 | + disconnect: () => void |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Composable for streaming MCP installation status via Server-Sent Events |
| 20 | + * |
| 21 | + * Handles two event types from the backend: |
| 22 | + * - `snapshot`: Initial status snapshot |
| 23 | + * - `status_update`: Real-time status updates |
| 24 | + * |
| 25 | + * @param options - Configuration options |
| 26 | + * @returns Reactive state and control methods |
| 27 | + * |
| 28 | + * @example |
| 29 | + * const { statusData, isConnected, error, connect, disconnect } = useStatusStream() |
| 30 | + * onMounted(() => connect(`${baseUrl}/api/teams/${teamId}/mcp/installations/${installationId}/status/stream`)) |
| 31 | + */ |
| 32 | +export function useStatusStream( |
| 33 | + options: UseStatusStreamOptions = {} |
| 34 | +): UseStatusStreamReturn { |
| 35 | + const { reconnectDelay = 5000, withCredentials = true } = options |
| 36 | + |
| 37 | + const statusData = ref<InstallationStatusData | null>(null) |
| 38 | + const isConnected = ref(false) |
| 39 | + const isLoading = ref(true) |
| 40 | + const error = ref<string | null>(null) |
| 41 | + |
| 42 | + let eventSource: EventSource | null = null |
| 43 | + let reconnectTimeout: number | null = null |
| 44 | + let currentUrl: string | null = null |
| 45 | + let isUnloading = false |
| 46 | + |
| 47 | + const handleBeforeUnload = () => { |
| 48 | + isUnloading = true |
| 49 | + disconnect() |
| 50 | + } |
| 51 | + |
| 52 | + if (typeof window !== 'undefined') { |
| 53 | + window.addEventListener('beforeunload', handleBeforeUnload) |
| 54 | + } |
| 55 | + |
| 56 | + function connect(url: string) { |
| 57 | + currentUrl = url |
| 58 | + disconnect() |
| 59 | + isLoading.value = true |
| 60 | + error.value = null |
| 61 | + |
| 62 | + try { |
| 63 | + eventSource = new EventSource(url, { withCredentials }) |
| 64 | + |
| 65 | + // Handle initial snapshot |
| 66 | + eventSource.addEventListener('snapshot', (event: MessageEvent) => { |
| 67 | + try { |
| 68 | + const data = JSON.parse(event.data) as InstallationStatusData |
| 69 | + statusData.value = data |
| 70 | + error.value = null |
| 71 | + isLoading.value = false |
| 72 | + isConnected.value = true |
| 73 | + } catch (err) { |
| 74 | + console.error('useStatusStream: Failed to parse snapshot data:', err) |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + // Handle status updates |
| 79 | + eventSource.addEventListener('status_update', (event: MessageEvent) => { |
| 80 | + try { |
| 81 | + const data = JSON.parse(event.data) as InstallationStatusData |
| 82 | + statusData.value = data |
| 83 | + } catch (err) { |
| 84 | + console.error('useStatusStream: Failed to parse status update:', err) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + eventSource.addEventListener('error', (event: Event) => { |
| 89 | + try { |
| 90 | + const messageEvent = event as MessageEvent |
| 91 | + if (messageEvent.data) { |
| 92 | + const parsed = JSON.parse(messageEvent.data) |
| 93 | + error.value = parsed.error || 'Stream error' |
| 94 | + } |
| 95 | + } catch { |
| 96 | + // Not a JSON error event |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + eventSource.onopen = () => { |
| 101 | + isConnected.value = true |
| 102 | + error.value = null |
| 103 | + } |
| 104 | + |
| 105 | + eventSource.onerror = () => { |
| 106 | + isConnected.value = false |
| 107 | + isLoading.value = false |
| 108 | + |
| 109 | + if (isUnloading) return |
| 110 | + |
| 111 | + if (reconnectTimeout) clearTimeout(reconnectTimeout) |
| 112 | + reconnectTimeout = window.setTimeout(() => { |
| 113 | + if (currentUrl) { |
| 114 | + connect(currentUrl) |
| 115 | + } |
| 116 | + }, reconnectDelay) |
| 117 | + } |
| 118 | + } catch (err) { |
| 119 | + error.value = err instanceof Error ? err.message : 'Failed to connect' |
| 120 | + isLoading.value = false |
| 121 | + console.error('useStatusStream: Connection error:', err) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + function disconnect() { |
| 126 | + if (eventSource) { |
| 127 | + eventSource.close() |
| 128 | + eventSource = null |
| 129 | + } |
| 130 | + if (reconnectTimeout) { |
| 131 | + clearTimeout(reconnectTimeout) |
| 132 | + reconnectTimeout = null |
| 133 | + } |
| 134 | + isConnected.value = false |
| 135 | + } |
| 136 | + |
| 137 | + onUnmounted(() => { |
| 138 | + disconnect() |
| 139 | + if (typeof window !== 'undefined') { |
| 140 | + window.removeEventListener('beforeunload', handleBeforeUnload) |
| 141 | + } |
| 142 | + }) |
| 143 | + |
| 144 | + return { |
| 145 | + statusData, |
| 146 | + isConnected, |
| 147 | + isLoading, |
| 148 | + error, |
| 149 | + connect, |
| 150 | + disconnect |
| 151 | + } |
| 152 | +} |
0 commit comments