Skip to content

Commit

Permalink
feat: load all connections at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
invm authored and Michael Ionov committed Jan 18, 2024
1 parent 80b7b7a commit 0939db7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src-tauri/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use chrono;
use log::error;
use noir::database::queries::get_all_connections;
use state::AppState;
use std::path::PathBuf;
use std::{fs, panic};
Expand Down Expand Up @@ -43,10 +44,12 @@ fn main() {
.manage(AsyncState {
tasks: Mutex::new(async_proc_input_tx),
connections: Default::default(),
all_connections: Default::default(),
})
.manage(AppState {
db: Default::default(),
connections: Default::default(),
all_connections: Default::default(),
})
.plugin(tauri_plugin_store::Builder::default().build())
.plugin(tauri_plugin_window_state::Builder::default().build())
Expand All @@ -62,6 +65,8 @@ fn main() {

let app_state: State<AppState> = handle.state();
let db = initialize_database().expect("Database initialize should succeed");
let all_connections = get_all_connections(&db)?;
*app_state.all_connections.lock().unwrap() = all_connections;
*app_state.db.lock().unwrap() = Some(db);

tauri::async_runtime::spawn(async move {
Expand Down
6 changes: 2 additions & 4 deletions src-tauri/src/handlers/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ pub fn delete_connection(app_handle: AppHandle, id: String) -> CommandResult<()>
}

#[command]
pub fn get_connections(app_handle: AppHandle) -> CommandResult<Vec<ConnectionConfig>> {
pub fn get_connections(mut app_handle: AppHandle) -> CommandResult<Vec<ConnectionConfig>> {
info!("get_connections");
app_handle
.db(queries::get_all_connections)
.map_err(Error::from)
Ok(app_handle.get_all_connections()?)
}

#[command]
Expand Down
23 changes: 21 additions & 2 deletions src-tauri/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ use tauri::{AppHandle, Manager, State};
use tokio::sync::{mpsc, Mutex};
use tracing::error;

use crate::{database::types::connection::InitiatedConnection, queues::query::QueryTask};
use crate::{
database::types::{config::ConnectionConfig, connection::InitiatedConnection},
queues::query::QueryTask,
};

pub struct AppState {
pub db: std::sync::Mutex<Option<Connection>>,
pub connections: std::sync::Mutex<HashMap<String, InitiatedConnection>>,
pub all_connections: std::sync::Mutex<Vec<ConnectionConfig>>,
}

pub struct AsyncState {
pub tasks: Mutex<mpsc::Sender<QueryTask>>,
pub connections: Mutex<HashMap<String, InitiatedConnection>>,
pub all_connections: std::sync::Mutex<Vec<ConnectionConfig>>,
}

pub trait ServiceAccess {
Expand All @@ -30,6 +35,7 @@ pub trait ServiceAccess {
fn update_connection(&self, conn: InitiatedConnection) -> Result<()>;
fn disconnect(&mut self, conn_id: &str) -> Result<()>;
fn connect(&mut self, conn: &InitiatedConnection) -> Result<()>;
fn get_all_connections(&mut self) -> Result<Vec<ConnectionConfig>>;
}

impl ServiceAccess for AppHandle {
Expand Down Expand Up @@ -102,4 +108,17 @@ impl ServiceAccess for AppHandle {
}
Ok(())
}
}

fn get_all_connections(&mut self) -> Result<Vec<ConnectionConfig>> {
let app_state: State<AppState> = self.state();
let mut binding = app_state.all_connections.lock();
let connection_guard = binding.as_mut();
match connection_guard {
Ok(connection_guard) => Ok(connection_guard.clone()),
Err(e) => {
error!("Error: {}", e);
Ok(vec![])
}
}
}
}

0 comments on commit 0939db7

Please sign in to comment.