@@ -160,46 +160,47 @@ fn spawn_file_watcher(script_path: PathBuf, tx: mpsc::Sender<String>) {
160160 . watch ( & watch_dir, RecursiveMode :: Recursive )
161161 . expect ( "Failed to watch directory" ) ;
162162
163- // Keep watcher alive
164- let _watcher = watcher;
165-
166- // Set to past time so first event isn't debounced
167- let mut last_reload = std:: time:: Instant :: now ( ) - Duration :: from_secs ( 1 ) ;
168163 let debounce = Duration :: from_millis ( 100 ) ;
164+ let mut pending_reload = false ;
165+
166+ loop {
167+ let timeout = if pending_reload {
168+ debounce
169+ } else {
170+ Duration :: from_secs ( 86400 )
171+ } ;
169172
170- for result in raw_rx {
171- match result {
172- Ok ( event) => {
173- // Only react to modifications, not access/open events
173+ match raw_rx. recv_timeout ( timeout) {
174+ Ok ( Ok ( event) ) => {
174175 use notify:: EventKind ;
175- let is_modification = matches ! (
176+ let dominated_by = matches ! (
176177 event. kind,
177178 EventKind :: Create ( _) | EventKind :: Modify ( _) | EventKind :: Remove ( _)
178179 ) ;
179- if !is_modification {
180- continue ;
181- }
182-
183- // Debounce rapid events
184- if last_reload. elapsed ( ) < debounce {
185- continue ;
180+ if dominated_by {
181+ pending_reload = true ;
186182 }
187- last_reload = std:: time:: Instant :: now ( ) ;
188-
189- // Re-read and send the script file
190- match std:: fs:: read_to_string ( & script_path) {
191- Ok ( content) => {
192- if tx. blocking_send ( content) . is_err ( ) {
193- break ;
183+ }
184+ Ok ( Err ( e) ) => {
185+ eprintln ! ( "Watch error: {e:?}" ) ;
186+ }
187+ Err ( std:: sync:: mpsc:: RecvTimeoutError :: Timeout ) => {
188+ if pending_reload {
189+ pending_reload = false ;
190+ match std:: fs:: read_to_string ( & script_path) {
191+ Ok ( content) => {
192+ if tx. blocking_send ( content) . is_err ( ) {
193+ break ;
194+ }
195+ }
196+ Err ( e) => {
197+ eprintln ! ( "Error reading script file: {e}" ) ;
194198 }
195- }
196- Err ( e) => {
197- eprintln ! ( "Error reading script file: {e}" ) ;
198199 }
199200 }
200201 }
201- Err ( e ) => {
202- eprintln ! ( "Watch error: {e:?}" ) ;
202+ Err ( std :: sync :: mpsc :: RecvTimeoutError :: Disconnected ) => {
203+ break ;
203204 }
204205 }
205206 }
0 commit comments