2026-04-19 14:18:20 +08:00
|
|
|
import { computed, onMounted, onUnmounted, readonly, shallowRef } from "vue";
|
|
|
|
|
|
|
|
|
|
import type { DesktopRuntimeSnapshot } from "../types";
|
|
|
|
|
|
|
|
|
|
const snapshot = shallowRef<DesktopRuntimeSnapshot | null>(null);
|
|
|
|
|
const loading = shallowRef(false);
|
|
|
|
|
const error = shallowRef<string | null>(null);
|
|
|
|
|
|
|
|
|
|
let intervalHandle: ReturnType<typeof setInterval> | null = null;
|
|
|
|
|
let subscribers = 0;
|
|
|
|
|
|
|
|
|
|
async function refreshRuntimeSnapshot() {
|
|
|
|
|
loading.value = true;
|
|
|
|
|
error.value = null;
|
|
|
|
|
|
|
|
|
|
try {
|
2026-04-20 09:52:48 +08:00
|
|
|
if (!window.desktopBridge?.app?.runtimeSnapshot) {
|
|
|
|
|
snapshot.value = null;
|
|
|
|
|
error.value = "desktop runtime bridge unavailable";
|
2026-04-19 14:18:20 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
snapshot.value = await window.desktopBridge.app.runtimeSnapshot();
|
2026-04-19 14:18:20 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
error.value = err instanceof Error ? err.message : "runtime snapshot unavailable";
|
|
|
|
|
} finally {
|
|
|
|
|
loading.value = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
async function refreshAccounts() {
|
|
|
|
|
if (!window.desktopBridge?.app?.refreshRuntimeAccounts) {
|
|
|
|
|
await refreshRuntimeSnapshot();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loading.value = true;
|
|
|
|
|
error.value = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await window.desktopBridge.app.refreshRuntimeAccounts();
|
|
|
|
|
snapshot.value = await window.desktopBridge.app.runtimeSnapshot();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
error.value = err instanceof Error ? err.message : "refresh accounts failed";
|
|
|
|
|
} finally {
|
|
|
|
|
loading.value = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
function startPolling() {
|
|
|
|
|
if (intervalHandle) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intervalHandle = setInterval(() => {
|
|
|
|
|
void refreshRuntimeSnapshot();
|
|
|
|
|
}, 15_000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stopPolling() {
|
|
|
|
|
if (subscribers > 0 || !intervalHandle) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearInterval(intervalHandle);
|
|
|
|
|
intervalHandle = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useDesktopRuntime() {
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
subscribers += 1;
|
|
|
|
|
if (!snapshot.value && !loading.value) {
|
|
|
|
|
void refreshRuntimeSnapshot();
|
|
|
|
|
}
|
|
|
|
|
startPolling();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
subscribers = Math.max(0, subscribers - 1);
|
|
|
|
|
stopPolling();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
snapshot: readonly(snapshot),
|
|
|
|
|
loading: readonly(loading),
|
|
|
|
|
error: readonly(error),
|
|
|
|
|
refresh: refreshRuntimeSnapshot,
|
2026-04-20 09:52:48 +08:00
|
|
|
refreshAccounts,
|
2026-04-19 14:18:20 +08:00
|
|
|
hasSnapshot: computed(() => snapshot.value !== null),
|
|
|
|
|
};
|
|
|
|
|
}
|