1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#![allow(dead_code)]

use futures_channel::mpsc::UnboundedReceiver;

use dioxus_core::Template;

pub(crate) fn init() -> UnboundedReceiver<Template<'static>> {
    use std::convert::TryInto;
    use wasm_bindgen::closure::Closure;
    use wasm_bindgen::JsCast;
    use web_sys::{MessageEvent, WebSocket};

    use serde::Deserialize;

    let window = web_sys::window().unwrap();

    let protocol = match window.location().protocol().unwrap() {
        prot if prot == "https:" => "wss:",
        _ => "ws:",
    };

    let url = format!(
        "{protocol}//{}/_dioxus/hot_reload",
        window.location().host().unwrap()
    );

    let ws = WebSocket::new(&url).unwrap();

    let (tx, rx) = futures_channel::mpsc::unbounded();

    // 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>() {
            let text: Result<String, _> = text.try_into();
            if let Ok(string) = text {
                let val = serde_json::from_str::<serde_json::Value>(&string).unwrap();
                // leak the value
                let val: &'static serde_json::Value = Box::leak(Box::new(val));
                let template: Template<'_> = Template::deserialize(val).unwrap();
                tx.unbounded_send(template).unwrap();
            }
        }
    }) as Box<dyn FnMut(MessageEvent)>);

    ws.set_onmessage(Some(cl.as_ref().unchecked_ref()));
    cl.forget();

    rx
}