From 455dd331f3867f73f70f2b58e1e265d362b5dd6d Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 20:32:00 +0100 Subject: [PATCH] Add commands to close and disconnect repositories Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 22 +++++++++++++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 2 ++ mediarepo-api/src/tauri_plugin/state.rs | 11 ++++++++++ 3 files changed, 35 insertions(+) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index d6aaa59..2977030 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -3,6 +3,7 @@ use crate::tauri_plugin::commands::{ApiAccess, AppAccess}; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{save_settings, Repository}; use serde::{Deserialize, Serialize}; +use std::mem; use std::path::PathBuf; use tokio::fs; @@ -59,6 +60,27 @@ pub async fn add_repository( Ok(repositories) } +#[tauri::command] +pub async fn disconnect_repository(api_state: ApiAccess<'_>) -> PluginResult<()> { + api_state.disconnect().await; + + Ok(()) +} + +#[tauri::command] +pub async fn close_local_repository( + app_state: AppAccess<'_>, + api_state: ApiAccess<'_>, +) -> PluginResult<()> { + let mut active_repo = app_state.active_repo.write().await; + if let Some(path) = mem::take(&mut *active_repo).and_then(|r| r.path) { + app_state.stop_running_daemon(&path).await?; + } + api_state.disconnect().await; + + Ok(()) +} + #[tauri::command] pub async fn select_repository( name: String, diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 072107f..04825a5 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -44,6 +44,8 @@ impl MediarepoPlugin { start_daemon, check_daemon_running, stop_daemon, + disconnect_repository, + close_local_repository ]), } } diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 489a24c..e59ea80 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -24,6 +24,7 @@ impl ApiState { } } + /// Sets the active api client and disconnects the old one pub async fn set_api(&self, client: ApiClient) { let mut inner = self.inner.write().await; let old_client = mem::replace(&mut *inner, Some(client)); @@ -33,6 +34,16 @@ impl ApiState { } } + /// Disconnects the api client + pub async fn disconnect(&self) { + let mut inner = self.inner.write().await; + let old_client = mem::take(&mut *inner); + + if let Some(client) = old_client { + let _ = client.exit().await; + } + } + pub async fn api(&self) -> PluginResult { let inner = self.inner.read().await; inner