Skip to content

Commit 1e13e06

Browse files
committed
Make event-handling related Client fields private
1 parent 5a4aa71 commit 1e13e06

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

crates/matrix-sdk/src/client.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use matrix_sdk_base::{
3434
};
3535
use matrix_sdk_common::{
3636
instant::{Duration, Instant},
37-
locks::{Mutex, RwLock},
37+
locks::{Mutex, RwLock, RwLockReadGuard},
3838
};
3939
use mime::{self, Mime};
4040
use ruma::{
@@ -125,9 +125,9 @@ pub struct Client {
125125
pub(crate) members_request_locks: Arc<DashMap<RoomId, Arc<Mutex<()>>>>,
126126
pub(crate) typing_notice_times: Arc<DashMap<RoomId, Instant>>,
127127
/// Event handlers. See `register_event_handler`.
128-
pub(crate) event_handlers: Arc<RwLock<EventHandlerMap>>,
128+
event_handlers: Arc<RwLock<EventHandlerMap>>,
129129
/// Custom event handler context. See `register_event_handler_context`.
130-
pub(crate) event_handler_data: Arc<StdRwLock<AnyMap>>,
130+
event_handler_data: Arc<StdRwLock<AnyMap>>,
131131
/// Notification handlers. See `register_notification_handler`.
132132
notification_handlers: Arc<RwLock<Vec<NotificationHandlerFn>>>,
133133
/// Whether the client should operate in application service style mode.
@@ -637,6 +637,10 @@ impl Client {
637637
self
638638
}
639639

640+
pub(crate) async fn event_handlers(&self) -> RwLockReadGuard<'_, EventHandlerMap> {
641+
self.event_handlers.read().await
642+
}
643+
640644
/// Add an arbitrary value for use as event handler context.
641645
///
642646
/// The value can be obtained in an event handler by adding an argument of
@@ -682,6 +686,14 @@ impl Client {
682686
self
683687
}
684688

689+
pub(crate) fn event_handler_context<T>(&self) -> Option<T>
690+
where
691+
T: Clone + Send + Sync + 'static,
692+
{
693+
let map = self.event_handler_data.read().unwrap();
694+
map.get::<T>().cloned()
695+
}
696+
685697
/// Register a handler for a notification.
686698
///
687699
/// Similar to [`Client::register_event_handler`], but only allows functions

crates/matrix-sdk/src/event_handler.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ pub struct Ctx<T>(pub T);
186186

187187
impl<T: Clone + Send + Sync + 'static> EventHandlerContext for Ctx<T> {
188188
fn from_data(data: &EventHandlerData<'_>) -> Option<Self> {
189-
let anymap = data.client.event_handler_data.read().unwrap();
190-
Some(Ctx(anymap.get::<T>()?.clone()))
189+
data.client.event_handler_context::<T>().map(Ctx)
191190
}
192191
}
193192

@@ -330,8 +329,7 @@ impl Client {
330329

331330
// Construct event handler futures
332331
let futures: Vec<_> = self
333-
.event_handlers
334-
.read()
332+
.event_handlers()
335333
.await
336334
.get(&event_handler_id)
337335
.into_iter()

0 commit comments

Comments
 (0)