Skip to content

Commit 96078c6

Browse files
committed
chore(sdk): PinnedEventsCache::new takes a WeakRoom instead of a Room.
This patch changes `PinnedEventsCache::new` to receive a `WeakRoom` instead of a `Room`. It then returns a `Result<Self>`, with `Err` if the `WeakRoom` doesn't point to the `Room` anymore. Thus, this patch changes `get_or_init` by `get_or_try_init`, but this one is unstable. So this patch switches from `OnceLock` to `OnceCell`, which provides a stable one. Why this change? Because in a later refactoring, providing a `Room` is a bit annoying: all we will have is a `WeakRoom`. We could do this work of upgrading the `WeakRoom` to a `Room`, but it seems akward as all caches are holding a `WeakRoom` if room is necessary.
1 parent 3634fb2 commit 96078c6

6 files changed

Lines changed: 19 additions & 15 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ js-sys = { version = "0.3.82", default-features = false, features = ["std"] }
6262
mime = { version = "0.3.17", default-features = false }
6363
oauth2 = { version = "5.0.0", default-features = false, features = ["timing-resistant-secret-traits"] }
6464
oauth2-reqwest = { version = "0.1.0-alpha.3", default-features = false }
65+
once_cell = { version = "1.21", default-features = false }
6566
pbkdf2 = { version = "0.12.2", default-features = false }
6667
pin-project-lite = { version = "0.2.16", default-features = false }
6768
proc-macro2 = { version = "1.0.106", default-features = false }

crates/matrix-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ mime.workspace = true
128128
mime2ext = "0.1.54"
129129
oauth2.workspace = true
130130
oauth2-reqwest.workspace = true
131+
once_cell.workspace = true
131132
percent-encoding = "2.3.2"
132133
pin-project-lite.workspace = true
133134
rand = { workspace = true, optional = true }

crates/matrix-sdk/src/event_cache/caches/pinned_events/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,15 @@ pub struct PinnedEventsCache {
212212
impl PinnedEventsCache {
213213
/// Creates a new [`PinnedEventsCache`] for the given room.
214214
pub(in super::super) fn new(
215-
room: Room,
215+
weak_room: &WeakRoom,
216216
linked_chunk_update_sender: Sender<RoomEventCacheLinkedChunkUpdate>,
217217
store: EventCacheStoreLock,
218-
) -> Self {
219-
let sender = Sender::new(32);
220-
218+
) -> Result<Self> {
219+
let room = weak_room.get().ok_or(EventCacheError::ClientDropped)?;
221220
let room_id = room.room_id().to_owned();
222221

222+
let sender = Sender::new(32);
223+
223224
let chunk = EventLinkedChunk::new();
224225

225226
let state =
@@ -236,7 +237,7 @@ impl PinnedEventsCache {
236237
.abort_on_drop(),
237238
);
238239

239-
Self { state, _task: task }
240+
Ok(Self { state, _task: task })
240241
}
241242

242243
/// Subscribe to live events from this room's pinned events cache.

crates/matrix-sdk/src/event_cache/caches/room/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ impl RoomEventCache {
162162
pub async fn subscribe_to_pinned_events(
163163
&self,
164164
) -> Result<(Vec<Event>, Receiver<TimelineVectorDiffs>)> {
165-
let room = self.inner.weak_room.get().ok_or(EventCacheError::ClientDropped)?;
166165
let state = self.inner.state.read().await?;
167166

168-
state.subscribe_to_pinned_events(room).await
167+
state.subscribe_to_pinned_events(&self.inner.weak_room).await
169168
}
170169

171170
/// Create or get an event-focused timeline cache for this room.

crates/matrix-sdk/src/event_cache/caches/room/state.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use std::{
1616
collections::HashMap,
1717
sync::{
18-
Arc, OnceLock,
18+
Arc,
1919
atomic::{AtomicUsize, Ordering},
2020
},
2121
};
@@ -36,6 +36,7 @@ use matrix_sdk_base::{
3636
sync::Timeline,
3737
};
3838
use matrix_sdk_common::executor::spawn;
39+
use once_cell::sync::OnceCell;
3940
use ruma::{
4041
EventId, OwnedEventId, OwnedRoomId, OwnedUserId,
4142
events::{
@@ -72,7 +73,7 @@ use super::{
7273
EventsOrigin, RoomEventCacheGenericUpdate, RoomEventCacheLinkedChunkUpdate,
7374
RoomEventCacheUpdate, RoomEventCacheUpdateSender, sort_positions_descending,
7475
};
75-
use crate::{Room, room::WeakRoom};
76+
use crate::room::WeakRoom;
7677

7778
/// Key for the event-focused caches.
7879
#[derive(Hash, PartialEq, Eq)]
@@ -111,7 +112,7 @@ pub struct RoomEventCacheState {
111112
event_focused_caches: HashMap<EventFocusedCacheKey, EventFocusedCache>,
112113

113114
/// Cache for pinned events in this room, initialized on-demand.
114-
pinned_events_cache: OnceLock<PinnedEventsCache>,
115+
pinned_events_cache: OnceCell<PinnedEventsCache>,
115116

116117
pagination_status: SharedObservable<SharedPaginationStatus>,
117118

@@ -260,7 +261,7 @@ impl LockedRoomEventCacheState {
260261
room_version_rules,
261262
waited_for_initial_prev_token: false,
262263
subscriber_count: Default::default(),
263-
pinned_events_cache: OnceLock::new(),
264+
pinned_events_cache: OnceCell::new(),
264265
automatic_pagination,
265266
}))
266267
}
@@ -373,15 +374,15 @@ impl<'a> RoomEventCacheStateLockReadGuard<'a> {
373374
/// This requires the room's event cache to be initialized.
374375
pub async fn subscribe_to_pinned_events(
375376
&self,
376-
room: Room,
377+
weak_room: &WeakRoom,
377378
) -> Result<(Vec<Event>, Receiver<TimelineVectorDiffs>), EventCacheError> {
378-
let pinned_events_cache = self.state.pinned_events_cache.get_or_init(|| {
379+
let pinned_events_cache = self.state.pinned_events_cache.get_or_try_init(|| {
379380
PinnedEventsCache::new(
380-
room,
381+
weak_room,
381382
self.state.linked_chunk_update_sender.clone(),
382383
self.state.store.clone(),
383384
)
384-
});
385+
})?;
385386

386387
pinned_events_cache.subscribe().await
387388
}

0 commit comments

Comments
 (0)