60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
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 }
|
||
}
|