use anyhow::anyhow;
use tokio::sync::{mpsc::Sender, oneshot};
use crate::types::{Activity, IPCCommand};
#[derive(Debug, Clone)]
pub struct PresenceClient {
pub(crate) tx: Sender<IPCCommand>,
pub(crate) client_id: String,
}
impl PresenceClient {
#[must_use]
pub fn client_id(&self) -> String {
self.client_id.clone()
}
pub async fn set_activity(&self, activity: Activity) -> Result<(), anyhow::Error> {
self.tx
.send(IPCCommand::SetActivity {
activity: Box::new(activity),
})
.await
.map_err(|_| anyhow!("Connection has already been closed."))?;
Ok(())
}
pub async fn clear_activity(&self) -> Result<(), anyhow::Error> {
self.tx
.send(IPCCommand::ClearActivity)
.await
.map_err(|_| anyhow!("Connection has already been closed."))?;
Ok(())
}
pub async fn close(&self) -> Result<(), anyhow::Error> {
let (done_tx, done_rx) = oneshot::channel::<()>();
if self
.tx
.send(IPCCommand::Close { done: done_tx })
.await
.is_err()
{
return Ok(());
}
done_rx.await?;
Ok(())
}
}