Skip to content

Commit

Permalink
gloo-timers: Use raw bindings to (set|clear)(Timeout|Interval) instea…
Browse files Browse the repository at this point in the history
…d of the Window API, since window isn't always present (e.g Web Workers)
  • Loading branch information
samcday committed Mar 28, 2019
1 parent b3316ef commit acb92d9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
26 changes: 11 additions & 15 deletions crates/timers/src/callback.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Callback-style timer APIs.
use super::window;
use super::{clear_interval, clear_timeout, set_interval, set_timeout};
use std::fmt;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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::<js_sys::Function>(),
millis as i32,
)
.unwrap_throw();
let id = set_timeout(
closure.as_ref().unchecked_ref::<js_sys::Function>(),
millis as i32,
);

Timeout {
id: Some(id),
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -158,12 +156,10 @@ impl Interval {
callback();
}) as Box<FnMut()>);

let id = window()
.set_interval_with_callback_and_timeout_and_arguments_0(
closure.as_ref().unchecked_ref::<js_sys::Function>(),
millis as i32,
)
.unwrap_throw();
let id = set_interval(
closure.as_ref().unchecked_ref::<js_sys::Function>(),
millis as i32,
);

Interval {
id: Some(id),
Expand Down
24 changes: 8 additions & 16 deletions crates/timers/src/future.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `Future`- and `Stream`-backed timers APIs.
use super::window;
use super::{clear_interval, clear_timeout, set_interval, set_timeout};
use futures::prelude::*;
use futures::sync::mpsc;
use std::fmt;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -192,14 +188,10 @@ impl Stream for IntervalStream {

fn poll(&mut self) -> Poll<Option<()>, ()> {
if self.id.is_none() {
self.id = Some(
window()
.set_interval_with_callback_and_timeout_and_arguments_0(
self.closure.as_ref().unchecked_ref::<js_sys::Function>(),
self.millis as i32,
)
.unwrap_throw(),
);
self.id = Some(set_interval(
self.closure.as_ref().unchecked_ref::<js_sys::Function>(),
self.millis as i32,
));
}

self.inner.poll()
Expand Down
16 changes: 14 additions & 2 deletions crates/timers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,22 @@ TODO
#[cfg(feature = "futures")]
extern crate futures_rs as futures;

use js_sys::Function;
use wasm_bindgen::prelude::*;

fn window() -> web_sys::Window {
web_sys::window().unwrap_throw()
#[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);
}

pub mod callback;
Expand Down

0 comments on commit acb92d9

Please sign in to comment.