From 67f8f5db49786cbf18deb6f9b3728d7e045d8d8d Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sun, 4 Oct 2026 12:08:06 +0700 Subject: [PATCH] chore: split cached and live loading of the group list --- crates/nip29/src/lib.rs | 45 +++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/crates/nip29/src/lib.rs b/crates/nip29/src/lib.rs index 18384202..cb6df300 100644 --- a/crates/nip29/src/lib.rs +++ b/crates/nip29/src/lib.rs @@ -126,6 +126,7 @@ impl GroupsRegistry { if event.signer_changed() { this.reset(cx); this.handle_notifications(cx); + this.load_cached(cx); this.load(cx); } })); @@ -133,6 +134,7 @@ impl GroupsRegistry { cx.defer(move |cx| { if let Err(error) = this.update(cx, |this, cx| { this.handle_notifications(cx); + this.load_cached(cx); if nostr.read(cx).current_user().is_some() { this.load(cx); @@ -349,20 +351,21 @@ impl GroupsRegistry { cx.notify(); } - fn load(&mut self, cx: &mut Context) { + fn load_cached(&mut self, cx: &mut Context) { let nostr = NostrRegistry::global(cx); + + if nostr.read(cx).current_user().is_none() { + return; + } + let client = nostr.read(cx).client(); let signer = nostr.read(cx).signer(); + // Show the cached groups before the relays answer. let task: Task> = cx.background_spawn(async move { let me = signer.get_public_key_async().await?; let filter = Filter::new().author(me).kind(Kind::SimpleGroups).limit(1); - client - .subscribe(ReqTarget::auto([filter.clone()])) - .with_id(list_subscription_id()) - .await?; - match client.database().query(filter).await?.into_iter().next() { Some(event) => GroupList::parse(&event), None => Ok(GroupList::default()), @@ -373,11 +376,37 @@ impl GroupsRegistry { .push(cx.spawn(async move |this, cx| match task.await { Ok(list) => { if let Err(error) = this.update(cx, |this, cx| this.merge(list, cx)) { - log::warn!("nip29: applying the group list failed: {error}"); + log::warn!("nip29: applying the cached group list failed: {error}"); } } - Err(error) => log::warn!("nip29: loading the group list failed: {error}"), + Err(error) => log::warn!("nip29: loading the cached group list failed: {error}"), })); + } + + fn load(&mut self, cx: &mut Context) { + let nostr = NostrRegistry::global(cx); + let client = nostr.read(cx).client(); + let signer = nostr.read(cx).signer(); + + let updates: Task> = cx.background_spawn(async move { + let me = signer.get_public_key_async().await?; + let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE); + let filter = Filter::new().author(me).kind(Kind::SimpleGroups).limit(1); + + client + .subscribe(ReqTarget::auto([filter])) + .with_id(list_subscription_id()) + .close_on(opts) + .await?; + + Ok(()) + }); + + self.tasks.push(cx.spawn(async move |_this, _cx| { + if let Err(error) = updates.await { + log::warn!("nip29: subscribing to the group list failed: {error}"); + } + })); self.watch_forks(cx); }