5 releases
0.2.2 | Jun 29, 2023 |
---|---|
0.2.1 | Dec 16, 2021 |
0.2.0 | Oct 22, 2021 |
0.1.1 | Oct 19, 2021 |
0.1.0 | Oct 19, 2021 |
#14 in #duplicate
670 downloads per month
Used in 7 crates
(3 directly)
15KB
270 lines
debounce
Data structures and helpers for debouncing a stream of events: removing duplicate events occurring closely in time.
lib.rs
:
Data structures and helpers for debouncing a stream of events: removing duplicate events occurring closely in time.
Examples of such events:
- File write operations coming from a file system monitor
- Similar log strings from a
tail -f
- Hardware signals from a sensor
Debouncers accept a raw stream of events and add a grace period during which similar events are considered duplicates. The event is delivered after the timeout expires after the last seen duplicate.
Data structures vs. debouncers
The debouncing logic itself is implemented as passive data structures in the
[buffer] module. A buffer only implements .put
and .get
, and
is independent of a particular way of organizing asynchronicity, be it
periodic polling, threads, async streams or some other magic.
The crate also provides threaded debouncers built on top of the buffers, which is what you probably want to use most of the time.
Example usage
use debounce::EventDebouncer;
use std::thread::sleep;
use std::time::Duration;
let delay = Duration::from_millis(10);
let debouncer = EventDebouncer::new(delay, move |data: String| {
println!("{}", data);
});
debouncer.put(String::from("foo"));
debouncer.put(String::from("foo"));
debouncer.put(String::from("bar"));
sleep(delay);
// prints one "foo" and one "bar" after the delay
A working command-line debouncer is implemented in examples/debounce.rs
and can be used right away:
inotifywait -m -e close_write . | debounce -d 100