futures_rx/stream_ext/
throttle.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use futures::{
    stream::{Fuse, FusedStream},
    Stream, StreamExt,
};
use pin_project_lite::pin_project;

pub enum ThrottleConfig {
    Leading,
    Trailing,
    All,
}

pin_project! {
    /// Stream for the [`throttle`](RxStreamExt::throttle) method.
    #[must_use = "streams do nothing unless polled"]
    pub struct Throttle<S: Stream, Fut, F> {
        config: ThrottleConfig,
        #[pin]
        stream: Fuse<S>,
        f: F,
        #[pin]
        current_interval: Option<Fut>,
        trailing: Option<S::Item>,
    }
}

impl<S: Stream, Fut, F> Throttle<S, Fut, F> {
    pub(crate) fn new(stream: S, f: F, config: ThrottleConfig) -> Self {
        Self {
            config,
            stream: stream.fuse(),
            f,
            current_interval: None,
            trailing: None,
        }
    }
}

impl<S: Stream, Fut, F> FusedStream for Throttle<S, Fut, F>
where
    F: for<'a> FnMut(&'a S::Item) -> Fut,
    Fut: Future,
{
    fn is_terminated(&self) -> bool {
        self.stream.is_terminated()
    }
}

impl<S: Stream, Fut, F> Stream for Throttle<S, Fut, F>
where
    F: for<'a> FnMut(&'a S::Item) -> Fut,
    Fut: Future,
{
    type Item = S::Item;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();
        let is_in_interval = this
            .current_interval
            .as_mut()
            .as_pin_mut()
            .map(|it| it.poll(cx).is_pending())
            .unwrap_or(false);

        if !is_in_interval && this.current_interval.is_some() {
            this.current_interval.set(None);

            if matches!(this.config, ThrottleConfig::All | ThrottleConfig::Trailing) {
                if let Some(trailing) = this.trailing.take() {
                    return Poll::Ready(Some(trailing));
                }
            }
        }

        match this.stream.poll_next(cx) {
            Poll::Ready(Some(item)) => {
                if is_in_interval {
                    this.trailing.replace(item);
                } else {
                    this.current_interval.set(Some((this.f)(&item)));

                    if matches!(this.config, ThrottleConfig::All | ThrottleConfig::Leading) {
                        return Poll::Ready(Some(item));
                    }
                }

                cx.waker().wake_by_ref();

                Poll::Pending
            }
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Pending => Poll::Pending,
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (lower, upper) = self.stream.size_hint();
        // we know for sure that the final event (if any) will always emit,
        // any other events depend on a time interval and must be discarded.
        let lower = if lower > 0 { 1 } else { 0 };

        (lower, upper)
    }
}

#[cfg(test)]
mod test {
    use futures::{executor::block_on, stream, Stream, StreamExt};
    use futures_time::{future::IntoFuture, time::Duration};

    use crate::RxExt;

    #[test]
    fn smoke() {
        block_on(async {
            let stream = create_stream();
            let all_events = stream
                .throttle(|_| Duration::from_millis(175).into_future())
                .collect::<Vec<_>>()
                .await;

            assert_eq!(all_events, [0, 4, 8]);
        });

        block_on(async {
            let stream = create_stream();
            let all_events = stream
                .throttle_trailing(|_| Duration::from_millis(175).into_future())
                .collect::<Vec<_>>()
                .await;

            assert_eq!(all_events, [3, 7]);
        });

        block_on(async {
            let stream = create_stream();
            let all_events = stream
                .throttle_all(|_| Duration::from_millis(175).into_future())
                .collect::<Vec<_>>()
                .await;

            assert_eq!(all_events, [0, 3, 4, 7, 8]);
        });
    }

    fn create_stream() -> impl Stream<Item = usize> {
        stream::unfold(0, move |count| async move {
            if count < 10 {
                Duration::from_millis(50).into_future().await;

                Some((count, count + 1))
            } else {
                None
            }
        })
    }
}