feat: disconnect devices on app background mobile

This commit is contained in:
Lucas
2026-11-04 11:08:22 +01:00
parent be713620ef
commit ddb7a907a1
15 changed files with 264 additions and 37 deletions
+4
View File
@@ -14,6 +14,7 @@ import { stopAllPolling } from './dsp/pollRegistry';
import { loadSavedDSPs, saveDSPs, toSaved } from './dsp/dspStorage';
import { DSPMode } from './features/dsp408/types';
import { useIsMobile } from './hooks/useIsMobile';
import { useMobileBackgroundDisconnect } from './dsp/useMobileBackgroundDisconnect';
function App() {
const [dsps, setDsps] = useState<DSP[]>([]);
@@ -30,6 +31,9 @@ function App() {
const dspsRef = useRef(dsps);
dspsRef.current = dsps;
useMobileBackgroundDisconnect(dsps, setDsps, notify); // NEW
useEffect(() => {
return () => stopAllPolling(dspsRef.current);
}, []);
+10
View File
@@ -0,0 +1,10 @@
import { platform } from '@tauri-apps/plugin-os';
export const isMobileOS: boolean = (() => {
try {
const p = platform();
return p === 'android' || p === 'ios';
} catch {
return false; // not running inside Tauri (tests, browser dev)
}
})();
+54
View File
@@ -0,0 +1,54 @@
import { useEffect, useRef } from 'react';
import { isMobileOS } from './platform';
import { forDSP408, setAppBackgrounded, isPolling } from '../features/dsp408/dsp408';
import { DSP, isConnected } from '../types';
import { SetDsps, NotifyFn } from './dspShared';
export function useMobileBackgroundDisconnect(
dsps: DSP[],
setDsps: SetDsps,
notify: NotifyFn
) {
const dspsRef = useRef(dsps);
dspsRef.current = dsps;
useEffect(() => {
if (!isMobileOS) return; // desktop: nothing is registered
const goBackground = () => {
setAppBackgrounded(true);
for (const d of dspsRef.current) {
if (d.type !== 'DSP408') continue;
// Rust disconnect is idempotent, so no connection-state filter is needed.
// ADJUST: skip demo-mode DSPs here if you don't want them disconnected.
void forDSP408(d, setDsps, notify).disconnectForBackground();
}
};
const onResume = () => {
setAppBackgrounded(false); // allows a MANUAL connect again; never reconnects
for (const d of dspsRef.current) {
if (d.type !== 'DSP408' || !isPolling(d.id)) continue;
const api = forDSP408(d, setDsps, notify);
api
.updateDSPState()
.then((s) => {
if (!isConnected({ ...d, state: s } as DSP)) return api.disconnectForBackground();
})
.catch(() => {});
}
};
const onVisibility = () => {
if (document.visibilityState === 'hidden') goBackground();
else onResume();
};
document.addEventListener('visibilitychange', onVisibility);
window.addEventListener('pagehide', goBackground);
return () => {
document.removeEventListener('visibilitychange', onVisibility);
window.removeEventListener('pagehide', goBackground);
};
}, [setDsps, notify]);
}
+46
View File
@@ -78,6 +78,16 @@ export function stopPolling(id: number) {
registerPollStopper('DSP408', stopPolling);
// --- background state, private to this module --------------------------
let appBackgrounded = false;
export function setAppBackgrounded(v: boolean) {
appBackgrounded = v;
}
export function isPolling(id: number): boolean {
return pollIntervals.has(id);
}
// --- pause helper ----------------------------------------------------------
async function withPollingPaused<T>(
@@ -141,6 +151,30 @@ export function forDSP408(dsp: DSP408, setDsps: SetDsps, notify: NotifyFn) {
}
}
async function disconnectForBackground() {
// synchronous part first: the webview may be frozen right after this
stopPolling(dsp.id);
updateDSP(setDsps, dsp.id, (d) => ({
...d,
isConnecting: false,
pollingPaused: true,
}));
try {
await invoke('disconnect_dsp408', { id: dsp.id });
} catch {
// socket may already be gone; Rust-side fallback covers the rest
}
// quiet state refresh (no error toast while backgrounded)
try {
const state = await invoke<DSP408['state']>('get_dsp_state', { id: dsp.id });
updateDSP(setDsps, dsp.id, (d) => ({ ...d, state }) as DSP408);
} catch {
/* ignore */
}
}
const isLocked = (s: DSP408['state']) => s.flags.is_locked;
async function abortLocked() {
@@ -155,6 +189,11 @@ export function forDSP408(dsp: DSP408, setDsps: SetDsps, notify: NotifyFn) {
try {
await invoke('connect_dsp408', { id: dsp.id });
if (appBackgrounded) { // NEW: backgrounded mid-connect
await disconnectForBackground();
return;
}
let state = await updateDSPState();
if (isLocked(state)) {
@@ -174,6 +213,11 @@ export function forDSP408(dsp: DSP408, setDsps: SetDsps, notify: NotifyFn) {
}
}
if (appBackgrounded) {
await disconnectForBackground();
return;
}
updateDSP(setDsps, dsp.id, (d) => ({ ...d, isConnecting: false, pollingPaused: false }));
notify('Connected', 'success', dsp.name);
startPolling(dsp, setDsps);
@@ -939,5 +983,7 @@ export function forDSP408(dsp: DSP408, setDsps: SetDsps, notify: NotifyFn) {
}
});
},
disconnectForBackground,
};
}