async_client: Remove the non-async callbacks.

master
Damir Jelić 2019-10-23 22:51:34 +02:00
parent cf17173037
commit f6d371b36f
2 changed files with 5 additions and 27 deletions

View File

@ -22,18 +22,7 @@ async fn login(
.disable_ssl_verification(); .disable_ssl_verification();
let mut client = AsyncClient::new_with_config(&homeserver_url, None, client_config).unwrap(); let mut client = AsyncClient::new_with_config(&homeserver_url, None, client_config).unwrap();
let callback = |event| { client.add_event_callback(EventType::RoomMessage, |event| {
if let RoomEvent::RoomMessage(MessageEvent {
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
sender,
..
}) = event
{
println!("{}: {}", sender, msg_body);
}
};
client.add_event_future(EventType::RoomMessage, |event| {
Box::pin(async { Box::pin(async {
if let RoomEvent::RoomMessage(MessageEvent { if let RoomEvent::RoomMessage(MessageEvent {
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),

View File

@ -27,8 +27,7 @@ pub struct AsyncClient {
/// User session data. /// User session data.
base_client: BaseClient, base_client: BaseClient,
/// Event callbacks /// Event callbacks
event_callbacks: HashMap<EventType, Box<dyn FnMut(RoomEvent)>>, event_callbacks:
event_futures:
HashMap<EventType, Box<dyn FnMut(RoomEvent) -> Pin<Box<dyn Future<Output = ()>>>>>, HashMap<EventType, Box<dyn FnMut(RoomEvent) -> Pin<Box<dyn Future<Output = ()>>>>>,
} }
@ -117,7 +116,6 @@ impl AsyncClient {
http_client, http_client,
base_client: BaseClient::new(session), base_client: BaseClient::new(session),
event_callbacks: HashMap::new(), event_callbacks: HashMap::new(),
event_futures: HashMap::new(),
}) })
} }
@ -160,24 +158,15 @@ impl AsyncClient {
http_client, http_client,
base_client: BaseClient::new(session), base_client: BaseClient::new(session),
event_callbacks: HashMap::new(), event_callbacks: HashMap::new(),
event_futures: HashMap::new(),
}) })
} }
pub fn add_event_callback( pub fn add_event_callback(
&mut self,
event_type: EventType,
callback: impl FnMut(RoomEvent) + 'static,
) {
self.event_callbacks.insert(event_type, Box::new(callback));
}
pub fn add_event_future(
&mut self, &mut self,
event_type: EventType, event_type: EventType,
callback: impl FnMut(RoomEvent) -> Pin<Box<dyn Future<Output = ()>>> + 'static, callback: impl FnMut(RoomEvent) -> Pin<Box<dyn Future<Output = ()>>> + 'static,
) { ) {
self.event_futures.insert(event_type, Box::new(callback)); self.event_callbacks.insert(event_type, Box::new(callback));
} }
pub async fn login<S: Into<String>>( pub async fn login<S: Into<String>>(
@ -246,8 +235,8 @@ impl AsyncClient {
cb(event.clone()); cb(event.clone());
} }
if self.event_futures.contains_key(&event_type) { if self.event_callbacks.contains_key(&event_type) {
let cb = self.event_futures.get_mut(&event_type).unwrap(); let cb = self.event_callbacks.get_mut(&event_type).unwrap();
let future = Pin::from(cb(event.clone())); let future = Pin::from(cb(event.clone()));
future.await; future.await;
} }