Merge pull request #1540 from gitbutlerapp/refactor-deltas-controller

Refactor deltas controller
This commit is contained in:
Nikita Galaiko 2023-11-08 12:41:43 +01:00 committed by GitHub
commit 2510e5b075
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 39 deletions

View File

@ -4,7 +4,7 @@ use anyhow::{Context, Result};
use tauri::{AppHandle, Manager};
use crate::{
bookmarks, deltas, gb_repository,
bookmarks, gb_repository,
git::{self, diff},
keys,
paths::DataDir,
@ -24,7 +24,6 @@ pub struct App {
keys: keys::Controller,
watchers: watcher::Watchers,
sessions_database: sessions::Database,
deltas_database: deltas::Database,
bookmarks_database: bookmarks::Database,
}
@ -51,7 +50,6 @@ impl TryFrom<&AppHandle> for App {
users: users::Controller::from(value),
watchers: value.state::<watcher::Watchers>().inner().clone(),
sessions_database: sessions::Database::from(value),
deltas_database: deltas::Database::from(value),
bookmarks_database: bookmarks::Database::from(value),
})
}
@ -224,17 +222,6 @@ impl App {
.map_err(Error::Other)
}
pub fn list_session_deltas(
&self,
project_id: &ProjectId,
session_id: &SessionId,
paths: &Option<Vec<&str>>,
) -> Result<HashMap<String, Vec<deltas::Delta>>, Error> {
self.deltas_database
.list_by_project_id_session_id(project_id, session_id, paths)
.map_err(Error::Other)
}
pub fn git_wd_diff(
&self,
project_id: &ProjectId,

View File

@ -5,8 +5,8 @@ use futures::executor::block_on;
use tauri::{generate_context, Manager};
use gblib::{
analytics, app, assets, commands, database, github, keys, logs, projects, sessions, storage,
users, virtual_branches, watcher, zip,
analytics, app, assets, commands, database, deltas, github, keys, logs, projects, sessions,
storage, users, virtual_branches, watcher, zip,
};
fn main() {
@ -118,6 +118,9 @@ fn main() {
.expect("failed to initialize zipc controller ");
tauri_app.manage(zipper);
let deltas_controller = deltas::Controller::from(&app_handle);
app_handle.manage(deltas_controller);
let sessions_controller = sessions::Controller::try_from(&app_handle)
.expect("failed to initialize sessions controller");
app_handle.manage(sessions_controller);
@ -153,7 +156,6 @@ fn main() {
.plugin(tauri_plugin_single_instance::init(|_, _, _| {}))
.plugin(tauri_plugin_context_menu::init())
.invoke_handler(tauri::generate_handler![
commands::list_deltas,
commands::list_session_files,
commands::git_remote_branches,
commands::git_remote_branches_data,
@ -178,6 +180,7 @@ fn main() {
projects::commands::delete_project,
projects::commands::list_projects,
sessions::commands::list_sessions,
deltas::commands::list_deltas,
virtual_branches::commands::list_virtual_branches,
virtual_branches::commands::create_virtual_branch,
virtual_branches::commands::commit_virtual_branch,

View File

@ -5,7 +5,7 @@ use tauri::Manager;
use tracing::instrument;
use crate::{
app, assets, bookmarks, deltas,
app, assets, bookmarks,
error::{Code, Error},
git, reader,
sessions::SessionId,
@ -47,27 +47,6 @@ pub async fn list_session_files(
Ok(files)
}
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn list_deltas(
handle: tauri::AppHandle,
project_id: &str,
session_id: &str,
paths: Option<Vec<&str>>,
) -> Result<HashMap<String, Vec<deltas::Delta>>, Error> {
let app = handle.state::<app::App>();
let session_id = session_id.parse().map_err(|_| Error::UserError {
message: "Malformed session id".to_string(),
code: Code::Validation,
})?;
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let deltas = app.list_session_deltas(&project_id, &session_id, &paths)?;
Ok(deltas)
}
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn git_wd_diff(

View File

@ -0,0 +1,41 @@
use std::collections::HashMap;
use tauri::{AppHandle, Manager};
use tracing::instrument;
use crate::error::{Code, Error};
use super::{controller::ListError, Controller, Delta};
impl From<ListError> for Error {
fn from(value: ListError) -> Self {
match value {
ListError::Other(error) => {
tracing::error!(?error);
Error::Unknown
}
}
}
}
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn list_deltas(
handle: AppHandle,
project_id: &str,
session_id: &str,
paths: Option<Vec<&str>>,
) -> Result<HashMap<String, Vec<Delta>>, Error> {
let session_id = session_id.parse().map_err(|_| Error::UserError {
message: "Malformed session id".to_string(),
code: Code::Validation,
})?;
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
handle
.state::<Controller>()
.list_by_session_id(&project_id, &session_id, &paths)
.map_err(Into::into)
}

View File

@ -0,0 +1,38 @@
use std::collections::HashMap;
use tauri::AppHandle;
use crate::{projects::ProjectId, sessions::SessionId};
use super::{database, Delta};
pub struct Controller {
database: database::Database,
}
impl From<&AppHandle> for Controller {
fn from(handle: &AppHandle) -> Self {
Self {
database: database::Database::from(handle),
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum ListError {
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl Controller {
pub fn list_by_session_id(
&self,
project_id: &ProjectId,
session_id: &SessionId,
paths: &Option<Vec<&str>>,
) -> Result<HashMap<String, Vec<Delta>>, ListError> {
self.database
.list_by_project_id_session_id(project_id, session_id, paths)
.map_err(Into::into)
}
}

View File

@ -1,3 +1,4 @@
mod controller;
mod database;
mod delta;
mod document;
@ -5,6 +6,9 @@ mod operations;
mod reader;
mod writer;
pub mod commands;
pub use controller::Controller;
pub use database::Database;
pub use delta::Delta;
pub use document::Document;