fix: defer visualizer, remote control, and last.fm to post-login

Visualizer FFT thread, remote control API server, and Last.fm session
restore were all initializing before user session activation, causing
errors ("No active session") and wasting resources.

Move all three to run after activate_user_session:
- Visualizer: add started guard to prevent double-start
- Remote control: sync_server now has per-user settings available
- Last.fm: restoreLastfmSession moved from bootstrapApp to handleLoginSuccess
This commit is contained in:
vicrodh committed 2026-02-06 09:27:09 -06:00
1 parent ff8c69f573
commit aef4285fd9
5 files changed
+32 -17

No files matched your search

+15 -1
View File
@@ -6,7 +6,7 @@
use std::path::Path;
use std::sync::{Arc, Mutex};
use tauri::State;
use tauri::{Manager, State};
use crate::api_cache::ApiCacheState;
use crate::artist_blacklist::BlacklistState;
@@ -69,6 +69,7 @@ pub fn get_last_user_id() -> Option<u64> {
/// and cache stores at `~/.cache/qbz/users/{user_id}/`.
#[tauri::command]
pub async fn activate_user_session(
app: tauri::AppHandle,
user_id: u64,
user_paths: State<'_, UserDataPaths>,
session_store: State<'_, SessionStoreState>,
@@ -180,6 +181,19 @@ pub async fn activate_user_session(
log::warn!("Failed to save last_user_id: {}", e);
}
// Start visualizer FFT thread (idempotent — only starts once even if called twice)
app.state::<crate::AppState>()
.visualizer
.start(app.clone());
// Start remote control API server if enabled in user settings
let app_clone = app.clone();
tauri::async_runtime::spawn(async move {
if let Err(e) = crate::api_server::sync_server(&app_clone).await {
log::error!("Remote control API init failed: {}", e);
}
});
log::info!("User session activated for user_id={}", user_id);
Ok(())
}
+3 -12
View File
@@ -263,18 +263,9 @@ pub fn run() {
.media_controls
.init(app.handle().clone());
// Start visualizer FFT thread (disabled by default, enabled via command)
app.state::<AppState>()
.visualizer
.start(app.handle().clone());
// Start remote control API server if enabled
let app_handle = app.handle().clone();
tauri::async_runtime::spawn(async move {
if let Err(e) = api_server::sync_server(&app_handle).await {
log::error!("Remote control API init failed: {}", e);
}
});
// NOTE: Visualizer FFT thread and Remote Control API server are started
// in activate_user_session (post-login), not here. They need per-user
// state to be initialized first.
// NOTE: Subscription purge check moved to activate_user_session
// (runs after login when per-user state is available)
+8 -1
View File
@@ -73,6 +73,8 @@ impl Default for VisualizerTap {
pub struct Visualizer {
/// Shared tap state (given to Player for sample capture)
tap: VisualizerTap,
/// Whether the FFT thread has been started (prevents double-start)
started: AtomicBool,
}
impl Visualizer {
@@ -80,6 +82,7 @@ impl Visualizer {
pub fn new() -> Self {
Self {
tap: VisualizerTap::new(),
started: AtomicBool::new(false),
}
}
@@ -88,8 +91,12 @@ impl Visualizer {
self.tap.clone()
}
/// Start the FFT processing thread
/// Start the FFT processing thread (idempotent — only starts once)
pub fn start(&self, app_handle: AppHandle) {
if self.started.swap(true, Ordering::SeqCst) {
log::debug!("Visualizer FFT thread already started, skipping");
return;
}
let state = VisualizerState {
ring_buffer: self.tap.ring_buffer.clone(),
enabled: self.tap.enabled.clone(),