From 70eb1ca121c3c198a5ae9b247c0beefc626132c7 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 8 Apr 2022 12:17:15 +0900 Subject: [PATCH] Fix not "disconnecting" on refresh (hopefully) --- front_end/src/yew_components.rs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/front_end/src/yew_components.rs b/front_end/src/yew_components.rs index 0ddeb79..03f6b67 100644 --- a/front_end/src/yew_components.rs +++ b/front_end/src/yew_components.rs @@ -1223,26 +1223,33 @@ impl Component for Wrapper { Promise::new(&mut |resolve: js_sys::Function, _reject| { let window = web_sys::window().expect("Should be able to get window"); + 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 + ), + ); + let binded_func = + outer_function.bind1(&outer_function, &resolve); window - .add_event_listener_with_callback("pagehide", &resolve) + .add_event_listener_with_callback("pagehide", &binded_func) .expect("Should be able to set \"pagehide\" callback"); window - .add_event_listener_with_callback("beforeunload", &resolve) + .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(); - let function = Function::new_no_args(&format!( - " - let xhr = new XMLHttpRequest(); - xhr.open('POST', '{}'); - xhr.send('{{\"type\": \"disconnect\", \"id\": {}}}'); - ", - BACKEND_URL, player_id - )); - function.call0(&function).ok(); - WrapperMsg::Reset });