Merge pull request #1213 from Demonthos/prep-sledgehammer-liveview

Prepare for Sledgehammer Bindgen in Liveview
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2023-07-18 18:10:48 -07:00
commit 1d371bf971
6 files changed
+29 -35

No files matched your search

+2 -2
View File
@@ -693,13 +693,13 @@ impl<'src> ScopeState {
raw_ref.downcast_mut::<State>() raw_ref.downcast_mut::<State>()
}) })
.expect( .expect(
r###" r#"
Unable to retrieve the hook that was initialized at this index. Unable to retrieve the hook that was initialized at this index.
Consult the `rules of hooks` to understand how to use hooks properly. 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. You likely used the hook in a conditional. Hooks rely on consistent ordering between renders.
Functions prefixed with "use" should never be called conditionally. Functions prefixed with "use" should never be called conditionally.
"###, "#,
) )
} }
} }
@@ -11,13 +11,14 @@ pub fn axum_socket(ws: WebSocket) -> impl LiveViewSocket {
.sink_map_err(|_| LiveViewError::SendingFailed) .sink_map_err(|_| LiveViewError::SendingFailed)
} }
fn transform_rx(message: Result<Message, axum::Error>) -> Result<String, LiveViewError> { fn transform_rx(message: Result<Message, axum::Error>) -> Result<Vec<u8>, LiveViewError> {
message message
.map_err(|_| LiveViewError::SendingFailed)? .map_err(|_| LiveViewError::SendingFailed)?
.into_text() .into_text()
.map(|s| s.into_bytes())
.map_err(|_| LiveViewError::SendingFailed) .map_err(|_| LiveViewError::SendingFailed)
} }
async fn transform_tx(message: String) -> Result<Message, axum::Error> { async fn transform_tx(message: Vec<u8>) -> Result<Message, axum::Error> {
Ok(Message::Text(message)) Ok(Message::Text(String::from_utf8_lossy(&message).to_string()))
} }
@@ -12,14 +12,12 @@ pub fn salvo_socket(ws: WebSocket) -> impl LiveViewSocket {
.sink_map_err(|_| LiveViewError::SendingFailed) .sink_map_err(|_| LiveViewError::SendingFailed)
} }
fn transform_rx(message: Result<Message, salvo::Error>) -> Result<String, LiveViewError> { fn transform_rx(message: Result<Message, salvo::Error>) -> Result<Vec<u8>, LiveViewError> {
let as_bytes = message.map_err(|_| LiveViewError::SendingFailed)?; let as_bytes = message.map_err(|_| LiveViewError::SendingFailed)?;
let msg = String::from_utf8(as_bytes.into_bytes()).map_err(|_| LiveViewError::SendingFailed)?; Ok(as_bytes.into())
Ok(msg)
} }
async fn transform_tx(message: String) -> Result<Message, salvo::Error> { async fn transform_tx(message: Vec<u8>) -> Result<Message, salvo::Error> {
Ok(Message::text(message)) Ok(Message::text(String::from_utf8_lossy(&message).to_string()))
} }
@@ -11,18 +11,15 @@ pub fn warp_socket(ws: WebSocket) -> impl LiveViewSocket {
.sink_map_err(|_| LiveViewError::SendingFailed) .sink_map_err(|_| LiveViewError::SendingFailed)
} }
fn transform_rx(message: Result<Message, warp::Error>) -> Result<String, LiveViewError> { fn transform_rx(message: Result<Message, warp::Error>) -> Result<Vec<u8>, LiveViewError> {
// destructure the message into the buffer we got from warp // destructure the message into the buffer we got from warp
let msg = message let msg = message
.map_err(|_| LiveViewError::SendingFailed)? .map_err(|_| LiveViewError::SendingFailed)?
.into_bytes(); .into_bytes();
// transform it back into a string, saving us the allocation
let msg = String::from_utf8(msg).map_err(|_| LiveViewError::SendingFailed)?;
Ok(msg) Ok(msg)
} }
async fn transform_tx(message: String) -> Result<Message, warp::Error> { async fn transform_tx(message: Vec<u8>) -> Result<Message, warp::Error> {
Ok(Message::text(message)) Ok(Message::text(String::from_utf8_lossy(&message).to_string()))
} }
+15 -11
View File
@@ -87,16 +87,16 @@ impl LiveViewPool {
/// } /// }
/// ``` /// ```
pub trait LiveViewSocket: pub trait LiveViewSocket:
SinkExt<String, Error = LiveViewError> SinkExt<Vec<u8>, Error = LiveViewError>
+ StreamExt<Item = Result<String, LiveViewError>> + StreamExt<Item = Result<Vec<u8>, LiveViewError>>
+ Send + Send
+ 'static + 'static
{ {
} }
impl<S> LiveViewSocket for S where impl<S> LiveViewSocket for S where
S: SinkExt<String, Error = LiveViewError> S: SinkExt<Vec<u8>, Error = LiveViewError>
+ StreamExt<Item = Result<String, LiveViewError>> + StreamExt<Item = Result<Vec<u8>, LiveViewError>>
+ Send + Send
+ 'static + 'static
{ {
@@ -126,7 +126,7 @@ pub async fn run(mut vdom: VirtualDom, ws: impl LiveViewSocket) -> Result<(), Li
pin_mut!(ws); pin_mut!(ws);
// send the initial render to the client // send the initial render to the client
ws.send(edits).await?; ws.send(edits.into_bytes()).await?;
// Create the a proxy for query engine // Create the a proxy for query engine
let (query_tx, mut query_rx) = tokio::sync::mpsc::unbounded_channel(); 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() => { evt = ws.next() => {
match evt.as_ref().map(|o| o.as_deref()) { match evt.as_ref().map(|o| o.as_deref()) {
// respond with a pong every ping to keep the websocket alive // respond with a pong every ping to keep the websocket alive
Some(Ok("__ping__")) => { Some(Ok(b"__ping__")) => {
ws.send("__pong__".to_string()).await?; ws.send(b"__pong__".to_vec()).await?;
} }
Some(Ok(evt)) => { Some(Ok(evt)) => {
if let Ok(message) = serde_json::from_str::<IpcMessage>(evt) { if let Ok(message) = serde_json::from_str::<IpcMessage>(&String::from_utf8_lossy(evt)) {
match message { match message {
IpcMessage::Event(evt) => { IpcMessage::Event(evt) => {
// Intercept the mounted event and insert a custom element type // 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 // handle any new queries
Some(query) = query_rx.recv() => { 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 => { 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))) .render_with_deadline(tokio::time::sleep(Duration::from_millis(10)))
.await; .await;
ws.send(serde_json::to_string(&ClientUpdate::Edits(edits)).unwrap()) ws.send(
.await?; serde_json::to_string(&ClientUpdate::Edits(edits))
.unwrap()
.into_bytes(),
)
.await?;
} }
} }
+1 -7
View File
@@ -10,8 +10,6 @@ mod rt;
use dioxus_core::ScopeState; use dioxus_core::ScopeState;
pub use rt::*; pub use rt::*;
use crate::rt::claim_rt;
pub fn use_init_signal_rt(cx: &ScopeState) { pub fn use_init_signal_rt(cx: &ScopeState) {
cx.use_hook(|| { cx.use_hook(|| {
let rt = claim_rt(cx.schedule_update_any()); let rt = claim_rt(cx.schedule_update_any());
@@ -96,11 +94,7 @@ impl<T: Clone + 'static> std::ops::Deref for Signal<T> {
impl<T> std::clone::Clone for Signal<T> { impl<T> std::clone::Clone for Signal<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { *self
t: PhantomData,
id: self.id,
rt: self.rt,
}
} }
} }