forked from notify-rs/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebouncer_mini.rs
More file actions
56 lines (50 loc) · 1.79 KB
/
debouncer_mini.rs
File metadata and controls
56 lines (50 loc) · 1.79 KB
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
53
54
55
56
use std::{path::Path, time::Duration};
use notify::WatchMode;
use notify_debouncer_mini::new_debouncer;
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
/// Example for debouncer mini
fn main() {
tracing_subscriber::registry()
.with(fmt::layer())
.with(
EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("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);
// tracing::info!("running 250ms events");
for _ in 0..20 {
tracing::trace!("writing..");
std::fs::write(path, b"Lorem ipsum").unwrap();
std::thread::sleep(Duration::from_millis(250));
}
// tracing::debug!("waiting 20s");
std::thread::sleep(Duration::from_millis(20000));
// tracing::info!("running 3s events");
for _ in 0..20 {
// tracing::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("."), WatchMode::recursive())
.unwrap();
// print all events, non returning
for result in rx {
match result {
Ok(events) => events
.iter()
.for_each(|event| tracing::info!("Event {event:?}")),
Err(error) => tracing::info!("Error {error:?}"),
}
}
}