Skip to content

Commit

Permalink
Migrate to futures 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-uk1 committed Dec 5, 2019
1 parent f756063 commit b9d53d7
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 160 deletions.
4 changes: 2 additions & 2 deletions crates/console-timer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ homepage = "https://github.com/rustwasm/gloo"
categories = ["api-bindings", "development-tools::profiling", "wasm"]

[dependencies.web-sys]
version = "0.3.17"
version = "0.3.31"
features = [
"console",
]

[dev-dependencies]
wasm-bindgen-test = "0.2.43"
wasm-bindgen-test = "0.3.4"
gloo-timers = { version = "0.1.0", path = "../timers" }
12 changes: 6 additions & 6 deletions crates/events/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ homepage = "https://github.com/rustwasm/gloo"
categories = ["api-bindings", "asynchronous", "web-programming", "wasm"]

[dependencies]
wasm-bindgen = "0.2.43"
wasm-bindgen = "0.2.54"

[dependencies.web-sys]
version = "0.3.14"
version = "0.3.31"
features = [
"Event",
"EventTarget",
"AddEventListenerOptions",
]

[dev-dependencies]
js-sys = "0.3.14"
futures = "0.1.25"
wasm-bindgen-test = "0.2.43"
js-sys = "0.3.31"
futures = "0.3"
wasm-bindgen-test = "0.3.4"

[dev-dependencies.web-sys]
version = "0.3.14"
version = "0.3.31"
features = [
"HtmlElement",
"Window",
Expand Down
14 changes: 9 additions & 5 deletions crates/events/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ thread_local! {
/// ```rust
/// # use gloo_events::EventListener;
/// # use wasm_bindgen::UnwrapThrowExt;
/// use futures::Poll;
/// use std::pin::Pin;
/// use std::task::{Context, Poll};
/// use futures::stream::Stream;
/// use futures::sync::mpsc;
/// use futures::channel::mpsc;
/// use web_sys::EventTarget;
///
/// pub struct OnClick {
Expand All @@ -214,14 +215,17 @@ thread_local! {
/// listener,
/// }
/// }
///
/// fn project_receiver(self: Pin<&mut Self>) -> Pin<&mut mpsc::UnboundedReceiver<()>> {
/// unsafe { self.map_unchecked_mut(|s| &mut s.receiver) }
/// }
/// }
///
/// impl Stream for OnClick {
/// type Item = ();
/// type Error = ();
///
/// fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
/// self.receiver.poll().map_err(|_| unreachable!())
/// fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
/// self.project_receiver().poll_next(cx)
/// }
/// }
/// ```
Expand Down
61 changes: 30 additions & 31 deletions crates/events/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![cfg(target_arch = "wasm32")]

use futures::prelude::*;
use futures::sync::mpsc;
use gloo_events::{EventListener, EventListenerOptions, EventListenerPhase};
use js_sys::Error;
use wasm_bindgen::{JsCast, JsValue, UnwrapThrowExt};
Expand Down Expand Up @@ -38,7 +37,7 @@ where

#[derive(Clone)]
struct Sender<A> {
sender: mpsc::UnboundedSender<Result<A, JsValue>>,
sender: futures::channel::mpsc::UnboundedSender<Result<A, JsValue>>,
}

impl<A> Sender<A> {
Expand All @@ -50,29 +49,29 @@ impl<A> Sender<A> {
}
}

fn mpsc<A, F>(f: F) -> impl Future<Item = Vec<A>, Error = JsValue>
async fn mpsc<A, F>(f: F) -> Vec<A>
where
F: FnOnce(Sender<A>),
{
let (sender, receiver) = futures::sync::mpsc::unbounded();
let (sender, receiver) = futures::channel::mpsc::unbounded();

f(Sender { sender });

receiver
.then(|x| match x {
.map(|x| match x {
Ok(a) => a,
Err(_) => unreachable!(),
})
.collect()
.collect().await
}

// ----------------------------------------------------------------------------
// Tests begin here
// ----------------------------------------------------------------------------

#[wasm_bindgen_test(async)]
fn new_with_options() -> impl Future<Item = (), Error = JsValue> {
mpsc(|sender| {
#[wasm_bindgen_test]
async fn new_with_options() {
let results = mpsc::<(), _>(|sender| {
let body = body();

let _handler = EventListener::new_with_options(
Expand All @@ -93,13 +92,13 @@ fn new_with_options() -> impl Future<Item = (), Error = JsValue> {

body.click();
body.click();
})
.and_then(|results| is(results, vec![(), ()]))
}).await;
is(results, vec![(), ()]).unwrap_throw();
}

#[wasm_bindgen_test(async)]
fn once_with_options() -> impl Future<Item = (), Error = JsValue> {
mpsc(|sender| {
#[wasm_bindgen_test]
async fn once_with_options() {
let results = mpsc::<(), _>(|sender| {
let body = body();

let _handler = EventListener::once_with_options(
Expand All @@ -120,13 +119,13 @@ fn once_with_options() -> impl Future<Item = (), Error = JsValue> {

body.click();
body.click();
})
.and_then(|results| is(results, vec![()]))
}).await;
is(results, vec![()]).unwrap_throw();
}

#[wasm_bindgen_test(async)]
fn new() -> impl Future<Item = (), Error = JsValue> {
mpsc(|sender| {
#[wasm_bindgen_test]
async fn new() {
let results = mpsc::<(), _>(|sender| {
let body = body();

let _handler = EventListener::new(&body, "click", move |e| {
Expand All @@ -139,13 +138,13 @@ fn new() -> impl Future<Item = (), Error = JsValue> {

body.click();
body.click();
})
.and_then(|results| is(results, vec![(), ()]))
}).await;
is(results, vec![(), ()]).unwrap_throw();
}

#[wasm_bindgen_test(async)]
fn once() -> impl Future<Item = (), Error = JsValue> {
mpsc(|sender| {
#[wasm_bindgen_test]
async fn once() {
let results = mpsc::<(), _>(|sender| {
let body = body();

let _handler = EventListener::once(&body, "click", move |e| {
Expand All @@ -158,8 +157,8 @@ fn once() -> impl Future<Item = (), Error = JsValue> {

body.click();
body.click();
})
.and_then(|results| is(results, vec![()]))
}).await;
is(results, vec![()]).unwrap_throw();
}

// TODO is it possible to somehow cleanup the closure after a timeout?
Expand All @@ -177,9 +176,9 @@ fn forget() {
handler.forget();
}

#[wasm_bindgen_test(async)]
fn dynamic_registration() -> impl Future<Item = (), Error = JsValue> {
mpsc(|sender| {
#[wasm_bindgen_test]
async fn dynamic_registration() {
let results = mpsc::<usize, _>(|sender| {
let body = body();

let handler1 = EventListener::new(&body, "click", {
Expand Down Expand Up @@ -212,6 +211,6 @@ fn dynamic_registration() -> impl Future<Item = (), Error = JsValue> {
drop(handler3);

body.click();
})
.and_then(|results| is(results, vec![1, 2, 2, 2, 3, 3]))
}).await;
is(results, vec![1, 2, 2, 2, 3, 3]).unwrap_throw();
}
10 changes: 5 additions & 5 deletions crates/timers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ homepage = "https://github.com/rustwasm/gloo"
categories = ["api-bindings", "asynchronous", "wasm"]

[dependencies]
wasm-bindgen = "0.2.43"
js-sys = "0.3.17"
wasm-bindgen = "0.2.54"
js-sys = "0.3.31"

[dependencies.futures_rs]
package = "futures"
version = "0.1.25"
version = "0.3"
optional = true

[dependencies.wasm-bindgen-futures]
version = "0.3.19"
version = "0.4.4"
optional = true

[dependencies.web-sys]
Expand All @@ -35,4 +35,4 @@ futures = ["futures_rs", "wasm-bindgen-futures"]


[dev-dependencies]
wasm-bindgen-test = "0.2.43"
wasm-bindgen-test = "0.3.4"
Loading

0 comments on commit b9d53d7

Please sign in to comment.