diff --git a/src/platform_impl/web_sys/event_loop.rs b/src/platform_impl/web_sys/event_loop.rs index 78a70f01..62bbd82a 100644 --- a/src/platform_impl/web_sys/event_loop.rs +++ b/src/platform_impl/web_sys/event_loop.rs @@ -259,7 +259,7 @@ fn add_event( { let elrs = elrs.clone(); - target.add_event_listener_with_callback(event, Closure::wrap(Box::new(move |event: E| { + let closure = Closure::wrap(Box::new(move |event: E| { // Don't capture the event if the events loop has been destroyed match &*elrs.runner.borrow() { Some(ref runner) if runner.control == ControlFlow::Exit => return, @@ -272,7 +272,10 @@ fn add_event( event_ref.cancel_bubble(); handler(&elrs, event); - }) as Box).as_ref().unchecked_ref()); + }) as Box); + + target.add_event_listener_with_callback(event, &closure.as_ref().unchecked_ref()); + closure.forget(); // TODO: don't leak this. } impl ELRShared { diff --git a/src/platform_impl/web_sys/window.rs b/src/platform_impl/web_sys/window.rs index 5a41c5f9..684c580c 100644 --- a/src/platform_impl/web_sys/window.rs +++ b/src/platform_impl/web_sys/window.rs @@ -69,12 +69,13 @@ impl Window { let runner = target.runner.clone(); let redraw = Box::new(move || { let runner = runner.clone(); - window().request_animation_frame(Closure::wrap(Box::new(move |_: f64| { + let closure = Closure::once_into_js(move |_: f64| { runner.send_event(Event::WindowEvent { window_id: RootWI(WindowId), event: WindowEvent::RedrawRequested }); - }) as Box).as_ref().unchecked_ref()); + }); + window().request_animation_frame(closure.as_ref().unchecked_ref()); }); let window = Window {