46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
#[derive(Debug, Clone)]
|
|
pub enum SseMessage {
|
|
RefreshAda,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct SseHandler {
|
|
pub ada_sender: tokio::sync::broadcast::Sender<String>,
|
|
pub sse_sender: tokio::sync::mpsc::Sender<SseMessage>,
|
|
}
|
|
|
|
impl SseHandler {
|
|
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 {
|
|
let mut buf = Vec::new();
|
|
|
|
crate::templates::components::ada_list_html(&mut buf).unwrap();
|
|
|
|
String::from_utf8(buf).unwrap()
|
|
}
|