Skip to content

Commit

Permalink
fix: return type of Timeout/Interval accepts both number and `NodeJ…
Browse files Browse the repository at this point in the history
…S.Timeout`
  • Loading branch information
flisky committed Jan 19, 2023
1 parent c6f71d8 commit 8b242b8
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 13 deletions.
20 changes: 10 additions & 10 deletions crates/timers/src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ extern "C" {
type GlobalThis;

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

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

#[wasm_bindgen(method, js_name = "clearTimeout")]
fn clear_timeout(this: &GlobalThis, handle: i32);
fn clear_timeout(this: &GlobalThis, handle: JsValue);

#[wasm_bindgen(method, js_name = "clearInterval")]
fn clear_interval(this: &GlobalThis, handle: i32);
fn clear_interval(this: &GlobalThis, handle: JsValue);
}

/// A scheduled timeout.
Expand All @@ -30,15 +30,15 @@ extern "C" {
#[derive(Debug)]
#[must_use = "timeouts cancel on drop; either call `forget` or `drop` explicitly"]
pub struct Timeout {
id: Option<i32>,
id: Option<JsValue>,
closure: Option<Closure<dyn FnMut()>>,
}

impl Drop for Timeout {
/// Disposes of the timeout, dually cancelling this timeout by calling
/// `clearTimeout` directly.
fn drop(&mut self) {
if let Some(id) = self.id {
if let Some(id) = self.id.take() {
let global = js_sys::global().unchecked_into::<GlobalThis>();
global.clear_timeout(id);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Timeout {
/// // Do stuff...
/// }).forget();
/// ```
pub fn forget(mut self) -> i32 {
pub fn forget(mut self) -> JsValue {
let id = self.id.take().unwrap_throw();
self.closure.take().unwrap_throw().forget();
id
Expand Down Expand Up @@ -135,15 +135,15 @@ impl Timeout {
#[derive(Debug)]
#[must_use = "intervals cancel on drop; either call `forget` or `drop` explicitly"]
pub struct Interval {
id: Option<i32>,
id: Option<JsValue>,
closure: Option<Closure<dyn FnMut()>>,
}

impl Drop for Interval {
/// Disposes of the interval, dually cancelling this interval by calling
/// `clearInterval` directly.
fn drop(&mut self) {
if let Some(id) = self.id {
if let Some(id) = self.id.take() {
let global = js_sys::global().unchecked_into::<GlobalThis>();
global.clear_interval(id);
}
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Interval {
/// // Do stuff...
/// }).forget();
/// ```
pub fn forget(mut self) -> i32 {
pub fn forget(mut self) -> JsValue {
let id = self.id.take().unwrap_throw();
self.closure.take().unwrap_throw().forget();
id
Expand Down
114 changes: 114 additions & 0 deletions crates/timers/tests/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#![cfg(all(target_family = "wasm", feature = "futures"))]

use futures_channel::{mpsc, oneshot};
use futures_util::{
future::{select, Either, FutureExt},
stream::StreamExt,
};
use gloo_timers::{
callback::{Interval, Timeout},
future::{sleep, IntervalStream, TimeoutFuture},
};
use std::cell::Cell;
use std::rc::Rc;
use std::time::Duration;
use wasm_bindgen_test::*;

#[wasm_bindgen_test]
async fn timeout() {
let (sender, receiver) = oneshot::channel();
Timeout::new(1, || sender.send(()).unwrap()).forget();
receiver.await.unwrap();
}

#[wasm_bindgen_test]
async fn timeout_cancel() {
let cell = Rc::new(Cell::new(false));

let t = Timeout::new(1, {
let cell = cell.clone();
move || {
cell.set(true);
panic!("should have been cancelled");
}
});
t.cancel();

let (sender, receiver) = oneshot::channel();

Timeout::new(2, move || {
sender.send(()).unwrap();
assert_eq!(cell.get(), false);
})
.forget();

receiver.await.unwrap();
}

#[wasm_bindgen_test]
async fn timeout_future() {
TimeoutFuture::new(1).await;
}

#[wasm_bindgen_test]
async fn timeout_future_cancel() {
let cell = Rc::new(Cell::new(false));

let a = TimeoutFuture::new(1).map({
let cell = cell.clone();
move |_| {
assert_eq!(cell.get(), false);
1
}
});

let b = TimeoutFuture::new(2).map({
let cell = cell.clone();
move |_| {
cell.set(true);
2u32
}
});

let (who, other) = match select(a, b).await {
Either::Left(x) => x,
Either::Right(_) => panic!("Timer for 2 ms finished before timer for 1 ms"),
};
assert_eq!(who, 1);
// Drop `b` so that its timer is canceled.
drop(other);
TimeoutFuture::new(3).await;
// We should never have fired `b`'s timer.
assert_eq!(cell.get(), false);
}

#[wasm_bindgen_test]
async fn interval() {
let (mut sender, receiver) = mpsc::channel(1);
let i = Interval::new(1, move || {
if !sender.is_closed() {
sender.try_send(()).unwrap()
}
});

let results: Vec<_> = receiver.take(5).collect().await;
drop(i);
assert_eq!(results.len(), 5);
}

#[wasm_bindgen_test]
async fn interval_cancel() {
let i = Interval::new(10, move || {
panic!("This should never be called");
});
i.cancel();

// This keeps us live for long enough that if any erroneous Interval callbacks fired, we'll have seen them.
sleep(Duration::from_millis(100)).await;
}

#[wasm_bindgen_test]
async fn interval_stream() {
let results: Vec<_> = IntervalStream::new(1).take(5).collect().await;
assert_eq!(results.len(), 5);
}
4 changes: 1 addition & 3 deletions crates/timers/tests/web.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! Test suite for the Web and headless browsers.
#![cfg(all(target_arch = "wasm32", feature = "futures"))]
#![cfg(all(target_family = "wasm", feature = "futures"))]

use futures_channel::{mpsc, oneshot};
use futures_util::{
Expand Down

0 comments on commit 8b242b8

Please sign in to comment.