Skip to content

Commit 1c14373

Browse files
authored
Remove debounced dependency (#2138)
1 parent 072642a commit 1c14373

File tree

7 files changed

+27
-39
lines changed

7 files changed

+27
-39
lines changed

Cargo.lock

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ calibright = { version = "0.1.9", features = ["watch"] }
3636
chrono = { version = "0.4", default-features = false, features = ["clock", "unstable-locales"] }
3737
chrono-tz = { version = "0.10", features = ["serde"] }
3838
clap = { version = "4.0", default-features = false, features = ["std", "derive", "help", "usage"] }
39-
debounced = "0.2.0"
4039
dirs = "5.0"
4140
env_logger = "0.11"
4241
futures = { version = "0.3.31", default-features = false }

src/blocks/bluetooth.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,7 @@ impl DeviceMonitor {
260260

261261
loop {
262262
select! {
263-
_ = updates.next() => {
264-
// avoid too frequent updates
265-
let _ = tokio::time::timeout(Duration::from_millis(100), async {
266-
loop { let _ = updates.next().await; }
267-
}).await;
263+
_ = updates.next_debounced() => {
268264
debug!("Got update for device");
269265
return Ok(());
270266
}

src/blocks/external_ip.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
180180
select! {
181181
_ = sleep(config.interval.0) => (),
182182
_ = api.wait_for_update_request() => (),
183-
_ = stream.next() => {
184-
// avoid too frequent updates
185-
let _ = tokio::time::timeout(Duration::from_millis(100), async {
186-
loop { let _ = stream.next().await; }
187-
}).await;
188-
}
183+
_ = stream.next_debounced() => ()
189184
}
190185
}
191186
}

src/blocks/prelude.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ pub use futures::{Stream, StreamExt};
2929
pub use smart_default::SmartDefault;
3030

3131
pub use async_trait::async_trait;
32+
33+
pub use crate::util::StreamExtDebounced as _;

src/blocks/privacy/v4l.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use debounced::{Debounced, debounced};
21
use inotify::{EventStream, Inotify, WatchDescriptor, WatchMask, Watches};
32
use tokio::fs::{File, read_dir};
43
use tokio::time::{Interval, interval};
@@ -20,27 +19,24 @@ pub(super) struct Monitor<'a> {
2019
devices: HashMap<PathBuf, WatchDescriptor>,
2120
interval: Interval,
2221
watches: Watches,
23-
updates: Debounced<EventStream<[u8; 1024]>>,
22+
stream: EventStream<[u8; 1024]>,
2423
}
2524

2625
impl<'a> Monitor<'a> {
2726
pub(super) async fn new(config: &'a Config, duration: Duration) -> Result<Self> {
2827
let notify = Inotify::init().error("Failed to start inotify")?;
2928
let watches = notify.watches();
3029

31-
let updates = debounced(
32-
notify
33-
.into_event_stream([0; 1024])
34-
.error("Failed to create event stream")?,
35-
Duration::from_millis(100),
36-
);
30+
let stream = notify
31+
.into_event_stream([0; 1024])
32+
.error("Failed to create event stream")?;
3733

3834
let mut s = Self {
3935
config,
4036
devices: HashMap::new(),
4137
interval: interval(duration),
4238
watches,
43-
updates,
39+
stream,
4440
};
4541
s.update_devices().await?;
4642

@@ -149,7 +145,7 @@ impl PrivacyMonitor for Monitor<'_> {
149145
break;
150146
}
151147
},
152-
_ = self.updates.next() => break,
148+
_ = self.stream.next_debounced() => break
153149
}
154150
}
155151
Ok(())

src/util.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@ pub fn default<T: Default>() -> T {
252252
Default::default()
253253
}
254254

255+
pub trait StreamExtDebounced: futures::StreamExt {
256+
fn next_debounced(&mut self) -> impl Future<Output = Option<Self::Item>>;
257+
}
258+
259+
impl<T: futures::StreamExt + Unpin> StreamExtDebounced for T {
260+
async fn next_debounced(&mut self) -> Option<Self::Item> {
261+
let mut result = self.next().await?;
262+
let mut noop_ctx = std::task::Context::from_waker(std::task::Waker::noop());
263+
loop {
264+
match self.poll_next_unpin(&mut noop_ctx) {
265+
std::task::Poll::Ready(Some(x)) => result = x,
266+
std::task::Poll::Ready(None) | std::task::Poll::Pending => return Some(result),
267+
}
268+
}
269+
}
270+
}
271+
255272
#[cfg(test)]
256273
mod tests {
257274
use super::*;

0 commit comments

Comments
 (0)