ratatui_toolkit/services/file_watcher/methods/
drain_events.rs

1//! Drain pending events without processing.
2
3use std::sync::mpsc::TryRecvError;
4
5use crate::services::file_watcher::FileWatcher;
6
7impl FileWatcher {
8    /// Drain all pending events without processing them.
9    ///
10    /// This clears the event queue and the changed paths list,
11    /// useful when you want to ignore accumulated changes
12    /// (e.g., after a batch operation).
13    ///
14    /// # Example
15    ///
16    /// ```no_run
17    /// use ratatui_toolkit::services::file_watcher::FileWatcher;
18    /// use std::path::Path;
19    ///
20    /// let mut watcher = FileWatcher::new().unwrap();
21    /// watcher.watch(Path::new("README.md")).unwrap();
22    ///
23    /// // After some operation that causes many changes:
24    /// watcher.drain_events();
25    /// ```
26    pub fn drain_events(&mut self) {
27        loop {
28            match self.rx.try_recv() {
29                Ok(_) => continue,
30                Err(TryRecvError::Empty) => break,
31                Err(TryRecvError::Disconnected) => break,
32            }
33        }
34        self.changed_paths.clear();
35    }
36}