diff --git a/crates/gitbutler-tauri/src/events.rs b/crates/gitbutler-tauri/src/events.rs index 9e2ef6e63..02ac30c5f 100644 --- a/crates/gitbutler-tauri/src/events.rs +++ b/crates/gitbutler-tauri/src/events.rs @@ -14,15 +14,9 @@ pub struct Sender { } impl Sender { - pub fn from_app(app: &AppHandle) -> Result { - if let Some(sender) = app.try_state::() { - Ok(sender.inner().clone()) - } else { - let sender = Sender { - app_handle: app.clone(), - }; - app.manage(sender.clone()); - Ok(sender) + pub fn from_app(app: &AppHandle) -> Self { + Sender { + app_handle: app.clone(), } } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers.rs b/crates/gitbutler-tauri/src/watcher/handlers.rs index 9318f2ed3..ebf1794d2 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers.rs @@ -12,7 +12,7 @@ pub mod push_project_to_gitbutler; use std::time; use anyhow::{Context, Result}; -use tauri::{AppHandle, Manager}; +use tauri::AppHandle; use tracing::instrument; use super::events; @@ -36,31 +36,29 @@ pub struct Handler { impl Handler { pub fn from_app(app: &AppHandle) -> Result { - if let Some(handler) = app.try_state::() { - // TODO(ST): figure out of this protections are necessary - is this happening? - // `.manage()` can deal with duplication, but maybe there is side-effects? - Ok(handler.inner().clone()) - } else { - let handler = Handler { - git_file_change_handler: git_file_change::Handler::from_app(app)?, - flush_session_handler: flush_session::Handler::from_app(app)?, - fetch_gitbutler_handler: fetch_gitbutler_data::Handler::from_app(app)?, - push_gitbutler_handler: push_gitbutler_data::Handler::from_app(app)?, - analytics_handler: analytics_handler::Handler::from_app(app)?, - index_handler: index_handler::Handler::from_app(app)?, - - push_project_to_gitbutler: push_project_to_gitbutler::Handler::from_app(app)?, - calculate_vbranches_handler: caltulate_virtual_branches_handler::Handler::from_app( - app, - )?, - calculate_deltas_handler: calculate_deltas_handler::Handler::from_app(app)?, - filter_ignored_files_handler: filter_ignored_files::Handler::from_app(app)?, - events_sender: app_events::Sender::from_app(app)?, - }; - - app.manage(handler.clone()); - Ok(handler) - } + let app_data_dir = app + .path_resolver() + .app_data_dir() + .context("failed to get app data dir")?; + Ok(Handler { + git_file_change_handler: git_file_change::Handler::from_app(app, &app_data_dir), + flush_session_handler: flush_session::Handler::from_app(app, &app_data_dir), + fetch_gitbutler_handler: fetch_gitbutler_data::Handler::from_app(app, &app_data_dir), + push_gitbutler_handler: push_gitbutler_data::Handler::from_app(app, &app_data_dir), + analytics_handler: analytics_handler::Handler::from_app(app), + index_handler: index_handler::Handler::from_app(app, &app_data_dir), + push_project_to_gitbutler: push_project_to_gitbutler::Handler::from_app( + app, + &app_data_dir, + ), + calculate_vbranches_handler: caltulate_virtual_branches_handler::Handler::from_app(app), + calculate_deltas_handler: calculate_deltas_handler::Handler::from_app( + app, + &app_data_dir, + ), + filter_ignored_files_handler: filter_ignored_files::Handler::from_app(app), + events_sender: app_events::Sender::from_app(app), + }) } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/analytics_handler.rs b/crates/gitbutler-tauri/src/watcher/handlers/analytics_handler.rs index 9252103c5..a80a066d2 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/analytics_handler.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/analytics_handler.rs @@ -12,20 +12,14 @@ pub struct Handler { } impl Handler { - pub fn from_app(value: &AppHandle) -> Result { - if let Some(handler) = value.try_state::() { - Ok(handler.inner().clone()) - } else { - let client = value - .try_state::() - .map_or(analytics::Client::default(), |client| { - client.inner().clone() - }); - let users = value.state::().inner().clone(); - let handler = Handler { users, client }; - value.manage(handler.clone()); - Ok(handler) - } + pub fn from_app(app: &AppHandle) -> Self { + let client = app + .try_state::() + .map_or(analytics::Client::default(), |client| { + client.inner().clone() + }); + let users = app.state::().inner().clone(); + Handler { users, client } } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/calculate_deltas_handler.rs b/crates/gitbutler-tauri/src/watcher/handlers/calculate_deltas_handler.rs index 137e9b61e..4df5a98cf 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/calculate_deltas_handler.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/calculate_deltas_handler.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::{path, vec}; use anyhow::{Context, Result}; @@ -18,19 +19,11 @@ pub struct Handler { } impl Handler { - pub fn from_app(app: &AppHandle) -> Result { - if let Some(handler) = app.try_state::() { - Ok(handler.inner().clone()) - } else if let Some(app_data_dir) = app.path_resolver().app_data_dir() { - let handler = Self { - local_data_dir: app_data_dir, - projects: app.state::().inner().clone(), - users: app.state::().inner().clone(), - }; - app.manage(handler.clone()); - Ok(handler) - } else { - Err(anyhow::anyhow!("failed to get app data dir")) + pub fn from_app(app: &AppHandle, app_data_dir: impl Into) -> Self { + Self { + local_data_dir: app_data_dir.into(), + projects: app.state::().inner().clone(), + users: app.state::().inner().clone(), } } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/caltulate_virtual_branches_handler.rs b/crates/gitbutler-tauri/src/watcher/handlers/caltulate_virtual_branches_handler.rs index 488bb3ca3..3d45fa3ae 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/caltulate_virtual_branches_handler.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/caltulate_virtual_branches_handler.rs @@ -24,20 +24,13 @@ pub struct Handler { } impl Handler { - pub fn from_app(app: &AppHandle) -> std::result::Result { - if let Some(handler) = app.try_state::() { - Ok(handler.inner().clone()) - } else { - let vbranches = app.state::().inner().clone(); - let proxy = app.state::().inner().clone(); - let inner = InnerHandler { - vbranch_controller: vbranches, - assets_proxy: proxy, - }; - let handler = Handler::new(inner); - app.manage(handler.clone()); - Ok(handler) - } + pub fn from_app(app: &AppHandle) -> Self { + let vbranches = app.state::().inner().clone(); + let proxy = app.state::().inner().clone(); + Handler::new(InnerHandler { + vbranch_controller: vbranches, + assets_proxy: proxy, + }) } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/fetch_gitbutler_data.rs b/crates/gitbutler-tauri/src/watcher/handlers/fetch_gitbutler_data.rs index e5106ea41..7406a3482 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/fetch_gitbutler_data.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/fetch_gitbutler_data.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::{path, sync::Arc, time}; use anyhow::{Context, Result}; @@ -13,18 +14,10 @@ pub struct Handler { } impl Handler { - pub fn from_app(app: &AppHandle) -> std::result::Result { - if let Some(handler) = app.try_state::() { - Ok(handler.inner().clone()) - } else if let Some(app_data_dir) = app.path_resolver().app_data_dir() { - let projects = app.state::().inner().clone(); - let users = app.state::().inner().clone(); - let handler = Handler::new(app_data_dir, projects, users); - app.manage(handler.clone()); - Ok(handler) - } else { - Err(anyhow::anyhow!("failed to get app data dir")) - } + pub fn from_app(app: &AppHandle, app_data_dir: impl Into) -> Self { + let projects = app.state::().inner().clone(); + let users = app.state::().inner().clone(); + Handler::new(app_data_dir.into(), projects, users) } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/filter_ignored_files.rs b/crates/gitbutler-tauri/src/watcher/handlers/filter_ignored_files.rs index e9867013f..82118b6d9 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/filter_ignored_files.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/filter_ignored_files.rs @@ -22,16 +22,10 @@ pub struct Handler { } impl Handler { - pub fn from_app(app: &AppHandle) -> std::result::Result { - if let Some(handler) = app.try_state::() { - Ok(handler.inner().clone()) - } else { - let projects = app.state::().inner().clone(); - let inner = InnerHandler { projects }; - let handler = Handler::new(inner); - app.manage(handler.clone()); - Ok(handler) - } + pub fn from_app(app: &AppHandle) -> Self { + let projects = app.state::().inner().clone(); + let inner = InnerHandler { projects }; + Handler::new(inner) } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/flush_session.rs b/crates/gitbutler-tauri/src/watcher/handlers/flush_session.rs index d23d5caf3..352190f0b 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/flush_session.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/flush_session.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::{path, sync::Arc}; use anyhow::{Context, Result}; @@ -15,25 +16,17 @@ pub struct Handler { } impl Handler { - pub fn from_app(app: &AppHandle) -> std::result::Result { - if let Some(handler) = app.try_state::() { - Ok(handler.inner().clone()) - } else if let Some(app_data_dir) = app.path_resolver().app_data_dir() { - let projects = app.state::().inner().clone(); - let users = app.state::().inner().clone(); - // TODO(ST): see if one day this can be more self-contained so all this nesting isn't required - let inner = HandlerInner { - local_data_dir: app_data_dir, - project_store: projects, - users, - }; - let handler = Handler { - inner: Arc::new(inner.into()), - }; - app.manage(handler.clone()); - Ok(handler) - } else { - Err(anyhow::anyhow!("failed to get app data dir")) + pub fn from_app(app: &AppHandle, app_data_dir: impl Into) -> Self { + let projects = app.state::().inner().clone(); + let users = app.state::().inner().clone(); + // TODO(ST): see if one day this can be more self-contained so all this nesting isn't required + let inner = HandlerInner { + local_data_dir: app_data_dir.into(), + project_store: projects, + users, + }; + Handler { + inner: Arc::new(inner.into()), } } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/git_file_change.rs b/crates/gitbutler-tauri/src/watcher/handlers/git_file_change.rs index 93fe3935b..838a9bb95 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/git_file_change.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/git_file_change.rs @@ -1,4 +1,5 @@ use std::path; +use std::path::PathBuf; use anyhow::{Context, Result}; use gitbutler_core::{ @@ -19,18 +20,10 @@ pub struct Handler { } impl Handler { - pub fn from_app(value: &AppHandle) -> Result { - if let Some(handler) = value.try_state::() { - Ok(handler.inner().clone()) - } else if let Some(app_data_dir) = value.path_resolver().app_data_dir() { - let projects = value.state::().inner().clone(); - let users = value.state::().inner().clone(); - let handler = Handler::new(app_data_dir, projects, users); - value.manage(handler.clone()); - Ok(handler) - } else { - Err(anyhow::anyhow!("failed to get app data dir")) - } + pub fn from_app(app: &AppHandle, app_data_dir: impl Into) -> Self { + let projects = app.state::().inner().clone(); + let users = app.state::().inner().clone(); + Handler::new(app_data_dir.into(), projects, users) } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/index_handler.rs b/crates/gitbutler-tauri/src/watcher/handlers/index_handler.rs index 5c1f33842..7874f0310 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/index_handler.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/index_handler.rs @@ -1,4 +1,5 @@ use std::path; +use std::path::PathBuf; use anyhow::{Context, Result}; use gitbutler_core::{ @@ -22,25 +23,17 @@ pub struct Handler { } impl Handler { - pub fn from_app(app: &AppHandle) -> Result { - if let Some(handler) = app.try_state::() { - Ok(handler.inner().clone()) - } else if let Some(app_data_dir) = app.path_resolver().app_data_dir() { - let projects = app.state::().inner().clone(); - let users = app.state::().inner().clone(); - let sessions_database = app.state::().inner().clone(); - let deltas_database = app.state::().inner().clone(); - let handler = Handler { - local_data_dir: app_data_dir, - projects, - users, - sessions_database, - deltas_database, - }; - app.manage(handler.clone()); - Ok(handler) - } else { - Err(anyhow::anyhow!("failed to get app data dir")) + pub fn from_app(app: &AppHandle, app_data_dir: impl Into) -> Self { + let projects = app.state::().inner().clone(); + let users = app.state::().inner().clone(); + let sessions_database = app.state::().inner().clone(); + let deltas_database = app.state::().inner().clone(); + Handler { + local_data_dir: app_data_dir.into(), + projects, + users, + sessions_database, + deltas_database, } } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/push_gitbutler_data.rs b/crates/gitbutler-tauri/src/watcher/handlers/push_gitbutler_data.rs index a59b9d944..dd3df337e 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/push_gitbutler_data.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/push_gitbutler_data.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::{ path, sync::{Arc, Mutex, TryLockError}, @@ -18,24 +19,16 @@ pub struct Handler { } impl Handler { - pub fn from_app(value: &AppHandle) -> std::result::Result { - if let Some(handler) = value.try_state::() { - Ok(handler.inner().clone()) - } else if let Some(app_data_dir) = value.path_resolver().app_data_dir() { - let projects = value.state::().inner().clone(); - let users = value.state::().inner().clone(); - let inner = HandlerInner { - local_data_dir: app_data_dir, - projects, - users, - }; - let handler = Handler { - inner: Arc::new(inner.into()), - }; - value.manage(handler.clone()); - Ok(handler) - } else { - Err(anyhow::anyhow!("failed to get app data dir")) + pub fn from_app(app: &AppHandle, app_data_dir: impl Into) -> Self { + let projects = app.state::().inner().clone(); + let users = app.state::().inner().clone(); + let inner = HandlerInner { + local_data_dir: app_data_dir.into(), + projects, + users, + }; + Handler { + inner: Arc::new(inner.into()), } } } diff --git a/crates/gitbutler-tauri/src/watcher/handlers/push_project_to_gitbutler.rs b/crates/gitbutler-tauri/src/watcher/handlers/push_project_to_gitbutler.rs index a75309d09..7d6e6a4e0 100644 --- a/crates/gitbutler-tauri/src/watcher/handlers/push_project_to_gitbutler.rs +++ b/crates/gitbutler-tauri/src/watcher/handlers/push_project_to_gitbutler.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::{path, sync::Arc, time}; use anyhow::{Context, Result}; @@ -20,18 +21,10 @@ pub struct Handler { } impl Handler { - pub fn from_app(app: &AppHandle) -> std::result::Result { - if let Some(handler) = app.try_state::() { - Ok(handler.inner().clone()) - } else if let Some(app_data_dir) = app.path_resolver().app_data_dir() { - let projects = app.state::().inner().clone(); - let users = app.state::().inner().clone(); - let handler = Handler::new(app_data_dir, projects, users, 1000); - app.manage(handler.clone()); - Ok(handler) - } else { - Err(anyhow::anyhow!("failed to get app data dir")) - } + pub fn from_app(app: &AppHandle, app_data_dir: impl Into) -> Self { + let projects = app.state::().inner().clone(); + let users = app.state::().inner().clone(); + Handler::new(app_data_dir.into(), projects, users, 1000) } }