diff --git a/crates/matrix-sdk/src/event_cache/pagination.rs b/crates/matrix-sdk/src/event_cache/pagination.rs index 16b131c183..7a74514daa 100644 --- a/crates/matrix-sdk/src/event_cache/pagination.rs +++ b/crates/matrix-sdk/src/event_cache/pagination.rs @@ -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) -> Option { - fn get_oldest(events: &RoomEvents) -> Option { - events.chunks().find_map(|chunk| match chunk.content() { + fn get_latest(events: &RoomEvents) -> Option { + events.rchunks().find_map(|chunk| match chunk.content() { ChunkContent::Gap(gap) => Some(gap.prev_token.clone()), ChunkContent::Items(..) => None, }) @@ -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, @@ -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 } diff --git a/crates/matrix-sdk/src/event_cache/room/events.rs b/crates/matrix-sdk/src/event_cache/room/events.rs index 6313ac3d3f..f52de50e60 100644 --- a/crates/matrix-sdk/src/event_cache/room/events.rs +++ b/crates/matrix-sdk/src/event_cache/room/events.rs @@ -18,7 +18,7 @@ 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, @@ -169,11 +169,11 @@ impl RoomEvents { self.chunks.chunk_identifier(predicate) } - /// Iterate over the chunks, forward. + /// Iterate over the chunks, backward. /// - /// The oldest chunk comes first. - pub fn chunks(&self) -> Iter<'_, DEFAULT_CHUNK_CAPACITY, Event, Gap> { - self.chunks.chunks() + /// The most recent chunk comes first. + pub fn rchunks(&self) -> IterBackward<'_, DEFAULT_CHUNK_CAPACITY, Event, Gap> { + self.chunks.rchunks() } /// Iterate over the events, backward.