Skip to content

Commit

Permalink
Fix watching for newly saved files
Browse files Browse the repository at this point in the history
  • Loading branch information
1whatleytay committed Nov 2, 2023
1 parent f7dc0f1 commit 43dda6f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src-tauri/src/access_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ffi::OsStr;
use std::fmt::{Display, Formatter};
use std::fs;
use std::future::Future;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use notify::event::ModifyKind;
Expand Down Expand Up @@ -111,10 +111,6 @@ impl AccessManager {
notify::recommended_watcher(move |event: Result<notify::Event, notify::Error>| {
let Ok(event) = event else { return };

let Some(path) = event.paths.first() else {
return
};

let mut details = details.get_silent();

let Some(path) = event.paths.first() else {
Expand Down Expand Up @@ -223,6 +219,12 @@ impl AccessManager {
Self::new(app, config)
}

pub fn watch(&self, path: &Path) {
if let Some(watcher) = &self.watcher {
watcher.lock().unwrap().watch(path, RecursiveMode::NonRecursive).ok();
}
}

pub fn sync(&self, items: HashSet<PathBuf>) {
let mut value = self.state.get_mut();

Expand Down Expand Up @@ -349,7 +351,12 @@ pub fn access_write_text(

state.state.get_silent().dismiss.insert(path.clone());

fs::write(&path, content).map_err(|_| AccessError::NotFound(path))
fs::write(&path, content).map_err(|_| AccessError::NotFound(path.clone()))?;

// Initial watch from select_open may fail because the file does not exist.
state.watch(&path);

Ok(())
}

#[tauri::command]
Expand Down

0 comments on commit 43dda6f

Please sign in to comment.