refactor(ui): Add ObservableItemsTransaction::has_local.

This patch implements the `has_local` method on
`ObservableItemsTransaction`, which is way faster than the previous the
previous solution which was to iterate over all items to find at least
one local timeline item.
This commit is contained in:
Ivan Enderlin
2025-05-06 16:21:02 +02:00
parent 55ea80b485
commit 94b76168e8
2 changed files with 9 additions and 4 deletions

View File

@@ -320,11 +320,18 @@ impl<'observable_items> ObservableItemsTransaction<'observable_items> {
self.push_back(timeline_item, None);
}
/// Check whether there is at least one [`Local`] timeline item.
///
/// [`Local`]: super::EventTimelineItemKind::Local
pub fn has_local(&self) -> bool {
matches!(self.items.last(), Some(timeline_item) if timeline_item.is_local_echo())
}
/// Push a new [`TimelineStart`] virtual timeline item.
///
/// # Invariant
///
/// A [`TimelineStart`] is always the first item if present..
/// A [`TimelineStart`] is always the first item if present.
///
/// # Panics
///

View File

@@ -476,14 +476,12 @@ impl<'a> TimelineStateTransaction<'a> {
}
pub(super) fn clear(&mut self) {
let has_local_echoes = self.items.iter().any(|item| item.is_local_echo());
// By first checking if there are any local echoes first, we do a bit
// more work in case some are found, but it should be worth it because
// there will often not be any, and only emitting a single
// `VectorDiff::Clear` should be much more efficient to process for
// subscribers.
if has_local_echoes {
if self.items.has_local() {
// Remove all remote events and virtual items that aren't date dividers.
self.items.for_each(|entry| {
if entry.is_remote_event()