From ccd6a6fe0dab755d4461a3979ad8fdbe9d8bee39 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 10 Jul 2024 19:02:06 +0200 Subject: [PATCH] let UI trigger necessary updates on addition and removal --- Cargo.lock | 2 -- crates/gitbutler-project/Cargo.toml | 1 - crates/gitbutler-project/src/controller.rs | 31 ++-------------------- crates/gitbutler-tauri/Cargo.toml | 1 - crates/gitbutler-tauri/src/main.rs | 6 ++--- crates/gitbutler-tauri/src/projects.rs | 2 +- crates/gitbutler-tauri/src/window.rs | 25 ++--------------- 7 files changed, 7 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index def26cd2a..68e2452d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2175,7 +2175,6 @@ name = "gitbutler-project" version = "0.0.0" dependencies = [ "anyhow", - "async-trait", "git2", "gitbutler-error", "gitbutler-id", @@ -2292,7 +2291,6 @@ name = "gitbutler-tauri" version = "0.0.0" dependencies = [ "anyhow", - "async-trait", "backtrace", "console-subscriber", "dirs 5.0.1", diff --git a/crates/gitbutler-project/Cargo.toml b/crates/gitbutler-project/Cargo.toml index dec7fa001..eba8ec409 100644 --- a/crates/gitbutler-project/Cargo.toml +++ b/crates/gitbutler-project/Cargo.toml @@ -14,7 +14,6 @@ gitbutler-serde.workspace = true gitbutler-id.workspace = true gitbutler-storage.workspace = true git2.workspace = true -async-trait = "0.1.80" gix = { workspace = true, features = ["dirwalk", "credentials", "parallel"] } uuid.workspace = true tracing = "0.1.40" diff --git a/crates/gitbutler-project/src/controller.rs b/crates/gitbutler-project/src/controller.rs index 3b6ef3b7e..a803908a3 100644 --- a/crates/gitbutler-project/src/controller.rs +++ b/crates/gitbutler-project/src/controller.rs @@ -1,40 +1,22 @@ -use std::{ - path::{Path, PathBuf}, - sync::Arc, -}; +use std::path::{Path, PathBuf}; use anyhow::{bail, Context, Result}; -use async_trait::async_trait; use super::{storage, storage::UpdateRequest, Project, ProjectId}; use crate::AuthKey; use gitbutler_error::error; -#[async_trait] -pub trait Watchers { - /// Watch for filesystem changes on the given project. - fn watch(&self, project: &Project) -> anyhow::Result<()>; - /// Stop watching filesystem changes. - async fn stop(&self, id: ProjectId); -} - #[derive(Clone)] pub struct Controller { local_data_dir: PathBuf, projects_storage: storage::Storage, - watchers: Option>, } impl Controller { - pub fn new( - local_data_dir: PathBuf, - projects_storage: storage::Storage, - watchers: Option, - ) -> Self { + pub fn new(local_data_dir: PathBuf, projects_storage: storage::Storage) -> Self { Self { local_data_dir, projects_storage, - watchers: watchers.map(|w| Arc::new(w) as Arc<_>), } } @@ -43,7 +25,6 @@ impl Controller { Self { projects_storage: storage::Storage::from_path(&path), local_data_dir: path, - watchers: None, } } @@ -103,10 +84,6 @@ impl Controller { tracing::error!(project_id = %project.id, ?error, "failed to create {:?} on project add", project.gb_dir()); } - if let Some(watcher) = &self.watchers { - watcher.watch(&project)?; - } - Ok(project) } @@ -183,10 +160,6 @@ impl Controller { return Ok(()); }; - if let Some(watchers) = &self.watchers { - watchers.stop(id).await; - } - self.projects_storage .purge(project.id) .map_err(anyhow::Error::from)?; diff --git a/crates/gitbutler-tauri/Cargo.toml b/crates/gitbutler-tauri/Cargo.toml index a2cd6c190..cef99214d 100644 --- a/crates/gitbutler-tauri/Cargo.toml +++ b/crates/gitbutler-tauri/Cargo.toml @@ -23,7 +23,6 @@ gitbutler-testsupport.workspace = true [dependencies] anyhow = "1.0.86" -async-trait = "0.1.80" backtrace = { version = "0.3.72", optional = true } console-subscriber = "0.2.0" dirs = "5.0.1" diff --git a/crates/gitbutler-tauri/src/main.rs b/crates/gitbutler-tauri/src/main.rs index 6f8bd0243..a2e6d9a7d 100644 --- a/crates/gitbutler-tauri/src/main.rs +++ b/crates/gitbutler-tauri/src/main.rs @@ -101,8 +101,7 @@ fn main() { let storage_controller = storage::Storage::new(&app_data_dir); app_handle.manage(storage_controller.clone()); - let windows = WindowState::new(app_handle.clone()); - app_handle.manage(windows.clone()); + app_handle.manage(WindowState::new(app_handle.clone())); let projects_storage_controller = gitbutler_project::storage::Storage::new(storage_controller.clone()); app_handle.manage(projects_storage_controller.clone()); @@ -115,8 +114,7 @@ fn main() { let projects_controller = gitbutler_project::Controller::new( app_data_dir.clone(), - projects_storage_controller.clone(), - Some(windows.clone()) + projects_storage_controller.clone() ); app_handle.manage(projects_controller.clone()); diff --git a/crates/gitbutler-tauri/src/projects.rs b/crates/gitbutler-tauri/src/projects.rs index ea951c465..17592cd03 100644 --- a/crates/gitbutler-tauri/src/projects.rs +++ b/crates/gitbutler-tauri/src/projects.rs @@ -56,7 +56,7 @@ pub mod commands { id: ProjectId, ) -> Result<(), Error> { let project = controller.get(id).context("project not found")?; - Ok(watchers.watch(&project)?) + Ok(watchers.set_project_to_window(&project)?) } #[tauri::command(async)] diff --git a/crates/gitbutler-tauri/src/window.rs b/crates/gitbutler-tauri/src/window.rs index 0bcfebabc..148281a65 100644 --- a/crates/gitbutler-tauri/src/window.rs +++ b/crates/gitbutler-tauri/src/window.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use anyhow::{Context, Result}; use futures::executor::block_on; use gitbutler_project as projects; -use gitbutler_project::{Project, ProjectId}; +use gitbutler_project::ProjectId; use gitbutler_user as users; use tauri::{AppHandle, Manager}; use tracing::instrument; @@ -122,7 +122,7 @@ impl WindowState { /// Watch the project and assure no other instance can access it. #[instrument(skip(self, project), err(Debug))] - pub fn watch(&self, project: &projects::Project) -> Result<()> { + pub fn set_project_to_window(&self, project: &projects::Project) -> Result<()> { let mut lock_file = fslock::LockFile::open(project.gb_dir().join(WINDOW_LOCK_FILE).as_os_str())?; lock_file @@ -169,25 +169,4 @@ impl WindowState { Ok(()) } - - pub async fn stop(&self, project_id: ProjectId) { - let mut state = self.state.lock().await; - if state - .as_ref() - .map_or(false, |state| state.project_id == project_id) - { - state.take(); - } - } -} - -#[async_trait::async_trait] -impl projects::Watchers for WindowState { - fn watch(&self, project: &Project) -> Result<()> { - WindowState::watch(self, project) - } - - async fn stop(&self, id: ProjectId) { - WindowState::stop(self, id).await - } }