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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::cmp::Ordering;
use std::collections::{BinaryHeap, VecDeque};
use std::time::{Duration, Instant};

#[derive(Debug, PartialEq, Eq)]
struct Event<T> {
    item: T,
    release_at: Instant,
}

impl<T: Eq> Ord for Event<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        other.release_at.cmp(&self.release_at) // reverse ordering for min-heap
    }
}

impl<T: Eq> PartialOrd for Event<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// Current state of the debouncing buffer returned from `.get()`:
///
/// - `Ready(T)` when the event is ready to be delivered after the timeout
///   (moves data out of the buffer)
/// - `Wait(Duration)` indicates how much time is left until `Ready`
/// - `Empty` means the buffer is empty
#[derive(Debug, PartialEq)]
pub enum State<T> {
    Ready(T),
    Wait(Duration),
    Empty,
}

pub trait BufferGet<T> {
    fn get(&mut self) -> State<T>;
}

/// Debouncing buffer with a common delay for all events. Accepts events via
/// `.put()` which tracks the time of the event and de-duplicates them against
/// the current buffer content. Subsequent call to `.get()` which returns the
/// `State` of the buffer.
///
/// Implemented on top of a `VecDeque` and should be slightly more preformant
/// than `MixedEventBuffer`.
pub struct EventBuffer<T> {
    delay: Duration,
    events: VecDeque<Event<T>>,
}

impl<T: PartialEq> EventBuffer<T> {
    pub fn new(delay: Duration) -> EventBuffer<T> {
        EventBuffer {
            delay,
            events: VecDeque::new(),
        }
    }

    pub fn put(&mut self, item: T) {
        let time = Instant::now();
        self.events
            .retain(|e| e.release_at <= time || e.item != item);
        self.events.push_back(Event {
            item,
            release_at: time + self.delay,
        });
    }
}

impl<T> BufferGet<T> for EventBuffer<T> {
    fn get(&mut self) -> State<T> {
        let time = Instant::now();
        match self.events.get(0) {
            None => State::Empty,
            Some(e) if e.release_at > time => State::Wait(e.release_at - time),
            Some(_) => State::Ready(self.events.pop_front().unwrap().item),
        }
    }
}

/// Debouncing buffer with per-event delays. Accepts events via `.put(T, delay)`
/// which tracks the time of the event and de-duplicates them against the
/// current buffer content. Subsequent call to `.get()` which returns the
/// `State` of the buffer.
///
/// Implemented on top of a `BinaryHeap` and should be slightly less preformant
/// than `EventBuffer`.
pub struct MixedEventBuffer<T> {
    events: BinaryHeap<Event<T>>,
}

impl<T: Eq> MixedEventBuffer<T> {
    pub fn new() -> MixedEventBuffer<T> {
        MixedEventBuffer {
            events: BinaryHeap::new(),
        }
    }

    pub fn put(&mut self, item: T, delay: Duration) {
        let time = Instant::now();
        self.events
            .retain(|e| e.release_at <= time || e.item != item);
        self.events.push(Event {
            item,
            release_at: time + delay,
        });
    }
}

impl<T: Eq> BufferGet<T> for MixedEventBuffer<T> {
    fn get(&mut self) -> State<T> {
        let time = Instant::now();
        match self.events.peek() {
            None => State::Empty,
            Some(e) if e.release_at > time => State::Wait(e.release_at - time),
            Some(_) => State::Ready(self.events.pop().unwrap().item),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread::sleep;
    use std::time::Duration;

    mod event_buffer {
        use super::*;

        #[test]
        fn wait() {
            let mut debouncer = EventBuffer::new(Duration::from_millis(20));
            debouncer.put(1);
            assert!(matches!(debouncer.get(), State::Wait(_)));
            sleep(Duration::from_millis(10));
            assert!(matches!(debouncer.get(), State::Wait(_)));
            sleep(Duration::from_millis(10));
            assert!(matches!(debouncer.get(), State::Ready(_)));
        }

        #[test]
        fn deduplication() {
            let mut debouncer = EventBuffer::new(Duration::from_millis(20));
            debouncer.put(1);
            debouncer.put(2);
            sleep(Duration::from_millis(10));
            debouncer.put(1);
            sleep(Duration::from_millis(20));
            assert_eq!(
                vec![debouncer.get(), debouncer.get(), debouncer.get()],
                vec![State::Ready(2), State::Ready(1), State::Empty]
            );
        }
    }

    mod mixed_event_buffer {
        use super::*;

        #[test]
        fn wait() {
            let mut debouncer = MixedEventBuffer::new();
            debouncer.put(1, Duration::from_millis(20));
            assert!(matches!(debouncer.get(), State::Wait(_)));
            sleep(Duration::from_millis(10));
            assert!(matches!(debouncer.get(), State::Wait(_)));
            sleep(Duration::from_millis(10));
            assert!(matches!(debouncer.get(), State::Ready(_)));
        }

        #[test]
        fn deduplication() {
            let mut debouncer = MixedEventBuffer::new();
            debouncer.put(1, Duration::from_millis(20));
            debouncer.put(2, Duration::from_millis(30));
            sleep(Duration::from_millis(10));
            debouncer.put(1, Duration::from_millis(10));
            sleep(Duration::from_millis(20));
            assert_eq!(
                vec![debouncer.get(), debouncer.get(), debouncer.get()],
                vec![State::Ready(1), State::Ready(2), State::Empty]
            );
        }

        #[test]
        fn event_order() {
            let mut debouncer = MixedEventBuffer::new();
            debouncer.put(2, Duration::from_millis(20));
            debouncer.put(1, Duration::from_millis(10));
            sleep(Duration::from_millis(30));
            assert_eq!(
                vec![debouncer.get(), debouncer.get()],
                vec![State::Ready(1), State::Ready(2)]
            );
        }
    }
}