From 33c265d3cf268d97594ae6248adef8c8ff026f33 Mon Sep 17 00:00:00 2001 From: Julia Date: Sat, 4 Feb 2023 01:30:24 -0500 Subject: [PATCH] Abuse a closure instead of abusing options/iterators quite so much --- crates/call/src/room.rs | 4 +- crates/collab_ui/src/collab_titlebar_item.rs | 45 +++++++------------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/crates/call/src/room.rs b/crates/call/src/room.rs index d3d7facf3b..2f4fd56e47 100644 --- a/crates/call/src/room.rs +++ b/crates/call/src/room.rs @@ -459,10 +459,10 @@ impl Room { self.participant_user_ids.contains(&user_id) } - pub fn follows(&self, leader_id: PeerId) -> &[PeerId] { + pub fn follows(&self, leader_id: PeerId) -> Option<&[PeerId]> { self.follows_by_leader_id .get(&leader_id) - .map_or(&[], |v| v.as_slice()) + .map(|v| v.as_slice()) } async fn handle_room_updated( diff --git a/crates/collab_ui/src/collab_titlebar_item.rs b/crates/collab_ui/src/collab_titlebar_item.rs index 518df16169..f9b8df3488 100644 --- a/crates/collab_ui/src/collab_titlebar_item.rs +++ b/crates/collab_ui/src/collab_titlebar_item.rs @@ -569,11 +569,6 @@ impl CollabTitlebarItem { workspace.read(cx).is_following(peer_id) }); - let room = ActiveCall::global(cx).read(cx).room(); - let get_followers = |leader_id: PeerId| -> &[PeerId] { - room.map_or(&[], |room| room.read(cx).follows(leader_id)) - }; - let mut avatar_style; if let Some((_, location)) = peer_id_and_location { if let ParticipantLocation::SharedProject { project_id } = location { @@ -603,31 +598,21 @@ impl CollabTitlebarItem { Flex::row() .with_child(Self::render_face(avatar.clone(), avatar_style, theme)) .with_children( - peer_id_and_location - .map(|(peer_id, _)| { - get_followers(peer_id) - .into_iter() - .map(|&follower| { - room.map(|room| { - room.read(cx) - .remote_participant_for_peer_id(follower) - .map(|participant| { - participant.user.avatar.as_ref().map(|avatar| { - Self::render_face( - avatar.clone(), - avatar_style, - theme, - ) - }) - }) - .flatten() - }) - .flatten() - }) - .flatten() - }) - .into_iter() - .flatten(), + (|| { + let peer_id_and_location = peer_id_and_location?; + let peer_id = peer_id_and_location.0; + + let room = ActiveCall::global(cx).read(cx).room()?.read(cx); + let followers = room.follows(peer_id)?; + + Some(followers.into_iter().flat_map(|&follower| { + let participant = room.remote_participant_for_peer_id(follower)?; + let avatar = participant.user.avatar.as_ref()?; + Some(Self::render_face(avatar.clone(), avatar_style, theme)) + })) + })() + .into_iter() + .flatten(), ) .with_reversed_paint_order() .boxed()