add examples dir

This commit is contained in:
pantonshire committed 2023-08-19 10:16:18 +01:00
1 parent 7424e36e56
commit fe87e12256
1 file changed
+33
+33
View File
@@ -0,0 +1,33 @@
use std::{error::Error, panic, thread, time::Duration};
use treacle::{fold, Debouncer};
fn main() -> Result<(), Box<dyn Error>> {
// Create a debouncer which combines `u32` events which occur within the same 500ms window by
// pushing them to a `Vec`.
let (debouncer, rx) =
Debouncer::new(Duration::from_millis(500), fold::fold_vec_push::<u32>)?;
// Spawn a thread which receives the debounced events and prints them.
let t = thread::spawn(move || {
while let Ok(event) = rx.recv() {
println!("{:?}", event);
}
});
// Create 100 raw events for the debouncer to debounce.
for i in 0..100 {
debouncer.debounce(i);
thread::sleep(Duration::from_millis(50));
}
// Dropping the debouncer will gracefully close the mpsc channel, stopping the loop in the
// other thread.
drop(debouncer);
if let Err(err) = t.join() {
panic::resume_unwind(err);
}
Ok(())
}