|
1 | 1 | use crate::build; |
2 | | -use crate::helpers; |
3 | | -use futures::{ |
4 | | - channel::mpsc::{channel, Receiver}, |
5 | | - SinkExt, StreamExt, |
6 | | -}; |
7 | | -use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher}; |
| 2 | +use notify::{Config, RecursiveMode}; |
| 3 | +use notify_debouncer_mini::new_debouncer_opt; |
8 | 4 | use std::path::PathBuf; |
9 | | -use std::thread; |
10 | 5 | use std::time::Duration; |
11 | | -pub static FILE_TYPES: &[&str] = &["re", "res", "ml", "rei", "resi", "mli"]; |
12 | 6 |
|
13 | | -fn async_watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Result<Event>>)> { |
14 | | - let (mut tx, rx) = channel(1); |
| 7 | +pub static FILE_EXTENSIONS: &[&str] = &["re", "res", "ml", "mli", "rei", "resi"]; |
15 | 8 |
|
16 | | - // Automatically select the best implementation for your platform. |
17 | | - // You can also access each implementation directly e.g. INotifyWatcher. |
18 | | - let watcher = RecommendedWatcher::new( |
19 | | - move |res| { |
20 | | - futures::executor::block_on(async { |
21 | | - tx.send(res).await.unwrap(); |
22 | | - }) |
23 | | - }, |
24 | | - Config::default(), |
25 | | - )?; |
26 | | - |
27 | | - Ok((watcher, rx)) |
28 | | -} |
29 | | - |
30 | | -async fn async_watch(path: String) -> notify::Result<()> { |
31 | | - let (mut watcher, mut rx) = async_watcher()?; |
32 | | - |
33 | | - // Add a path to be watched. All files and directories at that path and |
34 | | - // below will be monitored for changes. |
35 | | - watcher.watch(path.as_ref(), RecursiveMode::Recursive)?; |
| 9 | +pub fn start(folder: &str) { |
| 10 | + let (tx, rx) = std::sync::mpsc::channel(); |
36 | 11 |
|
37 | | - while let Some(res) = rx.next().await { |
38 | | - let files_to_compile = res |
39 | | - .iter() |
40 | | - .map(|event| { |
41 | | - event |
42 | | - .paths |
| 12 | + let mut debouncer = new_debouncer_opt::<_, notify::RecommendedWatcher>( |
| 13 | + Duration::from_millis(200), |
| 14 | + None, |
| 15 | + tx, |
| 16 | + Config::default(), |
| 17 | + ) |
| 18 | + .unwrap(); |
| 19 | + |
| 20 | + debouncer |
| 21 | + .watcher() |
| 22 | + .watch(folder.as_ref(), RecursiveMode::Recursive) |
| 23 | + .unwrap(); |
| 24 | + |
| 25 | + for events in rx { |
| 26 | + match events { |
| 27 | + Ok(events) => { |
| 28 | + let paths = events |
43 | 29 | .iter() |
44 | | - .map(|path| path.to_path_buf()) |
45 | | - .filter(|path| helpers::string_ends_with_any(path, FILE_TYPES)) |
46 | | - .collect::<Vec<PathBuf>>() |
47 | | - }) |
48 | | - .flatten() |
49 | | - .into_iter() |
50 | | - .collect::<Vec<PathBuf>>(); |
51 | | - |
52 | | - let delay = Duration::from_millis(10); |
53 | | - if files_to_compile.len() > 0 { |
54 | | - thread::sleep(delay); |
55 | | - build::build(&path); |
| 30 | + .filter_map(|event| { |
| 31 | + let path_buf = event.path.to_path_buf(); |
| 32 | + let extension = path_buf.extension().and_then(|ext| ext.to_str()); |
| 33 | + if let Some(extension) = extension { |
| 34 | + if FILE_EXTENSIONS.contains(&extension) { |
| 35 | + Some(path_buf) |
| 36 | + } else { |
| 37 | + None |
| 38 | + } |
| 39 | + } else { |
| 40 | + None |
| 41 | + } |
| 42 | + }) |
| 43 | + .collect::<Vec<PathBuf>>(); |
| 44 | + |
| 45 | + if paths.len() > 0 { |
| 46 | + build::build(&folder); |
| 47 | + } |
| 48 | + } |
| 49 | + Err(_) => (), |
56 | 50 | } |
57 | 51 | } |
58 | | - |
59 | | - Ok(()) |
60 | | -} |
61 | | - |
62 | | -pub fn start(folder: &str) { |
63 | | - futures::executor::block_on(async { |
64 | | - if let Err(e) = async_watch(folder.to_string()).await { |
65 | | - println!("error: {:?}", e) |
66 | | - } |
67 | | - }); |
68 | 52 | } |
0 commit comments