Make event-handling related Client fields private

This commit is contained in:
Jonas Platte
2021-11-19 20:38:49 +01:00
parent 5a4aa71f6a
commit 1e13e06e34
2 changed files with 17 additions and 7 deletions

View File

@@ -34,7 +34,7 @@ use matrix_sdk_base::{
};
use matrix_sdk_common::{
instant::{Duration, Instant},
locks::{Mutex, RwLock},
locks::{Mutex, RwLock, RwLockReadGuard},
};
use mime::{self, Mime};
use ruma::{
@@ -125,9 +125,9 @@ pub struct Client {
pub(crate) members_request_locks: Arc<DashMap<RoomId, Arc<Mutex<()>>>>,
pub(crate) typing_notice_times: Arc<DashMap<RoomId, Instant>>,
/// Event handlers. See `register_event_handler`.
pub(crate) event_handlers: Arc<RwLock<EventHandlerMap>>,
event_handlers: Arc<RwLock<EventHandlerMap>>,
/// Custom event handler context. See `register_event_handler_context`.
pub(crate) event_handler_data: Arc<StdRwLock<AnyMap>>,
event_handler_data: Arc<StdRwLock<AnyMap>>,
/// Notification handlers. See `register_notification_handler`.
notification_handlers: Arc<RwLock<Vec<NotificationHandlerFn>>>,
/// Whether the client should operate in application service style mode.
@@ -637,6 +637,10 @@ impl Client {
self
}
pub(crate) async fn event_handlers(&self) -> RwLockReadGuard<'_, EventHandlerMap> {
self.event_handlers.read().await
}
/// Add an arbitrary value for use as event handler context.
///
/// The value can be obtained in an event handler by adding an argument of
@@ -682,6 +686,14 @@ impl Client {
self
}
pub(crate) fn event_handler_context<T>(&self) -> Option<T>
where
T: Clone + Send + Sync + 'static,
{
let map = self.event_handler_data.read().unwrap();
map.get::<T>().cloned()
}
/// Register a handler for a notification.
///
/// Similar to [`Client::register_event_handler`], but only allows functions

View File

@@ -186,8 +186,7 @@ pub struct Ctx<T>(pub T);
impl<T: Clone + Send + Sync + 'static> EventHandlerContext for Ctx<T> {
fn from_data(data: &EventHandlerData<'_>) -> Option<Self> {
let anymap = data.client.event_handler_data.read().unwrap();
Some(Ctx(anymap.get::<T>()?.clone()))
data.client.event_handler_context::<T>().map(Ctx)
}
}
@@ -330,8 +329,7 @@ impl Client {
// Construct event handler futures
let futures: Vec<_> = self
.event_handlers
.read()
.event_handlers()
.await
.get(&event_handler_id)
.into_iter()