Skip to content

Update winit_runner to use spawn_app for wasm32 target #16630

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

Merged
merged 4 commits into from
Jan 6, 2025
Merged
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
30 changes: 20 additions & 10 deletions crates/bevy_winit/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use bevy_tasks::tick_global_task_pools_on_main_thread;
use bevy_utils::HashMap;
use bevy_utils::Instant;
use core::marker::PhantomData;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::EventLoopExtWebSys;
use winit::{
application::ApplicationHandler,
dpi::PhysicalSize,
Expand Down Expand Up @@ -855,19 +857,27 @@ pub fn winit_runner<T: Event>(mut app: App) -> AppExit {
app.world_mut()
.insert_resource(EventLoopProxyWrapper(event_loop.create_proxy()));

let mut runner_state = WinitAppRunnerState::new(app);
let runner_state = WinitAppRunnerState::new(app);

trace!("starting winit event loop");
// TODO(clean): the winit docs mention using `spawn` instead of `run` on Wasm.
if let Err(err) = event_loop.run_app(&mut runner_state) {
error!("winit event loop returned an error: {err}");
// The winit docs mention using `spawn` instead of `run` on Wasm.
// https://docs.rs/winit/latest/winit/platform/web/trait.EventLoopExtWebSys.html#tymethod.spawn_app
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
event_loop.spawn_app(runner_state);
AppExit::Success
} else {
let mut runner_state = runner_state;
if let Err(err) = event_loop.run_app(&mut runner_state) {
error!("winit event loop returned an error: {err}");
}
// If everything is working correctly then the event loop only exits after it's sent an exit code.
runner_state.app_exit.unwrap_or_else(|| {
error!("Failed to receive an app exit code! This is a bug");
AppExit::error()
})
}
}

// If everything is working correctly then the event loop only exits after it's sent an exit code.
runner_state.app_exit.unwrap_or_else(|| {
error!("Failed to receive a app exit code! This is a bug");
AppExit::error()
})
}

pub(crate) fn react_to_resize(
Expand Down
Loading