let UI trigger necessary updates on addition and removal

This commit is contained in:
Sebastian Thiel 2024-07-10 19:02:06 +02:00
parent 00303704f4
commit ccd6a6fe0d
No known key found for this signature in database
GPG Key ID: 9CB5EE7895E8268B
7 changed files with 7 additions and 61 deletions

2
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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<Arc<dyn Watchers + Send + Sync>>,
}
impl Controller {
pub fn new(
local_data_dir: PathBuf,
projects_storage: storage::Storage,
watchers: Option<impl Watchers + Send + Sync + 'static>,
) -> 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)?;

View File

@ -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"

View File

@ -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());

View File

@ -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)]

View File

@ -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
}
}