From ba6e158ee29e494d252df417cd93e3172b20abfd Mon Sep 17 00:00:00 2001 From: Qdust41 Date: Sun, 19 Apr 2026 01:06:40 -0400 Subject: [PATCH] moved the AppState to lib.rs and refactored accordingly --- src-tauri/src/commands/chat.rs | 2 +- src-tauri/src/commands/user.rs | 9 ++------- src-tauri/src/db.rs | 19 +------------------ src-tauri/src/lib.rs | 13 +++++++++++-- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/src-tauri/src/commands/chat.rs b/src-tauri/src/commands/chat.rs index 607b1ba..d8668a3 100644 --- a/src-tauri/src/commands/chat.rs +++ b/src-tauri/src/commands/chat.rs @@ -6,9 +6,9 @@ use surrealdb::Notification; use tauri::{AppHandle, Emitter, State}; use uuid::Uuid; -use crate::db::AppState; use crate::error::{into_err, AppError}; use crate::models::{Message, MessageReaction, MessageReactionSummary, Room, User}; +use crate::AppState; const DEFAULT_PAGE_SIZE: i64 = 50; const MAX_PAGE_SIZE: i64 = 100; diff --git a/src-tauri/src/commands/user.rs b/src-tauri/src/commands/user.rs index 58bae53..95d48f2 100644 --- a/src-tauri/src/commands/user.rs +++ b/src-tauri/src/commands/user.rs @@ -1,9 +1,10 @@ use tauri::{AppHandle, State}; use tauri_plugin_store::StoreExt; -use crate::db::{AppState, SURREAL_ACCESS, SURREAL_DB, SURREAL_NS}; +use crate::db::{SURREAL_ACCESS, SURREAL_DB, SURREAL_NS}; use crate::error::{into_err, AppError}; use crate::models::{Contact, User}; +use crate::AppState; const SESSION_STORE: &str = "session.json"; const TOKEN_KEY: &str = "token"; @@ -86,7 +87,6 @@ pub async fn signup( }; let token = state.db.signup(credentials).await.map_err(into_err)?; let token_str = token.access.into_insecure_token(); - *state.token.lock().unwrap() = Some(token_str.clone()); save_token(&app_handle, &token_str)?; let mut result: Vec = state @@ -130,7 +130,6 @@ pub async fn signin( .map_err(into_err)? .access .into_insecure_token(); - *state.token.lock().unwrap() = Some(token_str.clone()); save_token(&app_handle, &token_str)?; Ok(token_str) } @@ -139,7 +138,6 @@ pub async fn signin( #[tauri::command] pub async fn signout(state: State<'_, AppState>, app_handle: AppHandle) -> Result<(), String> { state.db.invalidate().await.map_err(into_err)?; - *state.token.lock().unwrap() = None; clear_token(&app_handle)?; Ok(()) } @@ -162,8 +160,6 @@ pub async fn restore_session( .await { Ok(_) => { - *state.token.lock().unwrap() = Some(token_str); - let mut result: Vec = state .db .query("SELECT * FROM $auth") @@ -178,7 +174,6 @@ pub async fn restore_session( } Err(_) => { let _ = clear_token(&app_handle); - *state.token.lock().unwrap() = None; Err(AppError::Auth("session expired, please sign in again".into()).to_string()) } } diff --git a/src-tauri/src/db.rs b/src-tauri/src/db.rs index ef9c175..2e3460f 100644 --- a/src-tauri/src/db.rs +++ b/src-tauri/src/db.rs @@ -1,15 +1,11 @@ -use std::collections::HashMap; use std::env; -use std::sync::{Arc, LazyLock, Mutex}; +use std::sync::LazyLock; use surrealdb::engine::remote::ws::{Client, Ws, Wss}; use surrealdb::Surreal; -use tokio::task::JoinHandle; -use uuid::Uuid; use crate::error::AppError; -// This should set the env variable correctly both during compile time and runtime (for development). pub static SURREAL_URL: LazyLock = LazyLock::new(|| { option_env!("SURREAL_URL") .map(str::to_string) @@ -33,19 +29,6 @@ pub static SURREAL_ACCESS: LazyLock = LazyLock::new(|| { .unwrap_or_else(|| env::var("SURREAL_ACCESS").unwrap_or_else(|_| "account".to_string())) }); -pub struct AppState { - /// Long-lived authenticated WebSocket connection to SurrealDB. - pub db: Arc>, - /// JWT token from Record Auth signin. Used to re-authenticate on reconnect. - /// std::sync::Mutex is intentional: lock is acquired and released before any .await. - pub token: Mutex>, - /// Active LIVE query tasks keyed by their SurrealDB LIVE query UUID. - /// Abort a handle + KILL the query to clean up. - /// std::sync::Mutex is intentional: guards are never held across .await points. - /// If a future command needs to lock across .await, switch to tokio::sync::Mutex. - pub subscriptions: Mutex>>, -} - /// Connect to SurrealDB over WebSocket and select namespace/database. /// URL may include protocol prefix: `ws://`, `wss://`, `http://`, or `https://`. /// `wss://` and `https://` use TLS; others use plain WebSocket. diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index a56d076..9ee136f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,14 +1,24 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; +use surrealdb::engine::remote::ws::Client; +use surrealdb::Surreal; use tauri::Manager; +use tokio::task::JoinHandle; +use uuid::Uuid; mod commands; mod db; mod error; mod models; -use db::{init_db, AppState, SURREAL_DB, SURREAL_NS, SURREAL_URL}; +use db::{init_db, SURREAL_DB, SURREAL_NS, SURREAL_URL}; + +pub struct AppState { + pub db: Arc>, + /// std::sync::Mutex is intentional: guards are never held across .await points. + pub subscriptions: Mutex>>, +} #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { @@ -28,7 +38,6 @@ pub fn run() { let state = AppState { db: Arc::new(surreal), - token: Mutex::new(None), subscriptions: Mutex::new(HashMap::new()), };