Enhance DeviceContext to fetch and manage PEQ state; update EQPage to synchronize PEQ catalog and improve headphone selection logic based on PEQ state changes. Refactor related functions for better state handling and synchronization.
This commit is contained in:
@@ -67,6 +67,11 @@ export function DeviceProvider({ children }: { children: React.ReactNode }) {
|
||||
lastMsgCountRef.current = state.msgCount;
|
||||
}, []);
|
||||
|
||||
const fetchPeqState = useCallback(async (apiInstance: LuxsinAPI) => {
|
||||
const peq = await apiInstance.getPeqState();
|
||||
setPeqState(peq);
|
||||
}, []);
|
||||
|
||||
const connect = useCallback(async (forceDemo?: boolean, connectIp?: string) => {
|
||||
const useDemo = forceDemo !== undefined ? forceDemo : isDemoMode;
|
||||
const targetIp = (connectIp ?? ip).trim();
|
||||
@@ -138,10 +143,11 @@ export function DeviceProvider({ children }: { children: React.ReactNode }) {
|
||||
if (!apiInstance) return;
|
||||
try {
|
||||
await fetchState(apiInstance);
|
||||
await fetchPeqState(apiInstance).catch(() => undefined);
|
||||
} catch (e) {
|
||||
setError(`Refresh failed: ${e instanceof Error ? e.message : String(e)}`);
|
||||
}
|
||||
}, [api, isConnected, ip, isDemoMode, fetchState]);
|
||||
}, [api, isConnected, ip, isDemoMode, fetchState, fetchPeqState]);
|
||||
|
||||
const updateSetting = useCallback(async (params: Record<string, string | number>) => {
|
||||
if (isDemoMode) {
|
||||
@@ -235,7 +241,10 @@ export function DeviceProvider({ children }: { children: React.ReactNode }) {
|
||||
try {
|
||||
const count = await api.getMsgCount();
|
||||
if (count !== lastMsgCountRef.current) {
|
||||
await fetchState(api);
|
||||
await Promise.all([
|
||||
fetchState(api),
|
||||
fetchPeqState(api).catch(() => undefined),
|
||||
]);
|
||||
}
|
||||
} catch {
|
||||
// Silently fail polling
|
||||
@@ -246,7 +255,7 @@ export function DeviceProvider({ children }: { children: React.ReactNode }) {
|
||||
return () => {
|
||||
if (pollIntervalRef.current) clearInterval(pollIntervalRef.current);
|
||||
};
|
||||
}, [isConnected, isDemoMode, api, fetchState]);
|
||||
}, [isConnected, isDemoMode, api, fetchState, fetchPeqState]);
|
||||
|
||||
return (
|
||||
<DeviceContext.Provider value={{
|
||||
|
||||
Reference in New Issue
Block a user