fix: timer restore at app launch

This commit is contained in:
FoundTheWOUT 2022-08-21 13:33:12 +08:00 committed by GyDi
parent 3f58d05aa7
commit 30f9f1a021
2 changed files with 35 additions and 1 deletions

View File

@ -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

View File

@ -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();