subscribe to file changes before sending them

This commit is contained in:
Nikita Galaiko 2023-03-22 09:12:37 +01:00
parent 217ede1771
commit fa3a6271f9
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D
2 changed files with 8 additions and 7 deletions

View File

@ -32,8 +32,9 @@ impl FileWatchers {
pub fn watch(
&mut self,
rtx: tokio::sync::mpsc::Sender<Event>,
project: projects::Project,
) -> Result<tokio::sync::broadcast::Receiver<Event>> {
) -> Result<()> {
let (tx, mut rx) = tokio::sync::mpsc::channel(32);
let mut watcher = notify::recommended_watcher(move |res| {
let _ = tx.try_send(res);
@ -44,7 +45,6 @@ impl FileWatchers {
let project = Arc::new(Mutex::new(project.clone()));
let (rtx, rrx) = tokio::sync::broadcast::channel::<Event>(32);
tauri::async_runtime::spawn(async move {
log::info!("{}: watching files", project.lock().unwrap().id);
@ -65,7 +65,7 @@ impl FileWatchers {
if let Err(e) = rtx.send(Event::FileChange((
project.clone(),
relative_file_path.to_path_buf(),
))) {
))).await {
log::error!(
"{}: failed to send file change event: {:#}",
project.id,
@ -78,7 +78,7 @@ impl FileWatchers {
log::info!("{}: stopped watching files", project.id);
});
Ok(rrx)
Ok(())
}
}

View File

@ -51,8 +51,6 @@ impl Watcher {
let repo = git2::Repository::open(project.path.clone())?;
repo.add_ignore_rule("*.lock")?;
let mut fsevents = self.files_watcher.watch(project.clone())?;
let shared_sender = Arc::new(sender.clone());
let shared_deltas_store = Arc::new(deltas_storage.clone());
let shared_lock_file = Arc::new(tokio::sync::Mutex::new(lock_file));
@ -65,11 +63,13 @@ impl Watcher {
sessions_storage,
)?;
let (fstx, mut fsevents) = tokio::sync::mpsc::channel::<files::Event>(32);
tauri::async_runtime::spawn(async move {
let sender = shared_sender;
let deltas_storage = shared_deltas_store;
let lock_file = shared_lock_file;
while let Ok(event) = fsevents.recv().await {
while let Some(event) = fsevents.recv().await {
match event {
files::Event::FileChange((project, path)) => {
if path.starts_with(Path::new(".git")) {
@ -94,6 +94,7 @@ impl Watcher {
}
}
});
self.files_watcher.watch(fstx, project.clone())?;
Ok(())
}