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)>);