use std::convert::Infallible; use tokio::sync::mpsc::UnboundedSender; use axum::{ response::{Html, IntoResponse}, routing::get, Router, }; use tokio_stream::wrappers::UnboundedReceiverStream; #[tokio::main] async fn main() { // build our application with a route let app = Router::new().route("/", get(handler)); // run it let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") .await .unwrap(); println!("listening on http://{}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); } async fn streamer(sender: UnboundedSender>) { // Write initial template // Notes on not obvious things: // - The template element has to nested within another element to attach the shadow root to // - The elements to slot have to be siblings of the template element // - Sibling elements next to the template will not get rendered // Meaning only elements to be slotted should be placed as siblings to the template const TEMPLATE: &str = r#" Document
"#; sender.send(Ok(TEMPLATE.to_string())).unwrap(); // Showcase out of order let slot = |slot: u8| format!("Slot {}", slot, slot); let next = slot(3); sender.send(Ok(next)).unwrap(); tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; let next = slot(1); sender.send(Ok(next)).unwrap(); tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; let next = slot(4); sender.send(Ok(next)).unwrap(); tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; let next = slot(2); sender.send(Ok(next)).unwrap(); // Close the document sender .send(Ok("

Done 👍

".to_string())) .unwrap(); } async fn handler() -> impl IntoResponse { // We know the response is max size will be the HTML document size // so we can use an unbounded channel let (sender, receiver) = tokio::sync::mpsc::unbounded_channel(); tokio::spawn(streamer(sender)); let stream = UnboundedReceiverStream::new(receiver); let body = axum::body::Body::from_stream(stream); Html(body) }