refactor: optimize profile change notifications to use references

This commit is contained in:
Tunglies
2026-02-20 17:58:04 +08:00
parent 5f573ca2d6
commit 119aaee546
5 changed files with 25 additions and 28 deletions
+6 -7
View File
@@ -44,27 +44,26 @@ impl Handle {
Self::send_event(FrontendEvent::RefreshVerge);
}
pub fn notify_profile_changed(profile_id: String) {
pub fn notify_profile_changed(profile_id: &String) {
Self::send_event(FrontendEvent::ProfileChanged {
current_profile_id: profile_id,
});
}
pub fn notify_timer_updated(profile_index: String) {
pub fn notify_timer_updated(profile_index: &String) {
Self::send_event(FrontendEvent::TimerUpdated { profile_index });
}
pub fn notify_profile_update_started(uid: String) {
pub fn notify_profile_update_started(uid: &String) {
Self::send_event(FrontendEvent::ProfileUpdateStarted { uid });
}
pub fn notify_profile_update_completed(uid: String) {
pub fn notify_profile_update_completed(uid: &String) {
Self::send_event(FrontendEvent::ProfileUpdateCompleted { uid });
}
// TODO 利用 &str 等缩短 Clone
pub fn notice_message<S: Into<String>, M: Into<String>>(status: S, msg: M) {
let status_str = status.into();
pub fn notice_message<S: AsRef<str>, M: Into<String>>(status: S, msg: M) {
let status_str = status.as_ref();
let msg_str = msg.into();
Self::send_event(FrontendEvent::NoticeMessage {
+7 -8
View File
@@ -5,16 +5,15 @@ use smartstring::alias::String;
use tauri::{Emitter as _, WebviewWindow};
// TODO 重构或优化,避免 Clone 过多
#[derive(Debug, Clone)]
pub enum FrontendEvent {
#[derive(Debug)]
pub enum FrontendEvent<'a> {
RefreshClash,
RefreshVerge,
NoticeMessage { status: String, message: String },
ProfileChanged { current_profile_id: String },
TimerUpdated { profile_index: String },
ProfileUpdateStarted { uid: String },
ProfileUpdateCompleted { uid: String },
NoticeMessage { status: &'a str, message: String },
ProfileChanged { current_profile_id: &'a String },
TimerUpdated { profile_index: &'a String },
ProfileUpdateStarted { uid: &'a String },
ProfileUpdateCompleted { uid: &'a String },
}
#[derive(Debug)]
+4 -4
View File
@@ -409,12 +409,12 @@ impl Timer {
}
/// Emit update events for frontend notification
fn emit_update_event(_uid: &str, _is_start: bool) {
fn emit_update_event(uid: &String, is_start: bool) {
{
if _is_start {
super::handle::Handle::notify_profile_update_started(_uid.into());
if is_start {
super::handle::Handle::notify_profile_update_started(uid);
} else {
super::handle::Handle::notify_profile_update_completed(_uid.into());
super::handle::Handle::notify_profile_update_completed(uid);
}
}
}