Skip to content

Commit

Permalink
web: Fix Closure being dropped before timer is done
Browse files Browse the repository at this point in the history
  • Loading branch information
lann authored and DoumanAsh committed Dec 31, 2021
1 parent 5973dd2 commit 0dccab3
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/timer/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ extern "C" {
fn clearTimeout(id: i32);
}

struct TimerHandle(i32);
struct TimerHandle {
timeout_id: i32,
_closure: wasm_bindgen::closure::Closure<dyn FnMut()>,
}

impl TimerHandle {
#[inline]
fn clear(&mut self) {
clearTimeout(self.0)
clearTimeout(self.timeout_id)
}
}

Expand All @@ -31,12 +34,15 @@ impl Drop for TimerHandle {
fn timer_create(timeout: time::Duration, state: *const TimerState) -> TimerHandle {
let timeout = timeout.as_millis() as u32;

let cb = wasm_bindgen::closure::Closure::once(move || unsafe {
let closure = wasm_bindgen::closure::Closure::once(move || unsafe {
(*state).wake();
});
let handle = setTimeout(&cb, timeout);
let timeout_id = setTimeout(&closure, timeout);

TimerHandle(handle)
TimerHandle {
timeout_id,
_closure: closure,
}
}

enum State {
Expand Down

0 comments on commit 0dccab3

Please sign in to comment.