Skip to content

Commit

Permalink
call window methods directly
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKavik committed Oct 20, 2019
1 parent b35b89f commit be7b178
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
14 changes: 7 additions & 7 deletions crates/timers/src/callback.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand All @@ -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);
}
}
}
Expand All @@ -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::<js_sys::Function>(),
millis as i32,
);
).expect("`set_timeout` failed");

Timeout {
id: Some(id),
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -141,10 +141,10 @@ impl Interval {
{
let closure = Closure::wrap(Box::new(callback) as Box<dyn FnMut()>);

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::<js_sys::Function>(),
millis as i32,
);
).expect("`set_interval` failed");

Interval {
id: Some(id),
Expand Down
12 changes: 6 additions & 6 deletions crates/timers/src/future.rs
Original file line number Diff line number Diff line change
@@ -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`.
///
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 {
clear_timeout(id);
web_sys::window().expect("cannot use `window`").clear_timeout_with_handle(id);
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -173,10 +173,10 @@ impl Stream for IntervalStream {

fn poll(&mut self) -> Poll<Option<()>, ()> {
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::<js_sys::Function>(),
self.millis as i32,
));
).expect("`set_interval` failed"));
}

self.inner.poll()
Expand Down
4 changes: 1 addition & 3 deletions crates/timers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,4 @@ extern crate futures_rs as futures;
pub mod callback;

#[cfg(feature = "futures")]
pub mod future;

mod sys;
pub mod future;
20 changes: 0 additions & 20 deletions crates/timers/src/sys.rs

This file was deleted.

0 comments on commit be7b178

Please sign in to comment.