Merge pull request #1791 from ealmloff/breaking

Remove scope, use_state, use_ref, bump allocator and make everything 'static
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2024-02-05 14:19:47 -08:00
commit 4f8868d43d
517 files changed
+17126 -20669

No files matched your search

+69 -2
View File
@@ -1,5 +1,15 @@
use crate::{LiveViewError, LiveViewSocket};
use axum::extract::ws::{Message, WebSocket};
use std::sync::Arc;
use crate::{interpreter_glue, LiveViewError, LiveViewSocket, LiveviewRouter};
use axum::{
extract::{
ws::{Message, WebSocket},
WebSocketUpgrade,
},
response::Html,
routing::*,
Router,
};
use futures_util::{SinkExt, StreamExt};
/// Convert an Axum WebSocket into a `LiveViewSocket`.
@@ -22,3 +32,60 @@ fn transform_rx(message: Result<Message, axum::Error>) -> Result<Vec<u8>, LiveVi
async fn transform_tx(message: Vec<u8>) -> Result<Message, axum::Error> {
Ok(Message::Binary(message))
}
impl LiveviewRouter for Router {
fn create_default_liveview_router() -> Self {
Router::new()
}
fn with_virtual_dom(
self,
route: &str,
app: impl Fn() -> dioxus_core::prelude::VirtualDom + Send + Sync + 'static,
) -> Self {
let view = crate::LiveViewPool::new();
let ws_path = format!("{}/ws", route);
let title = crate::app_title();
let index_page_with_glue = move |glue: &str| {
Html(format!(
r#"
<!DOCTYPE html>
<html>
<head> <title>{title}</title> </head>
<body> <div id="main"></div> </body>
{glue}
</html>
"#,
))
};
let app = Arc::new(app);
self.route(
&ws_path,
get(move |ws: WebSocketUpgrade| async move {
let app = app.clone();
ws.on_upgrade(move |socket| async move {
_ = view
.launch_virtualdom(axum_socket(socket), move || app())
.await;
})
}),
)
.route(
route,
get(move || async move { index_page_with_glue(&interpreter_glue(&ws_path)) }),
)
}
async fn start(self, address: impl Into<std::net::SocketAddr>) {
if let Err(err) = axum::Server::bind(&address.into())
.serve(self.into_make_service())
.await
{
eprintln!("Failed to start axum server: {}", err);
}
}
}