mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-25 14:12:13 +00:00
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
91 lines
2.3 KiB
Rust
Executable File
91 lines
2.3 KiB
Rust
Executable File
#![cfg(target_arch = "wasm32")]
|
|
|
|
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
|
|
|
|
use futures_channel::oneshot;
|
|
use wasm_bindgen::prelude::*;
|
|
use wasm_bindgen_futures::{future_to_promise, spawn_local, JsFuture};
|
|
use wasm_bindgen_test::*;
|
|
|
|
#[wasm_bindgen_test]
|
|
async fn promise_resolve_is_ok_future() {
|
|
let p = js_sys::Promise::resolve(&JsValue::from(42));
|
|
let x = JsFuture::from(p).await.unwrap();
|
|
assert_eq!(x, 42);
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
async fn promise_reject_is_error_future() {
|
|
let p = js_sys::Promise::reject(&JsValue::from(42));
|
|
let e = JsFuture::from(p).await.unwrap_err();
|
|
assert_eq!(e, 42);
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
async fn ok_future_is_resolved_promise() {
|
|
let p = future_to_promise(async { Ok(JsValue::from(42)) });
|
|
let x = JsFuture::from(p).await.unwrap();
|
|
assert_eq!(x, 42);
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
async fn error_future_is_rejected_promise() {
|
|
let p = future_to_promise(async { Err(JsValue::from(42)) });
|
|
let e = JsFuture::from(p).await.unwrap_err();
|
|
assert_eq!(e, 42);
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn debug_jsfuture() {
|
|
let p = js_sys::Promise::resolve(&JsValue::from(42));
|
|
let f = JsFuture::from(p);
|
|
assert_eq!(&format!("{:?}", f), "JsFuture { ... }");
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
extern "C" {
|
|
fn setTimeout(c: &Closure<dyn FnMut()>);
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
async fn oneshot_works() {
|
|
let (tx, rx) = oneshot::channel::<u32>();
|
|
let mut tx = Some(tx);
|
|
let closure = Closure::wrap(Box::new(move || {
|
|
drop(tx.take().unwrap());
|
|
}) as Box<dyn FnMut()>);
|
|
setTimeout(&closure);
|
|
closure.forget();
|
|
rx.await.unwrap_err();
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
async fn spawn_local_runs() {
|
|
let (tx, rx) = oneshot::channel::<u32>();
|
|
spawn_local(async {
|
|
tx.send(42).unwrap();
|
|
});
|
|
assert_eq!(rx.await.unwrap(), 42);
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
async fn spawn_local_err_no_exception() {
|
|
let (tx, rx) = oneshot::channel::<u32>();
|
|
spawn_local(async { });
|
|
spawn_local(async {
|
|
tx.send(42).unwrap();
|
|
});
|
|
let val = rx.await.unwrap();
|
|
assert_eq!(val, 42);
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
async fn can_create_multiple_futures_from_same_promise() {
|
|
let promise = js_sys::Promise::resolve(&JsValue::null());
|
|
let a = JsFuture::from(promise.clone());
|
|
let b = JsFuture::from(promise);
|
|
|
|
a.await.unwrap();
|
|
b.await.unwrap();
|
|
}
|