From 19b3899a1b60523e1ea55bd3072cbdaf9ba5905b Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Fri, 7 Nov 2025 00:47:14 +0800 Subject: [PATCH] refactor: improve notification handling and avoid backend loop with empty messages --- src-tauri/src/core/notification.rs | 15 +++++++++------ src-tauri/src/utils/resolve/scheme.rs | 15 ++++++--------- src/pages/_layout/notificationHandlers.ts | 5 ++++- src/pages/profiles.tsx | 8 ++++---- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src-tauri/src/core/notification.rs b/src-tauri/src/core/notification.rs index 2d5d2003..eaee5932 100644 --- a/src-tauri/src/core/notification.rs +++ b/src-tauri/src/core/notification.rs @@ -12,7 +12,7 @@ use std::{ mpsc, }, thread, - time::Instant, + time::{Duration, Instant}, }; use tauri::{Emitter, WebviewWindow}; @@ -92,12 +92,15 @@ impl NotificationSystem { } fn worker_loop(rx: mpsc::Receiver) { - let handle = Handle::global(); - while !handle.is_exiting() { - match rx.try_recv() { + loop { + let handle = Handle::global(); + if handle.is_exiting() { + break; + } + match rx.recv_timeout(Duration::from_millis(1_000)) { Ok(event) => Self::process_event(handle, event), - Err(mpsc::TryRecvError::Disconnected) => break, - Err(mpsc::TryRecvError::Empty) => break, + Err(mpsc::RecvTimeoutError::Timeout) => (), + Err(mpsc::RecvTimeoutError::Disconnected) => break, } } } diff --git a/src-tauri/src/utils/resolve/scheme.rs b/src-tauri/src/utils/resolve/scheme.rs index fc6c63d9..e36125c2 100644 --- a/src-tauri/src/utils/resolve/scheme.rs +++ b/src-tauri/src/utils/resolve/scheme.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use anyhow::{Result, bail}; use percent_encoding::percent_decode_str; use smartstring::alias::String; @@ -73,24 +75,23 @@ pub(super) async fn resolve_scheme(param: &str) -> Result<()> { "failed to parse profile from url: {:?}", e ); - // TODO 通知系统疑似损坏,前端无法显示通知事件 handle::Handle::notice_message("import_sub_url::error", e.to_string()); return Ok(()); } }; let uid = item.uid.clone().unwrap_or_default(); - // TODO 通过 deep link 导入后需要正确调用前端刷新订阅页面,以及通知结果 match profiles::profiles_append_item_safe(&mut item).await { Ok(_) => { Config::profiles().await.apply(); let _ = Config::profiles().await.data_arc().save_file().await; - // TODO 通知系统疑似损坏,前端无法显示通知事件 handle::Handle::notice_message( "import_sub_url::ok", - item.uid.clone().unwrap_or_default(), + "", // 空 msg 传入,我们不希望导致 后端-前端-后端 死循环,这里只做提醒。 ); - // TODO fuck me this shit is fucking broken as fucked + handle::Handle::refresh_verge(); + handle::Handle::notify_profile_changed(uid.clone()); + tokio::time::sleep(Duration::from_millis(100)).await; handle::Handle::notify_profile_changed(uid); } Err(e) => { @@ -101,14 +102,10 @@ pub(super) async fn resolve_scheme(param: &str) -> Result<()> { e ); Config::profiles().await.discard(); - // TODO 通知系统疑似损坏,前端无法显示通知事件 handle::Handle::notice_message("import_sub_url::error", e.to_string()); return Ok(()); } } - handle::Handle::refresh_verge(); - handle::Handle::refresh_clash(); - Ok(()) } diff --git a/src/pages/_layout/notificationHandlers.ts b/src/pages/_layout/notificationHandlers.ts index afa674c8..8d5bb544 100644 --- a/src/pages/_layout/notificationHandlers.ts +++ b/src/pages/_layout/notificationHandlers.ts @@ -11,7 +11,10 @@ export const handleNoticeMessage = ( ) => { const handlers: Record void> = { "import_sub_url::ok": () => { - navigate("/profile", { state: { current: msg } }); + // 空 msg 传入,我们不希望导致 后端-前端-后端 死循环,这里只做提醒。 + // 未来细分事件通知时,可以考虑传入订阅 ID 或其他标识符 + // navigate("/profile", { state: { current: msg } }); + navigate("/profile"); showNotice("success", t("Import Subscription Successful")); }, "import_sub_url::error": () => { diff --git a/src/pages/profiles.tsx b/src/pages/profiles.tsx index 0643c3d0..fa3bbff0 100644 --- a/src/pages/profiles.tsx +++ b/src/pages/profiles.tsx @@ -13,15 +13,15 @@ import { sortableKeyboardCoordinates, } from "@dnd-kit/sortable"; import { + CheckBoxOutlineBlankRounded, + CheckBoxRounded, ClearRounded, ContentPasteRounded, + DeleteRounded, + IndeterminateCheckBoxRounded, LocalFireDepartmentRounded, RefreshRounded, TextSnippetOutlined, - CheckBoxOutlineBlankRounded, - CheckBoxRounded, - IndeterminateCheckBoxRounded, - DeleteRounded, } from "@mui/icons-material"; import { LoadingButton } from "@mui/lab"; import { Box, Button, Divider, Grid, IconButton, Stack } from "@mui/material";