Skip to content

Commit 8a9cae4

Browse files
committed
refactor(event cache): have even fewer methods return timelinediff updates
1 parent 22a15f1 commit 8a9cae4

File tree

1 file changed

+30
-30
lines changed
  • crates/matrix-sdk/src/event_cache/room

1 file changed

+30
-30
lines changed

crates/matrix-sdk/src/event_cache/room/mod.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -730,10 +730,7 @@ mod private {
730730
/// pending diff updates with the result of this function.
731731
///
732732
/// Otherwise, returns `None`.
733-
#[must_use = "Updates as `VectorDiff` must probably be propagated via `RoomEventCacheUpdate`"]
734-
pub(super) async fn shrink_to_last_chunk(
735-
&mut self,
736-
) -> Result<Option<Vec<VectorDiff<TimelineEvent>>>, EventCacheError> {
733+
pub(super) async fn shrink_to_last_chunk(&mut self) -> Result<(), EventCacheError> {
737734
let store_lock = self.store.lock().await?;
738735

739736
// Attempt to load the last chunk.
@@ -762,7 +759,7 @@ mod private {
762759
// updates the chunk identifier generator.
763760
if let Err(err) = self.events.replace_with(last_chunk, chunk_identifier_generator) {
764761
error!("error when replacing the linked chunk: {err}");
765-
return self.reset().await.map(Some);
762+
return self.reset_internal().await;
766763
}
767764

768765
// Let pagination observers know that we may have not reached the start of the
@@ -774,12 +771,7 @@ mod private {
774771
// representation that we're doing this. Let's drain those store updates.
775772
let _ = self.events.store_updates().take();
776773

777-
// However, we want to get updates as `VectorDiff`s, for the external listeners.
778-
// Check we're respecting the contract defined in the doc comment.
779-
let diffs = self.events.updates_as_vector_diffs();
780-
assert!(matches!(diffs[0], VectorDiff::Clear));
781-
782-
Ok(Some(diffs))
774+
Ok(())
783775
}
784776

785777
/// Automatically shrink the room if there are no listeners, as
@@ -795,12 +787,21 @@ mod private {
795787
if listener_count == 0 {
796788
// If we are the last strong reference to the auto-shrinker, we can shrink the
797789
// events data structure to its last chunk.
798-
self.shrink_to_last_chunk().await
790+
self.shrink_to_last_chunk().await?;
791+
Ok(Some(self.events.updates_as_vector_diffs()))
799792
} else {
800793
Ok(None)
801794
}
802795
}
803796

797+
#[cfg(test)]
798+
pub(crate) async fn force_shrink_to_last_chunk(
799+
&mut self,
800+
) -> Result<Vec<VectorDiff<TimelineEvent>>, EventCacheError> {
801+
self.shrink_to_last_chunk().await?;
802+
Ok(self.events.updates_as_vector_diffs())
803+
}
804+
804805
/// Removes the bundled relations from an event, if they were present.
805806
///
806807
/// Only replaces the present if it contained bundled relations.
@@ -953,6 +954,18 @@ mod private {
953954
/// with the result of this function.
954955
#[must_use = "Updates as `VectorDiff` must probably be propagated via `RoomEventCacheUpdate`"]
955956
pub async fn reset(&mut self) -> Result<Vec<VectorDiff<TimelineEvent>>, EventCacheError> {
957+
self.reset_internal().await?;
958+
959+
let diff_updates = self.events.updates_as_vector_diffs();
960+
961+
// Ensure the contract defined in the doc comment is true:
962+
debug_assert_eq!(diff_updates.len(), 1);
963+
debug_assert!(matches!(diff_updates[0], VectorDiff::Clear));
964+
965+
Ok(diff_updates)
966+
}
967+
968+
async fn reset_internal(&mut self) -> Result<(), EventCacheError> {
956969
self.events.reset();
957970

958971
self.propagate_changes().await?;
@@ -964,13 +977,7 @@ mod private {
964977
// TODO: likely must cancel any ongoing back-paginations too
965978
self.pagination_status.set(RoomPaginationStatus::Idle { hit_timeline_start: false });
966979

967-
let diff_updates = self.events.updates_as_vector_diffs();
968-
969-
// Ensure the contract defined in the doc comment is true:
970-
debug_assert_eq!(diff_updates.len(), 1);
971-
debug_assert!(matches!(diff_updates[0], VectorDiff::Clear));
972-
973-
Ok(diff_updates)
980+
Ok(())
974981
}
975982

976983
/// Returns a read-only reference to the underlying events.
@@ -1376,23 +1383,17 @@ mod private {
13761383
})
13771384
.await?;
13781385

1379-
let mut timeline_event_diffs = None;
1380-
13811386
if timeline.limited && prev_batch.is_some() {
13821387
// If there was a previous batch token for a limited timeline, unload the chunks
13831388
// so it only contains the last one; otherwise, there might be a
13841389
// valid gap in between, and observers may not render it (yet).
13851390
//
13861391
// We must do this *after* the above call to `.with_events_mut`, so the new
13871392
// events and gaps are properly persisted to storage.
1388-
1389-
// TODO(bnjbvr): could this method not return diff updates?
1390-
timeline_event_diffs = self.shrink_to_last_chunk().await?;
1393+
self.shrink_to_last_chunk().await?;
13911394
}
13921395

1393-
let timeline_event_diffs =
1394-
timeline_event_diffs.unwrap_or_else(|| self.events.updates_as_vector_diffs());
1395-
1396+
let timeline_event_diffs = self.events.updates_as_vector_diffs();
13961397
if !timeline_event_diffs.is_empty() {
13971398
let _ = sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {
13981399
diffs: timeline_event_diffs,
@@ -2508,10 +2509,9 @@ mod timed_tests {
25082509
.state
25092510
.write()
25102511
.await
2511-
.shrink_to_last_chunk()
2512+
.force_shrink_to_last_chunk()
25122513
.await
2513-
.expect("shrinking should succeed")
2514-
.unwrap();
2514+
.expect("shrinking should succeed");
25152515

25162516
// We receive updates about the changes to the linked chunk.
25172517
assert_eq!(diffs.len(), 2);

0 commit comments

Comments
 (0)