Skip to content

Commit 36e199c

Browse files
committed
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`.
1 parent ab9e4f7 commit 36e199c

File tree

1 file changed

+33
-0
lines changed
  • crates/matrix-sdk/src/event_cache

1 file changed

+33
-0
lines changed

crates/matrix-sdk/src/event_cache/store.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ impl RoomEvents {
230230
Self { chunks: LinkedChunk::new() }
231231
}
232232

233+
/// Clear all events.
234+
pub fn reset(&mut self) {
235+
self.chunks = LinkedChunk::new();
236+
}
237+
233238
/// Return the number of events.
234239
pub fn len(&self) -> usize {
235240
self.chunks.len()
@@ -251,6 +256,11 @@ impl RoomEvents {
251256
self.chunks.push_items_back(events)
252257
}
253258

259+
/// Push a gap after existing events.
260+
pub fn push_gap(&mut self, gap: Gap) {
261+
self.chunks.push_gap_back(gap)
262+
}
263+
254264
/// Insert events at a specified position.
255265
pub fn insert_events_at<I>(
256266
&mut self,
@@ -273,6 +283,22 @@ impl RoomEvents {
273283
self.chunks.insert_gap_at(gap, position)
274284
}
275285

286+
/// Replace the gap identified by `gap_identifier`, by events.
287+
///
288+
/// Because the `gap_identifier` can represent non-gap chunk, this method
289+
/// returns a `Result`.
290+
pub fn replace_gap_at<I>(
291+
&mut self,
292+
items: I,
293+
gap_identifier: ChunkIdentifier,
294+
) -> StdResult<(), LinkedChunkError>
295+
where
296+
I: IntoIterator<Item = SyncTimelineEvent>,
297+
I::IntoIter: ExactSizeIterator,
298+
{
299+
self.chunks.replace_gap_at(items, gap_identifier)
300+
}
301+
276302
/// Search for a chunk, and return its identifier.
277303
pub fn chunk_identifier<'a, P>(&'a self, predicate: P) -> Option<ChunkIdentifier>
278304
where
@@ -328,6 +354,13 @@ impl RoomEvents {
328354
self.chunks.ritems()
329355
}
330356

357+
/// Iterate over the events, forward.
358+
///
359+
/// The oldest event comes first.
360+
pub fn events(&self) -> impl Iterator<Item = (ItemPosition, &SyncTimelineEvent)> {
361+
self.chunks.items()
362+
}
363+
331364
/// Iterate over the events, starting from `position`, backward.
332365
pub fn revents_from(
333366
&self,

0 commit comments

Comments
 (0)