From 3f58d05aa7b33a9250fde77129b93da690024539 Mon Sep 17 00:00:00 2001 From: GyDi Date: Fri, 19 Aug 2022 17:11:59 +0800 Subject: [PATCH 1/3] fix: adjust log text --- src-tauri/src/core/profiles.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/core/profiles.rs b/src-tauri/src/core/profiles.rs index 16b75ec..6b4d6e1 100644 --- a/src-tauri/src/core/profiles.rs +++ b/src-tauri/src/core/profiles.rs @@ -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 @@ -178,7 +178,7 @@ impl Profiles { } 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 @@ -286,7 +286,7 @@ impl Profiles { return Ok(config::read_yaml::(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 From 30f9f1a021f5a2f6df11016fe31c182cdffbc0a1 Mon Sep 17 00:00:00 2001 From: FoundTheWOUT Date: Sun, 21 Aug 2022 13:33:12 +0800 Subject: [PATCH 2/3] fix: timer restore at app launch --- src-tauri/src/core/mod.rs | 2 +- src-tauri/src/core/timer.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index 6e467cf..c1e92a7 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -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 diff --git a/src-tauri/src/core/timer.rs b/src-tauri/src/core/timer.rs index 3e5ccb9..57341f9 100644 --- a/src-tauri/src/core/timer.rs +++ b/src-tauri/src/core/timer.rs @@ -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 { let profiles = self.core.as_ref().unwrap().profiles.lock(); From 3242efb1a29f1bfada2f3775268f67c746a51bdb Mon Sep 17 00:00:00 2001 From: GyDi Date: Tue, 23 Aug 2022 15:19:04 +0800 Subject: [PATCH 3/3] fix: reduce unsafe unwrap --- src-tauri/src/core/timer.rs | 51 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src-tauri/src/core/timer.rs b/src-tauri/src/core/timer.rs index 57341f9..181c0d5 100644 --- a/src-tauri/src/core/timer.rs +++ b/src-tauri/src/core/timer.rs @@ -1,10 +1,9 @@ use super::Core; use crate::log_if_err; +use crate::utils::help::get_now; 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; @@ -67,33 +66,29 @@ impl Timer { /// 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; + 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)); + } + }); - 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(()) }