Implement web_sys::Canvas event listeners

This commit is contained in:
Héctor Ramón Jiménez 2019-06-25 18:07:47 +02:00
parent c5703eb00a
commit b79089ea57
9 changed files with 242 additions and 62 deletions

View file

@ -1,12 +1,40 @@
use std::time::Duration;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsCast;
#[derive(Debug, Clone, Copy)]
pub struct Timeout {}
#[derive(Debug)]
pub struct Timeout {
handle: i32,
_closure: Closure<FnMut()>,
}
impl Timeout {
pub fn new<F>(f: F, duration: Duration) -> Timeout {
Timeout {}
}
pub fn new<F>(f: F, duration: Duration) -> Timeout
where
F: 'static + FnMut(),
{
let window = web_sys::window().expect("Failed to obtain window");
pub fn clear(self) {}
let closure = Closure::wrap(Box::new(f) as Box<dyn FnMut()>);
let handle = window
.set_timeout_with_callback_and_timeout_and_arguments_0(
&closure.as_ref().unchecked_ref(),
duration.as_millis() as i32,
)
.expect("Failed to set timeout");
Timeout {
handle,
_closure: closure,
}
}
}
impl Drop for Timeout {
fn drop(&mut self) {
let window = web_sys::window().expect("Failed to obtain window");
window.clear_timeout_with_handle(self.handle);
}
}