chore: refactor the backend #17

Merged
reya merged 10 commits from chore/refactor into master 2026-09-10 09:43:36 +00:00
5 changed files with 67 additions and 73 deletions
Showing only changes of commit 705d4ea046 - Show all commits
+15 -15
View File
@@ -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.
+10 -12
View File
@@ -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.
+7 -8
View File
@@ -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.
+15 -18
View File
@@ -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.
+20 -20
View File
@@ -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<PathBuf>, cx: &mut Context<Self>) -> 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<Result<(), Error>> = cx.spawn(async move |this, cx| {
let task: Task<Result<(), Error>> = 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();