lellm_graph/
stream_emitter.rs1use tokio::sync::mpsc;
6
7use crate::stream_chunk::StreamChunk;
8
9pub struct StreamEmitter {
13 tx: mpsc::Sender<StreamChunk>,
14}
15
16impl Clone for StreamEmitter {
17 fn clone(&self) -> Self {
18 Self {
19 tx: self.tx.clone(),
20 }
21 }
22}
23
24impl StreamEmitter {
25 pub fn new(tx: mpsc::Sender<StreamChunk>) -> Self {
27 Self { tx }
28 }
29
30 pub fn emit(&self, chunk: StreamChunk) {
32 let _ = self.tx.try_send(chunk);
34 }
35
36 pub async fn emit_async(
38 &self,
39 chunk: StreamChunk,
40 ) -> Result<(), mpsc::error::TrySendError<StreamChunk>> {
41 self.tx.send(chunk).await.map_err(|e| match e {
42 mpsc::error::SendError(chunk) => mpsc::error::TrySendError::Full(chunk),
43 })
44 }
45
46 pub fn sender(&self) -> mpsc::Sender<StreamChunk> {
48 self.tx.clone()
49 }
50}