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

Examples: The winit::EventLoop should not be run in wasm_bindgen_futures::spawn_local #1457

Closed
dragly opened this issue Jul 19, 2020 · 3 comments
Labels
area: ecosystem Help the connected projects grow and prosper help required We need community help to make this happen.

Comments

@dragly
Copy link
Contributor

dragly commented Jul 19, 2020

Running the event loop inside of an async function in spawn_local appears to block new JS promises from being run if they are created in an event callback:

rustwasm/wasm-bindgen#2246

I am not sure exactly how to restructure the examples, but figured I would post this here in case anyone knows the right way to do this or comes across the same issue as I did.

@zachreizner
Copy link

I had a similar problem to yours, in that I was trying to use fetch from web_sys inside of the winit event loop. Eventually I got around this by constructing wasm_bindgen's Closure, registering it to a given Promise using .then(), and calling .forget() on the closure. It seems that Closures can not be dropped without causing the Promise to get canceled in the wasm_bindgen implementation. I suspect keeping a reference to the Closure without calling .forget() would also work, but I haven't tried that.

let resolve = Closure::once(move |resp: JsValue| {
    console::log_1(&"resolved".into());
    let resp: web_sys::Response = resp.dyn_into().unwrap();
    let resolve = Closure::once(move |array: JsValue| {
        let array = js_sys::Uint8Array::new(&array);
        do_stuff_with_bytes(array.to_vec().into());
    });
    resp.array_buffer().unwrap().then(&resolve);
    resolve.forget();
});

let reject = Closure::once(move |resp: JsValue| {
    console::log_2(&"rejected".into(), &resp);
});

window.fetch_with_str(&url).then2(&resolve, &reject);
resolve.forget();
reject.forget();

dragly referenced this issue in dragly/wgpu-rs Aug 16, 2020
Running the event loop inside an async function blocks new futures from
being spawned in the examples using spawn_local. This change moves the
event loop out of the async function and runs it directly in main. To
still be able to initialize the wgpu-rs adapter and device asynchronously,
we therefore need to introduce a custom event that is sent to the event
loop once the wgpu-rs adapter and device have been created.

Fixes #454
dragly referenced this issue in dragly/wgpu-rs Aug 16, 2020
Running the event loop inside an async function blocks new futures from
being spawned in the examples using spawn_local. This change moves the
event loop out of the async function and runs it directly in main. To
still be able to initialize the wgpu-rs adapter and device asynchronously,
we therefore need to introduce a custom event that is sent to the event
loop once the wgpu-rs adapter and device have been created.

Fixes #454
kejor referenced this issue in kejor/wgpu-rs Nov 28, 2020
492: Encode render targets into the render pass blob r=grovesNL a=kvark

Fixes #454

Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
@kvark kvark transferred this issue from gfx-rs/wgpu-rs Jun 3, 2021
@kvark kvark added area: ecosystem Help the connected projects grow and prosper help required We need community help to make this happen. labels Jun 3, 2021
@kvark
Copy link
Member

kvark commented Jun 3, 2021

@grovesNL should this be closed by #1469 ?

@grovesNL
Copy link
Collaborator

grovesNL commented Jun 3, 2021

@kvark yeah, the wgpu examples should be ok now, so we can close this.

We'll also track the upstream fix for winit in rust-windowing/winit#1714

@grovesNL grovesNL closed this as completed Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: ecosystem Help the connected projects grow and prosper help required We need community help to make this happen.
Projects
None yet
4 participants