#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 1407/week @ 2024-08-21 1687/week @ 2024-08-28 1550/week @ 2024-09-04 1704/week @ 2024-09-11 1523/week @ 2024-09-18 1526/week @ 2024-09-25 1553/week @ 2024-10-02 1357/week @ 2024-10-09 1801/week @ 2024-10-16 1584/week @ 2024-10-23 1785/week @ 2024-10-30 1946/week @ 2024-11-06 1869/week @ 2024-11-13 2141/week @ 2024-11-20 1833/week @ 2024-11-27 1819/week @ 2024-12-04

7,950 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