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,
},
thread,
time::Instant,
time::{Duration, Instant},
};
use tauri::{Emitter, WebviewWindow};
@@ -92,12 +92,15 @@ impl NotificationSystem {
}
fn worker_loop(rx: mpsc::Receiver<FrontendEvent>) {
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,
}
}
}

View File

@@ -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(())
}

View File

@@ -11,7 +11,10 @@ export const handleNoticeMessage = (
) => {
const handlers: Record<string, () => 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": () => {

View File

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