diff --git a/crates/timers/src/callback.rs b/crates/timers/src/callback.rs index 74880c59..c9688abb 100644 --- a/crates/timers/src/callback.rs +++ b/crates/timers/src/callback.rs @@ -1,8 +1,8 @@ //! Callback-style timer APIs. -use super::sys::*; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; +use web_sys; /// A scheduled timeout. /// @@ -20,7 +20,7 @@ pub struct Timeout { impl Drop for Timeout { fn drop(&mut self) { if let Some(id) = self.id { - clear_timeout(id); + web_sys::window().expect("cannot use `window`").clear_timeout_with_handle(id); } } } @@ -44,10 +44,10 @@ impl Timeout { { let closure = Closure::once(callback); - let id = set_timeout( + let id = web_sys::window().expect("cannot use `window`").set_timeout_with_callback_and_timeout_and_arguments_0( closure.as_ref().unchecked_ref::(), millis as i32, - ); + ).expect("`set_timeout` failed"); Timeout { id: Some(id), @@ -118,7 +118,7 @@ pub struct Interval { impl Drop for Interval { fn drop(&mut self) { if let Some(id) = self.id { - clear_interval(id); + web_sys::window().expect("cannot use `window`").clear_timeout_with_handle(id); } } } @@ -141,10 +141,10 @@ impl Interval { { let closure = Closure::wrap(Box::new(callback) as Box); - let id = set_interval( + let id = web_sys::window().expect("cannot use `window`").set_interval_with_callback_and_timeout_and_arguments_0( closure.as_ref().unchecked_ref::(), millis as i32, - ); + ).expect("`set_interval` failed"); Interval { id: Some(id), diff --git a/crates/timers/src/future.rs b/crates/timers/src/future.rs index 6055bd33..5eaea0e7 100644 --- a/crates/timers/src/future.rs +++ b/crates/timers/src/future.rs @@ -1,11 +1,11 @@ //! `Future`- and `Stream`-backed timers APIs. -use super::sys::*; use futures::prelude::*; use futures::sync::mpsc; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; use wasm_bindgen_futures::JsFuture; +use web_sys; /// A scheduled timeout as a `Future`. /// @@ -56,7 +56,7 @@ pub struct TimeoutFuture { impl Drop for TimeoutFuture { fn drop(&mut self) { if let Some(id) = self.id { - clear_timeout(id); + web_sys::window().expect("cannot use `window`").clear_timeout_with_handle(id); } } } @@ -84,7 +84,7 @@ impl TimeoutFuture { pub fn new(millis: u32) -> TimeoutFuture { let mut id = None; let promise = js_sys::Promise::new(&mut |resolve, _reject| { - id = Some(set_timeout(&resolve, millis as i32)); + id = Some( web_sys::window().expect("cannot use `window`").set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, millis as i32).expect("`set_timeout` failed")); }); debug_assert!(id.is_some()); let inner = JsFuture::from(promise); @@ -162,7 +162,7 @@ impl IntervalStream { impl Drop for IntervalStream { fn drop(&mut self) { if let Some(id) = self.id { - clear_interval(id); + web_sys::window().expect("cannot use `window`").clear_timeout_with_handle(id); } } } @@ -173,10 +173,10 @@ impl Stream for IntervalStream { fn poll(&mut self) -> Poll, ()> { if self.id.is_none() { - self.id = Some(set_interval( + self.id = Some( web_sys::window().expect("cannot use `window`").set_interval_with_callback_and_timeout_and_arguments_0( self.closure.as_ref().unchecked_ref::(), self.millis as i32, - )); + ).expect("`set_interval` failed")); } self.inner.poll() diff --git a/crates/timers/src/lib.rs b/crates/timers/src/lib.rs index 0187b93f..964db6f0 100644 --- a/crates/timers/src/lib.rs +++ b/crates/timers/src/lib.rs @@ -70,6 +70,4 @@ extern crate futures_rs as futures; pub mod callback; #[cfg(feature = "futures")] -pub mod future; - -mod sys; +pub mod future; \ No newline at end of file diff --git a/crates/timers/src/sys.rs b/crates/timers/src/sys.rs deleted file mode 100644 index 03bdbc71..00000000 --- a/crates/timers/src/sys.rs +++ /dev/null @@ -1,20 +0,0 @@ -//! Raw bindings to the Javascript APIs we need, namely set(Timeout|Interval) and clear(Timeout|Interval). -//! Depending on how rustwasm/wasm-bindgen#1046 is resolved, we may be able to remove this at a later date. - -use js_sys::Function; -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(js_name = "setTimeout")] - pub fn set_timeout(handler: &Function, timeout: i32) -> i32; - - #[wasm_bindgen(js_name = "clearTimeout")] - pub fn clear_timeout(token: i32); - - #[wasm_bindgen(js_name = "setInterval")] - pub fn set_interval(handler: &Function, timeout: i32) -> i32; - - #[wasm_bindgen(js_name = "clearInterval")] - pub fn clear_interval(token: i32); -}