60 lines
2.0 KiB
Rust
60 lines
2.0 KiB
Rust
#[derive(Debug, Clone)]
|
|
pub enum SseMessage {
|
|
RefreshAda,
|
|
}
|
|
|
|
// I think for the ada_sender, a watch would be ideal if I want to always use full renders of the
|
|
// watchlist, however if I want more atomic updates, it may be useful to keep the broadcast's
|
|
// buffer and return an html component that requests a complete rerender if the handler lagged
|
|
/// A container struct for keeping access to the channel senders for both channels used in the sse
|
|
/// pipeline
|
|
///
|
|
/// For something wishing to listen in on the broadcast html stream, you should call `.subscribe()`
|
|
/// on ada_sender
|
|
///
|
|
/// Anything wishing to send to sse should pass an [SseMessage] to the [tokio::sync::mpsc::Sender]
|
|
/// in sse_sender
|
|
#[derive(Debug, Clone)]
|
|
pub struct SseHandler {
|
|
pub ada_sender: tokio::sync::broadcast::Sender<String>,
|
|
pub sse_sender: tokio::sync::mpsc::Sender<SseMessage>,
|
|
}
|
|
|
|
impl SseHandler {
|
|
/// This function initializes the channels used for sse, as well as spawning a manager task for
|
|
/// handling sse messages
|
|
pub fn init() -> Self {
|
|
let (ada_sender, _) = tokio::sync::broadcast::channel(10);
|
|
let (sse_sender, mut sse_receiver) = tokio::sync::mpsc::channel(10);
|
|
|
|
tokio::spawn({
|
|
let sender = ada_sender.clone();
|
|
async move {
|
|
while let Some(message) = sse_receiver.recv().await {
|
|
match message {
|
|
SseMessage::RefreshAda => {
|
|
if sender.receiver_count() > 0 {
|
|
sender.send(get_ada_list().await).unwrap();
|
|
}
|
|
}
|
|
};
|
|
}
|
|
}
|
|
});
|
|
|
|
Self {
|
|
ada_sender,
|
|
sse_sender,
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn get_ada_list() -> String {
|
|
// TODO: need to scope out if these errors need to be handled
|
|
let mut buf = Vec::new();
|
|
|
|
crate::templates::components::ada_list_html(&mut buf).unwrap();
|
|
|
|
String::from_utf8(buf).unwrap()
|
|
}
|