use tokio::sync::mpsc;
use crate::stream_chunk::StreamChunk;
pub struct StreamEmitter {
tx: mpsc::Sender<StreamChunk>,
}
impl Clone for StreamEmitter {
fn clone(&self) -> Self {
Self {
tx: self.tx.clone(),
}
}
}
impl StreamEmitter {
pub fn new(tx: mpsc::Sender<StreamChunk>) -> Self {
Self { tx }
}
pub fn emit(&self, chunk: StreamChunk) {
let _ = self.tx.try_send(chunk);
}
pub async fn emit_async(
&self,
chunk: StreamChunk,
) -> Result<(), mpsc::error::TrySendError<StreamChunk>> {
self.tx.send(chunk).await.map_err(|e| match e {
mpsc::error::SendError(chunk) => mpsc::error::TrySendError::Full(chunk),
})
}
pub fn sender(&self) -> mpsc::Sender<StreamChunk> {
self.tx.clone()
}
}