import { useEffect, useState } from 'react' import { VMStats } from '../types' import { api } from '../wails' // Shared VM stats poller, keyed by VMRef so same-name VMs never share samples. const RUNNING_MS = 3000 const STOPPED_MS = 30000 interface Entry { subs: Set<(s: VMStats) => void> running: boolean last: VMStats | null timer?: number seq: number appliedSeq: number } const entries = new Map() const statsKey = (driverID: string, name: string) => `${driverID}:${name}` function sample(key: string, driverID: string, name: string, entryRef: Entry) { if (entries.get(key) !== entryRef) return const reqSeq = ++entryRef.seq api.GetVMStats(driverID, name) .then((s) => { if (entries.get(key) !== entryRef || reqSeq <= entryRef.appliedSeq) return entryRef.appliedSeq = reqSeq entryRef.last = s entryRef.subs.forEach((f) => f(s)) }) .catch((e) => console.warn('[vmStats] GetVMStats failed:', key, e)) } function schedule(key: string, driverID: string, name: string, e: Entry) { if (e.timer) window.clearInterval(e.timer) e.timer = undefined if (document.hidden || e.subs.size === 0) return e.timer = window.setInterval(() => sample(key, driverID, name, e), e.running ? RUNNING_MS : STOPPED_MS) } if (typeof document !== 'undefined') { document.addEventListener('visibilitychange', () => { entries.forEach((e, key) => { const split = key.indexOf(':') const driverID = key.slice(0, split) const name = key.slice(split + 1) if (!document.hidden && e.subs.size > 0) sample(key, driverID, name, e) schedule(key, driverID, name, e) }) }) } /** Latest stats seen for a VM, without subscribing (e.g. when a dialog opens). */ export function latestVMStats(driverID: string, name: string | undefined | null): VMStats | null { return name ? entries.get(statsKey(driverID, name))?.last ?? null : null } /** Immediately patch cached stats (if any) and re-sample a VM's stats. */ export function refreshVMStats(driverID: string, name: string, patch?: Partial) { const key = statsKey(driverID, name) const e = entries.get(key) if (!e) return e.appliedSeq = ++e.seq if (patch && e.last) { e.last = { ...e.last, ...patch } e.subs.forEach((f) => f(e.last!)) } sample(key, driverID, name, e) } /** Re-sample all currently subscribed VMs (e.g. after a tray-triggered state change). */ export function refreshSubscribedVMStats() { entries.forEach((e, key) => { if (e.subs.size === 0) return const split = key.indexOf(':') e.appliedSeq = ++e.seq sample(key, key.slice(0, split), key.slice(split + 1), e) }) } /** Live stats for one VM; `running` picks the sampling rate. Null name: none. */ export function useVMStats(driverID: string, name: string | null, running: boolean): VMStats | null { const [stats, setStats] = useState(() => latestVMStats(driverID, name)) useEffect(() => { if (!name) { setStats(null); return } const key = statsKey(driverID, name) let e = entries.get(key) if (!e) { e = { subs: new Set(), running, last: null, seq: 0, appliedSeq: 0 } entries.set(key, e) } setStats(e.last) const sub = (s: VMStats) => setStats(s) e.subs.add(sub) if (e.running !== running) { e.running = running e.appliedSeq = ++e.seq } sample(key, driverID, name, e) schedule(key, driverID, name, e) return () => { const cur = entries.get(key) if (!cur || cur !== e) return cur.subs.delete(sub) if (cur.subs.size === 0) { if (cur.timer) window.clearInterval(cur.timer) entries.delete(key) } } }, [driverID, name, running]) return stats }