Skip to content

Commit 4ce1880

Browse files
committed
fix: the firing of event time and again and bumped the version to 0.1.21
1 parent a561dd6 commit 4ce1880

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mdwatch"
3-
version = "0.1.20"
3+
version = "0.1.21"
44
edition = "2024"
55
authors = ["Santosh Shrestha <santoshxshrestha@gmail.com> "]
66
description = "A simple CLI tool to live-preview Markdown files in your browser."

src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use notify_debouncer_full::DebouncedEvent;
44
use notify_debouncer_full::{DebounceEventResult, new_debouncer, notify::*};
55
use pulldown_cmark::Options;
66
use std::path::Path;
7+
use std::time::{Duration, Instant};
78
use tokio::fs;
8-
use tokio::time::Duration;
99
mod args;
1010
use actix_web::App;
1111
use actix_web::HttpServer;
@@ -52,7 +52,7 @@ async fn ws_handler(
5252
let (watch_tx, mut notify_rx) = mpsc::unbounded_channel::<DebouncedEvent>();
5353

5454
let mut debouncer = new_debouncer(
55-
Duration::from_secs(2),
55+
Duration::from_millis(200),
5656
None,
5757
move |result: DebounceEventResult| match result {
5858
Ok(events) => events.into_iter().for_each(|event| {
@@ -73,19 +73,25 @@ async fn ws_handler(
7373
// Keep the watcher alive in this async task to keep the msg_stream alive
7474
let _watcher = debouncer;
7575

76+
// here we initially set last_sent to 1 second ago to allow the first update to be sent immediately
77+
let mut last_sent = Instant::now() - Duration::from_secs(1);
78+
7679
while let Some(event) = notify_rx.recv().await {
7780
if matches!(event.kind, EventKind::Remove(RemoveKind::File)) {
7881
eprintln!("File removed: {}", file_path);
7982
break;
8083
}
81-
if matches!(event.kind, EventKind::Modify(ModifyKind::Data(_))) {
84+
if matches!(event.kind, EventKind::Modify(ModifyKind::Data(_)))
85+
&& last_sent.elapsed() >= Duration::from_secs(1)
86+
{
8287
let latest_markdown = match get_markdown(&file_path).await {
8388
Ok(md) => md,
8489
Err(e) => {
8590
eprintln!("Error reading markdown file: {e}");
8691
continue;
8792
}
8893
};
94+
last_sent = Instant::now();
8995
if session.text(latest_markdown).await.is_err() {
9096
break;
9197
}

0 commit comments

Comments
 (0)