Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawn local #1148

Merged
merged 4 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,22 @@ fn _future_to_promise(future: Box<Future<Item = JsValue, Error = JsValue>>) -> P
}
}
}

/// Converts a Rust `Future` on a local task queue.
///
/// The `future` provided must adhere to `'static` because it'll be scheduled
/// to run in the background and cannot contain any stack references.
///
/// # Panics
///
/// This function has the same panic behavior as `future_to_promise`.
pub fn spawn_local<F>(future: F)
where
F: Future<Item=(), Error=()> + 'static,
{
Copy link
Contributor

@lcnr lcnr Jan 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why only a F: Future<Item = (), Error = ()> is allowed?
I would personally prefer

pub fn spawn_local(future: impl Future + 'static) {
    future_to_promise(
        future
            .map(|_| JsValue::undefined())
            .map_err(|_| JsValue::undefined()),
    );
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to what you suggest :)

Copy link
Contributor

@Pauan Pauan Jan 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lcnr It is standard practice to require (). See tokio::run and stdweb::spawn_local.

The reason this is done is to prevent mistakes, and also to force the user to handle errors (rather than silently ignoring them, which is very bad!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to do it either way. Maybe we should match other libraries though @lcnr. You can always make your own wrapper

pub fn spawn_local(future: impl Future + 'static) {
    wasm_bindgen_futures::spawn_local(
        future.map(|_| ()).map_err(|_| ())
    )
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it and agree with @Pauan .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woop consensus.

future_to_promise(
future
.map(|()| JsValue::undefined())
.map_err(|()| JsValue::undefined()),
);
}
20 changes: 19 additions & 1 deletion crates/futures/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate wasm_bindgen_test;
use futures::unsync::oneshot;
use futures::Future;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::{future_to_promise, JsFuture};
use wasm_bindgen_futures::{future_to_promise, spawn_local, JsFuture};
use wasm_bindgen_test::*;

#[wasm_bindgen_test(async)]
Expand Down Expand Up @@ -68,3 +68,21 @@ fn oneshot_works() -> impl Future<Item = (), Error = JsValue> {
closure.forget();
rx.then(|_| Ok(()))
}

#[wasm_bindgen_test(async)]
fn spawn_local_runs() -> impl Future<Item = (), Error = JsValue> {
let (tx, rx) = oneshot::channel::<u32>();
let fn_box = Box::new(move || {
tx.send(42).unwrap();
});
spawn_local(futures::future::ok::<(), ()>(()).map(|_| {
fn_box();
}));
rx.then(|val| {
if val == Ok(42) {
Ok(())
} else {
Err(JsValue::undefined())
}
})
}