refactor: 优化侧边栏进程清理时机与错误提示
- 仅在应用退出事件时清理 Sidecar 进程,避免重复执行。 - 在开发模式下优化窗口创建错误提示并增强可读性,同时在生产模式下才执行 Sidecar 进程清理逻辑。
This commit is contained in:
@@ -56,17 +56,22 @@ pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
||||
// 开发模式:直接创建窗口连接到 Vite 开发服务器
|
||||
println!("🔧 开发模式:连接到 Vite 开发服务器 (localhost:3000)");
|
||||
|
||||
let url = "http://localhost:3000";
|
||||
tauri::WebviewWindowBuilder::new(
|
||||
match tauri::WebviewWindowBuilder::new(
|
||||
&app_handle,
|
||||
"main",
|
||||
tauri::WebviewUrl::External(url.parse().unwrap()),
|
||||
tauri::WebviewUrl::External("http://localhost:3000".parse().unwrap()),
|
||||
)
|
||||
.title(WINDOW_TITLE)
|
||||
.inner_size(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)
|
||||
.center()
|
||||
.build()
|
||||
.expect("创建窗口失败");
|
||||
{
|
||||
Ok(_) => println!("✓ 开发窗口创建成功"),
|
||||
Err(e) => {
|
||||
eprintln!("✗ 窗口创建失败: {}", e);
|
||||
eprintln!("提示: 请确保 Vite 开发服务器在 localhost:3000 运行");
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -127,10 +132,7 @@ pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
||||
|
||||
// 超时检查
|
||||
if start_time.elapsed() > timeout {
|
||||
eprintln!(
|
||||
"✗ 启动超时: App 未能在 {} 秒内启动",
|
||||
STARTUP_TIMEOUT_SECS
|
||||
);
|
||||
eprintln!("✗ 启动超时: App 未能在 {} 秒内启动", STARTUP_TIMEOUT_SECS);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -144,6 +146,12 @@ pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
||||
|
||||
/// 清理 Sidecar 进程 (在应用退出时调用)
|
||||
pub fn cleanup_sidecar_process(app_handle: &tauri::AppHandle) {
|
||||
// 只在生产模式下清理 sidecar 进程
|
||||
let is_dev = cfg!(debug_assertions);
|
||||
if is_dev {
|
||||
return;
|
||||
}
|
||||
|
||||
println!("应用退出,正在清理 Sidecar 进程...");
|
||||
if let Some(state) = app_handle.try_state::<SidecarProcess>() {
|
||||
if let Ok(mut process) = state.0.lock() {
|
||||
|
||||
Reference in New Issue
Block a user