lifecycle refactor into session service

This commit is contained in:
2026-03-11 16:47:40 +08:00
parent da93c2e4a4
commit 2f0c967bc0
11 changed files with 60 additions and 64 deletions

View File

@@ -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]

View File

@@ -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<String>) {
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

View File

@@ -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,

View File

@@ -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};

View File

@@ -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,
};

View File

@@ -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;

View File

@@ -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<String>) {
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;
}

View File

@@ -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;
}

View File

@@ -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};

View File

@@ -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

View File

@@ -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;