Replace 1s polling with event-driven updates on sample rate change

Changed hardware status polling from continuous (every 1s) to event-driven
(only when sample rate changes).

Rationale:
- Hardware sample rate ONLY changes when playing new song with different rate
- Continuous polling was overkill - 3600 unnecessary calls per hour
- If system is properly configured, status doesn't change mid-song
- Polling was originally added for "instant feedback" but wastes resources

Changes:
- Removed setInterval polling (lines 162-171)
- Added $effect that watches samplingRate prop changes
- Only calls get_hardware_audio_status when:
  * samplingRate changes (new song or format change)
  * Using DAC Passthrough or ALSA Direct (bit-perfect modes)
- Tracks previousSamplingRate to detect actual changes

Benefits:
- Zero backend calls during playback (unless rate changes)
- Still validates bit-perfect when it matters (new sample rate)
- Massive reduction in /proc/asound reads
- Better battery life, lower CPU usage
- Same UX - badge updates when song changes

Testing plan:
- Play songs with same sample rate → zero backend calls
- Play songs with different rates → update on each rate change
- Verify badge still shows correct status
- If issues found, can revert to 1s polling
This commit is contained in:
vicrodh committed 2026-01-19 12:13:14 -06:00
1 parent 19a571dddc
commit 9242b95939
1 file changed
+16 -16
+16 -16
View File
@@ -44,6 +44,7 @@
let pipewireSinks = $state<PipewireSink[]>([]); let pipewireSinks = $state<PipewireSink[]>([]);
let hardwareStatus = $state<HardwareAudioStatus | null>(null); let hardwareStatus = $state<HardwareAudioStatus | null>(null);
let isHovering = $state(false); let isHovering = $state(false);
let previousSamplingRate = $state<number | undefined>(undefined);
// Derived state // Derived state
const currentDevice = $derived(outputStatus?.device_name ?? null); const currentDevice = $derived(outputStatus?.device_name ?? null);
@@ -147,24 +148,23 @@
} }
} }
// Update hardware status when sampling rate changes (new song with different rate)
$effect(() => {
if (samplingRate !== undefined && samplingRate !== previousSamplingRate) {
// Sampling rate changed - new song or format change
previousSamplingRate = samplingRate;
// Update hardware status to verify bit-perfect playback
if (settings?.dac_passthrough || settings?.backend_type === 'Alsa') {
invoke<HardwareAudioStatus>('get_hardware_audio_status')
.then(status => hardwareStatus = status)
.catch(() => hardwareStatus = null);
}
}
});
onMount(() => { onMount(() => {
loadStatus(); loadStatus();
// Auto-refresh hardware status when DAC passthrough is active
// Fast polling (1s) for instant badge updates without needing hover
const pollInterval = setInterval(async () => {
// Only poll if DAC passthrough is enabled (otherwise wastes resources)
if (settings?.dac_passthrough) {
try {
hardwareStatus = await invoke<HardwareAudioStatus>('get_hardware_audio_status').catch(() => null);
} catch (err) {
// Silently fail - don't spam console
}
}
}, 1000); // 1 second - fast enough for instant feedback, light enough not to impact performance
// Cleanup on unmount
return () => clearInterval(pollInterval);
}); });
</script> </script>