feat(sdk): Make EventHandlerDropGuard public (#1055)
This commit is contained in:
parent
e8278b5d68
commit
0a5ebe4dce
|
@ -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
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue