add project files

This commit is contained in:
wyhaya
2019-08-24 15:19:48 +08:00
commit 74a2b3f89d
11 changed files with 2239 additions and 0 deletions

46
src/watch.rs Normal file
View File

@@ -0,0 +1,46 @@
use async_std::fs;
use async_std::io;
use async_std::stream;
use async_std::stream::Stream;
use async_std::task;
use std::path::PathBuf;
use std::time::{Duration, SystemTime};
pub struct Watch {
path: PathBuf,
interval: u64,
}
impl Watch {
pub fn new(path: PathBuf, interval: u64) -> Watch {
Watch { interval, path }
}
async fn modified(&self) -> io::Result<SystemTime> {
let file = fs::File::open(&self.path).await?;
let modified = file.metadata().await?.modified()?;
Ok(modified)
}
pub async fn change(&mut self, func: fn(path: &PathBuf)) {
let mut repeat = stream::repeat(0);
let mut before = match self.modified().await {
Ok(time) => Some(time),
Err(_) => None,
};
while let Some(_) = repeat.next().await {
task::sleep(Duration::from_millis(self.interval)).await;
let after = match self.modified().await {
Ok(time) => Some(time),
Err(_) => None,
};
if before != after {
before = after;
func(&self.path);
}
}
}
}