refactor: improve notification handling and avoid backend loop with empty messages

This commit is contained in:
Tunglies
2025-11-07 00:47:14 +08:00
Unverified
parent 333440535b
commit 19b3899a1b
4 changed files with 23 additions and 20 deletions

View File

@@ -12,7 +12,7 @@ use std::{
mpsc, mpsc,
}, },
thread, thread,
time::Instant, time::{Duration, Instant},
}; };
use tauri::{Emitter, WebviewWindow}; use tauri::{Emitter, WebviewWindow};
@@ -92,12 +92,15 @@ impl NotificationSystem {
} }
fn worker_loop(rx: mpsc::Receiver<FrontendEvent>) { fn worker_loop(rx: mpsc::Receiver<FrontendEvent>) {
let handle = Handle::global(); loop {
while !handle.is_exiting() { let handle = Handle::global();
match rx.try_recv() { if handle.is_exiting() {
break;
}
match rx.recv_timeout(Duration::from_millis(1_000)) {
Ok(event) => Self::process_event(handle, event), Ok(event) => Self::process_event(handle, event),
Err(mpsc::TryRecvError::Disconnected) => break, Err(mpsc::RecvTimeoutError::Timeout) => (),
Err(mpsc::TryRecvError::Empty) => break, Err(mpsc::RecvTimeoutError::Disconnected) => break,
} }
} }
} }

View File

@@ -1,3 +1,5 @@
use std::time::Duration;
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use percent_encoding::percent_decode_str; use percent_encoding::percent_decode_str;
use smartstring::alias::String; use smartstring::alias::String;
@@ -73,24 +75,23 @@ pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
"failed to parse profile from url: {:?}", "failed to parse profile from url: {:?}",
e e
); );
// TODO 通知系统疑似损坏,前端无法显示通知事件
handle::Handle::notice_message("import_sub_url::error", e.to_string()); handle::Handle::notice_message("import_sub_url::error", e.to_string());
return Ok(()); return Ok(());
} }
}; };
let uid = item.uid.clone().unwrap_or_default(); let uid = item.uid.clone().unwrap_or_default();
// TODO 通过 deep link 导入后需要正确调用前端刷新订阅页面,以及通知结果
match profiles::profiles_append_item_safe(&mut item).await { match profiles::profiles_append_item_safe(&mut item).await {
Ok(_) => { Ok(_) => {
Config::profiles().await.apply(); Config::profiles().await.apply();
let _ = Config::profiles().await.data_arc().save_file().await; let _ = Config::profiles().await.data_arc().save_file().await;
// TODO 通知系统疑似损坏,前端无法显示通知事件
handle::Handle::notice_message( handle::Handle::notice_message(
"import_sub_url::ok", "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); handle::Handle::notify_profile_changed(uid);
} }
Err(e) => { Err(e) => {
@@ -101,14 +102,10 @@ pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
e e
); );
Config::profiles().await.discard(); Config::profiles().await.discard();
// TODO 通知系统疑似损坏,前端无法显示通知事件
handle::Handle::notice_message("import_sub_url::error", e.to_string()); handle::Handle::notice_message("import_sub_url::error", e.to_string());
return Ok(()); return Ok(());
} }
} }
handle::Handle::refresh_verge();
handle::Handle::refresh_clash();
Ok(()) Ok(())
} }

View File

@@ -11,7 +11,10 @@ export const handleNoticeMessage = (
) => { ) => {
const handlers: Record<string, () => void> = { const handlers: Record<string, () => void> = {
"import_sub_url::ok": () => { "import_sub_url::ok": () => {
navigate("/profile", { state: { current: msg } }); // 空 msg 传入,我们不希望导致 后端-前端-后端 死循环,这里只做提醒。
// 未来细分事件通知时,可以考虑传入订阅 ID 或其他标识符
// navigate("/profile", { state: { current: msg } });
navigate("/profile");
showNotice("success", t("Import Subscription Successful")); showNotice("success", t("Import Subscription Successful"));
}, },
"import_sub_url::error": () => { "import_sub_url::error": () => {

View File

@@ -13,15 +13,15 @@ import {
sortableKeyboardCoordinates, sortableKeyboardCoordinates,
} from "@dnd-kit/sortable"; } from "@dnd-kit/sortable";
import { import {
CheckBoxOutlineBlankRounded,
CheckBoxRounded,
ClearRounded, ClearRounded,
ContentPasteRounded, ContentPasteRounded,
DeleteRounded,
IndeterminateCheckBoxRounded,
LocalFireDepartmentRounded, LocalFireDepartmentRounded,
RefreshRounded, RefreshRounded,
TextSnippetOutlined, TextSnippetOutlined,
CheckBoxOutlineBlankRounded,
CheckBoxRounded,
IndeterminateCheckBoxRounded,
DeleteRounded,
} from "@mui/icons-material"; } from "@mui/icons-material";
import { LoadingButton } from "@mui/lab"; import { LoadingButton } from "@mui/lab";
import { Box, Button, Divider, Grid, IconButton, Stack } from "@mui/material"; import { Box, Button, Divider, Grid, IconButton, Stack } from "@mui/material";