@@ -730,10 +730,7 @@ mod private {
730
730
/// pending diff updates with the result of this function.
731
731
///
732
732
/// 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 > {
737
734
let store_lock = self . store . lock ( ) . await ?;
738
735
739
736
// Attempt to load the last chunk.
@@ -762,7 +759,7 @@ mod private {
762
759
// updates the chunk identifier generator.
763
760
if let Err ( err) = self . events . replace_with ( last_chunk, chunk_identifier_generator) {
764
761
error ! ( "error when replacing the linked chunk: {err}" ) ;
765
- return self . reset ( ) . await . map ( Some ) ;
762
+ return self . reset_internal ( ) . await ;
766
763
}
767
764
768
765
// Let pagination observers know that we may have not reached the start of the
@@ -774,12 +771,7 @@ mod private {
774
771
// representation that we're doing this. Let's drain those store updates.
775
772
let _ = self . events . store_updates ( ) . take ( ) ;
776
773
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 ( ( ) )
783
775
}
784
776
785
777
/// Automatically shrink the room if there are no listeners, as
@@ -795,12 +787,21 @@ mod private {
795
787
if listener_count == 0 {
796
788
// If we are the last strong reference to the auto-shrinker, we can shrink the
797
789
// 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 ( ) ) )
799
792
} else {
800
793
Ok ( None )
801
794
}
802
795
}
803
796
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
+
804
805
/// Removes the bundled relations from an event, if they were present.
805
806
///
806
807
/// Only replaces the present if it contained bundled relations.
@@ -953,6 +954,18 @@ mod private {
953
954
/// with the result of this function.
954
955
#[ must_use = "Updates as `VectorDiff` must probably be propagated via `RoomEventCacheUpdate`" ]
955
956
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 > {
956
969
self . events . reset ( ) ;
957
970
958
971
self . propagate_changes ( ) . await ?;
@@ -964,13 +977,7 @@ mod private {
964
977
// TODO: likely must cancel any ongoing back-paginations too
965
978
self . pagination_status . set ( RoomPaginationStatus :: Idle { hit_timeline_start : false } ) ;
966
979
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 ( ( ) )
974
981
}
975
982
976
983
/// Returns a read-only reference to the underlying events.
@@ -1376,23 +1383,17 @@ mod private {
1376
1383
} )
1377
1384
. await ?;
1378
1385
1379
- let mut timeline_event_diffs = None ;
1380
-
1381
1386
if timeline. limited && prev_batch. is_some ( ) {
1382
1387
// If there was a previous batch token for a limited timeline, unload the chunks
1383
1388
// so it only contains the last one; otherwise, there might be a
1384
1389
// valid gap in between, and observers may not render it (yet).
1385
1390
//
1386
1391
// We must do this *after* the above call to `.with_events_mut`, so the new
1387
1392
// 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 ?;
1391
1394
}
1392
1395
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 ( ) ;
1396
1397
if !timeline_event_diffs. is_empty ( ) {
1397
1398
let _ = sender. send ( RoomEventCacheUpdate :: UpdateTimelineEvents {
1398
1399
diffs : timeline_event_diffs,
@@ -2508,10 +2509,9 @@ mod timed_tests {
2508
2509
. state
2509
2510
. write ( )
2510
2511
. await
2511
- . shrink_to_last_chunk ( )
2512
+ . force_shrink_to_last_chunk ( )
2512
2513
. await
2513
- . expect ( "shrinking should succeed" )
2514
- . unwrap ( ) ;
2514
+ . expect ( "shrinking should succeed" ) ;
2515
2515
2516
2516
// We receive updates about the changes to the linked chunk.
2517
2517
assert_eq ! ( diffs. len( ) , 2 ) ;
0 commit comments