Skip to content
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

Fix timers to work in workers too. #106

Merged
merged 4 commits into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions crates/timers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ futures-channel = { version = "0.3", optional = true }
version = "0.3.19"
features = [
"Window",
"WorkerGlobalScope",
]

[features]
Expand Down
84 changes: 62 additions & 22 deletions crates/timers/src/callback.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
//! Callback-style timer APIs.

use js_sys::{Function, Reflect};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::window;
use wasm_bindgen::{throw_val, 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 {
let global: JsValue = js_sys::global().into();

if Reflect::has(&global, &String::from("Window").into()).unwrap_throw() {
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
WindowOrWorker::Window(global.into())
} else if Reflect::has(&global, &String::from("WorkerGlobalScope").into()).unwrap_throw() {
WindowOrWorker::Worker(global.into())
} else {
throw_val(global)
}
}
}

macro_rules! impl_window_or_worker {
($name:ident, ($($par_name:ident: $par_type:ty),*)$(, $return:ty)?) => {
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
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),*),
}
}
}
};
}

impl_window_or_worker!(set_timeout_with_callback_and_timeout_and_arguments_0, (handler: &Function, timeout: i32), Result<i32, JsValue>);
impl_window_or_worker!(clear_timeout_with_handle, (handle: i32));
impl_window_or_worker!(clear_interval_with_handle, (handle: i32));
impl_window_or_worker!(set_interval_with_callback_and_timeout_and_arguments_0, (handler: &Function, timeout: i32), Result<i32, JsValue>);

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

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();
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()
});

Timeout {
id: Some(id),
Expand Down Expand Up @@ -124,9 +165,7 @@ pub struct Interval {
impl Drop for Interval {
fn drop(&mut self) {
if let Some(id) = self.id {
window()
.unwrap_throw()
.clear_interval_with_handle(id);
GLOBAL.with(|global| global.clear_interval_with_handle(id));
}
}
}
Expand All @@ -149,13 +188,14 @@ impl Interval {
{
let closure = Closure::wrap(Box::new(callback) as Box<dyn FnMut()>);

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();
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()
});

Interval {
id: Some(id),
Expand Down