use dioxus_devtools::{DevserverMsg, HotReloadMsg};
use futures_channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use js_sys::JsString;
use std::fmt::Display;
use std::time::Duration;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen::{closure::Closure, JsValue};
use web_sys::{window, CloseEvent, MessageEvent, WebSocket};
const POLL_INTERVAL_MIN: i32 = 250;
const POLL_INTERVAL_MAX: i32 = 4000;
const POLL_INTERVAL_SCALE_FACTOR: i32 = 2;
const TOAST_TIMEOUT: Duration = Duration::from_secs(5);
const TOAST_TIMEOUT_LONG: Duration = Duration::from_secs(3600);
pub(crate) fn init(config: &crate::Config) -> UnboundedReceiver<HotReloadMsg> {
let (tx, rx) = unbounded();
make_ws(tx.clone(), POLL_INTERVAL_MIN, false);
playground(tx);
if config.panic_hook {
std::panic::set_hook(Box::new(|info| {
hook_impl(info);
}));
}
rx
}
fn make_ws(tx: UnboundedSender<HotReloadMsg>, poll_interval: i32, reload: bool) {
let location = web_sys::window().unwrap().location();
let url = format!(
"{protocol}//{host}/_dioxus?build_id={build_id}",
protocol = match location.protocol().unwrap() {
prot if prot == "https:" => "wss:",
_ => "ws:",
},
host = location.host().unwrap(),
build_id = dioxus_cli_config::build_id(),
);
let ws = WebSocket::new(&url).unwrap();
let tx_ = tx.clone();
ws.set_onmessage(Some(
Closure::<dyn FnMut(MessageEvent)>::new(move |e: MessageEvent| {
let Ok(text) = e.data().dyn_into::<JsString>() else {
return;
};
let string: String = text.into();
let string = Box::leak(string.into_boxed_str());
match serde_json::from_str::<DevserverMsg>(string) {
Ok(DevserverMsg::HotReload(hr)) => _ = tx_.unbounded_send(hr),
Ok(DevserverMsg::Shutdown) => {
web_sys::console::error_1(&"Connection to the devserver was closed".into())
}
Ok(DevserverMsg::FullReloadStart) => show_toast(
"Your app is being rebuilt.",
"A non-hot-reloadable change occurred and we must rebuild.",
ToastLevel::Info,
TOAST_TIMEOUT_LONG,
false,
),
Ok(DevserverMsg::HotPatchStart) => show_toast(
"Hot-patching app...",
"Hot-patching modified Rust code.",
ToastLevel::Info,
TOAST_TIMEOUT_LONG,
false,
),
Ok(DevserverMsg::FullReloadFailed) => show_toast(
"Oops! The build failed.",
"We tried to rebuild your app, but something went wrong.",
ToastLevel::Error,
TOAST_TIMEOUT_LONG,
false,
),
Ok(DevserverMsg::FullReloadCommand) => {
show_toast(
"Successfully rebuilt.",
"Your app was rebuilt successfully and without error.",
ToastLevel::Success,
TOAST_TIMEOUT,
true,
);
window().unwrap().location().reload().unwrap()
}
Err(e) => web_sys::console::error_1(
&format!("Error parsing devserver message: {}", e).into(),
),
e => {
web_sys::console::error_1(
&format!("Error parsing devserver message: {:?}", e).into(),
);
}
}
})
.into_js_value()
.as_ref()
.unchecked_ref(),
));
ws.set_onclose(Some(
Closure::<dyn FnMut(CloseEvent)>::new(move |e: CloseEvent| {
if e.code() == 1001 {
return;
}
let tx = tx.clone();
web_sys::window()
.unwrap()
.set_timeout_with_callback_and_timeout_and_arguments_0(
Closure::<dyn FnMut()>::new(move || {
make_ws(
tx.clone(),
POLL_INTERVAL_MAX.min(poll_interval * POLL_INTERVAL_SCALE_FACTOR),
true,
);
})
.into_js_value()
.as_ref()
.unchecked_ref(),
poll_interval,
)
.unwrap();
})
.into_js_value()
.as_ref()
.unchecked_ref(),
));
ws.set_onopen(Some(
Closure::<dyn FnMut(MessageEvent)>::new(move |_evt| {
if reload {
window().unwrap().location().reload().unwrap();
}
})
.into_js_value()
.as_ref()
.unchecked_ref(),
));
if !reload {
let ws: &JsValue = ws.as_ref();
dioxus_interpreter_js::minimal_bindings::monkeyPatchConsole(ws.clone());
}
}
pub(crate) enum ToastLevel {
Success,
Info,
Error,
}
impl Display for ToastLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ToastLevel::Success => write!(f, "success"),
ToastLevel::Info => write!(f, "info"),
ToastLevel::Error => write!(f, "error"),
}
}
}
#[wasm_bindgen(inline_js = r#"
export function js_show_toast(header_text, message, level, as_ms) {
if (typeof showDXToast !== "undefined") {{
window.showDXToast(header_text, message, level, as_ms);
}}
}
export function js_schedule_toast(header_text, message, level, as_ms) {
if (typeof scheduleDXToast !== "undefined") {{
window.scheduleDXToast(header_text, message, level, as_ms);
}}
}
"#)]
extern "C" {
fn js_schedule_toast(header_text: &str, message: &str, level: String, as_ms: u32);
fn js_show_toast(header_text: &str, message: &str, level: String, as_ms: u32);
}
pub(crate) fn show_toast(
header_text: &str,
message: &str,
level: ToastLevel,
duration: Duration,
after_reload: bool,
) {
let as_ms: u32 = duration.as_millis().try_into().unwrap();
match after_reload {
true => js_schedule_toast(header_text, message, level.to_string(), as_ms),
false => js_show_toast(header_text, message, level.to_string(), as_ms),
}
}
pub(crate) fn invalidate_browser_asset_cache() {
let links = web_sys::window()
.unwrap()
.document()
.unwrap()
.query_selector_all("link[rel=stylesheet]")
.unwrap();
let noise = js_sys::Math::random();
for x in 0..links.length() {
use wasm_bindgen::JsCast;
let link: web_sys::Element = links.get(x).unwrap().unchecked_into();
if let Some(href) = link.get_attribute("href") {
let (url, query) = href.split_once('?').unwrap_or((&href, ""));
let mut query_params: Vec<&str> = query.split('&').collect();
query_params.retain(|param| !param.starts_with("dx_force_reload="));
let force_reload = format!("dx_force_reload={noise}");
query_params.push(&force_reload);
let query = query_params.join("&");
_ = link.set_attribute("href", &format!("{url}?{query}"));
}
}
}
fn playground(tx: UnboundedSender<HotReloadMsg>) {
let window = web_sys::window().expect("this code should be running in a web context");
let binding = Closure::<dyn FnMut(MessageEvent)>::new(move |e: MessageEvent| {
let Ok(text) = e.data().dyn_into::<JsString>() else {
return;
};
let string: String = text.into();
let Ok(hr_msg) = serde_json::from_str::<HotReloadMsg>(&string) else {
return;
};
_ = tx.unbounded_send(hr_msg);
});
let callback = binding.as_ref().unchecked_ref();
window
.add_event_listener_with_callback("message", callback)
.expect("event listener should be added successfully");
binding.forget();
}
fn hook_impl(info: &std::panic::PanicHookInfo) {
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn error(msg: String);
type Error;
#[wasm_bindgen(constructor)]
fn new() -> Error;
#[wasm_bindgen(structural, method, getter)]
fn stack(error: &Error) -> String;
}
let mut msg = info.to_string();
msg.push_str("\n\nStack:\n\n");
let e = Error::new();
let stack = e.stack();
msg.push_str(&stack);
msg.push_str("\n\n");
error(msg.clone());
show_toast(
"App panicked! See console for details.",
&msg,
ToastLevel::Error,
TOAST_TIMEOUT_LONG,
false,
)
}