Files
dip-ids/frontend/composables/useCaptureStatus.ts
T
2026-04-12 22:26:26 +04:00

60 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
interface CaptureStatus {
running: boolean
session_id?: number
source?: string
mode?: string
pkts?: number
bytes?: number
last_session?: {
id: number
source: string
mode: string
status: string
total_pkts: number
total_bytes: number
created_at: string
ended_at?: string
}
}
// Синглтон — один интервал на всё приложение
let interval: number | null = null
const status = ref<CaptureStatus>({ running: false })
export function useCaptureStatus() {
const { get, post } = useApi()
async function fetchStatus() {
try {
status.value = await get<CaptureStatus>('/capture/status')
} catch {}
}
function startPolling() {
if (interval || !import.meta.client) return
fetchStatus()
interval = window.setInterval(fetchStatus, 3000)
}
function stopPolling() {
if (interval) {
clearInterval(interval)
interval = null
}
}
async function stopCapture() {
try {
await post('/capture/stop')
await fetchStatus()
} catch {}
}
onMounted(startPolling)
onUnmounted(() => {
// Не останавливаем polling при размонтировании layout'а
})
return { status, fetchStatus, stopCapture, startPolling, stopPolling }
}