Skip to content

Commit e2983ed

Browse files
committed
fix: clearTimeout illegal invocation in browser (#187)
1 parent 8cf3724 commit e2983ed

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

crates/timers/src/callback.rs

+28-20
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ use wasm_bindgen::{JsCast, JsValue};
66

77
#[wasm_bindgen]
88
extern "C" {
9-
#[wasm_bindgen(js_name = "setTimeout", catch)]
10-
fn set_timeout(handler: &Function, timeout: i32) -> Result<i32, JsValue>;
9+
type GlobalThis;
1110

12-
#[wasm_bindgen(js_name = "setInterval", catch)]
13-
fn set_interval(handler: &Function, timeout: i32) -> Result<i32, JsValue>;
11+
#[wasm_bindgen(method, js_name = "setTimeout", catch)]
12+
fn set_timeout(this: &GlobalThis, handler: &Function, timeout: i32) -> Result<i32, JsValue>;
1413

15-
#[wasm_bindgen(js_name = "clearTimeout")]
16-
fn clear_timeout(handle: i32);
14+
#[wasm_bindgen(method, js_name = "setInterval", catch)]
15+
fn set_interval(this: &GlobalThis, handler: &Function, timeout: i32) -> Result<i32, JsValue>;
1716

18-
#[wasm_bindgen(js_name = "clearInterval")]
19-
fn clear_interval(handle: i32);
17+
#[wasm_bindgen(method, js_name = "clearTimeout")]
18+
fn clear_timeout(this: &GlobalThis, handle: i32);
19+
20+
#[wasm_bindgen(method, js_name = "clearInterval")]
21+
fn clear_interval(this: &GlobalThis, handle: i32);
2022
}
2123

2224
/// A scheduled timeout.
@@ -37,7 +39,8 @@ impl Drop for Timeout {
3739
/// `clearTimeout` directly.
3840
fn drop(&mut self) {
3941
if let Some(id) = self.id {
40-
clear_timeout(id);
42+
let global = js_sys::global().unchecked_into::<GlobalThis>();
43+
global.clear_timeout(id);
4144
}
4245
}
4346
}
@@ -61,11 +64,13 @@ impl Timeout {
6164
{
6265
let closure = Closure::once(callback);
6366

64-
let id = set_timeout(
65-
closure.as_ref().unchecked_ref::<js_sys::Function>(),
66-
millis as i32,
67-
)
68-
.unwrap_throw();
67+
let global = js_sys::global().unchecked_into::<GlobalThis>();
68+
let id = global
69+
.set_timeout(
70+
closure.as_ref().unchecked_ref::<js_sys::Function>(),
71+
millis as i32,
72+
)
73+
.unwrap_throw();
6974

7075
Timeout {
7176
id: Some(id),
@@ -139,7 +144,8 @@ impl Drop for Interval {
139144
/// `clearInterval` directly.
140145
fn drop(&mut self) {
141146
if let Some(id) = self.id {
142-
clear_interval(id);
147+
let global = js_sys::global().unchecked_into::<GlobalThis>();
148+
global.clear_interval(id);
143149
}
144150
}
145151
}
@@ -162,11 +168,13 @@ impl Interval {
162168
{
163169
let closure = Closure::wrap(Box::new(callback) as Box<dyn FnMut()>);
164170

165-
let id = set_interval(
166-
closure.as_ref().unchecked_ref::<js_sys::Function>(),
167-
millis as i32,
168-
)
169-
.unwrap_throw();
171+
let global = js_sys::global().unchecked_into::<GlobalThis>();
172+
let id = global
173+
.set_interval(
174+
closure.as_ref().unchecked_ref::<js_sys::Function>(),
175+
millis as i32,
176+
)
177+
.unwrap_throw();
170178

171179
Interval {
172180
id: Some(id),

0 commit comments

Comments
 (0)