Skip to content

Commit

Permalink
fix: emit immediately with SyncInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Nov 19, 2024
1 parent 1c4218e commit f44fd42
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/desktop/src/common/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@ use std::time::{Duration, Instant};
pub struct SyncInterval {
interval: Duration,
next_tick: Instant,
is_first: bool,
}

impl SyncInterval {
pub fn new(interval_ms: u64) -> Self {
Self {
interval: Duration::from_millis(interval_ms),
next_tick: Instant::now(),
is_first: true,
}
}

/// Returns a receiver that will get a message at the next tick time.
pub fn tick(&mut self) -> crossbeam::channel::Receiver<Instant> {
if let Some(wait_duration) =
if self.is_first {
// Emit immediately on the first tick.
self.is_first = false;
crossbeam::channel::after(Duration::from_secs(0))
} else if let Some(wait_duration) =
self.next_tick.checked_duration_since(Instant::now())
{
// Wait normally until the next tick.
Expand Down

0 comments on commit f44fd42

Please sign in to comment.