Fix not "disconnecting" on refresh (hopefully)

This commit is contained in:
Stephen Seo 2022-04-08 12:17:15 +09:00
parent ee8187cabb
commit 70eb1ca121

View file

@ -1223,25 +1223,32 @@ impl Component for Wrapper {
Promise::new(&mut |resolve: js_sys::Function, _reject| {
let window =
web_sys::window().expect("Should be able to get window");
window
.add_event_listener_with_callback("pagehide", &resolve)
.expect("Should be able to set \"pagehide\" callback");
window
.add_event_listener_with_callback("beforeunload", &resolve)
.expect("Should be able to set \"beforeunload\" callback");
});
let js_fut = JsFuture::from(promise);
js_fut.await.ok();
let function = Function::new_no_args(&format!(
let outer_function = Function::new_with_args(
"resolve",
&format!(
"
let xhr = new XMLHttpRequest();
xhr.open('POST', '{}');
xhr.send('{{\"type\": \"disconnect\", \"id\": {}}}');
resolve();
",
BACKEND_URL, player_id
));
function.call0(&function).ok();
),
);
let binded_func =
outer_function.bind1(&outer_function, &resolve);
window
.add_event_listener_with_callback("pagehide", &binded_func)
.expect("Should be able to set \"pagehide\" callback");
window
.add_event_listener_with_callback(
"beforeunload",
&binded_func,
)
.expect("Should be able to set \"beforeunload\" callback");
});
let js_fut = JsFuture::from(promise);
js_fut.await.ok();
WrapperMsg::Reset
});