Skip to content

Commit

Permalink
debouncer mini: passivate debounce loop and overhaul config
Browse files Browse the repository at this point in the history
Switches from active polling to an event loop that can fully sleep.
Also overhauls the configuration with a builder.
Bumps version to 0.4
  • Loading branch information
0xpr03 committed Jul 15, 2023
1 parent 7da85a9 commit 9dd1e8c
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 169 deletions.
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"

[dev-dependencies]
notify = { version = "6.0.1", path = "../notify" }
notify-debouncer-mini = { version = "0.3.0", path = "../notify-debouncer-mini" }
notify-debouncer-mini = { version = "0.4.0", path = "../notify-debouncer-mini" }
notify-debouncer-full = { version = "0.2.0", path = "../notify-debouncer-full" }
futures = "0.3"
tempfile = "3.5.0"
Expand Down
25 changes: 18 additions & 7 deletions examples/debouncer_mini.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
use std::{path::Path, time::Duration};
use std::{io::Write, path::Path, time::Duration};

use notify::RecursiveMode;
use notify_debouncer_mini::new_debouncer;

/// Example for debouncer
/// Example for debouncer mini
fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debouncer_mini=trace")).init();
// emit some events by changing a file
std::thread::spawn(|| {
let path = Path::new("test.txt");
let _ = std::fs::remove_file(&path);
loop {
// log::info!("running 250ms events");
for _ in 0..20 {
log::trace!("writing..");
std::fs::write(&path, b"Lorem ipsum").unwrap();
std::thread::sleep(Duration::from_millis(250));
}
// log::debug!("waiting 20s");
std::thread::sleep(Duration::from_millis(20000));
// log::info!("running 3s events");
for _ in 0..20 {
// log::debug!("writing..");
std::fs::write(&path, b"Lorem ipsum").unwrap();
std::thread::sleep(Duration::from_millis(3000));
}
});

// setup debouncer
let (tx, rx) = std::sync::mpsc::channel();

// No specific tickrate, max debounce time 2 seconds
let mut debouncer = new_debouncer(Duration::from_secs(2), None, tx).unwrap();
// No specific tickrate, max debounce time 1 seconds
let mut debouncer = new_debouncer(Duration::from_secs(1), tx).unwrap();

debouncer
.watcher()
Expand All @@ -29,8 +40,8 @@ fn main() {
// print all events, non returning
for result in rx {
match result {
Ok(events) => events.iter().for_each(|event| println!("{event:?}")),
Err(errors) => errors.iter().for_each(|error| println!("{error:?}")),
Ok(events) => events.iter().for_each(|event| log::info!("Event {event:?}")),
Err(error) => log::info!("Error {error:?}"),
}
}
}
16 changes: 9 additions & 7 deletions examples/debouncer_mini_custom.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{path::Path, time::Duration};

use notify::{Config, RecursiveMode};
use notify_debouncer_mini::new_debouncer_opt;
use notify::{self, RecursiveMode};
use notify_debouncer_mini::{new_debouncer_opt, Config};

/// Debouncer with custom backend and waiting for exit
fn main() {
Expand All @@ -17,12 +17,14 @@ fn main() {

// setup debouncer
let (tx, rx) = std::sync::mpsc::channel();
// notify backend configuration
let backend_config = notify::Config::default().with_poll_interval(Duration::from_secs(1));
// debouncer configuration
let debouncer_config = Config::default().with_timeout(Duration::from_millis(1000)).with_notify_config(backend_config);
// select backend via fish operator, here PollWatcher backend
let mut debouncer = new_debouncer_opt::<_, notify::PollWatcher>(
Duration::from_secs(2),
None,
debouncer_config,
tx,
Config::default(),
)
.unwrap();

Expand All @@ -33,8 +35,8 @@ fn main() {
// print all events, non returning
for result in rx {
match result {
Ok(events) => events.iter().for_each(|event| println!("{event:?}")),
Err(errors) => errors.iter().for_each(|error| println!("{error:?}")),
Ok(event) => println!("Event {event:?}"),
Err(error) => println!("Error {error:?}"),
}
}
}
2 changes: 1 addition & 1 deletion notify-debouncer-mini/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "notify-debouncer-mini"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
rust-version = "1.60"
description = "notify mini debouncer for events"
Expand Down
Loading

0 comments on commit 9dd1e8c

Please sign in to comment.