Skip to content

Commit

Permalink
Changing to use web-sys; this fixes a Webpack error
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Dec 12, 2019
1 parent 0ab85a0 commit 8a4842a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
33 changes: 22 additions & 11 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::window;

/// A scheduled timeout.
///
Expand All @@ -20,7 +20,9 @@ pub struct Timeout {
impl Drop for Timeout {
fn drop(&mut self) {
if let Some(id) = self.id {
clear_timeout(id);
window()
.unwrap_throw()
.clear_timeout_with_handle(id);
}
}
}
Expand All @@ -44,10 +46,13 @@ impl Timeout {
{
let closure = Closure::once(callback);

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

Timeout {
id: Some(id),
Expand Down Expand Up @@ -102,6 +107,7 @@ impl Timeout {
self.closure.take().unwrap_throw()
}
}

/// A scheduled interval.
///
/// See `Interval::new` for scheduling new intervals.
Expand All @@ -118,7 +124,9 @@ pub struct Interval {
impl Drop for Interval {
fn drop(&mut self) {
if let Some(id) = self.id {
clear_interval(id);
window()
.unwrap_throw()
.clear_interval_with_handle(id);
}
}
}
Expand All @@ -141,10 +149,13 @@ impl Interval {
{
let closure = Closure::wrap(Box::new(callback) as Box<dyn FnMut()>);

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

Interval {
id: Some(id),
Expand Down
2 changes: 0 additions & 2 deletions crates/timers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,3 @@ pub mod callback;

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

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

This file was deleted.

0 comments on commit 8a4842a

Please sign in to comment.