refactor: wrap tunnel connect/disconnect operations in spawn_blocking and add pending state UI feedback
Move tunnel_manager::connect and disconnect calls into spawn_blocking tasks to prevent blocking async runtime. Clone app handle and profile path before spawning. Add map_err for task join failures. Add tunnelActionPending state to track in-progress tunnel operations. Pass busy prop to AppHeader and disable sync/logout/connect buttons during tunnel actions. Update connect button text to show "
This commit is contained in:
@@ -393,20 +393,31 @@ async fn select_access_profile(app: AppHandle, profile_id: String) -> Result<Enr
|
||||
#[tauri::command]
|
||||
async fn connect_tunnel(app: AppHandle) -> Result<EnrollmentResult, String> {
|
||||
let session_state = sync_current_session(&app).await?;
|
||||
let result = tunnel_manager::connect(&app, std::path::Path::new(&session_state.profile_path));
|
||||
let app_handle = app.clone();
|
||||
let profile_path = session_state.profile_path.clone();
|
||||
let result = tauri::async_runtime::spawn_blocking(move || {
|
||||
tunnel_manager::connect(&app_handle, std::path::Path::new(&profile_path))
|
||||
})
|
||||
.await
|
||||
.map_err(|err| format!("Unable to join tunnel connect task: {err}"))?;
|
||||
refresh_tray_menu(&app);
|
||||
result?;
|
||||
Ok(session_state.enrollment)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn disconnect_tunnel(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> {
|
||||
async fn disconnect_tunnel(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> {
|
||||
let profile_path = {
|
||||
let session = state.session.lock().map_err(|_| "Unable to read client state".to_string())?;
|
||||
let session = session.as_ref().ok_or_else(|| "No active session is available".to_string())?;
|
||||
session.profile_path.clone()
|
||||
};
|
||||
let result = tunnel_manager::disconnect(&app, std::path::Path::new(&profile_path));
|
||||
let app_handle = app.clone();
|
||||
let result = tauri::async_runtime::spawn_blocking(move || {
|
||||
tunnel_manager::disconnect(&app_handle, std::path::Path::new(&profile_path))
|
||||
})
|
||||
.await
|
||||
.map_err(|err| format!("Unable to join tunnel disconnect task: {err}"))?;
|
||||
refresh_tray_menu(&app);
|
||||
result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user