made some refactoring
This commit is contained in:
@@ -1,16 +1,16 @@
|
|||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
use futures::StreamExt;
|
||||||
use live_server::listen;
|
use live_server::listen;
|
||||||
use notify::{
|
use notify::{
|
||||||
event::ModifyKind,
|
event::ModifyKind,
|
||||||
Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
|
Event, EventKind, RecursiveMode, Watcher,
|
||||||
};
|
};
|
||||||
use std::{env, path::Path};
|
use std::{env, path::Path};
|
||||||
use tokio::sync::mpsc::{channel, Receiver};
|
|
||||||
|
|
||||||
use crate::app::builder::{
|
use crate::app::{builder::{
|
||||||
asciidoctor::{AsciiDoctorDocsBuilder, AsciiDoctorSlideBuilder},
|
asciidoctor::{AsciiDoctorDocsBuilder, AsciiDoctorSlideBuilder},
|
||||||
Builder,
|
Builder,
|
||||||
};
|
}, watcher::watcher};
|
||||||
|
|
||||||
|
|
||||||
pub async fn serve() {
|
pub async fn serve() {
|
||||||
@@ -38,11 +38,9 @@ async fn watch(path: &Path) -> notify::Result<()> {
|
|||||||
|
|
||||||
watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
|
watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
|
||||||
|
|
||||||
while let Some(res) = rx.recv().await {
|
while let Some(res) = rx.next().await {
|
||||||
match res {
|
let event = res.expect("watching failed");
|
||||||
Ok(event) => file_change(event),
|
file_change(event)
|
||||||
Err(e) => println!("watch error: {:?}", e),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -92,17 +90,3 @@ fn current_dir() -> String {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Result<Event>>)> {
|
|
||||||
let (tx, rx) = channel(1);
|
|
||||||
|
|
||||||
let watcher = RecommendedWatcher::new(
|
|
||||||
move |res| {
|
|
||||||
futures::executor::block_on(async {
|
|
||||||
tx.send(res).await.unwrap();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
Config::default(),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok((watcher, rx))
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
mod commands;
|
mod commands;
|
||||||
pub mod builder;
|
pub mod builder;
|
||||||
pub mod fs_util;
|
pub mod fs_util;
|
||||||
|
pub mod watcher;
|
||||||
mod args;
|
mod args;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|||||||
17
src/app/watcher/mod.rs
Normal file
17
src/app/watcher/mod.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use futures::{channel::mpsc::{Receiver, channel}, SinkExt};
|
||||||
|
use notify::{RecommendedWatcher, Event, Watcher, Config};
|
||||||
|
|
||||||
|
pub fn watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Result<Event>>)> {
|
||||||
|
let (mut tx, rx) = channel(1);
|
||||||
|
|
||||||
|
let watcher = RecommendedWatcher::new(
|
||||||
|
move |res| {
|
||||||
|
futures::executor::block_on(async {
|
||||||
|
tx.send(res).await.unwrap();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
Config::default(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok((watcher, rx))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user