feat(sdk): Don't remove and re-insert a room when it exists.

Prior to this patch, to check a room exists, it was removed from the
collection, then re-added if it exists. Instead of doing this `remove`
+ `insert` dance, we can simply use `get_mut`, which also returns an
`Option`. This patch does that, in addition to rewrite the code to use a
`match` instead of a `if` + `else`.
This commit is contained in:
Ivan Enderlin
2023-05-04 09:05:57 +02:00
parent 6eeee8ee53
commit 3e6dd1cecb

View File

@@ -325,23 +325,24 @@ impl SlidingSync {
room_data.timeline.drain(..).map(Into::into).collect()
};
if let Some(mut room) = rooms_map.remove(&room_id) {
match rooms_map.get_mut(&room_id) {
// The room existed before, let's update it.
Some(room) => {
room.update(room_data, timeline);
}
room.update(room_data, timeline);
rooms_map.insert(room_id.clone(), room);
} else {
// First time we need this room, let's create it.
rooms_map.insert(
room_id.clone(),
SlidingSyncRoom::new(
self.inner.client.clone(),
None => {
rooms_map.insert(
room_id.clone(),
room_data,
timeline,
),
);
SlidingSyncRoom::new(
self.inner.client.clone(),
room_id.clone(),
room_data,
timeline,
),
);
}
}
updated_rooms.push(room_id);