futures_time/stream/
debounce.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::pin::Pin;
use std::task::{Context, Poll};

use futures_core::ready;
use futures_core::stream::Stream;
use pin_project_lite::pin_project;

use crate::future::Timer;

pin_project! {
    /// Debounce the stream.
    ///
    /// This `struct` is created by the [`debounce`] method on [`StreamExt`]. See its
    /// documentation for more.
    ///
    /// [`debounce`]: crate::stream::StreamExt::debounce
    /// [`StreamExt`]: crate::stream::StreamExt
    #[derive(Debug)]
    #[must_use = "streams do nothing unless polled or .awaited"]
    pub struct Debounce<S: Stream, D> {
        #[pin]
        stream: S,
        #[pin]
        deadline: D,
        slot: Option<S::Item>,
        state: State,
    }
}

/// Internal state.
#[derive(Debug)]
enum State {
    /// We're actively streaming and may have data.
    Streaming,
    /// The stream has ended, but we need to send the final `Ready(Some(Item))`
    /// and `Ready(None)` messages.
    FinalItem,
    /// The stream has ended, but we need to send the final `Ready(None)` message.
    SendingNone,
    /// The stream has completed.
    Finished,
}

impl<S: Stream, D> Debounce<S, D> {
    pub(crate) fn new(stream: S, deadline: D) -> Self {
        Self {
            stream,
            deadline,
            slot: None,
            state: State::Streaming,
        }
    }
}

impl<S, D> Stream for Debounce<S, D>
where
    S: Stream,
    D: Timer,
{
    type Item = S::Item;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();

        // See if we need to get more data from the stream.
        if let State::Streaming = this.state {
            match this.stream.poll_next(cx) {
                Poll::Ready(Some(item)) => {
                    *this.slot = Some(item);
                    this.deadline.as_mut().reset_timer();
                }
                Poll::Ready(None) => match *this.slot {
                    Some(_) => *this.state = State::FinalItem,
                    None => *this.state = State::SendingNone,
                },
                _ => {}
            };
        }

        // Handle the timer.
        match this.state {
            State::Streaming => match this.slot.is_some() {
                true => {
                    ready!(this.deadline.as_mut().poll(cx));
                    Poll::Ready(this.slot.take())
                }
                false => Poll::Pending,
            },

            State::FinalItem => {
                let _ = futures_core::ready!(this.deadline.as_mut().poll(cx));
                *this.state = State::SendingNone;
                cx.waker().wake_by_ref();
                Poll::Ready(this.slot.take())
            }

            State::SendingNone => {
                *this.state = State::Finished;
                Poll::Ready(None)
            }
            State::Finished => panic!("stream polled after completion"),
        }
    }
}

#[cfg(test)]
mod test {
    use crate::prelude::*;
    use crate::time::Duration;
    use futures_lite::prelude::*;

    #[test]
    fn all_values_debounce() {
        async_io::block_on(async {
            let interval = Duration::from_millis(10);
            let debounce = Duration::from_millis(20);

            let mut counter = 0;
            crate::stream::interval(interval)
                .take(10)
                .debounce(debounce)
                .for_each(|_| counter += 1)
                .await;

            assert_eq!(counter, 1);
        })
    }

    #[test]
    fn no_debounces_hit() {
        async_io::block_on(async {
            let interval = Duration::from_millis(40);
            let debounce = Duration::from_millis(10);

            let mut counter = 0;
            crate::stream::interval(interval)
                .take(10)
                .debounce(debounce)
                .for_each(|_| counter += 1)
                .await;

            assert_eq!(counter, 10);
        })
    }
}