From 76943f33624fe75bacf4029d997e9fda754e33ff Mon Sep 17 00:00:00 2001 From: Wind-Explorer Date: Sun, 25 Jan 2026 23:10:09 +0800 Subject: [PATCH] refactoring of app.rs and relevant flow files --- src-tauri/src/commands/auth.rs | 2 +- src-tauri/src/lib.rs | 5 ++-- src-tauri/src/lifecycle.rs | 42 ++++++++++++++++++++++++++++ src-tauri/src/{app.rs => startup.rs} | 25 ++--------------- 4 files changed, 49 insertions(+), 25 deletions(-) create mode 100644 src-tauri/src/lifecycle.rs rename src-tauri/src/{app.rs => startup.rs} (81%) diff --git a/src-tauri/src/commands/auth.rs b/src-tauri/src/commands/auth.rs index 04e702a..808b626 100644 --- a/src-tauri/src/commands/auth.rs +++ b/src-tauri/src/commands/auth.rs @@ -18,7 +18,7 @@ pub fn start_auth_flow() -> Result<(), String> { // Close welcome window if it's still open crate::services::welcome::close_welcome_window(); tauri::async_runtime::spawn(async { - crate::app::bootstrap().await; + crate::startup::bootstrap().await; }); }) .map_err(|e| e.to_string()) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2b5f8ec..252144a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -22,11 +22,12 @@ use tracing_subscriber::{self, util::SubscriberInitExt}; static APP_HANDLE: std::sync::OnceLock> = std::sync::OnceLock::new(); -mod app; mod commands; +mod lifecycle; mod models; mod remotes; mod services; +mod startup; mod state; mod system_tray; mod utilities; @@ -86,7 +87,7 @@ fn setup_fdoll() -> Result<(), tauri::Error> { open_splash_window(); state::init_fdoll_state(Some(_guard)); - async_runtime::spawn(async move { app::start_fdoll().await }); + async_runtime::spawn(async move { lifecycle::start_fdoll().await }); Ok(()) } diff --git a/src-tauri/src/lifecycle.rs b/src-tauri/src/lifecycle.rs new file mode 100644 index 0000000..61d7ff7 --- /dev/null +++ b/src-tauri/src/lifecycle.rs @@ -0,0 +1,42 @@ +use crate::{ + lock_w, + services::{ + active_app::init_active_app_changes_listener, + health_manager::show_health_manager_with_error, + }, + startup::init_startup_sequence, + state::FDOLL, + system_tray::init_system_tray, +}; + +/// Initializes and starts the core app lifecycle after initial setup. +/// +/// This function handles: +/// - System tray initialization and storage in app state +/// - Active app change listener setup +/// - Startup sequence execution with error handling +/// +/// # Errors +/// If the startup sequence fails, displays a health manager dialog +/// with the error details. +/// +/// # Example +/// ``` +/// // Called automatically during app setup in setup_fdoll() +/// lifecycle::start_fdoll().await; +/// ``` +pub async fn start_fdoll() { + let tray = init_system_tray(); + { + let mut guard = lock_w!(FDOLL); + guard.tray = Some(tray); + } + + // Begin listening for foreground app changes + init_active_app_changes_listener(); + + if let Err(err) = init_startup_sequence().await { + tracing::warn!("Startup sequence encountered an error: {}", err); + show_health_manager_with_error(Some(err.to_string())); + } +} diff --git a/src-tauri/src/app.rs b/src-tauri/src/startup.rs similarity index 81% rename from src-tauri/src/app.rs rename to src-tauri/src/startup.rs index ae0137a..770e13f 100644 --- a/src-tauri/src/app.rs +++ b/src-tauri/src/startup.rs @@ -4,37 +4,18 @@ use tokio::time::{sleep, Instant}; use tracing::{info, warn}; use crate::{ - lock_w, models::health::HealthError, remotes::health::HealthRemote, services::{ - active_app::init_active_app_changes_listener, auth::{get_access_token, get_tokens}, - health_manager::show_health_manager_with_error, scene::{close_splash_window, open_scene_window, open_splash_window}, welcome::open_welcome_window, ws::init_ws_client, }, - state::{init_app_data, FDOLL}, - system_tray::{init_system_tray, update_system_tray}, + state::init_app_data, + system_tray::update_system_tray, }; -pub async fn start_fdoll() { - let tray = init_system_tray(); - { - let mut guard = lock_w!(FDOLL); - guard.tray = Some(tray); - } - - // Begin listening for foreground app changes - init_active_app_changes_listener(); - - if let Err(err) = init_startup_sequence().await { - tracing::error!("startup sequence failed: {err}"); - show_health_manager_with_error(Some(err.to_string())); - } -} - async fn init_ws_after_auth() { const MAX_ATTEMPTS: u8 = 5; const BACKOFF: Duration = Duration::from_millis(300); @@ -90,7 +71,7 @@ pub async fn bootstrap() { /// Perform checks for environment, network condition /// and handle situations where startup would not be appropriate. -async fn init_startup_sequence() -> Result<(), HealthError> { +pub async fn init_startup_sequence() -> Result<(), HealthError> { let health_remote = HealthRemote::try_new()?; // simple retry loop to smooth transient network issues