Skip to content
Snippets Groups Projects
Commit 30f9f1a0 authored by FoundTheWOUT's avatar FoundTheWOUT Committed by GyDi
Browse files

fix: timer restore at app launch

parent 3f58d05a
No related branches found
No related tags found
No related merge requests found
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment