mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-23 09:41:33 +00:00
Default all async support to std::future
(#1741)
This commit defaults all crates in-tree to use `std::future` by default and none of them support the crates.io `futures` 0.1 crate any more. This is a breaking change for `wasm-bindgen-futures` and `wasm-bindgen-test` so they've both received a major version bump to reflect the new defaults. Historical versions of these crates should continue to work if necessary, but they won't receive any more maintenance after this is merged. The movement here liberally uses `async`/`await` to remove the need for using any combinators on the `Future` trait. As a result many of the crates now rely on a much more recent version of the compiler, especially to run tests. The `wasm-bindgen-futures` crate was updated to remove all of its futures-related dependencies and purely use `std::future`, hopefully improving its compatibility by not having any version compat considerations over time. The implementations of the executors here are relatively simple and only delve slightly into the `RawWaker` business since there are no other stable APIs in `std::task` for wrapping these. This commit also adds support for: #[wasm_bindgen_test] async fn foo() { // ... } where previously you needed to pass `(async)` now that's inferred because it's an `async fn`. Closes #1558 Closes #1695
This commit is contained in:
@ -8,10 +8,9 @@ edition = "2018"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
futures = "0.1.20"
|
||||
wasm-bindgen = { version = "0.2.50", features = ["serde-serialize"] }
|
||||
js-sys = "0.3.27"
|
||||
wasm-bindgen-futures = "0.3.27"
|
||||
wasm-bindgen-futures = "0.4.0"
|
||||
serde = { version = "1.0.80", features = ["derive"] }
|
||||
serde_derive = "^1.0.59"
|
||||
|
||||
|
@ -1,4 +1,3 @@
|
||||
use futures::{future, Future};
|
||||
use js_sys::Promise;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen::prelude::*;
|
||||
@ -37,6 +36,10 @@ pub struct Signature {
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn run() -> Promise {
|
||||
future_to_promise(run_())
|
||||
}
|
||||
|
||||
async fn run_() -> Result<JsValue, JsValue> {
|
||||
let mut opts = RequestInit::new();
|
||||
opts.method("GET");
|
||||
opts.mode(RequestMode::Cors);
|
||||
@ -44,36 +47,25 @@ pub fn run() -> Promise {
|
||||
let request = Request::new_with_str_and_init(
|
||||
"https://api.github.com/repos/rustwasm/wasm-bindgen/branches/master",
|
||||
&opts,
|
||||
)
|
||||
.unwrap();
|
||||
)?;
|
||||
|
||||
request
|
||||
.headers()
|
||||
.set("Accept", "application/vnd.github.v3+json")
|
||||
.unwrap();
|
||||
.set("Accept", "application/vnd.github.v3+json")?;
|
||||
|
||||
let window = web_sys::window().unwrap();
|
||||
let request_promise = window.fetch_with_request(&request);
|
||||
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;
|
||||
|
||||
let future = JsFuture::from(request_promise)
|
||||
.and_then(|resp_value| {
|
||||
// `resp_value` is a `Response` object.
|
||||
assert!(resp_value.is_instance_of::<Response>());
|
||||
let resp: Response = resp_value.dyn_into().unwrap();
|
||||
resp.json()
|
||||
})
|
||||
.and_then(|json_value: Promise| {
|
||||
// Convert this other `Promise` into a rust `Future`.
|
||||
JsFuture::from(json_value)
|
||||
})
|
||||
.and_then(|json| {
|
||||
// Use serde to parse the JSON into a struct.
|
||||
let branch_info: Branch = json.into_serde().unwrap();
|
||||
// `resp_value` is a `Response` object.
|
||||
assert!(resp_value.is_instance_of::<Response>());
|
||||
let resp: Response = resp_value.dyn_into().unwrap();
|
||||
|
||||
// Send the `Branch` struct back to JS as an `Object`.
|
||||
future::ok(JsValue::from_serde(&branch_info).unwrap())
|
||||
});
|
||||
// Convert this other `Promise` into a rust `Future`.
|
||||
let json = JsFuture::from(resp.json()?).await?;
|
||||
|
||||
// Convert this Rust `Future` back into a JS `Promise`.
|
||||
future_to_promise(future)
|
||||
// Use serde to parse the JSON into a struct.
|
||||
let branch_info: Branch = json.into_serde().unwrap();
|
||||
|
||||
// Send the `Branch` struct back to JS as an `Object`.
|
||||
Ok(JsValue::from_serde(&branch_info).unwrap())
|
||||
}
|
||||
|
Reference in New Issue
Block a user