diff --git a/src-tauri/src/commands/app.rs b/src-tauri/src/commands/app.rs index 1897f29..c4011e4 100644 --- a/src-tauri/src/commands/app.rs +++ b/src-tauri/src/commands/app.rs @@ -1,6 +1,7 @@ use crate::get_app_handle; -use crate::init::lifecycle::{construct_user_session, validate_server_health}; +use crate::init::lifecycle::validate_server_health; use crate::services::auth::get_session_token; +use crate::services::session::construct_user_session; use tracing::info; #[tauri::command] diff --git a/src-tauri/src/init/lifecycle.rs b/src-tauri/src/init/lifecycle.rs index 4200708..c2060b6 100644 --- a/src-tauri/src/init/lifecycle.rs +++ b/src-tauri/src/init/lifecycle.rs @@ -3,58 +3,7 @@ use std::time::Duration; use tokio::time::sleep; use tracing::warn; -use crate::{ - models::health::HealthError, - remotes::health::HealthRemote, - services::{ - app_data::{clear_app_data, init_app_data_scoped, AppDataRefreshScope}, - health_manager::open_health_manager_window, - health_monitor::{start_health_monitor, stop_health_monitor}, - scene::open_scene_window, - session_windows::close_all_windows, - ws::client::{clear_ws_client, establish_websocket_connection}, - }, - state::auth::{start_background_token_refresh, stop_background_token_refresh}, - system_tray::update_system_tray, -}; - -/// Connects the user profile and opens the scene window. -pub async fn construct_user_session() { - connect_user_profile().await; - close_all_windows(); - open_scene_window(); - update_system_tray(true); -} - -/// Disconnects the user profile and closes the scene window. -pub async fn destruct_user_session() { - disconnect_user_profile().await; - close_all_windows(); - update_system_tray(false); -} - -/// Initializes the user profile and establishes a WebSocket connection. -async fn connect_user_profile() { - init_app_data_scoped(AppDataRefreshScope::All).await; - establish_websocket_connection().await; - start_background_token_refresh().await; - start_health_monitor().await; -} - -/// Clears the user profile and WebSocket connection. -async fn disconnect_user_profile() { - stop_health_monitor(); - stop_background_token_refresh(); - clear_app_data(); - clear_ws_client().await; -} - -/// Destructs the user session and show health manager window -/// with error message, offering troubleshooting options. -pub async fn handle_disastrous_failure(error_message: Option) { - destruct_user_session().await; - open_health_manager_window(error_message); -} +use crate::{models::health::HealthError, remotes::health::HealthRemote}; /// Pings the server's health endpoint a maximum of /// three times with a backoff of 500ms between diff --git a/src-tauri/src/init/mod.rs b/src-tauri/src/init/mod.rs index 8194055..30ee1c0 100644 --- a/src-tauri/src/init/mod.rs +++ b/src-tauri/src/init/mod.rs @@ -1,6 +1,6 @@ use crate::{ init::{ - lifecycle::{construct_user_session, handle_disastrous_failure, validate_server_health}, + lifecycle::validate_server_health, tracing::init_logging, }, services::{ @@ -8,6 +8,7 @@ use crate::{ cursor::init_cursor_tracking, presence_modules::init_modules, scene::{close_splash_window, open_splash_window}, + session::{construct_user_session, handle_disastrous_failure}, welcome::open_welcome_window, }, state::init_app_state, diff --git a/src-tauri/src/services/auth/session.rs b/src-tauri/src/services/auth/session.rs index 6d29554..5925ea2 100644 --- a/src-tauri/src/services/auth/session.rs +++ b/src-tauri/src/services/auth/session.rs @@ -1,9 +1,7 @@ use tracing::info; use crate::get_app_handle; -use crate::init::lifecycle::construct_user_session; -use crate::services::scene::close_splash_window; -use crate::services::welcome::close_welcome_window; +use crate::services::{scene::close_splash_window, session::construct_user_session, welcome::close_welcome_window}; use crate::state::auth::get_auth_pass_with_refresh; use crate::{lock_w, state::FDOLL}; diff --git a/src-tauri/src/services/health_monitor.rs b/src-tauri/src/services/health_monitor.rs index 321224d..21fd961 100644 --- a/src-tauri/src/services/health_monitor.rs +++ b/src-tauri/src/services/health_monitor.rs @@ -1,6 +1,7 @@ use crate::{ - init::lifecycle::{handle_disastrous_failure, validate_server_health}, + init::lifecycle::validate_server_health, lock_w, + services::session::handle_disastrous_failure, services::ws::client::establish_websocket_connection, state::FDOLL, }; diff --git a/src-tauri/src/services/mod.rs b/src-tauri/src/services/mod.rs index ce95651..5693e47 100644 --- a/src-tauri/src/services/mod.rs +++ b/src-tauri/src/services/mod.rs @@ -12,6 +12,7 @@ pub mod interaction; pub mod petpet; pub mod presence_modules; pub mod scene; +pub mod session; pub mod session_windows; pub mod sprite; pub mod sprite_recolor; diff --git a/src-tauri/src/services/session.rs b/src-tauri/src/services/session.rs new file mode 100644 index 0000000..b4468c3 --- /dev/null +++ b/src-tauri/src/services/session.rs @@ -0,0 +1,44 @@ +use crate::{ + services::{ + app_data::{clear_app_data, init_app_data_scoped, AppDataRefreshScope}, + health_manager::open_health_manager_window, + health_monitor::{start_health_monitor, stop_health_monitor}, + scene::open_scene_window, + session_windows::close_all_windows, + ws::client::{clear_ws_client, establish_websocket_connection}, + }, + state::auth::{start_background_token_refresh, stop_background_token_refresh}, + system_tray::update_system_tray, +}; + +pub async fn construct_user_session() { + connect_user_profile().await; + close_all_windows(); + open_scene_window(); + update_system_tray(true); +} + +pub async fn destruct_user_session() { + disconnect_user_profile().await; + close_all_windows(); + update_system_tray(false); +} + +pub async fn handle_disastrous_failure(error_message: Option) { + destruct_user_session().await; + open_health_manager_window(error_message); +} + +async fn connect_user_profile() { + init_app_data_scoped(AppDataRefreshScope::All).await; + establish_websocket_connection().await; + start_background_token_refresh().await; + start_health_monitor().await; +} + +async fn disconnect_user_profile() { + stop_health_monitor(); + stop_background_token_refresh(); + clear_app_data(); + clear_ws_client().await; +} diff --git a/src-tauri/src/services/ws/client.rs b/src-tauri/src/services/ws/client.rs index 6a8c911..83cb696 100644 --- a/src-tauri/src/services/ws/client.rs +++ b/src-tauri/src/services/ws/client.rs @@ -23,7 +23,7 @@ pub async fn establish_websocket_connection() { return; // Success } else { // Connection failed, trigger disaster recovery - crate::init::lifecycle::handle_disastrous_failure( + crate::services::session::handle_disastrous_failure( Some("WebSocket connection failed. Please check your network and try again.".to_string()) ).await; return; @@ -34,7 +34,7 @@ pub async fn establish_websocket_connection() { } // If we exhausted retries without valid token - crate::init::lifecycle::handle_disastrous_failure( + crate::services::session::handle_disastrous_failure( Some("Failed to authenticate. Please restart and sign in again.".to_string()) ).await; } diff --git a/src-tauri/src/services/ws/connection.rs b/src-tauri/src/services/ws/connection.rs index 9dc10cf..c1cf4ef 100644 --- a/src-tauri/src/services/ws/connection.rs +++ b/src-tauri/src/services/ws/connection.rs @@ -2,7 +2,9 @@ use rust_socketio::{Payload, RawClient}; use tracing::info; use crate::{ - init::lifecycle::construct_user_session, lock_w, services::health_manager::close_health_manager_window, state::FDOLL + lock_w, + services::{health_manager::close_health_manager_window, session::construct_user_session}, + state::FDOLL, }; use super::{types::WS_EVENT, utils}; diff --git a/src-tauri/src/services/ws/emitter.rs b/src-tauri/src/services/ws/emitter.rs index 10423e1..8b7d01c 100644 --- a/src-tauri/src/services/ws/emitter.rs +++ b/src-tauri/src/services/ws/emitter.rs @@ -5,7 +5,7 @@ use tauri_specta::Event; use tracing::{error, warn}; use crate::{ - get_app_handle, init::lifecycle::handle_disastrous_failure, lock_r, lock_w, state::FDOLL, + get_app_handle, lock_r, lock_w, services::session::handle_disastrous_failure, state::FDOLL, }; /// Acquire WebSocket client and initialization state from app state diff --git a/src-tauri/src/state/auth.rs b/src-tauri/src/state/auth.rs index e2746fd..0f884e0 100644 --- a/src-tauri/src/state/auth.rs +++ b/src-tauri/src/state/auth.rs @@ -1,6 +1,5 @@ -use crate::init::lifecycle::destruct_user_session; use crate::services::auth::{clear_auth_pass, load_auth_pass, refresh_token, AuthPass}; -use crate::services::welcome::open_welcome_window; +use crate::services::{session::destruct_user_session, welcome::open_welcome_window}; use crate::{lock_r, lock_w, state::FDOLL}; use std::time::{SystemTime, UNIX_EPOCH}; use tokio::sync::Mutex;