Compare commits
5
Commits
fe87e12256
...
7780dabed7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7780dabed7 | ||
|
|
d1ed8b3f0e | ||
|
|
b2cd28690b | ||
|
|
1b79af50d1 | ||
|
|
8b70bbdf15 |
No files matched your search
+8
-11
@@ -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
|
// Create a debouncer which combines `u32` events which occur within the same 500ms window by
|
||||||
// pushing them to a `Vec`.
|
// pushing them to a `Vec`.
|
||||||
let (debouncer, rx) =
|
let (tx, rx) = debouncer(Duration::from_millis(500), fold::fold_vec_push::<u32>);
|
||||||
Debouncer::new(Duration::from_millis(500), fold::fold_vec_push::<u32>)?;
|
|
||||||
|
|
||||||
// Spawn a thread which receives the debounced events and prints them.
|
// Spawn a thread which receives the debounced events and prints them.
|
||||||
let t = thread::spawn(move || {
|
let t = thread::spawn(move || {
|
||||||
@@ -17,17 +16,15 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
// Create 100 raw events for the debouncer to debounce.
|
// Create 100 raw events for the debouncer to debounce.
|
||||||
for i in 0..100 {
|
for i in 0..100 {
|
||||||
debouncer.debounce(i);
|
tx.send(i).unwrap();
|
||||||
thread::sleep(Duration::from_millis(50));
|
thread::sleep(Duration::from_millis(50));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dropping the debouncer will gracefully close the mpsc channel, stopping the loop in the
|
// Drop the tx, so there are no more `DebouncerTx`s left. This will gracefully shut down the
|
||||||
// other thread.
|
// debouncer, stopping the loop in the other thread once it has received all the events.
|
||||||
drop(debouncer);
|
drop(tx);
|
||||||
|
|
||||||
if let Err(err) = t.join() {
|
if let Err(err) = t.join() {
|
||||||
panic::resume_unwind(err);
|
panic::resume_unwind(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
+15
-27
@@ -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.
|
//! 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.
|
/// 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};
|
/// use std::{time::Duration, thread};
|
||||||
///
|
///
|
||||||
/// let (debouncer, rx) = Debouncer::new(
|
/// let (tx, rx) = debouncer(Duration::from_millis(100), fold_vec_push::<u32>);
|
||||||
/// Duration::from_millis(100),
|
|
||||||
/// fold_vec_push::<u32>
|
|
||||||
/// )?;
|
|
||||||
///
|
///
|
||||||
/// debouncer.debounce(4);
|
/// tx.send(4).unwrap();
|
||||||
/// debouncer.debounce(5);
|
/// tx.send(5).unwrap();
|
||||||
///
|
///
|
||||||
/// thread::sleep(Duration::from_millis(150));
|
/// thread::sleep(Duration::from_millis(150));
|
||||||
/// debouncer.debounce(3);
|
/// tx.send(3).unwrap();
|
||||||
/// debouncer.debounce(2);
|
/// tx.send(2).unwrap();
|
||||||
/// debouncer.debounce(1);
|
/// tx.send(1).unwrap();
|
||||||
///
|
///
|
||||||
/// assert_eq!(rx.recv().unwrap(), vec![4, 5]);
|
/// assert_eq!(rx.recv().unwrap(), vec![4, 5]);
|
||||||
/// assert_eq!(rx.recv().unwrap(), vec![3, 2, 1]);
|
/// assert_eq!(rx.recv().unwrap(), vec![3, 2, 1]);
|
||||||
/// # Ok(())
|
|
||||||
/// # }
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn fold_vec_push<T>(acc: Option<Vec<T>>, e: T) -> Vec<T> {
|
pub fn fold_vec_push<T>(acc: Option<Vec<T>>, e: T) -> Vec<T> {
|
||||||
let mut acc = acc.unwrap_or_default();
|
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.
|
/// 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};
|
/// use std::{time::Duration, thread};
|
||||||
///
|
///
|
||||||
/// let (debouncer, rx) = Debouncer::new(
|
/// let (tx, rx) = debouncer(Duration::from_millis(100), fold_unit::<u32>);
|
||||||
/// Duration::from_millis(100),
|
|
||||||
/// fold_unit::<u32>
|
|
||||||
/// )?;
|
|
||||||
///
|
///
|
||||||
/// debouncer.debounce(4);
|
/// tx.send(4).unwrap();
|
||||||
/// debouncer.debounce(5);
|
/// tx.send(5).unwrap();
|
||||||
///
|
///
|
||||||
/// thread::sleep(Duration::from_millis(150));
|
/// thread::sleep(Duration::from_millis(150));
|
||||||
/// debouncer.debounce(3);
|
/// tx.send(3).unwrap();
|
||||||
/// debouncer.debounce(2);
|
/// tx.send(2).unwrap();
|
||||||
/// debouncer.debounce(1);
|
/// tx.send(1).unwrap();
|
||||||
///
|
///
|
||||||
/// assert_eq!(rx.recv().unwrap(), ());
|
/// assert_eq!(rx.recv().unwrap(), ());
|
||||||
/// assert_eq!(rx.recv().unwrap(), ());
|
/// assert_eq!(rx.recv().unwrap(), ());
|
||||||
/// # Ok(())
|
|
||||||
/// # }
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn fold_unit<T>(_acc: Option<()>, _e: T) {}
|
pub fn fold_unit<T>(_acc: Option<()>, _e: T) {}
|
||||||
+634
-297
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user