Skip to content

Commit

Permalink
fix(ui): merge_stream_and_receiver gives priority to raw_stream.
Browse files Browse the repository at this point in the history
This patch rewrites `merge_stream_and_receiver` to switch the order
of `roominfo_update_recv` and `raw_stream`. The idea is to give the
priority to `raw_stream` since it will necessarily trigger the room
items recomputation.

This patch also remove the `for` loop with `Iterator::enumerate`, to
simply use `Iterator::position`: it's more compact and it removes a
`break` (it makes the code simpler to understand).

Finally, this patch renames `merged_stream` into `merged_streams`.
  • Loading branch information
Hywan committed Jul 1, 2024
1 parent e91af60 commit 98aae99
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions crates/matrix-sdk-ui/src/room_list_service/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ impl RoomList {
let (raw_values, raw_stream) = self.entries();

// Combine normal stream events with other updates from rooms
let merged_stream = merge_stream_and_receiver(raw_values.clone(), raw_stream, roominfo_update_recv.resubscribe());
let merged_streams = merge_stream_and_receiver(raw_values.clone(), raw_stream, roominfo_update_recv.resubscribe());

let (values, stream) = (raw_values, merged_stream)
let (values, stream) = (raw_values, merged_streams)
.filter(filter_fn)
.sort_by(new_sorter_or(vec![
Box::new(new_sorter_recency()),
Expand Down Expand Up @@ -195,34 +195,37 @@ fn merge_stream_and_receiver(

loop {
select! {
biased; // Prefer manual updates for easier test code
// We want to give priority on updates from `raw_stream` as it will necessarily trigger a “refresh” of the rooms.
biased;

Ok(update) = roominfo_update_recv.recv() => {
if !update.trigger_room_list_update {
continue;
}
diffs = raw_stream.next() => {
tracing::error!("merge streams: receive diff");

// Search list for the updated room
for (index, room) in raw_current_values.iter().enumerate() {
if room.room_id() == update.room_id {
let update = VectorDiff::Set { index, value: raw_current_values[index].clone() };
yield vec![update];
break;
if let Some(diffs) = diffs {
for diff in &diffs {
diff.clone().apply(&mut raw_current_values);
}
}
}

v = raw_stream.next() => {
if let Some(v) = v {
for change in &v {
change.clone().apply(&mut raw_current_values);
}
yield v;
yield diffs;
} else {
// Restart immediately, don't keep on waiting for the receiver
break;
}
}

Ok(update) = roominfo_update_recv.recv() => {
tracing::error!(trigger_update = ?update.trigger_room_list_update, "merge streams: receive roominfo update");

if !update.trigger_room_list_update {
continue;
}

// Search list for the updated room
if let Some(index) = raw_current_values.iter().position(|room| room.room_id() == update.room_id) {
let update = VectorDiff::Set { index, value: raw_current_values[index].clone() };
yield vec![update];
}
}
}
}
}
Expand Down

0 comments on commit 98aae99

Please sign in to comment.