support starting liveview with props

This commit is contained in:
Ian committed 2022-04-24 00:37:46 -04:00
1 parent 608795426a
commit f332ffc5ad
2 files changed
+28 -6

No files matched your search

+16 -3
View File
@@ -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<T>(&self, ws: WebSocket, app: fn(Scope<T>) -> 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<T>(
socket: WebSocket,
pool: LocalPoolHandle,
app: fn(Scope<T>) -> 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();
+12 -3
View File
@@ -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<T>(&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<T>(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();