mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2024-11-16 11:42:21 +08:00
fix: timer restore at app launch
This commit is contained in:
parent
3f58d05aa7
commit
30f9f1a021
|
@ -91,7 +91,7 @@ impl Core {
|
|||
// timer initialize
|
||||
let mut timer = self.timer.lock();
|
||||
timer.set_core(self.clone());
|
||||
log_if_err!(timer.refresh());
|
||||
log_if_err!(timer.restore());
|
||||
}
|
||||
|
||||
/// save the window instance
|
||||
|
|
|
@ -3,6 +3,8 @@ use crate::log_if_err;
|
|||
use anyhow::{bail, Context, Result};
|
||||
use delay_timer::prelude::{DelayTimer, DelayTimerBuilder, TaskBuilder};
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Mul;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
type TaskID = u64;
|
||||
|
||||
|
@ -63,6 +65,38 @@ impl Timer {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// restore timer
|
||||
pub fn restore(&mut self) -> Result<()> {
|
||||
log_if_err!(self.refresh());
|
||||
let profiles = self.core.as_ref().unwrap().profiles.lock();
|
||||
let cur_timestamp = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs() as usize;
|
||||
|
||||
for item in profiles.get_items().unwrap() {
|
||||
// if current_time - last_update_time >= interval, cron job should execute immediately.
|
||||
if cur_timestamp - item.updated.unwrap()
|
||||
>= item
|
||||
.option
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.update_interval
|
||||
.unwrap_or(0xffffffff)
|
||||
.mul(60) // minute to secs
|
||||
.try_into()
|
||||
.unwrap()
|
||||
{
|
||||
let (task_id, _) = self
|
||||
.timer_map
|
||||
.get(&item.uid.as_ref().unwrap().clone())
|
||||
.unwrap();
|
||||
log_if_err!(self.delay_timer.advance_task(*task_id));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// generate a uid -> update_interval map
|
||||
fn gen_map(&self) -> HashMap<String, u64> {
|
||||
let profiles = self.core.as_ref().unwrap().profiles.lock();
|
||||
|
|
Loading…
Reference in New Issue
Block a user