Skip to content

Commit acb92d9

Browse files
committed
gloo-timers: Use raw bindings to (set|clear)(Timeout|Interval) instead of the Window API, since window isn't always present (e.g Web Workers)
1 parent b3316ef commit acb92d9

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

crates/timers/src/callback.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Callback-style timer APIs.
22
3-
use super::window;
3+
use super::{clear_interval, clear_timeout, set_interval, set_timeout};
44
use std::fmt;
55
use wasm_bindgen::prelude::*;
66
use wasm_bindgen::JsCast;
@@ -20,7 +20,7 @@ pub struct Timeout {
2020
impl Drop for Timeout {
2121
fn drop(&mut self) {
2222
if let Some(id) = self.id {
23-
window().clear_timeout_with_handle(id);
23+
clear_timeout(id);
2424
}
2525
}
2626
}
@@ -50,12 +50,10 @@ impl Timeout {
5050
{
5151
let closure = Closure::once(callback);
5252

53-
let id = window()
54-
.set_timeout_with_callback_and_timeout_and_arguments_0(
55-
closure.as_ref().unchecked_ref::<js_sys::Function>(),
56-
millis as i32,
57-
)
58-
.unwrap_throw();
53+
let id = set_timeout(
54+
closure.as_ref().unchecked_ref::<js_sys::Function>(),
55+
millis as i32,
56+
);
5957

6058
Timeout {
6159
id: Some(id),
@@ -125,7 +123,7 @@ pub struct Interval {
125123
impl Drop for Interval {
126124
fn drop(&mut self) {
127125
if let Some(id) = self.id {
128-
window().clear_interval_with_handle(id);
126+
clear_interval(id);
129127
}
130128
}
131129
}
@@ -158,12 +156,10 @@ impl Interval {
158156
callback();
159157
}) as Box<FnMut()>);
160158

161-
let id = window()
162-
.set_interval_with_callback_and_timeout_and_arguments_0(
163-
closure.as_ref().unchecked_ref::<js_sys::Function>(),
164-
millis as i32,
165-
)
166-
.unwrap_throw();
159+
let id = set_interval(
160+
closure.as_ref().unchecked_ref::<js_sys::Function>(),
161+
millis as i32,
162+
);
167163

168164
Interval {
169165
id: Some(id),

crates/timers/src/future.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! `Future`- and `Stream`-backed timers APIs.
22
3-
use super::window;
3+
use super::{clear_interval, clear_timeout, set_interval, set_timeout};
44
use futures::prelude::*;
55
use futures::sync::mpsc;
66
use std::fmt;
@@ -56,7 +56,7 @@ pub struct TimeoutFuture {
5656
impl Drop for TimeoutFuture {
5757
fn drop(&mut self) {
5858
if let Some(id) = self.id {
59-
window().clear_timeout_with_handle(id);
59+
clear_timeout(id);
6060
}
6161
}
6262
}
@@ -84,11 +84,7 @@ impl TimeoutFuture {
8484
pub fn new(millis: u32) -> TimeoutFuture {
8585
let mut id = None;
8686
let promise = js_sys::Promise::new(&mut |resolve, _reject| {
87-
id = Some(
88-
window()
89-
.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, millis as i32)
90-
.unwrap_throw(),
91-
);
87+
id = Some(set_timeout(&resolve, millis as i32));
9288
});
9389
debug_assert!(id.is_some());
9490
let inner = JsFuture::from(promise);
@@ -173,7 +169,7 @@ impl IntervalStream {
173169
impl Drop for IntervalStream {
174170
fn drop(&mut self) {
175171
if let Some(id) = self.id {
176-
window().clear_interval_with_handle(id);
172+
clear_interval(id);
177173
}
178174
}
179175
}
@@ -192,14 +188,10 @@ impl Stream for IntervalStream {
192188

193189
fn poll(&mut self) -> Poll<Option<()>, ()> {
194190
if self.id.is_none() {
195-
self.id = Some(
196-
window()
197-
.set_interval_with_callback_and_timeout_and_arguments_0(
198-
self.closure.as_ref().unchecked_ref::<js_sys::Function>(),
199-
self.millis as i32,
200-
)
201-
.unwrap_throw(),
202-
);
191+
self.id = Some(set_interval(
192+
self.closure.as_ref().unchecked_ref::<js_sys::Function>(),
193+
self.millis as i32,
194+
));
203195
}
204196

205197
self.inner.poll()

crates/timers/src/lib.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,22 @@ TODO
6767
#[cfg(feature = "futures")]
6868
extern crate futures_rs as futures;
6969

70+
use js_sys::Function;
7071
use wasm_bindgen::prelude::*;
7172

72-
fn window() -> web_sys::Window {
73-
web_sys::window().unwrap_throw()
73+
#[wasm_bindgen]
74+
extern "C" {
75+
#[wasm_bindgen(js_name = "setTimeout")]
76+
pub fn set_timeout(handler: &Function, timeout: i32) -> i32;
77+
78+
#[wasm_bindgen(js_name = "clearTimeout")]
79+
pub fn clear_timeout(token: i32);
80+
81+
#[wasm_bindgen(js_name = "setInterval")]
82+
pub fn set_interval(handler: &Function, timeout: i32) -> i32;
83+
84+
#[wasm_bindgen(js_name = "clearInterval")]
85+
pub fn clear_interval(token: i32);
7486
}
7587

7688
pub mod callback;

0 commit comments

Comments
 (0)