bouncing 0.1.0

A flexible async debouncer for Rust with cancellation support, max wait limits, and event hooks
Documentation
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::time::Instant;
use tokio::{sync::oneshot, task::JoinHandle};
use tokio_util::sync::CancellationToken;
use tracing::{debug, trace, warn};

#[derive(Clone)]
pub struct Debouncer {
    name: Arc<str>,
    debounce_duration: Duration,
    max_debounce: Option<Duration>,
    cancel_task_timeout: Duration,
    current_task: Arc<Mutex<Option<TaskToken>>>,
    timer_handle: Arc<Mutex<Option<TimerHandle>>>,
    event_handler: Option<EventHandler>,
    cancel_token: CancellationToken,
}

pub type StoredTask = Arc<
    dyn Fn(CancellationToken) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync,
>;

#[derive(Clone)]
pub struct StoredTaskDebouncer {
    debouncer: Debouncer,
    task_fn: StoredTask,
}

pub trait DebouncedTask: Send + Sync + 'static {
    fn execute(&self, token: CancellationToken) -> impl Future<Output = ()> + Send + Sync;
}

#[derive(Clone)]
pub struct TaskDebouncer<T: DebouncedTask> {
    debouncer: Debouncer,
    task: Arc<T>,
}

#[derive(Debug)]
pub struct TimerHandle {
    debounced_at: Instant,
    first_debounce_at: Option<Instant>,
    timer_token: CancellationToken,
    join_handle: JoinHandle<()>,
    exit_rx: oneshot::Receiver<TaskExit>,
}

#[derive(Debug)]
pub struct TaskToken {
    pub started_at: Instant,
    pub task_token: CancellationToken,
}

#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum TaskExit {
    Normal,
    Cancelled,
    Aborted,
    NotStarted,
}

#[derive(Clone, Debug)]
pub enum DebounceEvent {
    Debounced {
        instant: Instant,
        first_debounce_at: Option<Instant>,
        debounce_ends_at: Instant,
    },
    Started {
        instant: Instant,
    },
    Ended {
        instant: Instant,
        exit_status: TaskExit,
    },
}

pub type EventHandler = Arc<dyn Fn(DebounceEvent) + Send + Sync + 'static>;

macro_rules! impl_debouncer_builder {
    ($type:ty) => {
        impl $type {
            pub fn with_task_timeout(mut self, task_timeout: Duration) -> Self {
                self.debouncer = self.debouncer.with_task_timeout(task_timeout);
                self
            }

            pub fn with_max_wait(mut self, max_wait: Duration) -> Self {
                self.debouncer = self.debouncer.with_max_wait(max_wait);
                self
            }

            pub fn with_event_handler<E: Fn(DebounceEvent) + Send + Sync + 'static>(
                mut self,
                event_handler: E,
            ) -> Self {
                self.debouncer = self.debouncer.with_event_handler(event_handler);
                self
            }

            pub async fn stop(&self) {
                self.debouncer.stop().await
            }
        }
    };
}

impl Debouncer {
    pub fn new(
        debounce_duration: Duration,
        cancel_token: CancellationToken,
        task_name: impl AsRef<str>,
    ) -> Self {
        Self {
            debounce_duration,
            max_debounce: None,
            cancel_task_timeout: Duration::from_secs(2),
            timer_handle: Arc::new(Mutex::new(None)),
            cancel_token,
            name: Arc::from(task_name.as_ref()),
            current_task: Arc::new(Mutex::new(None)),
            event_handler: None,
        }
    }

    pub fn with_task_timeout(mut self, task_timeout: Duration) -> Self {
        self.cancel_task_timeout = task_timeout;
        self
    }

    pub fn with_max_wait(mut self, max_wait: Duration) -> Self {
        self.max_debounce = Some(max_wait);
        self
    }

    pub fn with_event_handler<E: Fn(DebounceEvent) + Send + Sync + 'static>(
        mut self,
        event_handler: E,
    ) -> Self {
        self.event_handler = Some(Arc::new(event_handler));
        self
    }

    fn fire_event(&self, event: DebounceEvent) {
        if let Some(event_handler) = &self.event_handler {
            event_handler(event)
        }
    }

    fn should_wait_for_debounce(&self, first_debounce_at: Option<Instant>) -> bool {
        if let Some(max_wait) = self.max_debounce {
            if let Some(first_debounce) = first_debounce_at {
                let wait_period = Instant::now().duration_since(first_debounce);
                if wait_period >= max_wait {
                    trace!(
                        "task {:?} exceeded the max debounce waiting period, {wait_period:?} >= {max_wait:?}",
                        self.name
                    );
                    return false;
                }
            }
        }
        true
    }

    async fn spawn_task<Task, Fut>(&self, task: Task) -> TaskExit
    where
        Task: FnOnce(CancellationToken) -> Fut + Send + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let task_token = CancellationToken::new();
        let now = {
            let mut current_task_token = self.current_task.lock().await;
            if let Some(current_token) = current_task_token.take() {
                current_token.task_token.cancel();
            }
            let now = Instant::now();
            *current_task_token = Some(TaskToken {
                started_at: now,
                task_token: task_token.clone(),
            });
            now
        };

        let task_handle = tokio::spawn({
            let task_token = task_token.clone();
            async move {
                task(task_token).await;
            }
        });

        tokio::pin!(task_handle);

        self.fire_event(DebounceEvent::Started { instant: now });

        let exit_status = tokio::select! {
            result = &mut task_handle => {
                match result {
                    Ok(()) => TaskExit::Normal,
                    Err(_) => TaskExit::Aborted,
                }
            }
            _ = self.wait_for_cancellation(&task_token) => {
                tokio::select! {
                    _ = tokio::time::sleep(self.cancel_task_timeout) => {
                        task_handle.abort();
                        TaskExit::Aborted
                    }
                    result = &mut task_handle => {
                        match result {
                            Ok(()) => TaskExit::Cancelled,
                            Err(_) => TaskExit::Aborted,
                        }
                    }
                }
            }
        };

        self.fire_event(DebounceEvent::Ended {
            exit_status,
            instant: Instant::now(),
        });

        exit_status
    }

    async fn wait_for_cancellation(&self, task_token: &CancellationToken) {
        tokio::select! {
            _ = self.cancel_token.cancelled() => {
                trace!("task {:?} debouncer token cancelled", self.name);
                task_token.cancel();
            }
            _ = task_token.cancelled() => {
                trace!("task {:?} task token cancelled", self.name);
            }
        }
    }

    async fn timer<F, Fut>(
        &self,
        now: Instant,
        first_debounce_at: Option<Instant>,
        exit_tx: oneshot::Sender<TaskExit>,
        timer_token: CancellationToken,
        f: F,
    ) where
        F: FnOnce(CancellationToken) -> Fut + Send + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        self.fire_event(DebounceEvent::Debounced {
            instant: now,
            debounce_ends_at: now.checked_add(self.debounce_duration).unwrap_or(now),
            first_debounce_at,
        });

        let exit_status = tokio::select! {
            _ = timer_token.cancelled() => {
                trace!("task {:?} timer cancelled by next trigger", self.name);
                TaskExit::NotStarted
            }
            _ = self.cancel_token.cancelled() => {
                trace!("task {:?} cancelled while waiting to execute next task", self.name);
                TaskExit::NotStarted
            }
            _ = self.wait_for_debounce(first_debounce_at) => {
                self.spawn_task(f).await
            }
        };
        let _ = exit_tx.send(exit_status);
    }

    async fn wait_for_debounce(&self, first_debounce_at: Option<Instant>) {
        if self.should_wait_for_debounce(first_debounce_at) {
            tokio::time::sleep(self.debounce_duration).await;
        }
    }

    pub async fn run_now<Task, Fut>(&self, task: Task)
    where
        Task: FnOnce(CancellationToken) -> Fut + Send + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut timer_handle = self.timer_handle.lock().await;

        if let Some(timer_handle) = timer_handle.take() {
            if !timer_handle
                .cancel_with_timeout(self.cancel_task_timeout)
                .await
            {
                warn!("task {:?} aborted timer handle", self.name);
            }
        }

        drop(timer_handle);
        self.spawn_task(task).await;
    }

    pub async fn debounce<Task, Fut>(&self, task: Task)
    where
        Task: FnOnce(CancellationToken) -> Fut + Send + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let timer_token = CancellationToken::new();
        let (exit_tx, exit_rx) = oneshot::channel();

        let mut timer_handle = self.timer_handle.lock().await;

        let first_debounce_at = if let Some(existing_timer) = timer_handle.take() {
            let first_debounce = Some(
                existing_timer
                    .first_debounce_at
                    .unwrap_or(existing_timer.debounced_at),
            );

            if !existing_timer
                .cancel_with_timeout(self.cancel_task_timeout)
                .await
            {
                warn!("task {:?} aborted timer handle", self.name);
            }

            first_debounce
        } else {
            None
        };

        let now = Instant::now();
        let debouncer = self.clone();

        *timer_handle = Some(TimerHandle {
            exit_rx,
            timer_token: timer_token.clone(),
            debounced_at: now,
            first_debounce_at,
            join_handle: tokio::spawn(async move {
                debouncer
                    .timer(now, first_debounce_at, exit_tx, timer_token, task)
                    .await;
            }),
        });
    }

    pub async fn stop(&self) {
        debug!("task {:?} stopping...", self.name);

        if let Some(current_token) = self.current_task.lock().await.take() {
            trace!("task {:?} running, cancelling task...", self.name);
            current_token.task_token.cancel();
        }

        if let Some(timer_handle) = self.timer_handle.lock().await.take() {
            trace!("task {:?} waiting on timer....", self.name);

            if !timer_handle
                .cancel_with_timeout(self.cancel_task_timeout)
                .await
            {
                warn!("task {:?} aborted timer handle", self.name);
            }
        }

        debug!("task {:?} stopped", self.name);
    }
}

impl TimerHandle {
    pub async fn cancel_with_timeout(self, timeout: Duration) -> bool {
        self.timer_token.cancel();

        tokio::select! {
            _ = tokio::time::sleep(timeout) => {
                self.join_handle.abort();
                false
            }
            _ = self.exit_rx => {
                true
            }
        }
    }
}

impl StoredTaskDebouncer {
    pub fn new<F, Fut>(
        debounce_timeout: Duration,
        debouncer_token: CancellationToken,
        task_type: impl AsRef<str>,
        task_fn: F,
    ) -> Self
    where
        F: Fn(CancellationToken) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        StoredTaskDebouncer {
            debouncer: Debouncer::new(debounce_timeout, debouncer_token, task_type),
            task_fn: Arc::new(move |token| Box::pin(task_fn(token))),
        }
    }

    pub fn set_task<F, Fut>(&mut self, task_fn: F)
    where
        F: Fn(CancellationToken) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        self.task_fn = Arc::new(move |token| Box::pin(task_fn(token)));
    }

    pub async fn debounce(&self) {
        let task_fn = self.task_fn.clone();
        self.debouncer
            .debounce(move |token| async move { task_fn(token).await })
            .await
    }
}

impl_debouncer_builder!(StoredTaskDebouncer);

impl<T: DebouncedTask> TaskDebouncer<T> {
    pub fn new(
        debounce_timeout: Duration,
        debouncer_token: CancellationToken,
        task_type: impl AsRef<str>,
        task: T,
    ) -> Self {
        TaskDebouncer {
            debouncer: Debouncer::new(debounce_timeout, debouncer_token, task_type),
            task: Arc::new(task),
        }
    }

    pub fn set_task(&mut self, task: T) {
        self.task = Arc::new(task);
    }

    pub async fn debounce(&self) {
        let task = self.task.clone();
        self.debouncer
            .debounce(move |token| async move { task.execute(token).await })
            .await
    }
}

impl<T: DebouncedTask> TaskDebouncer<T> {
    pub fn with_task_timeout(mut self, task_timeout: Duration) -> Self {
        self.debouncer = self.debouncer.with_task_timeout(task_timeout);
        self
    }

    pub fn with_max_wait(mut self, max_wait: Duration) -> Self {
        self.debouncer = self.debouncer.with_max_wait(max_wait);
        self
    }

    pub fn with_event_handler<E: Fn(DebounceEvent) + Send + Sync + 'static>(
        mut self,
        event_handler: E,
    ) -> Self {
        self.debouncer = self.debouncer.with_event_handler(event_handler);
        self
    }

    pub async fn stop(&self) {
        self.debouncer.stop().await
    }
}