From 8c619e895ce0b74e3aab86f5758d89b3e6d2567a Mon Sep 17 00:00:00 2001 From: PhilippGackstatter Date: Thu, 29 Jul 2021 08:35:08 +0200 Subject: [PATCH 1/3] Use `js_sys` to enable node.js compatibility --- src/timer/global/wasm.rs | 6 ++++-- src/wasm.rs | 14 +++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/timer/global/wasm.rs b/src/timer/global/wasm.rs index d04b0e2..756f06e 100644 --- a/src/timer/global/wasm.rs +++ b/src/timer/global/wasm.rs @@ -22,10 +22,12 @@ pub(crate) fn run() -> TimerHandle { handle } -/// Calls `Window::setTimeout` with the given `Duration`. The callback wakes up the timer and +/// Calls `setTimeout` with the given `Duration`. The callback wakes up the timer and /// processes everything. fn schedule_callback(timer: Arc>, when: Duration) { - let window = web_sys::window().expect("Unable to access Window"); + // Both node and web have `setTimeout` available in their global scope + // so we can treat this as a `Window`. + let window = js_sys::global().unchecked_into::(); let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0( &Closure::once_into_js(move || { let mut timer_lock = timer.lock(); diff --git a/src/wasm.rs b/src/wasm.rs index 57ee1e6..d187ba8 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -55,11 +55,15 @@ impl Ord for Instant { impl Instant { pub fn now() -> Instant { - let val = web_sys::window() - .expect("not in a browser") - .performance() - .expect("performance object not available") - .now(); + let val = { + use wasm_bindgen::prelude::*; + use wasm_bindgen::JsCast; + js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("performance")) + .expect("failed to get performance from global object") + .unchecked_into::() + .now() + }; + Instant { inner: val } } From 56b5f2e3776b8d1f834013a4e028a81dd0932fa5 Mon Sep 17 00:00:00 2001 From: PhilippGackstatter Date: Tue, 17 Aug 2021 17:19:31 +0200 Subject: [PATCH 2/3] Only schedule callback if next_event is > now Co-authored-by: Thoralf-M --- src/timer/global/wasm.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/timer/global/wasm.rs b/src/timer/global/wasm.rs index 756f06e..babb2cc 100644 --- a/src/timer/global/wasm.rs +++ b/src/timer/global/wasm.rs @@ -51,17 +51,11 @@ fn schedule_callback(timer: Arc>, when: Duration) { } // We call `schedule_callback` again for the next event. - let sleep_dur = timer_lock.next_event() - .map(|next_event| { - if next_event > now { - next_event - now - } else { - Duration::new(0, 0) - } - }) - .unwrap_or(Duration::from_secs(5)); - drop(timer_lock); - schedule_callback(timer, sleep_dur); + if let Some(next_event) = timer_lock.next_event() { + if next_event > now { + schedule_callback(timer.clone(), next_event - now); + } + } }).unchecked_ref(), i32::try_from(when.as_millis()).unwrap_or(0) From 448c2be0c4feeb6b5df1da265489232dff2f03c1 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 20 Aug 2021 08:53:23 +0200 Subject: [PATCH 3/3] Readd `drop(timer_lock)` Co-authored-by: Max Inden --- src/timer/global/wasm.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/timer/global/wasm.rs b/src/timer/global/wasm.rs index babb2cc..459cbab 100644 --- a/src/timer/global/wasm.rs +++ b/src/timer/global/wasm.rs @@ -53,6 +53,7 @@ fn schedule_callback(timer: Arc>, when: Duration) { // We call `schedule_callback` again for the next event. if let Some(next_event) = timer_lock.next_event() { if next_event > now { + drop(timer_lock); schedule_callback(timer.clone(), next_event - now); } }