From f7e1866bda307be4438dffbd81ba3a250934ec9f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 23 Sep 2025 13:46:29 +0200 Subject: [PATCH] feat(sdk,ui): Automatically subscribe to `LatestEvents` if `EventCache` has subscribed. --- crates/matrix-sdk-ui/src/room_list_service/mod.rs | 14 ++++++++------ crates/matrix-sdk/src/event_cache/mod.rs | 5 +++++ crates/matrix-sdk/src/sync.rs | 4 ++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/matrix-sdk-ui/src/room_list_service/mod.rs b/crates/matrix-sdk-ui/src/room_list_service/mod.rs index 96ae21655..4964e1b4d 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/mod.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/mod.rs @@ -465,13 +465,15 @@ impl RoomListService { // Before subscribing, let's listen these rooms to calculate their latest // events. - let latest_events = self.client.latest_events().await; + if self.client.event_cache().has_subscribed() { + let latest_events = self.client.latest_events().await; - for room_id in room_ids { - if let Err(error) = latest_events.listen_to_room(room_id).await { - // Let's not fail the room subscription. Instead, emit a log because it's very - // unlikely to happen. - error!(?error, ?room_id, "Failed to listen to the latest event for this room"); + for room_id in room_ids { + if let Err(error) = latest_events.listen_to_room(room_id).await { + // Let's not fail the room subscription. Instead, emit a log because it's very + // unlikely to happen. + error!(?error, ?room_id, "Failed to listen to the latest event for this room"); + } } } diff --git a/crates/matrix-sdk/src/event_cache/mod.rs b/crates/matrix-sdk/src/event_cache/mod.rs index b37303e5d..bdb37d382 100644 --- a/crates/matrix-sdk/src/event_cache/mod.rs +++ b/crates/matrix-sdk/src/event_cache/mod.rs @@ -408,6 +408,11 @@ impl EventCache { info!("Auto-shrink linked chunk task has been closed, exiting"); } + /// Check whether [`EventCache::subscribe`] has been called. + pub fn has_subscribed(&self) -> bool { + self.inner.drop_handles.get().is_some() + } + /// Return a room-specific view over the [`EventCache`]. pub(crate) async fn for_room( &self, diff --git a/crates/matrix-sdk/src/sync.rs b/crates/matrix-sdk/src/sync.rs index 5894e91ed..e21fff47e 100644 --- a/crates/matrix-sdk/src/sync.rs +++ b/crates/matrix-sdk/src/sync.rs @@ -359,6 +359,10 @@ pub(crate) async fn subscribe_to_room_latest_events<'a, R>(client: &'a Client, r where R: Iterator, { + if !client.event_cache().has_subscribed() { + return; + } + let latest_events = client.latest_events().await; for room_id in room_ids {