event cache: only emit an event update when there are, well, events to propagate

Mostly an optimization that was also revealed by the previous version of the test failing, because it would receive an initial update with empty
events.
This commit is contained in:
Benjamin Bouvier
2024-02-22 19:40:27 +01:00
parent 3ef5214587
commit 9f75552c9b
2 changed files with 25 additions and 12 deletions

View File

@@ -420,17 +420,24 @@ impl RoomEventCacheInner {
}
// Add all the events to the backend.
trace!("adding new events");
self.store.add_room_events(self.room.room_id(), timeline.events.clone()).await?;
if !timeline.events.is_empty()
|| timeline.prev_batch.is_some()
|| !ephemeral.is_empty()
|| !account_data.is_empty()
|| !ambiguity_changes.is_empty()
{
trace!("adding new events");
self.store.add_room_events(self.room.room_id(), timeline.events.clone()).await?;
// Propagate events to observers.
let _ = self.sender.send(RoomEventCacheUpdate::Append {
events: timeline.events,
prev_batch: timeline.prev_batch,
ephemeral,
account_data,
ambiguity_changes,
});
// Propagate events to observers.
let _ = self.sender.send(RoomEventCacheUpdate::Append {
events: timeline.events,
prev_batch: timeline.prev_batch,
ephemeral,
account_data,
ambiguity_changes,
});
}
Ok(())
}
@@ -444,6 +451,10 @@ impl RoomEventCacheInner {
/// Append a set of events to the room cache and storage, notifying
/// observers.
async fn append_events(&self, events: Vec<SyncTimelineEvent>) -> Result<()> {
if events.is_empty() {
return Ok(());
}
self.store.add_room_events(self.room.room_id(), events.clone()).await?;
let _ = self.sender.send(RoomEventCacheUpdate::Append {

View File

@@ -70,6 +70,9 @@ async fn test_add_initial_events() {
let event_cache = client.event_cache();
// Immediately subscribe the event cache to sync updates.
event_cache.subscribe().unwrap();
// If I sync and get informed I've joined The Room, but with no events,
let room_id = room_id!("!omelette:fromage.fr");
@@ -81,8 +84,7 @@ async fn test_add_initial_events() {
client.sync_once(Default::default()).await.unwrap();
server.reset().await;
// If I create a room event subscriber, just after subscribing,
event_cache.subscribe().unwrap();
// If I create a room event subscriber,
let (room_event_cache, _drop_handles) = event_cache.for_room(room_id).await.unwrap();
let (events, mut subscriber) = room_event_cache.subscribe().await.unwrap();