From ceda732e273f3361e2073681635aa523ed397222 Mon Sep 17 00:00:00 2001 From: Werner Kroneman Date: Fri, 8 Dec 2023 21:04:54 +0100 Subject: [PATCH] Minor refactor, removing the rooms state with just the messages state. --- src/main.rs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index ea3508c..cbbfd45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -174,11 +174,9 @@ fn SendMessage<'a>(cx: Scope<'a, SendMessageProps>) -> Element<'a> { } } - async fn handle_event(event: xmpp::Event, agent: &mut xmpp::Agent, - rooms: &mut UseState>, - messages: &mut UseState>>) { + messages: &mut UseRef>>) { match event { xmpp::Event::JoinRoom(jid, conference) => { println!("Will auto-join room: {}", &jid); @@ -186,34 +184,34 @@ async fn handle_event(event: xmpp::Event, } xmpp::Event::RoomJoined(room_jid) => { println!("Joined room: {}", &room_jid); - messages.make_mut().entry(room_jid.clone()).or_insert(vec![]); - rooms.make_mut().push(room_jid); + messages.with_mut(move |m| { m.entry(room_jid.clone()).or_insert(vec![]); }); } xmpp::Event::RoomLeft(room_jid) => { println!("Left room: {}", &room_jid); - messages.make_mut().remove(&room_jid); - rooms.make_mut().retain(|x| x != &room_jid); + messages.with_mut(move |m| { m.remove(&room_jid); }); } xmpp::Event::ChatMessage(id, sender, body) => { println!("Message from {}: {}", &sender, &body.0); - messages.make_mut().entry(sender.clone()).or_insert(vec![]).push(Message { - sender: sender.to_string(), - body: body.0, + messages.with_mut(move |m| { + m.entry(sender.clone()).or_insert(vec![]).push(Message { + sender: sender.to_string(), + body: body.0, + }); }); } xmpp::Event::RoomMessage(id, room_jid, sender_nick, body) => { println!("Message in {} from {}: {}", &room_jid, &sender_nick, &body.0); - messages.make_mut().entry(room_jid).or_insert(vec![]).push(Message { + messages.with_mut(move |m| m.entry(room_jid.clone()).or_insert(vec![]).push(Message { sender: sender_nick, body: body.0, - }); + })); } xmpp::Event::RoomPrivateMessage(id, room_jid, sender_nick, body) => { println!("Private message in {} from {}: {}", &room_jid, &sender_nick, &body.0); - messages.make_mut().entry(room_jid).or_insert(vec![]).push(Message { + messages.with_mut(move |m| m.entry(room_jid.clone()).or_insert(vec![]).push(Message { sender: sender_nick, body: body.0, - }); + })); } _ => { log::debug!("Received unsupported event {:?}", event); @@ -259,12 +257,11 @@ fn RoomJoinWidget<'a>(cx: Scope<'a, RoomJoinProps>) -> Element<'a> { // define a component that renders a div with the text "Hello, world!" fn App(cx: Scope) -> Element { - let rooms = use_state(cx, || vec![]); - let messages = use_state(cx, || HashMap::new()); + let messages = use_ref(cx, || HashMap::new()); let current_room = use_state(cx, || None::); let cr = use_coroutine(cx, - |rx: UnboundedReceiver| run_xmpp(rooms.clone(), messages.clone(), rx), + |rx: UnboundedReceiver| run_xmpp(messages.clone(), rx), ); render! { @@ -285,7 +282,7 @@ fn App(cx: Scope) -> Element { "Mizah" } RoomList { - rooms: rooms.to_vec(), + rooms: messages.read().keys().cloned().collect(), on_room_picked: move |x:BareJid| { println!("Room selected: {:?}", x); current_room.set(Some(x)); @@ -304,7 +301,7 @@ fn App(cx: Scope) -> Element { } if let Some(room) = current_room.get() { - let messages = messages.get().get(&room).expect("Selected non-existant room").to_vec(); + let messages = messages.read().get(&room).expect("Selected non-existant room").to_vec(); rsx! { RoomView { @@ -334,8 +331,7 @@ fn App(cx: Scope) -> Element { } } -fn run_xmpp(mut rooms: UseState>, - mut messages: UseState>>, +fn run_xmpp(mut room_data: UseRef>>, mut commands: UnboundedReceiver) -> impl Future + Sized { async move { @@ -354,7 +350,7 @@ fn run_xmpp(mut rooms: UseState>, events = agent.wait_for_events() => { if let Some(events) = events { for event in events { - handle_event(event, &mut agent, &mut rooms, &mut messages).await; + handle_event(event, &mut agent, &mut room_data).await; } } else { info!("Disconnected");