use std::time::Duration;
use super::SteamClient;
use crate::client::events::SteamEvent;
use crate::error::SteamError;
impl SteamClient {
pub fn try_poll_event(&mut self) -> Option<SteamEvent> {
if let Some(mut event) = self.event_queue.pop_front() {
self.handle_event(&mut event);
Some(event)
} else {
None
}
}
pub async fn poll_event_timeout(&mut self, timeout: Duration) -> Result<Option<SteamEvent>, SteamError> {
match tokio::time::timeout(timeout, self.poll_event()).await {
Ok(result) => result,
Err(_) => Ok(None),
}
}
pub async fn wait_for<F>(&mut self, predicate: F) -> Result<SteamEvent, SteamError>
where
F: Fn(&SteamEvent) -> bool,
{
loop {
if let Some(event) = self.poll_event().await? {
if predicate(&event) {
return Ok(event);
}
}
}
}
pub async fn wait_for_timeout<F>(&mut self, predicate: F, timeout: Duration) -> Result<Option<SteamEvent>, SteamError>
where
F: Fn(&SteamEvent) -> bool,
{
match tokio::time::timeout(timeout, self.wait_for(predicate)).await {
Ok(result) => result.map(Some),
Err(_) => Ok(None),
}
}
pub fn drain_queued_events(&mut self) -> Vec<SteamEvent> {
let mut events: Vec<_> = self.event_queue.drain(..).collect();
for event in &mut events {
self.handle_event(event);
}
events
}
}