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

Spawn local #1148

merged 4 commits into from
Jan 7, 2019

Conversation

derekdreery
Copy link
Contributor

This is a very simple implementation of spawn_local, a function that spawns a rust future using the javascript promise infrastructure as the executor.

This is just a naive implementation. It seems it can be improved using a
custom task queue, but that can be in a separate PR.
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.

@derekdreery
Copy link
Contributor Author

Are the docs good enough? It might be that the module documentation needs to be changed.

@alexcrichton alexcrichton merged commit 194a169 into rustwasm:master Jan 7, 2019
@alexcrichton
Copy link
Contributor

Thanks @derekdreery!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants