From f332ffc5ad7371a52aeaf93a70144222f8880f9f Mon Sep 17 00:00:00 2001 From: Ian Date: Sun, 24 Apr 2022 00:37:46 -0400 Subject: [PATCH] support starting liveview with props --- .../liveview/src/adapters/axum_adapter.rs | 19 ++++++++++++++++--- .../liveview/src/adapters/warp_adapter.rs | 15 ++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/liveview/src/adapters/axum_adapter.rs b/packages/liveview/src/adapters/axum_adapter.rs index d85b6da68..349ef5fe4 100644 --- a/packages/liveview/src/adapters/axum_adapter.rs +++ b/packages/liveview/src/adapters/axum_adapter.rs @@ -12,18 +12,31 @@ use tokio_util::task::LocalPoolHandle; #[cfg(feature = "axum")] impl crate::Liveview { pub async fn upgrade(&self, ws: WebSocket, app: fn(Scope) -> Element) { - connect(ws, self.pool.clone(), app).await; + connect(ws, self.pool.clone(), app, ()).await; + } + pub async fn upgrade_with_props(&self, ws: WebSocket, app: fn(Scope) -> Element, props: T) + where + T: Send + Sync + 'static, + { + connect(ws, self.pool.clone(), app, props).await; } } -pub async fn connect(socket: WebSocket, pool: LocalPoolHandle, app: fn(Scope) -> Element) { +pub async fn connect( + socket: WebSocket, + pool: LocalPoolHandle, + app: fn(Scope) -> Element, + props: T, +) where + T: Send + Sync + 'static, +{ let (mut user_ws_tx, mut user_ws_rx) = socket.split(); let (event_tx, event_rx) = mpsc::unbounded_channel(); let (edits_tx, edits_rx) = mpsc::unbounded_channel(); let mut edits_rx = UnboundedReceiverStream::new(edits_rx); let mut event_rx = UnboundedReceiverStream::new(event_rx); let vdom_fut = pool.clone().spawn_pinned(move || async move { - let mut vdom = VirtualDom::new(app); + let mut vdom = VirtualDom::new_with_props(app, props); let edits = vdom.rebuild(); let serialized = serde_json::to_string(&edits.edits).unwrap(); edits_tx.send(serialized).unwrap(); diff --git a/packages/liveview/src/adapters/warp_adapter.rs b/packages/liveview/src/adapters/warp_adapter.rs index 5bdcf671f..b73d02861 100644 --- a/packages/liveview/src/adapters/warp_adapter.rs +++ b/packages/liveview/src/adapters/warp_adapter.rs @@ -9,11 +9,20 @@ use warp::ws::{Message, WebSocket}; #[cfg(feature = "warp")] impl crate::Liveview { pub async fn upgrade(&self, ws: warp::ws::WebSocket, app: fn(Scope) -> Element) { - connect(ws, self.pool.clone(), app).await; + connect(ws, self.pool.clone(), app, ()).await; + } + pub async fn upgrade_with_props(&self, ws: warp::ws::WebSocket, app: fn(Scope) -> Element, props: T) + where + T: Send + Sync + 'static + { + connect(ws, self.pool.clone(), app, props) } } -pub async fn connect(ws: WebSocket, pool: LocalPoolHandle, app: fn(Scope) -> Element) { +pub async fn connect(ws: WebSocket, pool: LocalPoolHandle, app: fn(Scope) -> Element, props: T) +where + T: Send + Sync+ 'static +{ // Use a counter to assign a new unique ID for this user. // Split the socket into a sender and receive of messages. @@ -26,7 +35,7 @@ pub async fn connect(ws: WebSocket, pool: LocalPoolHandle, app: fn(Scope) -> Ele let mut event_rx = UnboundedReceiverStream::new(event_rx); let vdom_fut = pool.spawn_pinned(move || async move { - let mut vdom = VirtualDom::new(app); + let mut vdom = VirtualDom::new_with_props(app, props); let edits = vdom.rebuild();