From a1347def62f6549f5255c101f9f69c27f19419b0 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Wed, 15 Jan 2025 22:06:34 +0100 Subject: [PATCH] fix(event_sender): undo of custom time event creation and entering previously only the tracking would be undone after entering .NEW@TIME --- src/event_sender.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/event_sender.rs b/src/event_sender.rs index 727d9e4..024f89a 100644 --- a/src/event_sender.rs +++ b/src/event_sender.rs @@ -39,30 +39,29 @@ impl EventSender { // TODO this direly needs testing pub(crate) fn submit(&self, event_builder: EventBuilder) -> Result { - let min = Timestamp::now().sub(UNDO_DELAY); + let event = event_builder.sign_with_keys(&self.keys)?; + + let time = event.created_at; { - // Always flush if oldest event older than a minute or newer than now + // Always flush if any event is newer or more than a minute older than the current event let borrow = self.queue.borrow(); if borrow .iter() - .any(|e| e.created_at < min || e.created_at > Timestamp::now()) + .any(|e| e.created_at < time.sub(UNDO_DELAY) || e.created_at > time) { drop(borrow); - debug!("Flushing event queue because it is older than a minute"); + debug!("Flushing event queue because it is offset from the current event"); self.force_flush(); } } + let mut queue = self.queue.borrow_mut(); - Ok(event_builder.sign_with_keys(&self.keys).inspect(|event| { - if event.kind == TRACKING_KIND - && event.created_at > min - && event.created_at < tasks::now() - { - // Do not send redundant movements - queue.retain(|e| e.kind != TRACKING_KIND); - } - queue.push(event.clone()); - })?) + if event.kind == TRACKING_KIND { + // Remove extraneous movements if tracking event is not at a custom time + queue.retain(|e| e.kind != TRACKING_KIND); + } + queue.push(event.clone()); + Ok(event) } /// Sends all pending events pub(crate) fn force_flush(&self) {