replace threaded debouncer with threadless debouncer

This commit is contained in:
pantonshire committed 2023-08-28 10:46:53 +01:00
1 parent d1ed8b3f0e
commit 7780dabed7
4 files changed
+672 -1042

No files matched your search

+8 -11
View File
@@ -1,12 +1,11 @@
use std::{error::Error, panic, thread, time::Duration};
use std::{panic, thread, time::Duration};
use treacle::{fold, Debouncer};
use treacle::{fold, debouncer};
fn main() -> Result<(), Box<dyn Error>> {
fn main() {
// 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>)?;
let (tx, rx) = debouncer(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 || {
@@ -17,17 +16,15 @@ fn main() -> Result<(), Box<dyn Error>> {
// Create 100 raw events for the debouncer to debounce.
for i in 0..100 {
debouncer.debounce(i);
tx.send(i).unwrap();
thread::sleep(Duration::from_millis(50));
}
// Dropping the debouncer will gracefully close the mpsc channel, stopping the loop in the
// other thread.
drop(debouncer);
// Drop the tx, so there are no more `DebouncerTx`s left. This will gracefully shut down the
// debouncer, stopping the loop in the other thread once it has received all the events.
drop(tx);
if let Err(err) = t.join() {
panic::resume_unwind(err);
}
Ok(())
}