Skip to content

Commit

Permalink
Web: set control flow strategies on EventLoop (#3740)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Jun 20, 2024
1 parent db2c97a commit b4e83a5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ changelog entry.

## Unreleased

### Added

- On Web, add `EventLoopExtWebSys::(set_)poll_strategy()` to allow setting
control flow strategies before starting the event loop.

### Changed

- On Web, let events wake up event loop immediately when using
Expand Down
22 changes: 22 additions & 0 deletions src/platform/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ pub trait EventLoopExtWebSys {
)]
/// [^1]: `run_app()` is _not_ available on WASM when the target supports `exception-handling`.
fn spawn_app<A: ApplicationHandler<Self::UserEvent> + 'static>(self, app: A);

/// Sets the strategy for [`ControlFlow::Poll`].
///
/// See [`PollStrategy`].
///
/// [`ControlFlow::Poll`]: crate::event_loop::ControlFlow::Poll
fn set_poll_strategy(&self, strategy: PollStrategy);

/// Gets the strategy for [`ControlFlow::Poll`].
///
/// See [`PollStrategy`].
///
/// [`ControlFlow::Poll`]: crate::event_loop::ControlFlow::Poll
fn poll_strategy(&self) -> PollStrategy;
}

impl<T> EventLoopExtWebSys for EventLoop<T> {
Expand All @@ -191,6 +205,14 @@ impl<T> EventLoopExtWebSys for EventLoop<T> {
fn spawn_app<A: ApplicationHandler<Self::UserEvent> + 'static>(self, app: A) {
self.event_loop.spawn_app(app);
}

fn set_poll_strategy(&self, strategy: PollStrategy) {
self.event_loop.set_poll_strategy(strategy);
}

fn poll_strategy(&self) -> PollStrategy {
self.event_loop.poll_strategy()
}
}

pub trait ActiveEventLoopExtWebSys {
Expand Down
9 changes: 9 additions & 0 deletions src/platform_impl/web/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::application::ApplicationHandler;
use crate::error::EventLoopError;
use crate::event::Event;
use crate::event_loop::ActiveEventLoop as RootActiveEventLoop;
use crate::platform::web::{ActiveEventLoopExtWebSys, PollStrategy};

use super::{backend, device, window};

Expand Down Expand Up @@ -76,6 +77,14 @@ impl<T> EventLoop<T> {
pub fn window_target(&self) -> &RootActiveEventLoop {
&self.elw
}

pub fn set_poll_strategy(&self, strategy: PollStrategy) {
self.elw.set_poll_strategy(strategy);
}

pub fn poll_strategy(&self) -> PollStrategy {
self.elw.poll_strategy()
}
}

fn handle_event<T: 'static, A: ApplicationHandler<T>>(
Expand Down

0 comments on commit b4e83a5

Please sign in to comment.