This commit is contained in:
2026-01-09 16:43:02 +07:00
parent c7c10941be
commit 362b43e265

View File

@@ -63,11 +63,17 @@ pub struct ChatRegistry {
/// Loading status of the registry
loading: bool,
/// Tracking the status of unwrapping gift wrap events.
tracking_flag: Arc<AtomicBool>,
/// Channel's sender for communication between nostr and gpui
sender: Sender<NostrEvent>,
/// Handle notifications asynchronous task
notifications: Option<Task<Result<(), Error>>>,
/// Tasks for asynchronous operations
tasks: SmallVec<[Task<()>; 3]>,
tasks: Vec<Task<()>>,
/// Subscriptions
_subscriptions: SmallVec<[Subscription; 1]>,
@@ -89,68 +95,41 @@ impl ChatRegistry {
/// Create a new chat registry instance
fn new(cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let identity = nostr.read(cx).identity();
let device_signer = nostr.read(cx).device_signer();
// A flag to indicate if the registry is loading
let status = Arc::new(AtomicBool::new(true));
let tracking_flag = Arc::new(AtomicBool::new(true));
// Channel for communication between nostr and gpui
let (tx, rx) = flume::bounded::<NostrEvent>(2048);
let mut tasks = smallvec![];
let mut tasks = vec![];
let mut subscriptions = smallvec![];
let notifications =
Some(
cx.background_spawn({
let client = client.clone();
let device_signer = device_signer.read(cx).clone();
let loading = Arc::clone(&status);
let tx = tx.clone();
async move {
Self::handle_notifications(&client, &device_signer, &loading, &tx).await
subscriptions.push(
// Observe the identity
cx.observe(&identity, |this, state, cx| {
if state.read(cx).has_public_key() {
// Handle nostr notifications
this.handle_notifications(cx);
// Track unwrapping progress
this.tracking(cx);
}
}),
);
subscriptions.push(
// Observe the device signer state
cx.observe(&device_signer, {
let loading = Arc::clone(&status);
let tx = tx.clone();
move |this, state, cx| {
cx.observe(&device_signer, |this, state, cx| {
if state.read(cx).is_some() {
this.notifications = Some(cx.background_spawn({
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let device_signer = state.read(cx).clone();
let loading = Arc::clone(&loading);
let tx = tx.clone();
async move {
Self::handle_notifications(&client, &device_signer, &loading, &tx)
.await
}
}))
}
this.handle_notifications(cx);
}
}),
);
tasks.push(
// Handle unwrapping progress
cx.background_spawn(
async move { Self::unwrapping_status(&client, &status, &tx).await },
),
);
tasks.push(
// Handle new messages
// Update GPUI states
cx.spawn(async move |this, cx| {
while let Ok(message) = rx.recv_async().await {
match message {
@@ -181,18 +160,24 @@ impl ChatRegistry {
Self {
rooms: vec![],
loading: true,
notifications,
tracking_flag,
sender: tx.clone(),
notifications: None,
tasks,
_subscriptions: subscriptions,
}
}
async fn handle_notifications(
client: &Client,
device_signer: &Option<Arc<dyn NostrSigner>>,
loading: &Arc<AtomicBool>,
tx: &Sender<NostrEvent>,
) -> Result<(), Error> {
/// Handle nostr notifications
fn handle_notifications(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let device_signer = nostr.read(cx).device_signer().read(cx).clone();
let status = self.tracking_flag.clone();
let tx = self.sender.clone();
self.tasks.push(cx.background_spawn(async move {
let initialized_at = Timestamp::now();
let subscription_id = SubscriptionId::new(GIFTWRAP_SUBSCRIPTION);
@@ -218,7 +203,7 @@ impl ChatRegistry {
}
// Extract the rumor from the gift wrap event
match Self::extract_rumor(client, device_signer, event.as_ref()).await {
match Self::extract_rumor(&client, &device_signer, event.as_ref()).await {
Ok(rumor) => match rumor.created_at >= initialized_at {
true => {
// Check if the event is sent by coop
@@ -236,7 +221,7 @@ impl ChatRegistry {
}
}
false => {
loading.store(true, Ordering::Release);
status.store(true, Ordering::Release);
}
},
Err(e) => {
@@ -252,12 +237,20 @@ impl ChatRegistry {
_ => {}
}
}
Ok(())
}));
}
async fn unwrapping_status(client: &Client, status: &Arc<AtomicBool>, tx: &Sender<NostrEvent>) {
/// Tracking the status of unwrapping gift wrap events.
fn tracking(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let status = self.tracking_flag.clone();
let tx = self.sender.clone();
self.notifications = Some(cx.background_spawn(async move {
let loop_duration = Duration::from_secs(12);
let mut is_start_processing = false;
let mut total_loops = 0;
@@ -268,7 +261,12 @@ impl ChatRegistry {
if status.load(Ordering::Acquire) {
is_start_processing = true;
// Reset gift wrap processing flag
_ = status.compare_exchange(true, false, Ordering::Release, Ordering::Relaxed);
_ = status.compare_exchange(
true,
false,
Ordering::Release,
Ordering::Relaxed,
);
tx.send_async(NostrEvent::Unwrapping(true)).await.ok();
} else {
@@ -285,6 +283,7 @@ impl ChatRegistry {
}
smol::Timer::after(loop_duration).await;
}
}));
}
/// Get the loading status of the chat registry