use tokio_postgres::{
tls::{MakeTlsConnect, TlsConnect},
Socket,
};
use crate::{
pg_pubsub_connection::{PgPubSubConnection, PubSubError, Subscription},
PgPubSubOptions,
};
pub struct PgPubSub {
connection: PgPubSubConnection,
}
impl PgPubSub {
pub async fn connect<T>(options: PgPubSubOptions<T>) -> Result<Self, tokio_postgres::Error>
where
T: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
<T as MakeTlsConnect<Socket>>::Stream: Send + 'static,
<T as MakeTlsConnect<Socket>>::TlsConnect: Send,
<<T as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
let connection = PgPubSubConnection::connect(options).await?;
Ok(Self { connection })
}
pub async fn listen(&self, channel: &str) -> Result<Subscription, PubSubError> {
self.connection.listen(channel).await
}
pub async fn notify(&self, channel: &str, payload: Option<&str>) -> Result<(), PubSubError> {
self.connection.notify(channel, payload).await
}
pub async fn notify_batch(&self, items: &[(&str, Option<&str>)]) -> Result<(), PubSubError> {
self.connection.notify_batch(items).await
}
}