From a152f9c074a406bbff910ee3e1d4690ac324f219 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 3 Jun 2025 13:28:51 +0200 Subject: [PATCH] refactor(event cache): remove one caller of `with_events_mut` The actual code useful in `with_events_mut` and used in that function was to propagate the changes to the store and propagating diffs to observers. It often striked me as hacky to use this method to do that, so instead I'm proposing here to inline the useful bits. That way, `with_events_mut` is now clearly called only in two cases: after a sync, or after a successful network back-pagination. --- crates/matrix-sdk/src/event_cache/room/mod.rs | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/room/mod.rs b/crates/matrix-sdk/src/event_cache/room/mod.rs index 6015b4471..93dbca801 100644 --- a/crates/matrix-sdk/src/event_cache/room/mod.rs +++ b/crates/matrix-sdk/src/event_cache/room/mod.rs @@ -965,26 +965,21 @@ mod private { } // In-memory events. - let timeline_event_diffs = if !in_memory_events.is_empty() { - self.with_events_mut(|room_events| { - // `remove_events_by_position` sorts the positions by itself. - room_events - .remove_events_by_position( - in_memory_events - .into_iter() - .map(|(_event_id, position)| position) - .collect(), - ) - .expect("failed to remove an event"); + if in_memory_events.is_empty() { + // Nothing else to do, return early. + return Ok(Vec::new()); + } - vec![] - }) - .await? - } else { - Vec::new() - }; + // `remove_events_by_position` is responsible of sorting positions. + self.events + .remove_events_by_position( + in_memory_events.into_iter().map(|(_event_id, position)| position).collect(), + ) + .expect("failed to remove an event"); - Ok(timeline_event_diffs) + self.propagate_changes().await?; + + Ok(self.events.updates_as_vector_diffs()) } /// Propagate changes to the underlying storage.