Skip to content

Add node.js support for timers #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 22 additions & 70 deletions crates/timers/src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,20 @@
use js_sys::Function;
use wasm_bindgen::prelude::*;
use wasm_bindgen::{JsCast, JsValue};
use web_sys::{Window, WorkerGlobalScope};

thread_local! {
static GLOBAL: WindowOrWorker = WindowOrWorker::new();
}

enum WindowOrWorker {
Window(Window),
Worker(WorkerGlobalScope),
}

impl WindowOrWorker {
fn new() -> Self {
#[wasm_bindgen]
extern "C" {
type Global;

#[wasm_bindgen(method, getter, js_name = Window)]
fn window(this: &Global) -> JsValue;

#[wasm_bindgen(method, getter, js_name = WorkerGlobalScope)]
fn worker(this: &Global) -> JsValue;
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = "setTimeout", catch)]
fn set_timeout(handler: &Function, timeout: i32) -> Result<i32, JsValue>;

let global: Global = js_sys::global().unchecked_into();
#[wasm_bindgen(js_name = "setInterval", catch)]
fn set_interval(handler: &Function, timeout: i32) -> Result<i32, JsValue>;

if !global.window().is_undefined() {
Self::Window(global.unchecked_into())
} else if !global.worker().is_undefined() {
Self::Worker(global.unchecked_into())
} else {
panic!("Only supported in a browser or web worker");
}
}
}

macro_rules! impl_window_or_worker {
($(fn $name:ident($($par_name:ident: $par_type:ty),*)$( -> $return:ty)?;)+) => {
impl WindowOrWorker {
$(
fn $name(&self, $($par_name: $par_type),*)$( -> $return)? {
match self {
Self::Window(window) => window.$name($($par_name),*),
Self::Worker(worker) => worker.$name($($par_name),*),
}
}
)+
}
};
}
#[wasm_bindgen(js_name = "clearTimeout")]
fn clear_timeout(handle: i32);

impl_window_or_worker! {
fn set_timeout_with_callback_and_timeout_and_arguments_0(handler: &Function, timeout: i32) -> Result<i32, JsValue>;
fn set_interval_with_callback_and_timeout_and_arguments_0(handler: &Function, timeout: i32) -> Result<i32, JsValue>;
fn clear_timeout_with_handle(handle: i32);
fn clear_interval_with_handle(handle: i32);
#[wasm_bindgen(js_name = "clearInterval")]
fn clear_interval(handle: i32);
}

/// A scheduled timeout.
Expand All @@ -77,7 +35,7 @@ pub struct Timeout {
impl Drop for Timeout {
fn drop(&mut self) {
if let Some(id) = self.id {
GLOBAL.with(|global| global.clear_timeout_with_handle(id));
clear_timeout(id);
}
}
}
Expand All @@ -101,14 +59,11 @@ impl Timeout {
{
let closure = Closure::once(callback);

let id = GLOBAL.with(|global| {
global
.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,
)
.unwrap_throw();

Timeout {
id: Some(id),
Expand Down Expand Up @@ -180,7 +135,7 @@ pub struct Interval {
impl Drop for Interval {
fn drop(&mut self) {
if let Some(id) = self.id {
GLOBAL.with(|global| global.clear_interval_with_handle(id));
clear_interval(id);
}
}
}
Expand All @@ -203,14 +158,11 @@ impl Interval {
{
let closure = Closure::wrap(Box::new(callback) as Box<dyn FnMut()>);

let id = GLOBAL.with(|global| {
global
.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,
)
.unwrap_throw();

Interval {
id: Some(id),
Expand Down