From 23d5655901139d9b17a7da7b975ef9469e261adc Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 15 Jun 2023 09:24:47 +0200 Subject: [PATCH] feat(ui): `room_list::RoomInner` holds `Arc` to `Timeline`. This patch updates `RoomInner::timeline` and `::sneaky_timeline` to `AsyncOnceCell>`. Adding `Arc` allows to copy the timeline if necessary more easily. --- crates/matrix-sdk-ui/src/room_list/room.rs | 41 ++++++++++++---------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/crates/matrix-sdk-ui/src/room_list/room.rs b/crates/matrix-sdk-ui/src/room_list/room.rs index cabd63d4b15..97c280c9675 100644 --- a/crates/matrix-sdk-ui/src/room_list/room.rs +++ b/crates/matrix-sdk-ui/src/room_list/room.rs @@ -32,11 +32,11 @@ struct RoomInner { room: matrix_sdk::room::Room, /// The timeline of the room. - timeline: AsyncOnceCell, + timeline: AsyncOnceCell>, /// The “sneaky” timeline of the room, i.e. this timeline doesn't track the /// read marker nor the receipts. - sneaky_timeline: AsyncOnceCell, + sneaky_timeline: AsyncOnceCell>, } impl Room { @@ -98,20 +98,23 @@ impl Room { } /// Get the timeline of the room. - pub async fn timeline(&self) -> &Timeline { + pub async fn timeline(&self) -> Arc { self.inner .timeline .get_or_init(async { - Timeline::builder(&self.inner.room) - .events( - self.inner.sliding_sync_room.prev_batch(), - self.inner.sliding_sync_room.timeline_queue(), - ) - .track_read_marker_and_receipts() - .build() - .await + Arc::new( + Timeline::builder(&self.inner.room) + .events( + self.inner.sliding_sync_room.prev_batch(), + self.inner.sliding_sync_room.timeline_queue(), + ) + .track_read_marker_and_receipts() + .build() + .await, + ) }) .await + .clone() } /// Get the latest event of the timeline. @@ -122,13 +125,15 @@ impl Room { self.inner .sneaky_timeline .get_or_init(async { - Timeline::builder(&self.inner.room) - .events( - self.inner.sliding_sync_room.prev_batch(), - self.inner.sliding_sync_room.timeline_queue(), - ) - .build() - .await + Arc::new( + Timeline::builder(&self.inner.room) + .events( + self.inner.sliding_sync_room.prev_batch(), + self.inner.sliding_sync_room.timeline_queue(), + ) + .build() + .await, + ) }) .await .latest_event()