made some refactoring

This commit is contained in:
2023-03-12 21:44:42 +01:00
parent a1c1458a61
commit 125a9f0fe9
3 changed files with 25 additions and 23 deletions

View File

@@ -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))
}

View File

@@ -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
View 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))
}