Crate debounced

source ·
Expand description

Utility for building delayed Futures and debounced Streams that wait a given duration before yielding the most recent item.

Usage

The functionality that is implemented by this crate is pretty simple. Depending on whether you want to delay a single future, or debounce an entire stream, you should either use delayed or debounced.

Delaying a Single Value

If you want to delay a single future from resolving to a known value, you can use delayed.

use debounced::delayed;

let delayed = delayed(42, Duration::from_secs(1)).await;
assert_eq!(start.elapsed().as_secs(), 1);
assert_eq!(delayed, 42);

Debouncing a Stream

If you want to debounce an entire stream, you can use debounced.

use debounced::debounced;

let (mut sender, receiver) = futures_channel::mpsc::channel(1024);
let mut debounced = debounced(receiver, Duration::from_secs(1));
sender.send(21).await;
sender.send(42).await;
assert_eq!(debounced.next().await, Some(42));
assert_eq!(start.elapsed().as_secs(), 1);

Limitations

  • Leading debounce. This library currently only implements trailing debounce. It does not implement leading debounce.

Structs

Stream that delays its items for a given duration and only yields the most recent item afterwards.
Future that resolves into a value after a given duration has passed.

Functions

Returns a new stream that delays its items for a given duration and only yields the most recent item afterwards.
Returns a new future that resolves into the given value after the given duration has passed.