From 4259ab8d858bcbafe6a6c3f8d3be8979c61d80ac Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 14 Sep 2025 01:29:55 +0800 Subject: refactor(connector): move checking prev poll to connector instead of instance of it chore(connector): move sleep to the top of the loop --- src/client/connector.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/client/connector.rs') diff --git a/src/client/connector.rs b/src/client/connector.rs index cae944b..88b1939 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -15,14 +15,24 @@ pub(crate) trait Connection { type Err: std::fmt::Display; // should use channel to notify update - fn update(&self) -> Option>; // None if no update(content is same) + fn update(&self) -> Result; // None if no update(content is same) fn ntfy(&self, data: String); - fn r#loop(&self) -> ! { + fn is_prev(&mut self, str: &Self::Ok) -> bool; + + fn r#loop(&mut self) -> ! { loop { + // tradeoff: if I put this at the ened of the loop, then the continue would just skip it, and continue doing update() until with no waiting, this wastes lot of cpu/IO + // however, of course we can add a manual sleep before it continue and put this in the end, but that's code duplication + // the cons of the current approch is that the first time we've entered, it would start sleeping + sleep(Duration::from_millis(500)); match self.update() { - Some(Result::Ok(s)) => { + Result::Ok(s) => { + if self.is_prev(&s) { + continue; + } + let mut it = s.as_ref().split_whitespace(); let task = match it.next() { Some(t) => t, @@ -39,11 +49,8 @@ pub(crate) trait Connection { .unwrap(), ); } - Some(Result::Err(e)) => eprintln!("Error: {}", e), - _ => {} + Result::Err(e) => eprintln!("Error: {}", e), } - - sleep(Duration::from_millis(500)); } } } -- cgit v1.2.3