implement for web and desktop

This commit is contained in:
= committed 2022-12-15 21:16:37 -06:00
1 parent ab743e01b7
commit 0d9c350d5e
8 files changed
+69 -30

No files matched your search

+8 -4
View File
@@ -2,12 +2,13 @@
use futures_channel::mpsc::UnboundedReceiver;
use dioxus_core::Template;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsCast;
use web_sys::{MessageEvent, WebSocket};
#[cfg(not(debug_assertions))]
pub(crate) fn init() -> UnboundedReceiver<String> {
pub(crate) fn init() -> UnboundedReceiver<Template<'static>> {
let (tx, rx) = futures_channel::mpsc::unbounded();
std::mem::forget(tx);
@@ -16,7 +17,7 @@ pub(crate) fn init() -> UnboundedReceiver<String> {
}
#[cfg(debug_assertions)]
pub(crate) fn init() -> UnboundedReceiver<String> {
pub(crate) fn init() -> UnboundedReceiver<Template<'static>> {
use std::convert::TryInto;
let window = web_sys::window().unwrap();
@@ -39,8 +40,11 @@ pub(crate) fn init() -> UnboundedReceiver<String> {
// change the rsx when new data is received
let cl = Closure::wrap(Box::new(move |e: MessageEvent| {
if let Ok(text) = e.data().dyn_into::<js_sys::JsString>() {
if let Ok(val) = text.try_into() {
_ = tx.unbounded_send(val);
let text: Result<String, _> = text.try_into();
if let Ok(string) = text {
if let Ok(template) = serde_json::from_str(Box::leak(string.into_boxed_str())) {
_ = tx.unbounded_send(template);
}
}
}
}) as Box<dyn FnMut(MessageEvent)>);
+9 -5
View File
@@ -202,19 +202,23 @@ pub async fn run_with_props<T: 'static>(root: fn(Scope<T>) -> Element, root_prop
// if virtualdom has nothing, wait for it to have something before requesting idle time
// if there is work then this future resolves immediately.
let mut res = {
let (mut res, template) = {
let work = dom.wait_for_work().fuse();
pin_mut!(work);
futures_util::select! {
_ = work => None,
_new_template = hotreload_rx.next() => {
todo!("Implement hot reload");
_ = work => (None, None),
new_template = hotreload_rx.next() => {
(None, new_template)
}
evt = rx.next() => evt
evt = rx.next() => (evt, None)
}
};
if let Some(template) = template {
dom.replace_template(template);
}
// Dequeue all of the events from the channel in send order
// todo: we should re-order these if possible
while let Some(evt) = res {