diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-09-14 01:29:55 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-09-14 01:31:22 +0800 |
| commit | 4259ab8d858bcbafe6a6c3f8d3be8979c61d80ac (patch) | |
| tree | 00da4cb35d0ff5f859a419c04c9ff79fbb400bab /src/client/connector.rs | |
| parent | 4f75bc1b3fd85a7b54cf6bbb6e895c2deb353299 (diff) | |
refactor(connector): move checking prev poll to connector instead of instance of it
chore(connector): move sleep to the top of the loop
Diffstat (limited to 'src/client/connector.rs')
| -rw-r--r-- | src/client/connector.rs | 21 |
1 files changed, 14 insertions, 7 deletions
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<Result<Self::Ok, Self::Err>>; // None if no update(content is same) + fn update(&self) -> Result<Self::Ok, Self::Err>; // 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)); } } } |
