chore: fix some performance issues (#6)
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m14s

Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
2026-02-14 02:01:49 +00:00
parent ecd7f6aa9b
commit e327178161
11 changed files with 667 additions and 706 deletions

View File

@@ -1,13 +1,11 @@
use std::collections::{HashMap, HashSet};
use anyhow::{anyhow, Context as AnyhowContext, Error};
use anyhow::{anyhow, Error};
use common::config_dir;
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task};
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
use smallvec::{smallvec, SmallVec};
use state::NostrRegistry;
const SETTINGS_IDENTIFIER: &str = "coop:settings";
pub fn init(cx: &mut App) {
AppSettings::set_global(cx.new(AppSettings::new), cx)
@@ -118,10 +116,7 @@ pub struct AppSettings {
values: Settings,
/// Event subscriptions
_subscriptions: SmallVec<[Subscription; 1]>,
/// Background tasks
tasks: SmallVec<[Task<Result<(), Error>>; 1]>,
_subscriptions: SmallVec<[Subscription; 2]>,
}
impl AppSettings {
@@ -136,9 +131,6 @@ impl AppSettings {
}
fn new(cx: &mut Context<Self>) -> Self {
let load_settings = Self::get_from_database(cx);
let mut tasks = smallvec![];
let mut subscriptions = smallvec![];
subscriptions.push(
@@ -148,24 +140,16 @@ impl AppSettings {
}),
);
tasks.push(
// Load the initial settings
cx.spawn(async move |this, cx| {
let settings = load_settings.await.unwrap_or(Settings::default());
log::info!("Settings: {settings:?}");
cx.defer(|cx| {
let settings = AppSettings::global(cx);
// Update the settings state
this.update(cx, |this, cx| {
this.set_settings(settings, cx);
})?;
Ok(())
}),
);
settings.update(cx, |this, cx| {
this.load(cx);
});
});
Self {
values: Settings::default(),
tasks,
_subscriptions: subscriptions,
}
}
@@ -176,74 +160,45 @@ impl AppSettings {
cx.notify();
}
/// Get settings from the database
fn get_from_database(cx: &App) -> Task<Result<Settings, Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
/// Load settings
fn load(&mut self, cx: &mut Context<Self>) {
let task: Task<Result<Settings, Error>> = cx.background_spawn(async move {
let path = config_dir().join(".settings");
cx.background_spawn(async move {
// Construct a filter to get the latest settings
let mut filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.identifier(SETTINGS_IDENTIFIER)
.limit(1);
// If the signer is available, get settings belonging to the current user
if let Some(signer) = client.signer() {
if let Ok(public_key) = signer.get_public_key().await {
// Push author to the filter
filter = filter.author(public_key);
}
}
if let Some(event) = client.database().query(filter).await?.first_owned() {
Ok(serde_json::from_str(&event.content)?)
if let Ok(content) = smol::fs::read_to_string(&path).await {
Ok(serde_json::from_str(&content)?)
} else {
Err(anyhow!("Not found"))
}
});
cx.spawn(async move |this, cx| {
let settings = task.await.unwrap_or(Settings::default());
// Update settings
this.update(cx, |this, cx| {
this.set_settings(settings, cx);
})
.ok();
})
}
/// Load settings
pub fn load(&mut self, cx: &mut Context<Self>) {
let task = Self::get_from_database(cx);
self.tasks.push(
// Run task in the background
cx.spawn(async move |this, cx| {
let settings = task.await?;
// Update settings
this.update(cx, |this, cx| {
this.set_settings(settings, cx);
})?;
Ok(())
}),
);
.detach();
}
/// Save settings
pub fn save(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let settings = self.values.clone();
self.tasks.push(cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let path = config_dir().join(".settings");
let content = serde_json::to_string(&settings)?;
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
.tag(Tag::identifier(SETTINGS_IDENTIFIER))
.build(public_key)
.sign(&Keys::generate())
.await?;
// Save event to the local database only
client.database().save_event(&event).await?;
// Write settings to file
smol::fs::write(&path, content).await?;
Ok(())
}));
});
task.detach();
}
/// Check if the given relay is already authenticated