ppoppo-infra 0.1.0

Backend-agnostic infrastructure traits for caching, queuing, and messaging
Documentation
//! Backend-agnostic subscriber trait (read side of pub/sub).

use async_trait::async_trait;

use crate::Result;
use crate::types::Notification;

/// Subscriber (read side) of pub/sub messaging.
///
/// Holds a dedicated connection to the backend. The connection lifecycle
/// is managed by the implementation (PG LISTEN, Redis SUBSCRIBE, etc.).
///
/// Created via [`PubSub::subscribe()`](crate::PubSub::subscribe).
#[async_trait]
pub trait Subscriber: Send {
    /// Wait for the next notification. Returns `None` if the connection is closed.
    async fn recv(&mut self) -> Option<Notification>;

    /// Subscribe to an additional channel.
    async fn listen(&mut self, channel: &str) -> Result<()>;

    /// Unsubscribe from a channel.
    async fn unlisten(&mut self, channel: &str) -> Result<()>;
}