Skip to content

Commit

Permalink
fix(event cache): get the *most recent* pagination token, not the *ol…
Browse files Browse the repository at this point in the history
…dest* one
  • Loading branch information
bnjbvr committed Dec 3, 2024
1 parent cfc0aff commit 1473d60
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 4 additions & 4 deletions crates/matrix-sdk/src/event_cache/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ impl RoomPagination {
/// Otherwise, it will immediately skip.
#[doc(hidden)]
pub async fn get_or_wait_for_token(&self, wait_time: Option<Duration>) -> Option<String> {
fn get_oldest(events: &RoomEvents) -> Option<String> {
events.chunks().find_map(|chunk| match chunk.content() {
fn get_latest(events: &RoomEvents) -> Option<String> {
events.rchunks().find_map(|chunk| match chunk.content() {
ChunkContent::Gap(gap) => Some(gap.prev_token.clone()),
ChunkContent::Items(..) => None,
})
Expand All @@ -256,7 +256,7 @@ impl RoomPagination {
// Scope for the lock guard.
let state = self.inner.state.read().await;
// Fast-path: we do have a previous-batch token already.
if let Some(found) = get_oldest(state.events()) {
if let Some(found) = get_latest(state.events()) {
return Some(found);
}
// If we've already waited for an initial previous-batch token before,
Expand All @@ -275,7 +275,7 @@ impl RoomPagination {
let _ = timeout(wait_time, self.inner.pagination_batch_token_notifier.notified()).await;

let mut state = self.inner.state.write().await;
let token = get_oldest(state.events());
let token = get_latest(state.events());
state.waited_for_initial_prev_token = true;
token
}
Expand Down
16 changes: 13 additions & 3 deletions crates/matrix-sdk/src/event_cache/room/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use eyeball_im::VectorDiff;
pub use matrix_sdk_base::event_cache::{Event, Gap};
use matrix_sdk_base::{
event_cache::store::DEFAULT_CHUNK_CAPACITY,
linked_chunk::{AsVector, ObservableUpdates},
linked_chunk::{AsVector, IterBackward, ObservableUpdates},
};
use matrix_sdk_common::linked_chunk::{
Chunk, ChunkIdentifier, EmptyChunk, Error, Iter, LinkedChunk, Position,
Chunk, ChunkIdentifier, EmptyChunk, Error, LinkedChunk, Position,
};
use ruma::OwnedEventId;
use tracing::{debug, error, warn};
Expand Down Expand Up @@ -172,10 +172,20 @@ impl RoomEvents {
/// Iterate over the chunks, forward.
///
/// The oldest chunk comes first.
pub fn chunks(&self) -> Iter<'_, DEFAULT_CHUNK_CAPACITY, Event, Gap> {
#[cfg(test)]
pub fn chunks(
&self,
) -> matrix_sdk_common::linked_chunk::Iter<'_, DEFAULT_CHUNK_CAPACITY, Event, Gap> {
self.chunks.chunks()
}

/// Iterate over the chunks, backward.
///
/// The most recent chunk comes first.
pub fn rchunks(&self) -> IterBackward<'_, DEFAULT_CHUNK_CAPACITY, Event, Gap> {
self.chunks.rchunks()
}

/// Iterate over the events, backward.
///
/// The most recent event comes first.
Expand Down

0 comments on commit 1473d60

Please sign in to comment.