diff --git a/crates/timers/src/callback.rs b/crates/timers/src/callback.rs index b83372d9..142ee243 100644 --- a/crates/timers/src/callback.rs +++ b/crates/timers/src/callback.rs @@ -1,6 +1,6 @@ //! Callback-style timer APIs. -use super::window; +use super::sys::*; use std::fmt; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; @@ -20,7 +20,7 @@ pub struct Timeout { impl Drop for Timeout { fn drop(&mut self) { if let Some(id) = self.id { - window().clear_timeout_with_handle(id); + clear_timeout(id); } } } @@ -50,12 +50,10 @@ impl Timeout { { let closure = Closure::once(callback); - let id = window() - .set_timeout_with_callback_and_timeout_and_arguments_0( - closure.as_ref().unchecked_ref::(), - millis as i32, - ) - .unwrap_throw(); + let id = set_timeout( + closure.as_ref().unchecked_ref::(), + millis as i32, + ); Timeout { id: Some(id), @@ -125,7 +123,7 @@ pub struct Interval { impl Drop for Interval { fn drop(&mut self) { if let Some(id) = self.id { - window().clear_interval_with_handle(id); + clear_interval(id); } } } @@ -158,12 +156,10 @@ impl Interval { callback(); }) as Box); - let id = window() - .set_interval_with_callback_and_timeout_and_arguments_0( - closure.as_ref().unchecked_ref::(), - millis as i32, - ) - .unwrap_throw(); + let id = set_interval( + closure.as_ref().unchecked_ref::(), + millis as i32, + ); Interval { id: Some(id), diff --git a/crates/timers/src/future.rs b/crates/timers/src/future.rs index ece22fbc..8b3c2c9f 100644 --- a/crates/timers/src/future.rs +++ b/crates/timers/src/future.rs @@ -1,6 +1,6 @@ //! `Future`- and `Stream`-backed timers APIs. -use super::window; +use super::sys::*; use futures::prelude::*; use futures::sync::mpsc; use std::fmt; @@ -56,7 +56,7 @@ pub struct TimeoutFuture { impl Drop for TimeoutFuture { fn drop(&mut self) { if let Some(id) = self.id { - window().clear_timeout_with_handle(id); + clear_timeout(id); } } } @@ -84,11 +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( - window() - .set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, millis as i32) - .unwrap_throw(), - ); + id = Some(set_timeout(&resolve, millis as i32)); }); debug_assert!(id.is_some()); let inner = JsFuture::from(promise); @@ -173,7 +169,7 @@ impl IntervalStream { impl Drop for IntervalStream { fn drop(&mut self) { if let Some(id) = self.id { - window().clear_interval_with_handle(id); + clear_interval(id); } } } @@ -192,14 +188,10 @@ impl Stream for IntervalStream { fn poll(&mut self) -> Poll, ()> { if self.id.is_none() { - self.id = Some( - window() - .set_interval_with_callback_and_timeout_and_arguments_0( - self.closure.as_ref().unchecked_ref::(), - self.millis as i32, - ) - .unwrap_throw(), - ); + self.id = Some(set_interval( + self.closure.as_ref().unchecked_ref::(), + self.millis as i32, + )); } self.inner.poll() diff --git a/crates/timers/src/lib.rs b/crates/timers/src/lib.rs index 8797947e..0187b93f 100644 --- a/crates/timers/src/lib.rs +++ b/crates/timers/src/lib.rs @@ -67,13 +67,9 @@ TODO #[cfg(feature = "futures")] extern crate futures_rs as futures; -use wasm_bindgen::prelude::*; - -fn window() -> web_sys::Window { - web_sys::window().unwrap_throw() -} - pub mod callback; #[cfg(feature = "futures")] pub mod future; + +mod sys; diff --git a/crates/timers/src/sys.rs b/crates/timers/src/sys.rs new file mode 100644 index 00000000..03bdbc71 --- /dev/null +++ b/crates/timers/src/sys.rs @@ -0,0 +1,20 @@ +//! 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); +}