From b0274eaaf29a791f71e90dbc6132239474a781b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Mon, 10 Oct 2022 14:58:41 +0200 Subject: [PATCH] room-history: Don't loop when new messages are added The scrollbar has not been updated when the `load()` method returns, so wait until we get notified of the change if new messages are added. --- src/session/content/room_history/mod.rs | 17 +---------------- src/session/room/timeline/mod.rs | 16 +++++++++------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/session/content/room_history/mod.rs b/src/session/content/room_history/mod.rs index 665861be..f75bf518 100644 --- a/src/session/content/room_history/mod.rs +++ b/src/session/content/room_history/mod.rs @@ -842,22 +842,7 @@ impl RoomHistory { let obj_weak = self.downgrade(); spawn!(async move { loop { - // We don't want to hold a strong ref to `obj` on `await` - let need = if let Some(obj) = obj_weak.upgrade() { - if obj.room().as_ref() == Some(&room) { - obj.need_messages() || room.timeline().is_empty() - } else { - return; - } - } else { - return; - }; - - if need { - if !room.timeline().load().await { - break; - } - } else { + if !room.timeline().load().await { break; } } diff --git a/src/session/room/timeline/mod.rs b/src/session/room/timeline/mod.rs index 1bdd5f79..5c0fff5c 100644 --- a/src/session/room/timeline/mod.rs +++ b/src/session/room/timeline/mod.rs @@ -507,9 +507,9 @@ impl Timeline { } } - /// Load the timeline - /// This function should also be called to load more events - /// Returns `true` when messages where successfully added + /// Load events at the start of the timeline. + /// + /// Returns `true` when no messages were added, but more can be loaded. pub async fn load(&self) -> bool { let priv_ = self.imp(); @@ -584,8 +584,7 @@ impl Timeline { } self.set_state(TimelineState::Ready); - self.prepend(events); - true + !self.prepend(events) } Ok(None) => { self.remove_loading_spinner(); @@ -784,8 +783,10 @@ impl Timeline { } } - /// Prepends a batch of events - pub fn prepend(&self, batch: Vec) { + /// Prepends a batch of events. + /// + /// Returns `true` if new shown events where added to the timeline. + pub fn prepend(&self, batch: Vec) -> bool { let priv_ = self.imp(); let mut added = batch.len(); @@ -831,6 +832,7 @@ impl Timeline { } self.items_changed(0, 0, added as u32); + added > 0 } fn set_room(&self, room: Option) {