Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ammonia = "4.1.1"
askama = { version = "0.14.0", features = ["full"] }
clap = { version = "4.5.46", features = ["derive"] }
notify = "8.2.0"
notify-debouncer-full = "0.7.0"
pulldown-cmark = "0.13.0"
rust-embed = { version = "8.11.0", features = ["interpolate-folder-path"] }
tokio = { version = "1.49.0", features = ["full"] }
Expand Down
67 changes: 39 additions & 28 deletions src/main.rs
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove the dbg mode

Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#![allow(unused)]
use actix_web::web;
use notify::event::RemoveKind;
use notify_debouncer_full::DebouncedEvent;
use notify_debouncer_full::{DebounceEventResult, new_debouncer, notify::*};
use pulldown_cmark::Options;
use std::fs;
use std::path::Path;
use tokio::time::Duration;
mod args;
use actix_web::App;
use actix_web::HttpServer;
Expand All @@ -13,7 +18,7 @@ use args::MdwatchArgs;
use askama::Template;
use clap::Parser;

use notify::{Event, RecursiveMode, Result, Watcher};
use notify::{Event, RecursiveMode, Result, Watcher, event::ModifyKind};
use rust_embed::Embed;
use tokio::sync::mpsc;

Expand Down Expand Up @@ -63,41 +68,47 @@ async fn ws_handler(
) -> actix_web::Result<impl Responder> {
let (response, mut session, mut _msg_stream) = actix_ws::handle(&req, body)?;
let file_path = file.as_str().to_string();
let (watch_tx, mut notify_rx) = mpsc::unbounded_channel::<Result<Event>>();

let mut watcher = notify::recommended_watcher(move |res| {
let _ = watch_tx.send(res);
})
let (watch_tx, mut notify_rx) = mpsc::unbounded_channel::<DebouncedEvent>();

let mut debouncer = new_debouncer(
Duration::from_secs(2),
None,
move |result: DebounceEventResult| match result {
Ok(events) => events.into_iter().for_each(|event| {
let _ = watch_tx.send(event);
}),
Err(errors) => errors
.iter()
.for_each(|error| eprintln!("watch error: {error:?}")),
},
)
.map_err(actix_web::error::ErrorInternalServerError)?;

watcher
.watch(Path::new(&file_path), RecursiveMode::NonRecursive)
debouncer
.watch(&file_path, RecursiveMode::NonRecursive)
.map_err(actix_web::error::ErrorInternalServerError)?;

actix_web::rt::spawn(async move {
// Keep the watcher alive in this async task to keep the msg_stream alive
let _watcher = watcher;
while let Some(res) = notify_rx.recv().await {
match res {
Ok(event) => {
if event.kind.is_remove() {
eprintln!("File removed: {}", file_path);
break;
}
if event.kind.is_modify() {
let latest_markdown = match get_markdown(&file_path) {
Ok(md) => md,
Err(e) => {
eprintln!("Error reading markdown file: {e}");
continue;
}
};
if session.text(latest_markdown).await.is_err() {
break;
}
let _watcher = debouncer;

while let Some(event) = notify_rx.recv().await {
if matches!(event.kind, EventKind::Remove(RemoveKind::File)) {
eprintln!("File removed: {}", file_path);
break;
}
if matches!(event.kind, EventKind::Modify(ModifyKind::Data(_))) {
println!("File modified");
let latest_markdown = match get_markdown(&file_path) {
Ok(md) => md,
Err(e) => {
eprintln!("Error reading markdown file: {e}");
continue;
}
};
if session.text(latest_markdown).await.is_err() {
break;
}
Err(e) => eprintln!("watch error: {e:?}"),
}
}

Expand Down