use { eyre::Result, futures::Stream, lool::fail, std::pin::Pin, tokio::sync::broadcast::{self, Sender}, }; pub trait StreamMsg: Clone + Send + Sync + 'static {} impl StreamMsg for String {} impl StreamMsg for Vec {} impl StreamMsg for serde_json::Value {} impl StreamMsg for serde_json::Map {} // TODO: move this to a separate module, or maybe to the lool library pub struct SourceStream { sender: Option>, } impl Default for SourceStream { fn default() -> Self { Self::new() } } impl SourceStream { // Create a new SourceStream with a broadcast channel pub fn new() -> Self { let (sender, _) = broadcast::channel(100); // Adjust the buffer size as needed SourceStream { sender: Some(sender), } } pub fn sender(&self) -> Option> { self.sender.clone() } // Subscribe to the stream pub fn subscribe(&self) -> Result + Send + 'static>>> { if let Some(sender) = &self.sender { let mut receiver = sender.subscribe(); let stream = async_stream::stream! { while let Ok(item) = receiver.recv().await { yield item; } }; Ok(Box::pin(stream)) } else { fail!("SourceStream has been consumed") } } }