You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xplr/src/pwd_watcher.rs

64 lines
1.9 KiB
Rust

use crate::app::Task;
use crate::app::{ExternalMsg, MsgIn};
use anyhow::Result;
use std::path::PathBuf;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::time::Duration;
pub fn keep_watching(
pwd: &str,
tx_msg_in: Sender<Task>,
rx_pwd_watcher: Receiver<String>,
) -> Result<()> {
let mut pwd = PathBuf::from(pwd);
let mut last_modified = pwd.metadata().and_then(|m| m.modified())?;
thread::spawn(move || loop {
if let Ok(new_pwd) = rx_pwd_watcher.try_recv() {
pwd = PathBuf::from(new_pwd);
} else {
pwd.metadata()
.and_then(|m| m.modified())
.map(|modified| {
if modified != last_modified {
let msg = MsgIn::External(ExternalMsg::ExplorePwdAsync);
tx_msg_in.send(Task::new(msg, None)).unwrap();
last_modified = modified;
} else {
Optimize performance ``` Benchmarking focus next item: Collecting 100 samples in estimated 5.1972 s (126k itera focus next item time: [41.216 us 41.346 us 41.494 us] change: [-28.669% -28.110% -27.551%] (p = 0.00 < 0.05) Performance has improved. Found 9 outliers among 100 measurements (9.00%) 4 (4.00%) high mild 5 (5.00%) high severe Benchmarking focus previous item: Collecting 100 samples in estimated 5.0576 s (116k i focus previous item time: [43.589 us 43.754 us 43.927 us] change: [-29.506% -28.748% -28.039%] (p = 0.00 < 0.05) Performance has improved. Found 3 outliers among 100 measurements (3.00%) 1 (1.00%) high mild 2 (2.00%) high severe Benchmarking focus first item: Collecting 100 samples in estimated 5.1765 s (116k iter focus first item time: [44.071 us 44.340 us 44.634 us] change: [-26.739% -26.314% -25.885%] (p = 0.00 < 0.05) Performance has improved. Found 12 outliers among 100 measurements (12.00%) 8 (8.00%) high mild 4 (4.00%) high severe Benchmarking focus last item: Collecting 100 samples in estimated 5.1522 s (116k itera focus last item time: [43.950 us 44.214 us 44.541 us] change: [-27.571% -26.953% -26.337%] (p = 0.00 < 0.05) Performance has improved. Found 11 outliers among 100 measurements (11.00%) 5 (5.00%) high mild 6 (6.00%) high severe Benchmarking leave and enter directory: Collecting 100 samples in estimated 5.4863 s ( leave and enter directory time: [96.645 us 96.915 us 97.234 us] change: [-28.720% -27.224% -25.666%] (p = 0.00 < 0.05) Performance has improved. Found 9 outliers among 100 measurements (9.00%) 6 (6.00%) high mild 3 (3.00%) high severe ```
3 years ago
thread::sleep(Duration::from_secs(1));
};
})
.unwrap_or_else(|e| {
let msg = MsgIn::External(ExternalMsg::LogError(e.to_string()));
tx_msg_in.send(Task::new(msg, None)).unwrap();
Optimize performance ``` Benchmarking focus next item: Collecting 100 samples in estimated 5.1972 s (126k itera focus next item time: [41.216 us 41.346 us 41.494 us] change: [-28.669% -28.110% -27.551%] (p = 0.00 < 0.05) Performance has improved. Found 9 outliers among 100 measurements (9.00%) 4 (4.00%) high mild 5 (5.00%) high severe Benchmarking focus previous item: Collecting 100 samples in estimated 5.0576 s (116k i focus previous item time: [43.589 us 43.754 us 43.927 us] change: [-29.506% -28.748% -28.039%] (p = 0.00 < 0.05) Performance has improved. Found 3 outliers among 100 measurements (3.00%) 1 (1.00%) high mild 2 (2.00%) high severe Benchmarking focus first item: Collecting 100 samples in estimated 5.1765 s (116k iter focus first item time: [44.071 us 44.340 us 44.634 us] change: [-26.739% -26.314% -25.885%] (p = 0.00 < 0.05) Performance has improved. Found 12 outliers among 100 measurements (12.00%) 8 (8.00%) high mild 4 (4.00%) high severe Benchmarking focus last item: Collecting 100 samples in estimated 5.1522 s (116k itera focus last item time: [43.950 us 44.214 us 44.541 us] change: [-27.571% -26.953% -26.337%] (p = 0.00 < 0.05) Performance has improved. Found 11 outliers among 100 measurements (11.00%) 5 (5.00%) high mild 6 (6.00%) high severe Benchmarking leave and enter directory: Collecting 100 samples in estimated 5.4863 s ( leave and enter directory time: [96.645 us 96.915 us 97.234 us] change: [-28.720% -27.224% -25.666%] (p = 0.00 < 0.05) Performance has improved. Found 9 outliers among 100 measurements (9.00%) 6 (6.00%) high mild 3 (3.00%) high severe ```
3 years ago
thread::sleep(Duration::from_secs(1));
})
}
});
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::mpsc;
#[test]
fn test_pwd_watcher() {
let (tx_msg_in, rx_msg_in) = mpsc::channel();
let (tx_pwd_watcher, rx_pwd_watcher) = mpsc::channel();
let result = keep_watching("/", tx_msg_in, rx_pwd_watcher);
assert!(result.is_ok());
tx_pwd_watcher.send("/bin".to_string()).unwrap();
let task = rx_msg_in.recv().unwrap();
let msg = MsgIn::External(ExternalMsg::ExplorePwdAsync);
assert_eq!(task, Task::new(msg, None));
}
}