From 705d4ea0461ea626c3b4be4d8aa97d10ea12f0e9 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Thu, 10 Sep 2026 14:04:29 +0700 Subject: [PATCH] clean up --- crates/signed_state/src/backend.rs | 30 ++++++++++----------- crates/signed_state/src/checkouts.rs | 22 +++++++-------- crates/signed_state/src/profile.rs | 15 +++++------ crates/signed_state/src/repo.rs | 33 +++++++++++------------ crates/signed_state/src/repos.rs | 40 ++++++++++++++-------------- 5 files changed, 67 insertions(+), 73 deletions(-) diff --git a/crates/signed_state/src/backend.rs b/crates/signed_state/src/backend.rs index 24babd5..43765a2 100644 --- a/crates/signed_state/src/backend.rs +++ b/crates/signed_state/src/backend.rs @@ -130,6 +130,7 @@ impl Backend { // Collect everything else that arrives within the debounce window. let deadline = Instant::now() + PUMP_DEBOUNCE; + loop { let now = Instant::now(); if now >= deadline { @@ -152,12 +153,11 @@ impl Backend { } } + // Collect and emit the collected events. let batch = std::mem::take(&mut pending); - if this - .update(cx, |_, cx| cx.emit(BackendEvent::NostrUpdate(batch))) - .is_err() - { - break; + + if let Err(e) = this.update(cx, |_, cx| cx.emit(BackendEvent::NostrUpdate(batch))) { + log::warn!("failed to emit nostr update: {e}"); } } @@ -166,22 +166,22 @@ impl Backend { pump.detach(); - let this = Self { - client, - signer, - current_user: None, - sync_progress: None, - passphrase_required: false, - pushing_repos: cx.new(|_| HashSet::new()), - }; - + // Bootstrap the client. let weak = cx.entity().downgrade(); cx.defer(move |cx| { if let Err(error) = weak.update(cx, |this, cx| this.bootstrap(cx)) { log::warn!("backend dropped before bootstrap could run: {error}"); } }); - this + + Self { + client, + signer, + current_user: None, + sync_progress: None, + passphrase_required: false, + pushing_repos: cx.new(|_| HashSet::new()), + } } /// Bootstrap the client. diff --git a/crates/signed_state/src/checkouts.rs b/crates/signed_state/src/checkouts.rs index c14950b..723e978 100644 --- a/crates/signed_state/src/checkouts.rs +++ b/crates/signed_state/src/checkouts.rs @@ -153,7 +153,16 @@ impl CheckoutsStore { })); } - let store = Self { + if !cfg!(target_arch = "wasm32") { + let weak = cx.entity().downgrade(); + cx.defer(move |cx| { + if let Err(error) = weak.update(cx, |this, cx| this.refresh(cx)) { + log::warn!("checkouts store dropped before initial refresh could run: {error}"); + } + }); + } + + Self { by_repo: HashMap::new(), statuses: HashMap::new(), status_requested: HashSet::new(), @@ -164,18 +173,7 @@ impl CheckoutsStore { local_pending: false, last_full_sync: None, _subscriptions: subscriptions, - }; - - if !cfg!(target_arch = "wasm32") { - let weak = cx.entity().downgrade(); - cx.defer(move |cx| { - if let Err(error) = weak.update(cx, |this, cx| this.refresh(cx)) { - log::warn!("checkouts store dropped before initial refresh could run: {error}"); - } - }); } - - store } /// Remember a successful local-checkout use. diff --git a/crates/signed_state/src/profile.rs b/crates/signed_state/src/profile.rs index 7e91087..a525282 100644 --- a/crates/signed_state/src/profile.rs +++ b/crates/signed_state/src/profile.rs @@ -123,20 +123,19 @@ impl ProfileStore { }) .detach(); - let store = Self { - profiles: HashMap::new(), - seen: RefCell::new(HashSet::new()), - sender, - _subscription: subscription, - }; - let weak = cx.entity().downgrade(); cx.defer(move |cx| { if let Err(error) = weak.update(cx, |this, cx| this.load(cx)) { log::warn!("profile store dropped before initial load could run: {error}"); } }); - store + + Self { + profiles: HashMap::new(), + seen: RefCell::new(HashSet::new()), + sender, + _subscription: subscription, + } } /// Get a profile. diff --git a/crates/signed_state/src/repo.rs b/crates/signed_state/src/repo.rs index 636c0b4..2bd907b 100644 --- a/crates/signed_state/src/repo.rs +++ b/crates/signed_state/src/repo.rs @@ -127,7 +127,20 @@ impl RepoStore { } }); - let store = Self { + let weak = cx.entity().downgrade(); + cx.defer(move |cx| { + let result = weak.update(cx, |this, cx| { + this.subscribe_remote(cx); + this.connect_announced_relays(&announced_relays, cx); + this.refresh(cx); + }); + + if let Err(error) = result { + log::warn!("repo store dropped before bootstrap could run: {error}"); + } + }); + + Self { addr, announcement: None, head: None, @@ -148,23 +161,7 @@ impl RepoStore { root_fetches: HashSet::new(), refresh: RefreshGate::default(), _subscription: subscription, - }; - - let weak = cx.entity().downgrade(); - cx.defer(move |cx| { - let result = weak.update(cx, |this, cx| { - this.subscribe_remote(cx); - // The announcement we opened the repo from may already list its relays. - // Connect to them right away. - // Do not wait for the bootstrap fetch to return the same event. - this.connect_announced_relays(&announced_relays, cx); - this.refresh(cx); - }); - if let Err(error) = result { - log::warn!("repo store dropped before bootstrap could run: {error}"); - } - }); - store + } } /// Returns the repository's address. diff --git a/crates/signed_state/src/repos.rs b/crates/signed_state/src/repos.rs index 0ece0ad..b4a88f7 100644 --- a/crates/signed_state/src/repos.rs +++ b/crates/signed_state/src/repos.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use std::time::Duration; use anyhow::Error; -use gpui::{App, AppContext, Context, Entity, Global, Subscription}; +use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task}; use nostr_sdk::prelude::*; use signed_core::{Announcement, Deletions, RepoAddr, filters, repo_addr}; use signed_git::find_git_repos; @@ -40,20 +40,19 @@ impl LocalReposStore { /// Create a store scanning `roots` right away. pub fn new(roots: Vec, cx: &mut Context) -> Self { - let store = Self { - roots: Arc::new(roots), - repos: Arc::new(Vec::new()), - scanning: false, - scan_dirty: false, - }; - let weak = cx.entity().downgrade(); cx.defer(move |cx| { if let Err(error) = weak.update(cx, |this, cx| this.rescan(cx)) { log::warn!("local repos store dropped before initial scan could run: {error}"); } }); - store + + Self { + roots: Arc::new(roots), + repos: Arc::new(Vec::new()), + scanning: false, + scan_dirty: false, + } } /// Forget a repository that has just been published to NIP-34. @@ -74,6 +73,7 @@ impl LocalReposStore { self.scan_dirty = true; return; } + if self.roots.is_empty() { return; } @@ -82,6 +82,7 @@ impl LocalReposStore { cx.notify(); let roots = self.roots.clone(); + let work = cx.background_spawn(async move { let mut repos = Vec::new(); for root in roots.iter() { @@ -92,7 +93,7 @@ impl LocalReposStore { repos }); - let task: gpui::Task> = cx.spawn(async move |this, cx| { + let task: Task> = cx.spawn(async move |this, cx| { let repos = work.await; let again = this.update(cx, |this, cx| { this.repos = Arc::new(repos); @@ -111,6 +112,7 @@ impl LocalReposStore { Ok(()) }); + task.detach(); } } @@ -215,14 +217,6 @@ impl RepoListStore { } }); - let store = Self { - announcements: Arc::new(Vec::new()), - last_activity: Arc::new(HashMap::new()), - counts: Arc::new(HashMap::new()), - refresh: RefreshGate::default(), - _subscription: subscription, - }; - let weak = cx.entity().downgrade(); cx.defer(move |cx| { let result = weak.update(cx, |this, cx| { @@ -235,7 +229,14 @@ impl RepoListStore { log::warn!("repo list store dropped before bootstrap could run: {error}"); } }); - store + + Self { + announcements: Arc::new(Vec::new()), + last_activity: Arc::new(HashMap::new()), + counts: Arc::new(HashMap::new()), + refresh: RefreshGate::default(), + _subscription: subscription, + } } /// The announcements of `user`, newest first. @@ -279,7 +280,6 @@ impl RepoListStore { cx.spawn(async move |this, cx| { cx.background_executor().timer(REFRESH_DEBOUNCE).await; - this.update(cx, |this, cx| this.run_refresh(cx)) }) .detach();