Add commands to close and disconnect repositories

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 9b457564bb
commit 455dd331f3

@ -3,6 +3,7 @@ use crate::tauri_plugin::commands::{ApiAccess, AppAccess};
use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::error::{PluginError, PluginResult};
use crate::tauri_plugin::settings::{save_settings, Repository}; use crate::tauri_plugin::settings::{save_settings, Repository};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::mem;
use std::path::PathBuf; use std::path::PathBuf;
use tokio::fs; use tokio::fs;
@ -59,6 +60,27 @@ pub async fn add_repository(
Ok(repositories) 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] #[tauri::command]
pub async fn select_repository( pub async fn select_repository(
name: String, name: String,

@ -44,6 +44,8 @@ impl<R: Runtime> MediarepoPlugin<R> {
start_daemon, start_daemon,
check_daemon_running, check_daemon_running,
stop_daemon, stop_daemon,
disconnect_repository,
close_local_repository
]), ]),
} }
} }

@ -24,6 +24,7 @@ impl ApiState {
} }
} }
/// Sets the active api client and disconnects the old one
pub async fn set_api(&self, client: ApiClient) { pub async fn set_api(&self, client: ApiClient) {
let mut inner = self.inner.write().await; let mut inner = self.inner.write().await;
let old_client = mem::replace(&mut *inner, Some(client)); 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<ApiClient> { pub async fn api(&self) -> PluginResult<ApiClient> {
let inner = self.inner.read().await; let inner = self.inner.read().await;
inner inner

Loading…
Cancel
Save