Compare commits

..
5 Commits
3 changed files with 662 additions and 340 deletions

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(())
}
+15 -27
View File
@@ -1,30 +1,24 @@
//! Pre-written fold functions which can be passed to [`Debouncer::new`](crate::Debouncer::new) to
//! Pre-written fold functions which can be passed to [`debouncer`](crate::debouncer) to
//! specify how raw events should be combined into a single debounced event.
/// A fold function which combines events of type `T` into a `Vec<T>` by pushing them.
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use treacle::{Debouncer, fold::fold_vec_push};
/// # use treacle::{debouncer, fold::fold_vec_push};
/// use std::{time::Duration, thread};
///
/// let (debouncer, rx) = Debouncer::new(
/// Duration::from_millis(100),
/// fold_vec_push::<u32>
/// )?;
/// let (tx, rx) = debouncer(Duration::from_millis(100), fold_vec_push::<u32>);
///
/// debouncer.debounce(4);
/// debouncer.debounce(5);
/// tx.send(4).unwrap();
/// tx.send(5).unwrap();
///
/// thread::sleep(Duration::from_millis(150));
/// debouncer.debounce(3);
/// debouncer.debounce(2);
/// debouncer.debounce(1);
/// tx.send(3).unwrap();
/// tx.send(2).unwrap();
/// tx.send(1).unwrap();
///
/// assert_eq!(rx.recv().unwrap(), vec![4, 5]);
/// assert_eq!(rx.recv().unwrap(), vec![3, 2, 1]);
/// # Ok(())
/// # }
/// ```
pub fn fold_vec_push<T>(acc: Option<Vec<T>>, e: T) -> Vec<T> {
let mut acc = acc.unwrap_or_default();
@@ -36,26 +30,20 @@ pub fn fold_vec_push<T>(acc: Option<Vec<T>>, e: T) -> Vec<T> {
/// existence of events and not any data associated with them.
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use treacle::{Debouncer, fold::fold_unit};
/// # use treacle::{debouncer, fold::fold_unit};
/// use std::{time::Duration, thread};
///
/// let (debouncer, rx) = Debouncer::new(
/// Duration::from_millis(100),
/// fold_unit::<u32>
/// )?;
/// let (tx, rx) = debouncer(Duration::from_millis(100), fold_unit::<u32>);
///
/// debouncer.debounce(4);
/// debouncer.debounce(5);
/// tx.send(4).unwrap();
/// tx.send(5).unwrap();
///
/// thread::sleep(Duration::from_millis(150));
/// debouncer.debounce(3);
/// debouncer.debounce(2);
/// debouncer.debounce(1);
/// tx.send(3).unwrap();
/// tx.send(2).unwrap();
/// tx.send(1).unwrap();
///
/// assert_eq!(rx.recv().unwrap(), ());
/// assert_eq!(rx.recv().unwrap(), ());
/// # Ok(())
/// # }
/// ```
pub fn fold_unit<T>(_acc: Option<()>, _e: T) {}
+639 -302
View File
File diff suppressed because it is too large. Load diff