forked from notify-rs/notify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
debouncer_mini.rs
52 lines (46 loc) · 1.59 KB
/
debouncer_mini.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::{path::Path, time::Duration};
use notify::RecursiveMode;
use notify_debouncer_mini::new_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);
// 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 1 seconds
let mut debouncer = new_debouncer(Duration::from_secs(1), tx).unwrap();
debouncer
.watcher()
.watch(Path::new("."), RecursiveMode::Recursive)
.unwrap();
// print all events, non returning
for result in rx {
match result {
Ok(events) => events
.iter()
.for_each(|event| log::info!("Event {event:?}")),
Err(error) => log::info!("Error {error:?}"),
}
}
}