From 0434aa103bbd955cc8227e0d7a483b289e86a766 Mon Sep 17 00:00:00 2001 From: Demmie <2e3s19@gmail.com> Date: Thu, 16 May 2024 21:36:18 -0400 Subject: [PATCH] Use a single now time --- watchers/src/watchers/idle.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/watchers/src/watchers/idle.rs b/watchers/src/watchers/idle.rs index 3a32d06..4252f8e 100644 --- a/watchers/src/watchers/idle.rs +++ b/watchers/src/watchers/idle.rs @@ -38,9 +38,10 @@ impl State { seconds_since_input: u32, client: &Arc, ) -> anyhow::Result<()> { + let now = Utc::now(); let time_since_input = Duration::seconds(i64::from(seconds_since_input)); - self.last_input_time = Utc::now() - time_since_input; + self.last_input_time = now - time_since_input; if self.is_idle && u64::from(seconds_since_input) < self.idle_timeout.num_seconds().try_into().unwrap() @@ -56,9 +57,7 @@ impl State { self.is_changed = true; } - self.send_ping(client).await?; - - Ok(()) + self.send_ping(now, client).await } pub async fn send_reactive(&mut self, client: &Arc) -> anyhow::Result<()> { @@ -67,12 +66,10 @@ impl State { self.last_input_time = now; } - self.send_ping(client).await + self.send_ping(now, client).await } - async fn send_ping(&mut self, client: &Arc) -> anyhow::Result<()> { - let now = Utc::now(); - + async fn send_ping(&mut self, now: DateTime, client: &Arc) -> anyhow::Result<()> { if self.is_changed { let result = if self.is_idle { debug!("Reporting as changed to idle");