#stream #future #delay #duration #debounce #recent #wait

debounced

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

2 unstable releases

0.2.0 Aug 17, 2024
0.1.0 Dec 4, 2022

#146 in Asynchronous

Download history 1042/week @ 2024-07-29 989/week @ 2024-08-05 1378/week @ 2024-08-12 1524/week @ 2024-08-19 1836/week @ 2024-08-26 1422/week @ 2024-09-02 1707/week @ 2024-09-09 1445/week @ 2024-09-16 1586/week @ 2024-09-23 1586/week @ 2024-09-30 1401/week @ 2024-10-07 1700/week @ 2024-10-14 1536/week @ 2024-10-21 1722/week @ 2024-10-28 2027/week @ 2024-11-04 1880/week @ 2024-11-11

7,325 downloads per month
Used in 2 crates

MIT license

14KB
177 lines

Debounced

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.

License

Copyright 2022 Glacyr B.V.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependencies

~0.6–1MB
~19K SLoC