notify icon indicating copy to clipboard operation
notify copied to clipboard

`Modify()` gets triggered only once

Open thegeneralist01 opened this issue 11 months ago • 3 comments

Once a file (only a single file is being watched) gets modified, the Modify event gets triggered. Any consecutive modifications of the watched file do not result in the Modify event getting triggered. Not sure whether this is an issue with my code or not – help will be appreciated. Here is the code:

let (tx, rx) = mpsc::channel::<notify::Result<notify::Event>>();

let mut watcher =
    match notify::RecommendedWatcher::new(tx.clone(), notify::Config::default()) {
        Ok(w) => w,
        Err(e) => {
            eprintln!("Failed to create watcher: {:?}", e);
            return;
        }
    };

if let Err(e) = watcher.watch(
    Path::new(&config_path.clone()),
    notify::RecursiveMode::Recursive,
) {
    eprintln!("Failed to watch config file: {:?}", e);
    return;
}

// Block forever, printing out events as they come in
for res in rx {
    if let Err(e) = res {
        println!("watch error: {:?}", e);
        continue;
    }

    let event = res.unwrap();
    println!("event: {:?}", event);

    match event.kind {
        notify::EventKind::Modify(_) => {
            // Refresh config
            let config_file = get_file(&config_path);
            let mut p = Parser::new(config_file);
            let cfg = p.parse();
            if let Ok(cfg) = cfg {
                let mut config_guard = config.lock().unwrap();
                *config_guard = cfg;
                println!("Config updated: {:#?}", config_guard);
            }
        }
        _ => {}
    }
}

thegeneralist01 avatar Jan 26 '25 19:01 thegeneralist01

What operating system are you using?

dfaust avatar Jan 29 '25 10:01 dfaust

@dfaust Linux, the distro is Mint

thegeneralist01 avatar Jan 29 '25 18:01 thegeneralist01

I'm having similar (or the same) issue. In my case it seems to be because if I edit the file with nvim it also triggers the Remove event (maybe it has to do with the way that nvim handles the file) and after that no further events are received. If I used different editor (such as Kate), it doesn't send the Remove event and I can receive further events.

BonnyAD9 avatar Jul 26 '25 15:07 BonnyAD9