feat: get liveview working across the boar
This commit is contained in:
1 parent
7790d2c065
commit
b95069619f
9 files changed
+229
-312
No files matched your search
@@ -6,22 +6,60 @@ use std::time::Duration;
|
||||
use tokio_util::task::LocalPoolHandle;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LiveView {
|
||||
pub struct LiveViewPool {
|
||||
pub(crate) pool: LocalPoolHandle,
|
||||
}
|
||||
|
||||
impl Default for LiveView {
|
||||
impl Default for LiveViewPool {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl LiveView {
|
||||
impl LiveViewPool {
|
||||
pub fn new() -> Self {
|
||||
LiveView {
|
||||
LiveViewPool {
|
||||
pool: LocalPoolHandle::new(16),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn launch(
|
||||
&self,
|
||||
ws: impl LiveViewSocket,
|
||||
app: fn(Scope<()>) -> Element,
|
||||
) -> Result<(), LiveViewError> {
|
||||
self.launch_with_props(ws, app, ()).await
|
||||
}
|
||||
|
||||
pub async fn launch_with_props<T: Send + 'static>(
|
||||
&self,
|
||||
ws: impl LiveViewSocket,
|
||||
app: fn(Scope<T>) -> Element,
|
||||
props: T,
|
||||
) -> Result<(), LiveViewError> {
|
||||
match self.pool.spawn_pinned(move || run(app, props, ws)).await {
|
||||
Ok(Ok(_)) => Ok(()),
|
||||
Ok(Err(e)) => Err(e),
|
||||
Err(_) => Err(LiveViewError::SendingFailed),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A LiveViewSocket is a Sink and Stream of Strings that Dioxus uses to communicate with the client
|
||||
pub trait LiveViewSocket:
|
||||
SinkExt<String, Error = LiveViewError>
|
||||
+ StreamExt<Item = Result<String, LiveViewError>>
|
||||
+ Send
|
||||
+ 'static
|
||||
{
|
||||
}
|
||||
|
||||
impl<S> LiveViewSocket for S where
|
||||
S: SinkExt<String, Error = LiveViewError>
|
||||
+ StreamExt<Item = Result<String, LiveViewError>>
|
||||
+ Send
|
||||
+ 'static
|
||||
{
|
||||
}
|
||||
|
||||
/// The primary event loop for the VirtualDom waiting for user input
|
||||
@@ -31,11 +69,10 @@ impl LiveView {
|
||||
/// As long as your framework can provide a Sink and Stream of Strings, you can use this function.
|
||||
///
|
||||
/// You might need to transform the error types of the web backend into the LiveView error type.
|
||||
pub async fn liveview_eventloop<T>(
|
||||
pub async fn run<T>(
|
||||
app: Component<T>,
|
||||
props: T,
|
||||
ws_tx: impl SinkExt<String, Error = LiveViewError>,
|
||||
ws_rx: impl StreamExt<Item = Result<String, LiveViewError>>,
|
||||
ws: impl LiveViewSocket,
|
||||
) -> Result<(), LiveViewError>
|
||||
where
|
||||
T: Send + 'static,
|
||||
@@ -46,10 +83,10 @@ where
|
||||
let edits = serde_json::to_string(&vdom.rebuild()).unwrap();
|
||||
|
||||
// pin the futures so we can use select!
|
||||
pin_mut!(ws_tx);
|
||||
pin_mut!(ws_rx);
|
||||
pin_mut!(ws);
|
||||
|
||||
ws_tx.send(edits).await?;
|
||||
// send the initial render to the client
|
||||
ws.send(edits).await?;
|
||||
|
||||
// desktop uses this wrapper struct thing around the actual event itself
|
||||
// this is sorta driven by tao/wry
|
||||
@@ -63,17 +100,15 @@ where
|
||||
// poll any futures or suspense
|
||||
_ = vdom.wait_for_work() => {}
|
||||
|
||||
evt = ws_rx.next() => {
|
||||
evt = ws.next() => {
|
||||
match evt {
|
||||
Some(Ok(evt)) => {
|
||||
let event: IpcMessage = serde_json::from_str(&evt).unwrap();
|
||||
let event = event.params;
|
||||
vdom.handle_event(&event.name, event.data.into_any(), event.element, event.bubbles);
|
||||
}
|
||||
Some(Err(_e)) => {
|
||||
// log this I guess?
|
||||
// when would we get an error here?
|
||||
if let Ok(IpcMessage { params }) = serde_json::from_str::<IpcMessage>(&evt) {
|
||||
vdom.handle_event(¶ms.name, params.data.into_any(), params.element, params.bubbles);
|
||||
}
|
||||
}
|
||||
// log this I guess? when would we get an error here?
|
||||
Some(Err(_e)) => {},
|
||||
None => return Ok(()),
|
||||
}
|
||||
}
|
||||
@@ -83,6 +118,6 @@ where
|
||||
.render_with_deadline(tokio::time::sleep(Duration::from_millis(10)))
|
||||
.await;
|
||||
|
||||
ws_tx.send(serde_json::to_string(&edits).unwrap()).await?;
|
||||
ws.send(serde_json::to_string(&edits).unwrap()).await?;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user