mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-14 19:16:02 -04:00
chore(sdk): Add EventHandlerDropGuard
It isn't used for now, but will be soon.
This commit is contained in:
committed by
Damir Jelić
parent
5156fb7dd4
commit
10a37f6d51
@@ -2641,7 +2641,7 @@ pub(crate) mod tests {
|
||||
Client::builder().homeserver_url(homeserver).server_versions([MatrixVersion::V1_0])
|
||||
}
|
||||
|
||||
async fn no_retry_test_client(homeserver_url: Option<String>) -> Client {
|
||||
pub(crate) async fn no_retry_test_client(homeserver_url: Option<String>) -> Client {
|
||||
test_client_builder(homeserver_url)
|
||||
.request_config(RequestConfig::new().disable_retry())
|
||||
.build()
|
||||
|
||||
@@ -366,6 +366,14 @@ impl Client {
|
||||
EventHandlerHandle { key, handler_id }
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn event_handler_drop_guard(
|
||||
&self,
|
||||
handle: EventHandlerHandle,
|
||||
) -> EventHandlerDropGuard {
|
||||
EventHandlerDropGuard { client: self.clone(), handle }
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_sync_events<T>(
|
||||
&self,
|
||||
kind: EventKind,
|
||||
@@ -514,6 +522,18 @@ impl Client {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct EventHandlerDropGuard {
|
||||
handle: EventHandlerHandle,
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl Drop for EventHandlerDropGuard {
|
||||
fn drop(&mut self) {
|
||||
self.client.remove_event_handler(self.handle.clone());
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_event_handler {
|
||||
($($ty:ident),* $(,)?) => {
|
||||
impl<Ev, Fun, Fut, $($ty),*> EventHandler<Ev, ($($ty,)*)> for Fun
|
||||
@@ -909,4 +929,20 @@ mod tests {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_test]
|
||||
async fn event_handler_drop_guard() {
|
||||
let client = crate::client::tests::no_retry_test_client(None).await;
|
||||
|
||||
let handle = client.add_event_handler(|_ev: OriginalSyncRoomMemberEvent| async {});
|
||||
assert_eq!(client.event_handlers().len(), 1);
|
||||
|
||||
{
|
||||
let _guard = client.event_handler_drop_guard(handle);
|
||||
assert_eq!(client.event_handlers().len(), 1);
|
||||
// guard dropped here
|
||||
}
|
||||
|
||||
assert_eq!(client.event_handlers().len(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user