feat: add flush_vbranches method to Controller struct (#1383)

* feat: add flush_vbranches method to Controller struct

The `flush_vbranches` method has been added to the `Controller` struct. This method allows flushing virtual branches as a tree for a specific project. It iterates over the virtual branches of the project and calls the `flush_vbranch_as_tree` function to flush each virtual branch. This method is used in the `Handler` struct to flush virtual branches for a project before flushing the session.

* cleanup error handling

* make controller async for consistency and block on the caller site
This commit is contained in:
extrawurst 2023-10-16 13:18:01 +02:00 committed by GitHub
parent 8dacd371a0
commit ae3a6f3ebe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 11 deletions

View File

@ -17,6 +17,7 @@ use super::{
RemoteBranchFile, RemoteBranchFile,
}; };
#[derive(Clone)]
pub struct Controller { pub struct Controller {
local_data_dir: DataDir, local_data_dir: DataDir,
semaphores: Arc<tokio::sync::Mutex<HashMap<String, Semaphore>>>, semaphores: Arc<tokio::sync::Mutex<HashMap<String, Semaphore>>>,
@ -530,4 +531,21 @@ impl Controller {
let _permit = semaphore.acquire().await; let _permit = semaphore.acquire().await;
action() action()
} }
pub async fn flush_vbranches(&self, project_id: ProjectId) -> Result<(), Error> {
self.with_lock(&project_id, || {
self.with_verify_branch(&project_id, |gb_repository, project_repository, _| {
let vbranches = super::list_virtual_branches(gb_repository, project_repository)
.map_err(Error::Other)?;
for b in &vbranches {
super::flush_vbranch_as_tree(gb_repository, project_repository, &b.id, true)
.map_err(Error::Other)?;
}
Ok(())
})
})
.await
}
} }

View File

@ -1,12 +1,12 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use tauri::AppHandle; use tauri::{AppHandle, Manager};
use crate::{ use crate::{
gb_repository, gb_repository,
paths::DataDir, paths::DataDir,
project_repository, project_repository,
projects::{self, ProjectId}, projects::{self, ProjectId},
sessions, users, sessions, users, virtual_branches,
}; };
use super::events; use super::events;
@ -15,6 +15,7 @@ use super::events;
pub struct Handler { pub struct Handler {
local_data_dir: DataDir, local_data_dir: DataDir,
project_store: projects::Controller, project_store: projects::Controller,
vbrach_controller: virtual_branches::Controller,
users: users::Controller, users: users::Controller,
} }
@ -25,6 +26,10 @@ impl TryFrom<&AppHandle> for Handler {
Ok(Self { Ok(Self {
local_data_dir: DataDir::try_from(value)?, local_data_dir: DataDir::try_from(value)?,
project_store: projects::Controller::try_from(value)?, project_store: projects::Controller::try_from(value)?,
vbrach_controller: value
.state::<virtual_branches::Controller>()
.inner()
.clone(),
users: users::Controller::from(value), users: users::Controller::from(value),
}) })
} }
@ -51,15 +56,11 @@ impl Handler {
) )
.context("failed to open repository")?; .context("failed to open repository")?;
// TODO: fixme futures::executor::block_on(async {
// if let Some(branch_id) = &session.meta.branch { self.vbrach_controller
// virtual_branches::flush_vbranch_as_tree( .flush_vbranches(project_repository.project().id)
// &gb_repo, .await
// &project_repository, })?;
// branch_id,
// true,
// )?;
// }
let session = gb_repo let session = gb_repo
.flush_session(&project_repository, session, user.as_ref()) .flush_session(&project_repository, session, user.as_ref())