Skip to content

Commit 40a4c9a

Browse files
committed
refactor(event cache): move pushing the live events into its own method in EventLinkedChunk
1 parent ad35895 commit 40a4c9a

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,27 @@ impl EventLinkedChunk {
298298
.find_map(|chunk| as_variant!(chunk.content(), ChunkContent::Gap(gap) => gap.clone()))
299299
}
300300

301+
/// Add the previous back-pagination token (if present), followed by the
302+
/// timeline events themselves.
303+
pub fn push_live_events(&mut self, new_gap: Option<Gap>, events: &[Event]) {
304+
if let Some(new_gap) = new_gap {
305+
// As a tiny optimization: remove the last chunk if it's an empty event
306+
// one, as it's not useful to keep it before a gap.
307+
let prev_chunk_to_remove = self.rchunks().next().and_then(|chunk| {
308+
(chunk.is_items() && chunk.num_items() == 0).then_some(chunk.identifier())
309+
});
310+
311+
self.push_gap(new_gap);
312+
313+
if let Some(prev_chunk_to_remove) = prev_chunk_to_remove {
314+
self.remove_empty_chunk_at(prev_chunk_to_remove)
315+
.expect("we just checked the chunk is there, and it's an empty item chunk");
316+
}
317+
}
318+
319+
self.push_events(events.to_vec());
320+
}
321+
301322
/// Finish a network back-pagination for this linked chunk by updating the
302323
/// in-memory linked chunk with the results.
303324
///

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

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,43 +1533,27 @@ mod private {
15331533
return Ok((false, Vec::new()));
15341534
}
15351535

1536+
let has_new_gap = prev_batch.is_some();
1537+
1538+
// If we've never waited for an initial previous-batch token, and we've now
1539+
// inserted a gap, no need to wait for a previous-batch token later.
1540+
if !self.waited_for_initial_prev_token && has_new_gap {
1541+
self.waited_for_initial_prev_token = true;
1542+
}
1543+
15361544
// Remove the old duplicated events.
15371545
//
15381546
// We don't have to worry the removals can change the position of the existing
15391547
// events, because we are pushing all _new_ `events` at the back.
15401548
self.remove_events(in_memory_duplicated_event_ids, in_store_duplicated_event_ids)
15411549
.await?;
15421550

1543-
// Add the previous back-pagination token (if present), followed by the timeline
1544-
// events themselves.
1545-
if let Some(prev_token) = &prev_batch {
1546-
// As a tiny optimization: remove the last chunk if it's an empty event
1547-
// one, as it's not useful to keep it before a gap.
1548-
let prev_chunk_to_remove =
1549-
self.room_linked_chunk.rchunks().next().and_then(|chunk| {
1550-
(chunk.is_items() && chunk.num_items() == 0).then_some(chunk.identifier())
1551-
});
1552-
1553-
self.room_linked_chunk.push_gap(Gap { prev_token: prev_token.clone() });
1554-
1555-
// If we've never waited for an initial previous-batch token, and we've now
1556-
// inserted a gap, no need to wait for a previous-batch token later.
1557-
if !self.waited_for_initial_prev_token && prev_batch.is_some() {
1558-
self.waited_for_initial_prev_token = true;
1559-
}
1560-
1561-
if let Some(prev_chunk_to_remove) = prev_chunk_to_remove {
1562-
self.room_linked_chunk
1563-
.remove_empty_chunk_at(prev_chunk_to_remove)
1564-
.expect("we just checked the chunk is there, and it's an empty item chunk");
1565-
}
1566-
}
1567-
1568-
self.room_linked_chunk.push_events(events.clone());
1551+
self.room_linked_chunk
1552+
.push_live_events(prev_batch.map(|prev_token| Gap { prev_token }), &events);
15691553

15701554
self.post_process_new_events(events, true).await?;
15711555

1572-
if timeline.limited && prev_batch.is_some() {
1556+
if timeline.limited && has_new_gap {
15731557
// If there was a previous batch token for a limited timeline, unload the chunks
15741558
// so it only contains the last one; otherwise, there might be a
15751559
// valid gap in between, and observers may not render it (yet).
@@ -1581,7 +1565,7 @@ mod private {
15811565

15821566
let timeline_event_diffs = self.room_linked_chunk.updates_as_vector_diffs();
15831567

1584-
Ok((prev_batch.is_some(), timeline_event_diffs))
1568+
Ok((has_new_gap, timeline_event_diffs))
15851569
}
15861570

15871571
#[must_use = "Propagate `VectorDiff` updates via `RoomEventCacheUpdate`"]

0 commit comments

Comments
 (0)