@@ -4,8 +4,8 @@ use notify_debouncer_full::DebouncedEvent;
44use notify_debouncer_full:: { DebounceEventResult , new_debouncer, notify:: * } ;
55use pulldown_cmark:: Options ;
66use std:: path:: Path ;
7+ use std:: time:: { Duration , Instant } ;
78use tokio:: fs;
8- use tokio:: time:: Duration ;
99mod args;
1010use actix_web:: App ;
1111use 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