How to make good usage of the notify crate for responsive events?

I'm using the notify crate (stable version 4) to watch some files and re-run code when those change. Currently I'm using the default debounced watcher as follows.

let mut watcher = watcher(tx, Duration::from_secs(1))?;

However I struggle with how to make this more responsive. Is there a way to safely generate events as soon as files have been written instead of waiting for 1 second? Or is it safe to lower that value to something like 0.1s? I see in the doc and some notify examples that it is sometimes set to 35s. This is huge for the type of application I need. Sorry for all the newby questions on the subject.

It seems like the raw non-debounced version of the watcher can do this.

One issue I have is that I know next to nothing related to those system events. But I know for example that writing to a file is not instantaneous. That's why some editors write to temporary files and then move them. And the documentation of raw events is very sparse. Here is for example the documentation of the write event:

A WRITE event is emitted whenever a file has been written to.

If this is emitted when the write ends I guess it's fine but there doesn't seem to be any event related to when a write starts. Does that mean that it's not possible to detect those. It would be weird to me, because it means there would be no way of preventing reading access to a file being modified? Or maybe you can try read them but the OS will make that fail, etc.

I have so many unknowns, and it's not like I could try those and figure it out for Linux, Mac and Windows since I only have Linux at my disposal anyway.

So maybe two concrete questions:

  1. If I keep using debounced events, will bad things happen if I lower the debounced duration to something like 0.1s? what are the concrete implications of that duration?
  2. If I want to use raw events instead, does someone know of a nice open source project doing this so that I could have a look at how they do it?

The concrete implementation of the debounce system is that, whenever a raw event is triggered, it will keep the event around for the duration you give it. If the same event happens again the duration is reset such that you only get it once. Once the duration elapses, it gives you the event.

Setting it to a small value is fine.

1 Like

Thanks I'll try setting it to a low value then and we'll see what happens. It seems like events on macos will not be merged correctly but I think that's ok for what I do with it.