Skip to content

Commit

Permalink
feat: Add per-room sync status tracking for Freenet subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Dec 25, 2024
1 parent 50aba91 commit 82d64a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
32 changes: 28 additions & 4 deletions ui/src/components/app/freenet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,22 @@ impl FreenetApiSynchronizer {

use_effect(move || {
{
let rooms_ref = rooms.read();
for room in rooms_ref.map.values() {
let mut rooms = rooms.write();
for room in rooms.map.values_mut() {
// Subscribe to room if not already subscribed
if matches!(room.sync_status, RoomSyncStatus::Unsubscribed) {
room.sync_status = RoomSyncStatus::Subscribing;
let subscribe_request = ContractRequest::Subscribe {
key: room.contract_key,
summary: None,
};
let mut sender = request_sender.clone();
wasm_bindgen_futures::spawn_local(async move {
if let Err(e) = sender.send(subscribe_request.into()).await {
log::error!("Failed to subscribe to room: {}", e);
}
});
}
let state_bytes = to_cbor_vec(&room.room_state);
let update_request = ContractRequest::Update {
key: room.contract_key,
Expand Down Expand Up @@ -141,7 +155,8 @@ impl FreenetApiSynchronizer {
&room_state
) {
log::error!("Failed to merge room state: {}", e);
*SYNC_STATUS.write() = SyncStatus::Error(e);
*SYNC_STATUS.write() = SyncStatus::Error(e.clone());
room_data.sync_status = RoomSyncStatus::Error(e);
}
}
} else {
Expand All @@ -162,7 +177,8 @@ impl FreenetApiSynchronizer {
&Some(delta)
) {
log::error!("Failed to apply delta: {}", e);
*SYNC_STATUS.write() = SyncStatus::Error(e);
*SYNC_STATUS.write() = SyncStatus::Error(e.clone());
room_data.sync_status = RoomSyncStatus::Error(e);
}
}
}
Expand All @@ -172,6 +188,14 @@ impl FreenetApiSynchronizer {
},
HostResponse::Ok => {
*SYNC_STATUS.write() = SyncStatus::Connected;
// Update room status to Subscribed when subscription succeeds
let mut rooms = use_context::<Signal<Rooms>>();
let mut rooms = rooms.write();
for room in rooms.map.values_mut() {
if matches!(room.sync_status, RoomSyncStatus::Subscribing) {
room.sync_status = RoomSyncStatus::Subscribed;
}
}
},
_ => {}
}
Expand Down
10 changes: 10 additions & 0 deletions ui/src/room_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ pub enum SendMessageError {
UserBanned,
}

#[derive(Clone, PartialEq, Debug)]
pub enum RoomSyncStatus {
Unsubscribed,
Subscribing,
Subscribed,
Error(String),
}

#[derive(Clone, PartialEq)]
pub struct RoomData {
pub owner_vk: VerifyingKey,
pub room_state: ChatRoomStateV1,
pub self_sk: SigningKey,
pub contract_key: ContractKey,
pub sync_status: RoomSyncStatus,
}

impl RoomData {
Expand Down Expand Up @@ -136,6 +145,7 @@ impl Rooms {
room_state,
self_sk,
contract_key,
sync_status: RoomSyncStatus::Unsubscribed,
};

self.map.insert(owner_vk, room_data);
Expand Down

0 comments on commit 82d64a1

Please sign in to comment.