refactor: enhance async initialization and streamline setup process (#4560)

* feat: Implement DNS management for macOS

- Added `set_public_dns` and `restore_public_dns` functions in `dns.rs` to manage system DNS settings.
- Introduced `resolve` module to encapsulate DNS and scheme resolution functionalities.
- Implemented `resolve_scheme` function in `scheme.rs` to handle deep links and profile imports.
- Created UI readiness management in `ui.rs` to track and update UI loading states.
- Developed window management logic in `window.rs` to handle window creation and visibility.
- Added initial loading overlay script in `window_script.rs` for better user experience during startup.
- Updated server handling in `server.rs` to integrate new resolve functionalities.
- Refactored window creation calls in `window_manager.rs` to use the new window management logic.

* refactor: streamline asynchronous handling in config and resolve setup

* Revert "refactor: streamline asynchronous handling in config and resolve setup"

This reverts commit 23d7dc86d5.

* fix: optimize asynchronous memory handling

* fix: enhance task logging by adding size check for special cases

* refactor: enhance async initialization and streamline setup process

* refactor: optimize async setup by consolidating initialization tasks

* chore: update changelog for Mihomo(Meta) kernel upgrade to v1.19.13

* fix: improve startup phase initialization performance

* refactor: optimize file read/write performance to reduce application wait time

* refactor: simplify app instance exit logic and adjust system proxy guard initialization

* refactor: change resolve_setup_async to synchronous execution for improved performance

* refactor: update resolve_setup_async to accept AppHandle for improved initialization flow

* refactor: remove unnecessary initialization of portable flag in run function

* refactor: consolidate async initialization tasks into a single blocking call for improved execution flow

* refactor: optimize resolve_setup_async by restructuring async tasks for improved concurrency

* refactor: streamline resolve_setup_async and embed_server for improved async handling

* refactor: separate synchronous and asynchronous setup functions for improved clarity

* refactor: simplify async notification handling and remove redundant network manager initialization

* refactor: enhance async handling in proxy request cache and window creation logic

* refactor: improve code formatting and readability in ProxyRequestCache

* refactor: adjust singleton check timeout and optimize trace size conditions

* refactor: update TRACE_SPECIAL_SIZE to include additional size condition

* refactor: update kode-bridge dependency to version 0.2.1-rc2

* refactor: replace RwLock with AtomicBool for UI readiness and implement event-driven monitoring

* refactor: convert async functions to synchronous for window management

* Update src-tauri/src/utils/resolve/window.rs

* fix: handle missing app_handle in create_window function

* Update src-tauri/src/module/lightweight.rs
This commit is contained in:
Tunglies
2025-08-29 23:51:09 +08:00
committed by Tunglies
parent 2080dbdc0f
commit c6a6ea48dd
22 changed files with 956 additions and 879 deletions

View File

@@ -1,3 +1,6 @@
#![allow(non_snake_case)]
#![recursion_limit = "512"]
mod cmd;
pub mod config;
mod core;
@@ -11,9 +14,13 @@ mod utils;
#[cfg(target_os = "macos")]
use crate::utils::window_manager::WindowManager;
use crate::{
core::handle,
core::hotkey,
process::AsyncHandler,
utils::{resolve, resolve::resolve_scheme, server},
utils::{
resolve::{self, scheme::resolve_scheme},
server,
},
};
use config::Config;
use parking_lot::Mutex;
@@ -28,15 +35,13 @@ use utils::logging::Type;
/// Application initialization helper functions
mod app_init {
use crate::core::handle;
use super::*;
/// Initialize singleton monitoring for other instances
pub fn init_singleton_check() {
AsyncHandler::spawn(move || async move {
logging!(info, Type::Setup, true, "开始检查单例实例...");
match timeout(Duration::from_secs(3), server::check_singleton()).await {
match timeout(Duration::from_millis(500), server::check_singleton()).await {
Ok(result) => {
if result.is_err() {
logging!(info, Type::Setup, true, "检测到已有应用实例运行");
@@ -133,47 +138,6 @@ mod app_init {
Ok(())
}
/// Initialize core components asynchronously
pub fn init_core_async(app_handle: &AppHandle) {
let app_handle = app_handle.clone();
AsyncHandler::spawn(move || async move {
logging!(info, Type::Setup, true, "异步执行应用设置...");
match timeout(
Duration::from_secs(30),
resolve::resolve_setup_async(&app_handle),
)
.await
{
Ok(_) => {
logging!(info, Type::Setup, true, "应用设置成功完成");
}
Err(_) => {
logging!(
error,
Type::Setup,
true,
"应用设置超时(30秒),继续执行后续流程"
);
}
}
});
}
/// Initialize core components synchronously
pub async fn init_core_sync(app_handle: &AppHandle) -> Result<(), Box<dyn std::error::Error>> {
logging!(info, Type::Setup, true, "初始化核心句柄...");
core::handle::Handle::global().init(app_handle.clone());
logging!(info, Type::Setup, true, "初始化配置...");
utils::init::init_config().await?;
logging!(info, Type::Setup, true, "初始化资源...");
utils::init::init_resources().await?;
logging!(info, Type::Setup, true, "核心组件初始化完成");
Ok(())
}
/// Generate all command handlers for the application
pub fn generate_handlers(
) -> impl Fn(tauri::ipc::Invoke<tauri::Wry>) -> bool + Send + Sync + 'static {
@@ -308,11 +272,8 @@ pub fn run() {
// Setup singleton check
app_init::init_singleton_check();
// Initialize network manager
utils::network::NetworkManager::new().init();
// Initialize portable flag
let _ = utils::dirs::init_portable_flag();
// We don't need init_portable_flag here anymore due to the init_config will do the things
// let _ = utils::dirs::init_portable_flag();
// Set Linux environment variable
#[cfg(target_os = "linux")]
@@ -371,23 +332,11 @@ pub fn run() {
let app_handle = app.handle().clone();
// Initialize core components asynchronously
app_init::init_core_async(&app_handle);
logging!(info, Type::Setup, true, "执行主要设置操作...");
// Initialize core components synchronously
AsyncHandler::spawn(move || async move {
if let Err(e) = app_init::init_core_sync(&app_handle).await {
logging!(
error,
Type::Setup,
true,
"Failed to initialize core components: {}",
e
);
}
});
logging!(info, Type::Setup, true, "异步执行应用设置...");
resolve::resolve_setup_sync(app_handle);
resolve::resolve_setup_async();
logging!(info, Type::Setup, true, "初始化完成,继续执行");
Ok(())