Skip to content

Commit 224e437

Browse files
committed
refactor(event cache): simplify handling of previous-batch token in handle_backpagination too
1 parent 8a9cae4 commit 224e437

File tree

1 file changed

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

1 file changed

+20
-22
lines changed

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ mod private {
14071407
pub async fn handle_backpagination(
14081408
&mut self,
14091409
events: Vec<TimelineEvent>,
1410-
new_gap: Option<Gap>,
1410+
mut new_gap: Option<Gap>,
14111411
prev_gap_id: Option<ChunkIdentifier>,
14121412
sender: &Sender<RoomEventCacheUpdate>,
14131413
) -> Result<BackPaginationOutcome, EventCacheError> {
@@ -1444,17 +1444,16 @@ mod private {
14441444
} else {
14451445
// All new events are duplicated, they can all be ignored.
14461446
events.clear();
1447+
// The gap can be ditched too, as it won't be useful to backpaginate any
1448+
// further.
1449+
new_gap = None;
14471450
};
14481451

14491452
self.with_events_mut(false, |room_events| {
14501453
// Reverse the order of the events as `/messages` has been called with `dir=b`
14511454
// (backwards). The `RoomEvents` API expects the first event to be the oldest.
14521455
// Let's re-order them for this block.
1453-
let reversed_events = events
1454-
.iter()
1455-
.rev()
1456-
.cloned()
1457-
.collect::<Vec<_>>();
1456+
let reversed_events = events.iter().rev().cloned().collect::<Vec<_>>();
14581457

14591458
let first_event_pos = room_events.events().next().map(|(item_pos, _)| item_pos);
14601459

@@ -1467,9 +1466,11 @@ mod private {
14671466

14681467
trace!("replacing previous gap with the back-paginated events");
14691468

1470-
// Replace the gap with the events we just deduplicated. This might get rid of the
1471-
// underlying gap, if the conditions are favorable to us.
1472-
room_events.replace_gap_at(reversed_events.clone(), gap_id)
1469+
// Replace the gap with the events we just deduplicated. This might get rid of
1470+
// the underlying gap, if the conditions are favorable to
1471+
// us.
1472+
room_events
1473+
.replace_gap_at(reversed_events.clone(), gap_id)
14731474
.expect("gap_identifier is a valid chunk id we read previously")
14741475
} else if let Some(pos) = first_event_pos {
14751476
// No prior gap, but we had some events: assume we need to prepend events
@@ -1493,20 +1494,17 @@ mod private {
14931494

14941495
// And insert the new gap if needs be.
14951496
//
1496-
// We only do this when at least one new, non-duplicated event, has been added to
1497-
// the chunk. Otherwise it means we've back-paginated all the known events.
1498-
if !all_duplicates {
1499-
if let Some(new_gap) = new_gap {
1500-
if let Some(new_pos) = insert_new_gap_pos {
1501-
room_events
1502-
.insert_gap_at(new_gap, new_pos)
1503-
.expect("events_chunk_pos represents a valid chunk position");
1504-
} else {
1505-
room_events.push_gap(new_gap);
1506-
}
1497+
// We only do this when at least one new, non-duplicated event, has been added
1498+
// to the chunk. Otherwise it means we've back-paginated all the
1499+
// known events.
1500+
if let Some(new_gap) = new_gap {
1501+
if let Some(new_pos) = insert_new_gap_pos {
1502+
room_events
1503+
.insert_gap_at(new_gap, new_pos)
1504+
.expect("events_chunk_pos represents a valid chunk position");
1505+
} else {
1506+
room_events.push_gap(new_gap);
15071507
}
1508-
} else {
1509-
debug!("not storing previous batch token, because we deduplicated all new back-paginated events");
15101508
}
15111509

15121510
reversed_events

0 commit comments

Comments
 (0)