From f44fd428cb4ec76220edd150327588a541d536d5 Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Tue, 19 Nov 2024 15:31:40 +0800 Subject: [PATCH] fix: emit immediately with `SyncInterval` --- packages/desktop/src/common/interval.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/desktop/src/common/interval.rs b/packages/desktop/src/common/interval.rs index c1529a49..243d9451 100644 --- a/packages/desktop/src/common/interval.rs +++ b/packages/desktop/src/common/interval.rs @@ -6,6 +6,7 @@ use std::time::{Duration, Instant}; pub struct SyncInterval { interval: Duration, next_tick: Instant, + is_first: bool, } impl SyncInterval { @@ -13,12 +14,17 @@ impl SyncInterval { 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 { - 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.