From 41094d9e2df16141f162afae5ebbd91ea3519d9d Mon Sep 17 00:00:00 2001 From: "Ian Clarke (aider)" Date: Wed, 25 Dec 2024 01:51:47 -0600 Subject: [PATCH] feat: Add room state synchronization to Freenet node --- ui/src/components/app/freenet_api.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ui/src/components/app/freenet_api.rs b/ui/src/components/app/freenet_api.rs index 1ca5adc..51807be 100644 --- a/ui/src/components/app/freenet_api.rs +++ b/ui/src/components/app/freenet_api.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use futures::StreamExt; use common::room_state::ChatRoomParametersV1; -use dioxus::prelude::{Global, GlobalSignal, UnboundedSender, use_coroutine, use_context, Signal, Writable}; +use dioxus::prelude::{Global, GlobalSignal, UnboundedSender, use_coroutine, use_context, Signal, Writable, use_effect}; use freenet_scaffold::ComposableState; use ed25519_dalek::VerifyingKey; use freenet_stdlib::{ @@ -85,6 +85,28 @@ impl FreenetApiSynchronizer { ); log::info!("FreenetApi initialized"); + + // Watch for changes to Rooms signal + let rooms = use_context::>(); + let request_sender = request_sender.clone(); + + use_effect(move || { + let rooms_ref = rooms.read(); + for room in rooms_ref.map.values() { + let state_bytes = to_cbor_vec(&room.room_state); + let update_request = ContractRequest::Update { + key: room.contract_key, + data: freenet_stdlib::prelude::UpdateData::State(state_bytes.into()), + }; + let sender = request_sender.clone(); + wasm_bindgen_futures::spawn_local(async move { + if let Err(e) = sender.send(update_request.into()).await { + log::error!("Failed to send room update: {}", e); + } + }); + } + || () + }); // Main event loop loop {