Prevent callbacks from being destroyed too early

This commit is contained in:
Ben Merritt 2019-06-06 21:37:57 -07:00
parent 77cd3adb01
commit 94f6294c0a
No known key found for this signature in database
GPG key ID: F8AD20ED4E6239B7
2 changed files with 8 additions and 4 deletions

View file

@ -259,7 +259,7 @@ fn add_event<T: 'static, E, F>(
{
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<T: 'static, E, F>(
event_ref.cancel_bubble();
handler(&elrs, event);
}) as Box<FnMut(E)>).as_ref().unchecked_ref());
}) as Box<FnMut(E)>);
target.add_event_listener_with_callback(event, &closure.as_ref().unchecked_ref());
closure.forget(); // TODO: don't leak this.
}
impl<T> ELRShared<T> {

View file

@ -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<Fn(f64)>).as_ref().unchecked_ref());
});
window().request_animation_frame(closure.as_ref().unchecked_ref());
});
let window = Window {