From 29a8e2c8acdc57a61673e8c4fbb6d21191869b36 Mon Sep 17 00:00:00 2001 From: reya Date: Fri, 24 Jan 2025 14:03:31 +0700 Subject: [PATCH] wip: refactor --- Cargo.lock | 10 ++++++++++ crates/app/Cargo.toml | 1 + crates/app/src/main.rs | 36 ++---------------------------------- crates/state/Cargo.toml | 10 ++++++++++ crates/state/src/lib.rs | 28 ++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 crates/state/Cargo.toml create mode 100644 crates/state/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index abe0677..b230840 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1095,6 +1095,7 @@ dependencies = [ "serde", "serde_json", "smol", + "state", "tokio", "tracing-subscriber", "ui", @@ -4905,6 +4906,15 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" +[[package]] +name = "state" +version = "0.1.0" +dependencies = [ + "dirs 5.0.1", + "nostr-sdk", + "tokio", +] + [[package]] name = "static_assertions" version = "1.1.0" diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml index c5247c1..40da91e 100644 --- a/crates/app/Cargo.toml +++ b/crates/app/Cargo.toml @@ -11,6 +11,7 @@ path = "src/main.rs" [dependencies] ui = { path = "../ui" } common = { path = "../common" } +state = { path = "../state" } gpui.workspace = true reqwest_client.workspace = true diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index cec2f2b..5d91075 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -3,7 +3,6 @@ use common::constants::{ ALL_MESSAGES_SUB_ID, APP_ID, APP_NAME, FAKE_SIG, KEYRING_SERVICE, METADATA_DELAY, NEW_MESSAGE_SUB_ID, }; -use dirs::config_dir; use gpui::{ actions, point, px, size, App, AppContext, Bounds, SharedString, TitlebarOptions, VisualContext, WindowBounds, WindowKind, WindowOptions, @@ -11,14 +10,9 @@ use gpui::{ #[cfg(target_os = "linux")] use gpui::{WindowBackgroundAppearance, WindowDecorations}; use nostr_sdk::prelude::*; +use state::{get_client, initialize_client}; use states::{app::AppRegistry, chat::ChatRegistry}; -use std::{ - collections::HashSet, - fs, - str::FromStr, - sync::{Arc, OnceLock}, - time::Duration, -}; +use std::{collections::HashSet, str::FromStr, sync::Arc, time::Duration}; use tokio::{ sync::{mpsc, Mutex}, time::sleep, @@ -31,9 +25,6 @@ mod states; mod views; actions!(main_menu, [Quit]); -actions!(app, [ReloadMetadata]); - -static CLIENT: OnceLock = OnceLock::new(); #[derive(Clone)] pub enum Signal { @@ -43,29 +34,6 @@ pub enum Signal { Eose, } -pub fn initialize_client() { - // Setup app data folder - let config_dir = config_dir().expect("Config directory not found"); - let _ = fs::create_dir_all(config_dir.join("Coop/")); - - // Setup database - let lmdb = NostrLMDB::open(config_dir.join("Coop/nostr")).expect("Database is NOT initialized"); - - // Client options - let opts = Options::new() - .gossip(true) - .max_avg_latency(Duration::from_secs(2)); - - // Setup Nostr Client - let client = ClientBuilder::default().database(lmdb).opts(opts).build(); - - CLIENT.set(client).expect("Client is already initialized!"); -} - -pub fn get_client() -> &'static Client { - CLIENT.get().expect("Client is NOT initialized!") -} - #[tokio::main] async fn main() { tracing_subscriber::fmt::init(); diff --git a/crates/state/Cargo.toml b/crates/state/Cargo.toml new file mode 100644 index 0000000..43f8fcb --- /dev/null +++ b/crates/state/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "state" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +nostr-sdk.workspace = true +tokio.workspace = true +dirs.workspace = true diff --git a/crates/state/src/lib.rs b/crates/state/src/lib.rs new file mode 100644 index 0000000..a83c50a --- /dev/null +++ b/crates/state/src/lib.rs @@ -0,0 +1,28 @@ +use dirs::config_dir; +use nostr_sdk::prelude::*; +use std::{fs, sync::OnceLock, time::Duration}; + +static CLIENT: OnceLock = OnceLock::new(); + +pub fn initialize_client() { + // Setup app data folder + let config_dir = config_dir().expect("Config directory not found"); + let _ = fs::create_dir_all(config_dir.join("Coop/")); + + // Setup database + let lmdb = NostrLMDB::open(config_dir.join("Coop/nostr")).expect("Database is NOT initialized"); + + // Client options + let opts = Options::new() + .gossip(true) + .max_avg_latency(Duration::from_secs(2)); + + // Setup Nostr Client + let client = ClientBuilder::default().database(lmdb).opts(opts).build(); + + CLIENT.set(client).expect("Client is already initialized!"); +} + +pub fn get_client() -> &'static Client { + CLIENT.get().expect("Client is NOT initialized!") +}