use std::time::Duration;
mod common;
use common::Js;
const APP: &[u8] = br#"
(function () {
// Re-execution (hot update): the isolate is alive, so __n is preserved. Report
// it and re-park the event loop (the previous loop returned on the reload).
if (globalThis.__started) {
Deno.core.ops.op_emit("phase", "update:" + globalThis.__n);
globalThis.__loop();
return;
}
// Cold start.
globalThis.__started = true;
globalThis.__n = 41;
globalThis.__loop = () => (async () => {
for (;;) {
const m = await Deno.core.ops.op_next_event();
if (m == null) return; // shutdown
if (m.t === "reload") return; // yield to Rust so it can re-exec the app
if (m.t === "uiEvent") {
globalThis.__n++;
Deno.core.ops.op_emit("phase", "click:" + globalThis.__n);
}
}
})();
Deno.core.ops.op_emit("phase", "init:" + globalThis.__n);
globalThis.__loop();
})();
"#;
#[test]
fn hot_reload_preserves_isolate_state() {
let js = Js::spawn("hot_reload", APP);
let recv = || {
js.emitted("phase", Duration::from_secs(10))
.as_str()
.unwrap()
.to_string()
};
assert_eq!(recv(), "init:41");
js.click();
assert_eq!(recv(), "click:42");
js.reload.send(()).expect("send reload");
assert_eq!(
recv(),
"update:42",
"global state lost across reload — isolate was torn down, not refreshed"
);
js.click();
assert_eq!(
recv(),
"click:43",
"event loop did not resume after hot reload"
);
eprintln!("OK isolate persisted across hot reload; state preserved and loop resumed");
}