From ee28c89f5e4d2fab5d691108c3d32fae19379c77 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Tue, 18 Jul 2023 12:53:38 -0700 Subject: [PATCH 1/3] Change to a stream of bytes for liveview to make using sledgehammer later non-breaking --- packages/liveview/src/pool.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/liveview/src/pool.rs b/packages/liveview/src/pool.rs index 089d0bad4..5ed340eff 100644 --- a/packages/liveview/src/pool.rs +++ b/packages/liveview/src/pool.rs @@ -87,16 +87,16 @@ impl LiveViewPool { /// } /// ``` pub trait LiveViewSocket: - SinkExt - + StreamExt> + SinkExt, Error = LiveViewError> + + StreamExt, LiveViewError>> + Send + 'static { } impl LiveViewSocket for S where - S: SinkExt - + StreamExt> + S: SinkExt, Error = LiveViewError> + + StreamExt, LiveViewError>> + Send + 'static { From 8188011fe3d78e04204982558ec618c5f973d835 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Tue, 18 Jul 2023 13:03:34 -0700 Subject: [PATCH 2/3] fix adapters --- packages/liveview/src/adapters/axum_adapter.rs | 7 ++++--- .../liveview/src/adapters/salvo_adapter.rs | 10 ++++------ packages/liveview/src/adapters/warp_adapter.rs | 9 +++------ packages/liveview/src/pool.rs | 18 +++++++++++------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/liveview/src/adapters/axum_adapter.rs b/packages/liveview/src/adapters/axum_adapter.rs index 646cde38e..dda37acc1 100644 --- a/packages/liveview/src/adapters/axum_adapter.rs +++ b/packages/liveview/src/adapters/axum_adapter.rs @@ -11,13 +11,14 @@ pub fn axum_socket(ws: WebSocket) -> impl LiveViewSocket { .sink_map_err(|_| LiveViewError::SendingFailed) } -fn transform_rx(message: Result) -> Result { +fn transform_rx(message: Result) -> Result, LiveViewError> { message .map_err(|_| LiveViewError::SendingFailed)? .into_text() + .map(|s| s.into_bytes()) .map_err(|_| LiveViewError::SendingFailed) } -async fn transform_tx(message: String) -> Result { - Ok(Message::Text(message)) +async fn transform_tx(message: Vec) -> Result { + Ok(Message::Text(String::from_utf8_lossy(&message).to_string())) } diff --git a/packages/liveview/src/adapters/salvo_adapter.rs b/packages/liveview/src/adapters/salvo_adapter.rs index 2c8912a57..138196da7 100644 --- a/packages/liveview/src/adapters/salvo_adapter.rs +++ b/packages/liveview/src/adapters/salvo_adapter.rs @@ -12,14 +12,12 @@ pub fn salvo_socket(ws: WebSocket) -> impl LiveViewSocket { .sink_map_err(|_| LiveViewError::SendingFailed) } -fn transform_rx(message: Result) -> Result { +fn transform_rx(message: Result) -> Result, LiveViewError> { let as_bytes = message.map_err(|_| LiveViewError::SendingFailed)?; - let msg = String::from_utf8(as_bytes.into_bytes()).map_err(|_| LiveViewError::SendingFailed)?; - - Ok(msg) + Ok(as_bytes.into()) } -async fn transform_tx(message: String) -> Result { - Ok(Message::text(message)) +async fn transform_tx(message: Vec) -> Result { + Ok(Message::text(String::from_utf8_lossy(&message).to_string())) } diff --git a/packages/liveview/src/adapters/warp_adapter.rs b/packages/liveview/src/adapters/warp_adapter.rs index e5c821ce3..9ee8c6fe4 100644 --- a/packages/liveview/src/adapters/warp_adapter.rs +++ b/packages/liveview/src/adapters/warp_adapter.rs @@ -11,18 +11,15 @@ pub fn warp_socket(ws: WebSocket) -> impl LiveViewSocket { .sink_map_err(|_| LiveViewError::SendingFailed) } -fn transform_rx(message: Result) -> Result { +fn transform_rx(message: Result) -> Result, LiveViewError> { // destructure the message into the buffer we got from warp let msg = message .map_err(|_| LiveViewError::SendingFailed)? .into_bytes(); - // transform it back into a string, saving us the allocation - let msg = String::from_utf8(msg).map_err(|_| LiveViewError::SendingFailed)?; - Ok(msg) } -async fn transform_tx(message: String) -> Result { - Ok(Message::text(message)) +async fn transform_tx(message: Vec) -> Result { + Ok(Message::text(String::from_utf8_lossy(&message).to_string())) } diff --git a/packages/liveview/src/pool.rs b/packages/liveview/src/pool.rs index 5ed340eff..9bf62cefa 100644 --- a/packages/liveview/src/pool.rs +++ b/packages/liveview/src/pool.rs @@ -126,7 +126,7 @@ pub async fn run(mut vdom: VirtualDom, ws: impl LiveViewSocket) -> Result<(), Li pin_mut!(ws); // send the initial render to the client - ws.send(edits).await?; + ws.send(edits.into_bytes()).await?; // Create the a proxy for query engine let (query_tx, mut query_rx) = tokio::sync::mpsc::unbounded_channel(); @@ -156,11 +156,11 @@ pub async fn run(mut vdom: VirtualDom, ws: impl LiveViewSocket) -> Result<(), Li evt = ws.next() => { match evt.as_ref().map(|o| o.as_deref()) { // respond with a pong every ping to keep the websocket alive - Some(Ok("__ping__")) => { - ws.send("__pong__".to_string()).await?; + Some(Ok(b"__ping__")) => { + ws.send(b"__pong__".to_vec()).await?; } Some(Ok(evt)) => { - if let Ok(message) = serde_json::from_str::(evt) { + if let Ok(message) = serde_json::from_str::(&*String::from_utf8_lossy(evt)) { match message { IpcMessage::Event(evt) => { // Intercept the mounted event and insert a custom element type @@ -196,7 +196,7 @@ pub async fn run(mut vdom: VirtualDom, ws: impl LiveViewSocket) -> Result<(), Li // handle any new queries Some(query) = query_rx.recv() => { - ws.send(serde_json::to_string(&ClientUpdate::Query(query)).unwrap()).await?; + ws.send(serde_json::to_string(&ClientUpdate::Query(query)).unwrap().into_bytes()).await?; } Some(msg) = hot_reload_wait => { @@ -218,8 +218,12 @@ pub async fn run(mut vdom: VirtualDom, ws: impl LiveViewSocket) -> Result<(), Li .render_with_deadline(tokio::time::sleep(Duration::from_millis(10))) .await; - ws.send(serde_json::to_string(&ClientUpdate::Edits(edits)).unwrap()) - .await?; + ws.send( + serde_json::to_string(&ClientUpdate::Edits(edits)) + .unwrap() + .into_bytes(), + ) + .await?; } } From a96ccdc6f3ef536b1978d49df53660fa5089fe23 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Tue, 18 Jul 2023 13:16:17 -0700 Subject: [PATCH 3/3] fix clippy --- packages/core/src/scopes.rs | 4 ++-- packages/liveview/src/pool.rs | 2 +- packages/signals/src/lib.rs | 8 +------- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/core/src/scopes.rs b/packages/core/src/scopes.rs index ff94c456d..87e894ef3 100644 --- a/packages/core/src/scopes.rs +++ b/packages/core/src/scopes.rs @@ -688,13 +688,13 @@ impl<'src> ScopeState { raw_ref.downcast_mut::() }) .expect( - r###" + r#" Unable to retrieve the hook that was initialized at this index. Consult the `rules of hooks` to understand how to use hooks properly. You likely used the hook in a conditional. Hooks rely on consistent ordering between renders. Functions prefixed with "use" should never be called conditionally. - "###, + "#, ) } } diff --git a/packages/liveview/src/pool.rs b/packages/liveview/src/pool.rs index 9bf62cefa..dc7e4ea9e 100644 --- a/packages/liveview/src/pool.rs +++ b/packages/liveview/src/pool.rs @@ -160,7 +160,7 @@ pub async fn run(mut vdom: VirtualDom, ws: impl LiveViewSocket) -> Result<(), Li ws.send(b"__pong__".to_vec()).await?; } Some(Ok(evt)) => { - if let Ok(message) = serde_json::from_str::(&*String::from_utf8_lossy(evt)) { + if let Ok(message) = serde_json::from_str::(&String::from_utf8_lossy(evt)) { match message { IpcMessage::Event(evt) => { // Intercept the mounted event and insert a custom element type diff --git a/packages/signals/src/lib.rs b/packages/signals/src/lib.rs index 4e950e0dc..5b6a740cf 100644 --- a/packages/signals/src/lib.rs +++ b/packages/signals/src/lib.rs @@ -10,8 +10,6 @@ mod rt; use dioxus_core::ScopeState; pub use rt::*; -use crate::rt::claim_rt; - pub fn use_init_signal_rt(cx: &ScopeState) { cx.use_hook(|| { let rt = claim_rt(cx.schedule_update_any()); @@ -96,11 +94,7 @@ impl std::ops::Deref for Signal { impl std::clone::Clone for Signal { fn clone(&self) -> Self { - Self { - t: PhantomData, - id: self.id, - rt: self.rt, - } + *self } }