#events #event-stream #duplicate #structures #helper #removing #debouncing

debounce

Data structures and helpers for debouncing a stream of events: removing duplicate events occurring closely in time

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

#1718 in Data structures

Download history 333/week @ 2024-07-21 315/week @ 2024-07-28 61/week @ 2024-08-04 169/week @ 2024-08-11 45/week @ 2024-08-18 74/week @ 2024-08-25 100/week @ 2024-09-01 221/week @ 2024-09-08 90/week @ 2024-09-15 232/week @ 2024-09-22 695/week @ 2024-09-29 56/week @ 2024-10-06 85/week @ 2024-10-13 62/week @ 2024-10-20 71/week @ 2024-10-27 84/week @ 2024-11-03

304 downloads per month
Used in 8 crates (4 directly)

BSD-3-Clause

15KB
270 lines

debounce

Data structures and helpers for debouncing a stream of events: removing duplicate events occurring closely in time.

https://docs.rs/debounce


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

No runtime deps