notify-rs / notify

🔭 Cross-platform filesystem notification library for Rust.
https://docs.rs/notify
2.77k stars 222 forks source link

Watch code is not run inside helper function #548

Closed wvhulle closed 1 year ago

wvhulle commented 1 year ago

System details

What you did (as detailed as you can)

I installed notify, added an file event listener to the directory and file data/file.txt. I put the event listener and attachment inside a helper function.

What you expected

I expected updates to the file be shown when the file is written to.

What happened

The attachment of the watcher in the helper function does not work.

To reproduce: cargo run --example main_bug. This does not show the output from the handler.

It only works if I plug the watch code inside the main function. Why can I not put the watch code in a helper function?

To test workaround cargo run --example main_working. This does show the output, but it is unclear to me why it is necessary to put the watch inside the main.

wvhulle commented 1 year ago

I have found the answer in https://github.com/notify-rs/notify/issues/ So the solution is to keep the watcher in scope? Is there any other cleaner solution?

0xpr03 commented 1 year ago

cleaner solution

What do you mean with that ? If you drop the watcher, it will deconstruct its runtime. There are various options by which you could keep it alive for the duration of your application. My guess is that you're currently blocked by some specific idea of how the flow of your application looks like and can't see the solution.