mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-30 22:25:35 +03:00
a1da85a24b
Previously whenever a future readiness notification came in we would immediately start polling a future. This ends up having two downsides, however: * First, the stack depth may run a risk of getting blown. There's no recursion limit to defer execution to later, which means that if futures are always ready we'll keep making the stack deeper. * Second, and more worrisome in the near term, apparently future adapaters in the `futures` crate (namely the unsync oneshot channel) doesn't actually work if you immediately poll on readiness. This may or may not be a bug in the `futures` crate but it's good to fix it here anyway. As a result whenever a future is ready to get polled again we defer its polling to the next turn of the event loop. This should ensure that the current call stack is always drained and we're effectively enqueueing the future to be polled in the near future.
69 lines
1.9 KiB
Rust
69 lines
1.9 KiB
Rust
#![cfg(target_arch = "wasm32")]
|
|
|
|
extern crate futures;
|
|
extern crate js_sys;
|
|
extern crate wasm_bindgen;
|
|
extern crate wasm_bindgen_futures;
|
|
extern crate wasm_bindgen_test;
|
|
|
|
use futures::Future;
|
|
use futures::unsync::oneshot;
|
|
use wasm_bindgen::prelude::*;
|
|
use wasm_bindgen_futures::{future_to_promise, JsFuture};
|
|
use wasm_bindgen_test::*;
|
|
|
|
#[wasm_bindgen_test(async)]
|
|
fn promise_resolve_is_ok_future() -> impl Future<Item = (), Error = JsValue> {
|
|
let p = js_sys::Promise::resolve(&JsValue::from(42));
|
|
JsFuture::from(p)
|
|
.map(|x| {
|
|
assert_eq!(x, 42);
|
|
}).map_err(|_| unreachable!())
|
|
}
|
|
|
|
#[wasm_bindgen_test(async)]
|
|
fn promise_reject_is_error_future() -> impl Future<Item = (), Error = JsValue> {
|
|
let p = js_sys::Promise::reject(&JsValue::from(42));
|
|
JsFuture::from(p).map(|_| unreachable!()).or_else(|e| {
|
|
assert_eq!(e, 42);
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
#[wasm_bindgen_test(async)]
|
|
fn ok_future_is_resolved_promise() -> impl Future<Item = (), Error = JsValue> {
|
|
let f = futures::future::ok(JsValue::from(42));
|
|
let p = future_to_promise(f);
|
|
JsFuture::from(p)
|
|
.map(|x| {
|
|
assert_eq!(x, 42);
|
|
}).map_err(|_| unreachable!())
|
|
}
|
|
|
|
#[wasm_bindgen_test(async)]
|
|
fn error_future_is_rejected_promise() -> impl Future<Item = (), Error = JsValue> {
|
|
let f = futures::future::err(JsValue::from(42));
|
|
let p = future_to_promise(f);
|
|
JsFuture::from(p).map(|_| unreachable!()).or_else(|e| {
|
|
assert_eq!(e, 42);
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
extern {
|
|
fn setTimeout(c: &Closure<FnMut()>);
|
|
}
|
|
|
|
#[wasm_bindgen_test(async)]
|
|
fn oneshot_works() -> impl Future<Item = (), Error = JsValue> {
|
|
let (tx, rx) = oneshot::channel::<u32>();
|
|
let mut tx = Some(tx);
|
|
let closure = Closure::wrap(Box::new(move || {
|
|
drop(tx.take().unwrap());
|
|
}) as Box<FnMut()>);
|
|
setTimeout(&closure);
|
|
closure.forget();
|
|
rx.then(|_| Ok(()))
|
|
}
|