Skip to content

Commit d4a19b8

Browse files
committed
simple watcher
1 parent bb62ef3 commit d4a19b8

File tree

2 files changed

+43
-59
lines changed

2 files changed

+43
-59
lines changed

src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ fn gen_mlmap(
438438
pub fn generate_asts<'a>(
439439
version: &str,
440440
project_root: &str,
441-
mut modules: &'a mut AHashMap<String, Module>,
441+
modules: &'a mut AHashMap<String, Module>,
442442
all_modules: &AHashSet<String>,
443443
) {
444444
modules

src/watcher.rs

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,52 @@
11
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;
84
use std::path::PathBuf;
9-
use std::thread;
105
use std::time::Duration;
11-
pub static FILE_TYPES: &[&str] = &["re", "res", "ml", "rei", "resi", "mli"];
126

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"];
158

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();
3611

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
4329
.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(_) => (),
5650
}
5751
}
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-
});
6852
}

0 commit comments

Comments
 (0)