Skip to content

Commit

Permalink
Rename PollType to PollStrategy (#3089)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Sep 8, 2023
1 parent c00c1e9 commit 0caba93
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- Fix a bug where Wayland would be chosen on Linux even if the user specified `with_x11`. (#3058)
- **Breaking:** Moved `ControlFlow` to `EventLoopWindowTarget::set_control_flow()` and `EventLoopWindowTarget::control_flow()`.
- **Breaking:** Moved `ControlFlow::Exit` to `EventLoopWindowTarget::exit()` and `EventLoopWindowTarget::exiting()` and removed `ControlFlow::ExitWithCode(_)` entirely.
- On Web, add `EventLoopWindowTargetExtWebSys` and `PollType`, which allows to set different strategies for `ControlFlow::Poll`. By default the Prioritized Task Scheduling API is used, but an option to use `Window.requestIdleCallback` is available as well. Both use `setTimeout()`, with a trick to circumvent throttling to 4ms, as a fallback.
- On Web, add `EventLoopWindowTargetExtWebSys` and `PollStrategy`, which allows to set different strategies for `ControlFlow::Poll`. By default the Prioritized Task Scheduling API is used, but an option to use `Window.requestIdleCallback` is available as well. Both use `setTimeout()`, with a trick to circumvent throttling to 4ms, as a fallback.

# 0.29.1-beta

Expand Down
18 changes: 9 additions & 9 deletions src/platform/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,34 +138,34 @@ impl<T> EventLoopExtWebSys for EventLoop<T> {
pub trait EventLoopWindowTargetExtWebSys {
/// Sets the strategy for [`ControlFlow::Poll`].
///
/// See [`PollType`].
/// See [`PollStrategy`].
///
/// [`ControlFlow::Poll`]: crate::event_loop::ControlFlow::Poll
fn set_poll_type(&self, poll_type: PollType);
fn set_poll_strategy(&self, strategy: PollStrategy);

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

impl<T> EventLoopWindowTargetExtWebSys for EventLoopWindowTarget<T> {
#[inline]
fn set_poll_type(&self, poll_type: PollType) {
self.p.set_poll_type(poll_type);
fn set_poll_strategy(&self, strategy: PollStrategy) {
self.p.set_poll_strategy(strategy);
}

#[inline]
fn poll_type(&self) -> PollType {
self.p.poll_type()
fn poll_strategy(&self) -> PollStrategy {
self.p.poll_strategy()
}
}

/// Strategy used for [`ControlFlow::Poll`](crate::event_loop::ControlFlow::Poll).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PollType {
pub enum PollStrategy {
/// Uses [`Window.requestIdleCallback()`] to queue the next event loop. If not available
/// this will fallback to [`setTimeout()`].
///
Expand Down
16 changes: 8 additions & 8 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::event::{
WindowEvent,
};
use crate::event_loop::{ControlFlow, DeviceEvents};
use crate::platform::web::PollType;
use crate::platform::web::PollStrategy;
use crate::platform_impl::platform::backend::EventListenerHandle;
use crate::window::WindowId;

Expand Down Expand Up @@ -37,7 +37,7 @@ type OnEventHandle<T> = RefCell<Option<EventListenerHandle<dyn FnMut(T)>>>;

pub struct Execution {
control_flow: Cell<ControlFlow>,
poll_type: Cell<PollType>,
poll_strategy: Cell<PollStrategy>,
exit: Cell<bool>,
runner: RefCell<RunnerEnum>,
suspended: Cell<bool>,
Expand Down Expand Up @@ -142,7 +142,7 @@ impl Shared {

Shared(Rc::new(Execution {
control_flow: Cell::new(ControlFlow::default()),
poll_type: Cell::new(PollType::default()),
poll_strategy: Cell::new(PollStrategy::default()),
exit: Cell::new(false),
runner: RefCell::new(RunnerEnum::Pending),
suspended: Cell::new(false),
Expand Down Expand Up @@ -638,7 +638,7 @@ impl Shared {
let cloned = self.clone();
State::Poll {
request: backend::Schedule::new(
self.poll_type(),
self.poll_strategy(),
self.window(),
move || cloned.poll(),
),
Expand Down Expand Up @@ -773,12 +773,12 @@ impl Shared {
self.0.exit.get()
}

pub(crate) fn set_poll_type(&self, poll_type: PollType) {
self.0.poll_type.set(poll_type)
pub(crate) fn set_poll_strategy(&self, strategy: PollStrategy) {
self.0.poll_strategy.set(strategy)
}

pub(crate) fn poll_type(&self) -> PollType {
self.0.poll_type.get()
pub(crate) fn poll_strategy(&self) -> PollStrategy {
self.0.poll_strategy.get()
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::event::{
};
use crate::event_loop::{ControlFlow, DeviceEvents};
use crate::keyboard::ModifiersState;
use crate::platform::web::PollType;
use crate::platform::web::PollStrategy;
use crate::window::{Theme, WindowId as RootWindowId};

#[derive(Default)]
Expand Down Expand Up @@ -696,11 +696,11 @@ impl<T> EventLoopWindowTarget<T> {
self.runner.exiting()
}

pub(crate) fn set_poll_type(&self, poll_type: PollType) {
self.runner.set_poll_type(poll_type)
pub(crate) fn set_poll_strategy(&self, strategy: PollStrategy) {
self.runner.set_poll_strategy(strategy)
}

pub(crate) fn poll_type(&self) -> PollType {
self.runner.poll_type()
pub(crate) fn poll_strategy(&self) -> PollStrategy {
self.runner.poll_strategy()
}
}
8 changes: 4 additions & 4 deletions src/platform_impl/web/web_sys/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsCast, JsValue};
use web_sys::{AbortController, AbortSignal, MessageChannel, MessagePort};

use crate::platform::web::PollType;
use crate::platform::web::PollStrategy;

#[derive(Debug)]
pub struct Schedule {
Expand All @@ -32,13 +32,13 @@ enum Inner {
}

impl Schedule {
pub fn new<F>(poll_type: PollType, window: &web_sys::Window, f: F) -> Schedule
pub fn new<F>(strategy: PollStrategy, window: &web_sys::Window, f: F) -> Schedule
where
F: 'static + FnMut(),
{
if poll_type == PollType::Scheduler && has_scheduler_support(window) {
if strategy == PollStrategy::Scheduler && has_scheduler_support(window) {
Self::new_scheduler(window, f, None)
} else if poll_type == PollType::IdleCallback && has_idle_callback_support(window) {
} else if strategy == PollStrategy::IdleCallback && has_idle_callback_support(window) {
Self::new_idle_callback(window.clone(), f)
} else {
Self::new_timeout(window.clone(), f, None)
Expand Down

0 comments on commit 0caba93

Please sign in to comment.