feat(sdk): Make EventHandlerDropGuard public (#1055)

This commit is contained in:
Jonas Platte 2022-09-26 18:18:12 +02:00 committed by GitHub
parent e8278b5d68
commit 0a5ebe4dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 10 deletions

View File

@ -80,7 +80,8 @@ use crate::{
config::RequestConfig,
error::{HttpError, HttpResult},
event_handler::{
EventHandler, EventHandlerHandle, EventHandlerResult, EventHandlerStore, SyncEvent,
EventHandler, EventHandlerDropGuard, EventHandlerHandle, EventHandlerResult,
EventHandlerStore, SyncEvent,
},
http_client::HttpClient,
room, Account, Error, Media, RefreshTokenError, Result, RumaApiError,
@ -723,6 +724,14 @@ impl Client {
self.inner.event_handlers.remove(handle);
}
/// Create an [`EventHandlerDropGuard`] for the event handler identified by
/// the given handle.
///
/// When the returned value is dropped, the event handler will be removed.
pub fn event_handler_drop_guard(&self, handle: EventHandlerHandle) -> EventHandlerDropGuard {
EventHandlerDropGuard::new(handle, self.clone())
}
/// Add an arbitrary value for use as event handler context.
///
/// The value can be obtained in an event handler by adding an argument of

View File

@ -310,14 +310,6 @@ impl Client {
handle
}
#[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: HandlerKind,
@ -444,12 +436,22 @@ impl Client {
}
}
/// A guard type that removes an event handler when it drops (goes out of
/// scope).
///
/// Created with [`Client::event_handler_drop_guard`].
#[derive(Debug)]
pub(crate) struct EventHandlerDropGuard {
pub struct EventHandlerDropGuard {
handle: EventHandlerHandle,
client: Client,
}
impl EventHandlerDropGuard {
pub(crate) fn new(handle: EventHandlerHandle, client: Client) -> Self {
Self { handle, client }
}
}
impl Drop for EventHandlerDropGuard {
fn drop(&mut self) {
self.client.remove_event_handler(self.handle.clone());