mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 00:35:38 +08:00
refactor: remove redundant logic
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
use super::CmdResult;
|
||||
use crate::{core::handle, module::mihomo::MihomoManager};
|
||||
use crate::module::mihomo::MihomoManager;
|
||||
use once_cell::sync::Lazy;
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
static LAST_REFRESH_TIME: Lazy<Mutex<Option<Instant>>> = Lazy::new(|| Mutex::new(None));
|
||||
static LAST_EVENT_TIME: Lazy<Mutex<Option<Instant>>> = Lazy::new(|| Mutex::new(None));
|
||||
static IS_REFRESHING: AtomicBool = AtomicBool::new(false);
|
||||
const REFRESH_INTERVAL: Duration = Duration::from_secs(3);
|
||||
const EVENT_INTERVAL: Duration = Duration::from_secs(1);
|
||||
const REFRESH_INTERVAL: Duration = Duration::from_secs(5);
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_proxies() -> CmdResult<serde_json::Value> {
|
||||
@@ -51,31 +49,7 @@ pub async fn get_providers_proxies() -> CmdResult<serde_json::Value> {
|
||||
let manager = MihomoManager::global();
|
||||
match manager.refresh_providers_proxies().await {
|
||||
Ok(_) => {
|
||||
log::debug!(target: "app", "providers_proxies后台刷新成功");
|
||||
|
||||
let should_send_event = {
|
||||
let mut last_event = LAST_EVENT_TIME.lock();
|
||||
match *last_event {
|
||||
Some(last_time) => {
|
||||
if last_time.elapsed() > EVENT_INTERVAL {
|
||||
*last_event = Some(Instant::now());
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
None => {
|
||||
*last_event = Some(Instant::now());
|
||||
true
|
||||
}
|
||||
}
|
||||
};
|
||||
if should_send_event {
|
||||
handle::Handle::refresh_providers_proxies();
|
||||
log::debug!(target: "app", "已发送providers_proxies刷新事件");
|
||||
} else {
|
||||
log::debug!(target: "app", "跳过providers_proxies事件发送(频率限制)");
|
||||
}
|
||||
log::debug!(target: "app", "providers_proxies静默后台刷新成功");
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!(target: "app", "providers_proxies后台刷新失败: {}", e);
|
||||
|
||||
@@ -17,7 +17,6 @@ use crate::{logging, utils::logging::Type};
|
||||
enum FrontendEvent {
|
||||
RefreshClash,
|
||||
RefreshVerge,
|
||||
RefreshProvidersProxies,
|
||||
NoticeMessage { status: String, message: String },
|
||||
ProfileChanged { current_profile_id: String },
|
||||
TimerUpdated { profile_index: String },
|
||||
@@ -122,9 +121,6 @@ impl NotificationSystem {
|
||||
FrontendEvent::RefreshVerge => {
|
||||
("verge://refresh-verge-config", Ok(serde_json::json!("yes")))
|
||||
}
|
||||
FrontendEvent::RefreshProvidersProxies => {
|
||||
("verge://refresh-providers-proxies", Ok(serde_json::json!("yes")))
|
||||
}
|
||||
FrontendEvent::NoticeMessage { status, message } => {
|
||||
match serde_json::to_value((status, message)) {
|
||||
Ok(p) => ("verge://notice-message", Ok(p)),
|
||||
@@ -313,18 +309,6 @@ impl Handle {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refresh_providers_proxies() {
|
||||
let handle = Self::global();
|
||||
if handle.is_exiting() {
|
||||
return;
|
||||
}
|
||||
|
||||
let system_opt = handle.notification_system.read();
|
||||
if let Some(system) = system_opt.as_ref() {
|
||||
system.send_event(FrontendEvent::RefreshProvidersProxies);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn notify_profile_changed(profile_id: String) {
|
||||
let handle = Self::global();
|
||||
if handle.is_exiting() {
|
||||
|
||||
Reference in New Issue
Block a user