feat(sdk): Implement RoomEvents::reset, push_gap, replace_gap_at 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`.
This commit is contained in:
Ivan Enderlin
2024-03-18 12:22:58 +01:00
parent ab9e4f73b1
commit 36e199c31e

View File

@@ -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()
@@ -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,
@@ -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
@@ -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,