Merge branch 'main' of github.com:zzzgydi/clash-verge

This commit is contained in:
GyDi 2022-08-24 22:41:36 +08:00
commit 2f5b8d9abe
3 changed files with 33 additions and 4 deletions

View File

@ -91,7 +91,7 @@ impl Core {
// timer initialize // timer initialize
let mut timer = self.timer.lock(); let mut timer = self.timer.lock();
timer.set_core(self.clone()); timer.set_core(self.clone());
log_if_err!(timer.refresh()); log_if_err!(timer.restore());
} }
/// save the window instance /// save the window instance

View File

@ -121,7 +121,7 @@ impl Profiles {
} }
} }
bail!("failed to get the item by \"{}\"", uid); bail!("failed to get the profile item \"uid:{uid}\"");
} }
/// append new item /// append new item
@ -178,7 +178,7 @@ impl Profiles {
} }
self.items = Some(items); self.items = Some(items);
bail!("failed to found the uid \"{uid}\"") bail!("failed to find the profile item \"uid:{uid}\"")
} }
/// be used to update the remote item /// be used to update the remote item
@ -286,7 +286,7 @@ impl Profiles {
return Ok(config::read_yaml::<Mapping>(file_path.clone())); return Ok(config::read_yaml::<Mapping>(file_path.clone()));
} }
} }
bail!("failed to found the uid \"{current}\""); bail!("failed to find current profile \"uid:{current}\"");
} }
/// generate the data for activate clash config /// generate the data for activate clash config

View File

@ -1,5 +1,6 @@
use super::Core; use super::Core;
use crate::log_if_err; use crate::log_if_err;
use crate::utils::help::get_now;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use delay_timer::prelude::{DelayTimer, DelayTimerBuilder, TaskBuilder}; use delay_timer::prelude::{DelayTimer, DelayTimerBuilder, TaskBuilder};
use std::collections::HashMap; use std::collections::HashMap;
@ -63,6 +64,34 @@ impl Timer {
Ok(()) Ok(())
} }
/// restore timer
pub fn restore(&mut self) -> Result<()> {
self.refresh()?;
let cur_timestamp = get_now(); // seconds
let profiles = self.core.as_ref().unwrap().profiles.lock();
profiles
.get_items()
.unwrap_or(&vec![])
.iter()
.filter(|item| item.uid.is_some() && item.updated.is_some() && item.option.is_some())
.filter(|item| {
// mins to seconds
let interval = item.option.as_ref().unwrap().update_interval.unwrap_or(0) as usize * 60;
let updated = item.updated.unwrap();
return interval > 0 && cur_timestamp - updated >= interval;
})
.for_each(|item| {
let uid = item.uid.as_ref().unwrap();
if let Some((task_id, _)) = self.timer_map.get(uid) {
log_if_err!(self.delay_timer.advance_task(*task_id));
}
});
Ok(())
}
/// generate a uid -> update_interval map /// generate a uid -> update_interval map
fn gen_map(&self) -> HashMap<String, u64> { fn gen_map(&self) -> HashMap<String, u64> {
let profiles = self.core.as_ref().unwrap().profiles.lock(); let profiles = self.core.as_ref().unwrap().profiles.lock();