nstreams-core 0.3.1

Generic versioned event stream handler with history replay
Documentation
use crate::event::DeliveredEvent;

/// Client-facing subscription delivering versioned events for a single request.
pub struct Subscription {
    request_id: String,
    rx: tokio::sync::mpsc::Receiver<crate::Result<DeliveredEvent>>,
}

impl Subscription {
    pub(crate) fn new(
        request_id: String,
        rx: tokio::sync::mpsc::Receiver<crate::Result<DeliveredEvent>>,
    ) -> Self {
        Self { request_id, rx }
    }

    pub fn request_id(&self) -> &str {
        &self.request_id
    }

    pub async fn next(&mut self) -> Option<crate::Result<DeliveredEvent>> {
        self.rx.recv().await
    }

    pub fn into_parts(
        self,
    ) -> (
        String,
        tokio::sync::mpsc::Receiver<crate::Result<DeliveredEvent>>,
    ) {
        (self.request_id, self.rx)
    }
}