Skip to content

Commit

Permalink
feat(sdk): Implement RoomEvents::reset, push_gap, `replace_gap_at…
Browse files Browse the repository at this point in the history
…` and `events`.

This patch implements the following wrapper methods (over
`LinkedChunk`): `push_gap`, `replace_gap_at` and `events`. This patch
also implements the `reset` method that clears/drops all chunks in the
`LinkedChunk`.
  • Loading branch information
Hywan committed Mar 27, 2024
1 parent ab9e4f7 commit 36e199c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions crates/matrix-sdk/src/event_cache/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ impl RoomEvents {
Self { chunks: LinkedChunk::new() }
}

/// Clear all events.
pub fn reset(&mut self) {
self.chunks = LinkedChunk::new();
}

/// Return the number of events.
pub fn len(&self) -> usize {
self.chunks.len()
Expand All @@ -251,6 +256,11 @@ impl RoomEvents {
self.chunks.push_items_back(events)
}

/// Push a gap after existing events.
pub fn push_gap(&mut self, gap: Gap) {
self.chunks.push_gap_back(gap)
}

/// Insert events at a specified position.
pub fn insert_events_at<I>(
&mut self,
Expand All @@ -273,6 +283,22 @@ impl RoomEvents {
self.chunks.insert_gap_at(gap, position)
}

/// Replace the gap identified by `gap_identifier`, by events.
///
/// Because the `gap_identifier` can represent non-gap chunk, this method
/// returns a `Result`.
pub fn replace_gap_at<I>(
&mut self,
items: I,
gap_identifier: ChunkIdentifier,
) -> StdResult<(), LinkedChunkError>
where
I: IntoIterator<Item = SyncTimelineEvent>,
I::IntoIter: ExactSizeIterator,
{
self.chunks.replace_gap_at(items, gap_identifier)
}

/// Search for a chunk, and return its identifier.
pub fn chunk_identifier<'a, P>(&'a self, predicate: P) -> Option<ChunkIdentifier>
where
Expand Down Expand Up @@ -328,6 +354,13 @@ impl RoomEvents {
self.chunks.ritems()
}

/// Iterate over the events, forward.
///
/// The oldest event comes first.
pub fn events(&self) -> impl Iterator<Item = (ItemPosition, &SyncTimelineEvent)> {
self.chunks.items()
}

/// Iterate over the events, starting from `position`, backward.
pub fn revents_from(
&self,
Expand Down

0 comments on commit 36e199c

Please sign in to comment.