forked from notify-rs/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debouncer_mini_custom.rs
40 lines (36 loc) · 1.16 KB
/
debouncer_mini_custom.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
use std::{path::Path, time::Duration};
use notify::{Config, RecursiveMode};
use notify_debouncer_mini::new_debouncer_opt;
/// Debouncer with custom backend and waiting for exit
fn main() {
// emit some events by changing a file
std::thread::spawn(|| {
let path = Path::new("test.txt");
let _ = std::fs::remove_file(&path);
loop {
std::fs::write(&path, b"Lorem ipsum").unwrap();
std::thread::sleep(Duration::from_millis(250));
}
});
// setup debouncer
let (tx, rx) = std::sync::mpsc::channel();
// select backend via fish operator, here PollWatcher backend
let mut debouncer = new_debouncer_opt::<_, notify::PollWatcher>(
Duration::from_secs(2),
None,
tx,
Config::default(),
)
.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| println!("{event:?}")),
Err(errors) => errors.iter().for_each(|error| println!("{error:?}")),
}
}
}