Skip to content

Commit 0ab85a0

Browse files
authored
Merge pull request #98 from derekdreery/future_0_3
Migrate to futures 0.3
2 parents f756063 + 3f32fe6 commit 0ab85a0

File tree

13 files changed

+240
-241
lines changed

13 files changed

+240
-241
lines changed

crates/console-timer/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ homepage = "https://github.com/rustwasm/gloo"
1111
categories = ["api-bindings", "development-tools::profiling", "wasm"]
1212

1313
[dependencies.web-sys]
14-
version = "0.3.17"
14+
version = "0.3.31"
1515
features = [
1616
"console",
1717
]
1818

1919
[dev-dependencies]
20-
wasm-bindgen-test = "0.2.43"
20+
wasm-bindgen-test = "0.3.4"
2121
gloo-timers = { version = "0.1.0", path = "../timers" }

crates/events/Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ homepage = "https://github.com/rustwasm/gloo"
1111
categories = ["api-bindings", "asynchronous", "web-programming", "wasm"]
1212

1313
[dependencies]
14-
wasm-bindgen = "0.2.43"
14+
wasm-bindgen = "0.2.54"
1515

1616
[dependencies.web-sys]
17-
version = "0.3.14"
17+
version = "0.3.31"
1818
features = [
1919
"Event",
2020
"EventTarget",
2121
"AddEventListenerOptions",
2222
]
2323

2424
[dev-dependencies]
25-
js-sys = "0.3.14"
26-
futures = "0.1.25"
27-
wasm-bindgen-test = "0.2.43"
25+
js-sys = "0.3.31"
26+
futures = "0.3"
27+
wasm-bindgen-test = "0.3.4"
2828

2929
[dev-dependencies.web-sys]
30-
version = "0.3.14"
30+
version = "0.3.31"
3131
features = [
3232
"HtmlElement",
3333
"Window",

crates/events/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
2121
</div>
2222

23-
Using event listeners with [`web-sys`](https://crates.io/crates/web-sys) is hard! This crate provides an [`EventListener`](https://docs.rs/gloo-events/0.1.0/gloo_events/struct.EventListener.html) type which makes it easy!
23+
Using event listeners with [`web-sys`](https://crates.io/crates/web-sys) is hard! This crate
24+
provides an [`EventListener`] type which makes it easy!
2425

25-
See the documentation for [`EventListener`](https://docs.rs/gloo-events/0.1.0/gloo_events/struct.EventListener.html) for more information.
26+
See the documentation for [`EventListener`] for more information.
27+
28+
[`EventListener`]: struct.EventListener.html

crates/events/src/lib.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/*!
2-
Using event listeners with [`web-sys`](https://crates.io/crates/web-sys) is hard! This crate provides an [`EventListener`](struct.EventListener.html) type which makes it easy!
2+
Using event listeners with [`web-sys`](https://crates.io/crates/web-sys) is hard! This crate
3+
provides an [`EventListener`] type which makes it easy!
34
4-
See the documentation for [`EventListener`](struct.EventListener.html) for more information.
5+
See the documentation for [`EventListener`] for more information.
6+
7+
[`EventListener`]: struct.EventListener.html
58
*/
69
#![deny(missing_docs, missing_debug_implementations)]
710

@@ -182,16 +185,18 @@ thread_local! {
182185

183186
/// RAII type which is used to manage DOM event listeners.
184187
///
185-
/// When the `EventListener` is dropped, it will automatically deregister the event listener and clean up the closure's memory.
188+
/// When the `EventListener` is dropped, it will automatically deregister the event listener and
189+
/// clean up the closure's memory.
186190
///
187191
/// Normally the `EventListener` is stored inside of another struct, like this:
188192
///
189193
/// ```rust
190194
/// # use gloo_events::EventListener;
191195
/// # use wasm_bindgen::UnwrapThrowExt;
192-
/// use futures::Poll;
196+
/// use std::pin::Pin;
197+
/// use std::task::{Context, Poll};
193198
/// use futures::stream::Stream;
194-
/// use futures::sync::mpsc;
199+
/// use futures::channel::mpsc;
195200
/// use web_sys::EventTarget;
196201
///
197202
/// pub struct OnClick {
@@ -218,10 +223,9 @@ thread_local! {
218223
///
219224
/// impl Stream for OnClick {
220225
/// type Item = ();
221-
/// type Error = ();
222226
///
223-
/// fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
224-
/// self.receiver.poll().map_err(|_| unreachable!())
227+
/// fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
228+
/// Pin::new(&mut self.receiver).poll_next(cx)
225229
/// }
226230
/// }
227231
/// ```

crates/events/tests/web.rs

+29-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![cfg(target_arch = "wasm32")]
44

55
use futures::prelude::*;
6-
use futures::sync::mpsc;
6+
use futures::channel::mpsc;
77
use gloo_events::{EventListener, EventListenerOptions, EventListenerPhase};
88
use js_sys::Error;
99
use wasm_bindgen::{JsCast, JsValue, UnwrapThrowExt};
@@ -50,29 +50,22 @@ impl<A> Sender<A> {
5050
}
5151
}
5252

53-
fn mpsc<A, F>(f: F) -> impl Future<Item = Vec<A>, Error = JsValue>
53+
async fn mpsc<A, F>(f: F) -> Result<Vec<A>, JsValue>
5454
where
5555
F: FnOnce(Sender<A>),
5656
{
57-
let (sender, receiver) = futures::sync::mpsc::unbounded();
58-
57+
let (sender, receiver) = mpsc::unbounded();
5958
f(Sender { sender });
60-
61-
receiver
62-
.then(|x| match x {
63-
Ok(a) => a,
64-
Err(_) => unreachable!(),
65-
})
66-
.collect()
59+
receiver.try_collect().await
6760
}
6861

6962
// ----------------------------------------------------------------------------
7063
// Tests begin here
7164
// ----------------------------------------------------------------------------
7265

73-
#[wasm_bindgen_test(async)]
74-
fn new_with_options() -> impl Future<Item = (), Error = JsValue> {
75-
mpsc(|sender| {
66+
#[wasm_bindgen_test]
67+
async fn new_with_options() {
68+
let results = mpsc(|sender| {
7669
let body = body();
7770

7871
let _handler = EventListener::new_with_options(
@@ -93,13 +86,13 @@ fn new_with_options() -> impl Future<Item = (), Error = JsValue> {
9386

9487
body.click();
9588
body.click();
96-
})
97-
.and_then(|results| is(results, vec![(), ()]))
89+
}).await;
90+
assert_eq!(results, Ok(vec![(), ()]));
9891
}
9992

100-
#[wasm_bindgen_test(async)]
101-
fn once_with_options() -> impl Future<Item = (), Error = JsValue> {
102-
mpsc(|sender| {
93+
#[wasm_bindgen_test]
94+
async fn once_with_options() {
95+
let results = mpsc(|sender| {
10396
let body = body();
10497

10598
let _handler = EventListener::once_with_options(
@@ -120,13 +113,13 @@ fn once_with_options() -> impl Future<Item = (), Error = JsValue> {
120113

121114
body.click();
122115
body.click();
123-
})
124-
.and_then(|results| is(results, vec![()]))
116+
}).await;
117+
assert_eq!(results, Ok(vec![()]));
125118
}
126119

127-
#[wasm_bindgen_test(async)]
128-
fn new() -> impl Future<Item = (), Error = JsValue> {
129-
mpsc(|sender| {
120+
#[wasm_bindgen_test]
121+
async fn new() {
122+
let results = mpsc(|sender| {
130123
let body = body();
131124

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

140133
body.click();
141134
body.click();
142-
})
143-
.and_then(|results| is(results, vec![(), ()]))
135+
}).await;
136+
assert_eq!(results, Ok(vec![(), ()]));
144137
}
145138

146-
#[wasm_bindgen_test(async)]
147-
fn once() -> impl Future<Item = (), Error = JsValue> {
148-
mpsc(|sender| {
139+
#[wasm_bindgen_test]
140+
async fn once() {
141+
let results = mpsc(|sender| {
149142
let body = body();
150143

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

159152
body.click();
160153
body.click();
161-
})
162-
.and_then(|results| is(results, vec![()]))
154+
}).await;
155+
assert_eq!(results, Ok(vec![()]));
163156
}
164157

165158
// TODO is it possible to somehow cleanup the closure after a timeout?
@@ -177,9 +170,9 @@ fn forget() {
177170
handler.forget();
178171
}
179172

180-
#[wasm_bindgen_test(async)]
181-
fn dynamic_registration() -> impl Future<Item = (), Error = JsValue> {
182-
mpsc(|sender| {
173+
#[wasm_bindgen_test]
174+
async fn dynamic_registration() {
175+
let results = mpsc(|sender| {
183176
let body = body();
184177

185178
let handler1 = EventListener::new(&body, "click", {
@@ -212,6 +205,6 @@ fn dynamic_registration() -> impl Future<Item = (), Error = JsValue> {
212205
drop(handler3);
213206

214207
body.click();
215-
})
216-
.and_then(|results| is(results, vec![1, 2, 2, 2, 3, 3]))
208+
}).await;
209+
assert_eq!(results, Ok(vec![1, 2, 2, 2, 3, 3]));
217210
}

crates/timers/Cargo.toml

+8-13
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@ homepage = "https://github.com/rustwasm/gloo"
1111
categories = ["api-bindings", "asynchronous", "wasm"]
1212

1313
[dependencies]
14-
wasm-bindgen = "0.2.43"
15-
js-sys = "0.3.17"
16-
17-
[dependencies.futures_rs]
18-
package = "futures"
19-
version = "0.1.25"
20-
optional = true
21-
22-
[dependencies.wasm-bindgen-futures]
23-
version = "0.3.19"
24-
optional = true
14+
wasm-bindgen = "0.2.54"
15+
js-sys = "0.3.31"
16+
futures-core = { version = "0.3", optional = true }
17+
futures-channel = { version = "0.3", optional = true }
2518

2619
[dependencies.web-sys]
2720
version = "0.3.19"
@@ -31,8 +24,10 @@ features = [
3124

3225
[features]
3326
default = []
34-
futures = ["futures_rs", "wasm-bindgen-futures"]
27+
futures = ["futures-core", "futures-channel"]
3528

3629

3730
[dev-dependencies]
38-
wasm-bindgen-test = "0.2.43"
31+
wasm-bindgen-futures = "0.4.4"
32+
wasm-bindgen-test = "0.3.4"
33+
futures-util = "0.3"

0 commit comments

Comments
 (0)