#delay #stream #future #duration #most #recent #wait

debounced

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

1 unstable release

0.1.0 Dec 4, 2022

#645 in Asynchronous

Download history 1698/week @ 2023-12-11 1426/week @ 2023-12-18 658/week @ 2023-12-25 1139/week @ 2024-01-01 1265/week @ 2024-01-08 1337/week @ 2024-01-15 1098/week @ 2024-01-22 1427/week @ 2024-01-29 1089/week @ 2024-02-05 1549/week @ 2024-02-12 1742/week @ 2024-02-19 1487/week @ 2024-02-26 1452/week @ 2024-03-04 1157/week @ 2024-03-11 1588/week @ 2024-03-18 1920/week @ 2024-03-25

6,174 downloads per month
Used in i3status-rs

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

~775KB
~15K SLoC