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
use std::time::{Duration, Instant};

pub struct Bouncer<T> {
    pub delay: Duration,
    last_run: Option<Instant>,
    func: Option<fn() -> T>,
    result: Option<T>,
}

impl<T> Bouncer<T> {

    /// Creates a new bouncer object
    /// 
    /// Example Usage:
    /// ```rust
    ///     use rjdebounce::Bouncer;
    /// 
    ///     let mut bouncer = Bouncer::new(Duration::from_secs(1));
    /// 
    ///     let result = bouncer.debounce(|| {
    ///         return 5 + 5;
    ///     })
    ///  
    ///     assert_eq!(result.is_some(), true);
    ///     assert_eq!(result.unwrap(), 10);
    /// ```
    pub fn new(delay: Duration) -> Self {
        return Bouncer {
            delay,
            last_run: None,
            func: None,
            result: None,
        };
    }

    /// Binds a function internally to call using 
    /// the execute() function.
    /// 
    /// Example Usage:
    /// ```rust
    ///     use rjdebounce::Bouncer;
    /// 
    ///     let mut bouncer = Bouncer::new(Duration::from_secs(1)).with_func(|| 5 + 5);
    ///     
    ///     let result1 = bouncer.get_result();
    ///     bouncer.execute();
    ///     let result2 = bouncer.get_result();
    /// 
    ///     assert_equal!(result.is_some(), false);
    ///     assert_equal!(result.is_some(), true);
    /// ```
    pub fn with_func(mut self, func: fn() -> T) -> Self {
        self.func = Some(func);
        return self;
    }

    /// If Bouncer is created with_func(), call this
    /// function to execute the provided function.
    pub fn execute(&mut self) {
        if self.func.is_some() {
            let result = self.debounce(self.func.unwrap());
            self.result = result;
        }
    }

    /// If Bouncer is created with_func(), call this
    /// function in order to obtain a reference
    /// to the result.
    pub fn get_result(&mut self) -> Option<&mut T> {
        return self.result.as_mut();
    }
    
    /// debounces provided function only running
    /// if it has never been run, or, if the elasped 
    /// time has past since the function was last run.
    pub fn debounce(&mut self, func: fn() -> T) -> Option<T> {
        if self.last_run.is_some() {
            let then = self.last_run.unwrap();
            let now = Instant::now();

            if now.duration_since(then) > self.delay {
                self.last_run = Some(Instant::now());

                return Some(func());
            } else {
                return None;
            }
        } else {
            self.last_run = Some(Instant::now());
            return Some(func());
        }
    }

    /// Resets the last_run property
    /// to None. Makes the bouncer
    /// act as if it never ran a function.
    pub fn reset(&mut self) {
        self.last_run = None;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let delay = Duration::from_secs(1);
        let mut bouncer = Bouncer::new(delay);

        let result = bouncer.debounce(|| {
            return 5 + 6;
        });

        assert_eq!(result.is_some(), true);
        assert_eq!(result.unwrap(), 11);
    }

    #[test]
    fn it_binds_internal_func() {
        let func = || 5 + 6;
        let delay = Duration::from_secs(1);

        let mut bouncer = Bouncer::new(delay).with_func(func);

        assert_eq!(bouncer.get_result().is_some(), false);
        bouncer.execute();
        assert_eq!(bouncer.get_result().is_some(), true);
    }

    #[test]
    fn it_debounces() {
        let delay = Duration::from_millis(100);
        let mut bouncer = Bouncer::new(delay);

        let func = || {
            return 5 + 6;
        };

        let result1 = bouncer.debounce(func);
        let result2 = bouncer.debounce(func);

        std::thread::sleep(Duration::from_millis(101));

        let result3 = bouncer.debounce(func);

        assert_eq!(result1.is_some(), true);
        assert_eq!(result2.is_none(), true);
        assert_eq!(result3.is_some(), true);
    }
}