Skip to content

Commit

Permalink
Move WakerSpawner into EventLoopWindowTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Oct 16, 2023
1 parent 924da1e commit b376cf4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
10 changes: 1 addition & 9 deletions src/platform_impl/web/event_loop/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::iter;
use std::marker::PhantomData;
use std::sync::mpsc::{self, Receiver, Sender};

use crate::error::EventLoopError;
use crate::event::Event;
use crate::event_loop::EventLoopWindowTarget as RootEventLoopWindowTarget;

use super::r#async::WakerSpawner;
use super::{backend, device, window};

mod proxy;
Expand All @@ -19,7 +17,6 @@ pub use window_target::EventLoopWindowTarget;

pub struct EventLoop<T: 'static> {
elw: RootEventLoopWindowTarget<T>,
proxy_spawner: WakerSpawner<runner::Shared>,
user_event_sender: Sender<T>,
user_event_receiver: Receiver<T>,
}
Expand All @@ -34,13 +31,8 @@ impl<T> EventLoop<T> {
p: EventLoopWindowTarget::new(),
_marker: PhantomData,
};
let proxy_spawner = WakerSpawner::new(elw.p.runner.clone(), |runner, count| {
runner.send_events(iter::repeat(Event::UserEvent(())).take(count))
})
.expect("`EventLoop` has to be created in the main thread");
Ok(EventLoop {
elw,
proxy_spawner,
user_event_sender,
user_event_receiver,
})
Expand Down Expand Up @@ -110,7 +102,7 @@ impl<T> EventLoop<T> {
}

pub fn create_proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy::new(self.proxy_spawner.waker(), self.user_event_sender.clone())
EventLoopProxy::new(self.elw.p.waker(), self.user_event_sender.clone())
}

pub fn window_target(&self) -> &RootEventLoopWindowTarget<T> {
Expand Down
7 changes: 4 additions & 3 deletions src/platform_impl/web/event_loop/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::rc::Weak;
use std::sync::mpsc::{SendError, Sender};

use super::runner;
use super::runner::Execution;
use crate::event_loop::EventLoopClosed;
use crate::platform_impl::platform::r#async::Waker;

pub struct EventLoopProxy<T: 'static> {
runner: Waker<runner::Shared>,
runner: Waker<Weak<Execution>>,
sender: Sender<T>,
}

impl<T: 'static> EventLoopProxy<T> {
pub fn new(runner: Waker<runner::Shared>, sender: Sender<T>) -> Self {
pub fn new(runner: Waker<Weak<Execution>>, sender: Sender<T>) -> Self {
Self { runner, sender }
}

Expand Down
64 changes: 40 additions & 24 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::event::{
use crate::event_loop::{ControlFlow, DeviceEvents};
use crate::platform::web::PollStrategy;
use crate::platform_impl::platform::backend::EventListenerHandle;
use crate::platform_impl::platform::r#async::{Waker, WakerSpawner};
use crate::window::WindowId;

use std::{
Expand Down Expand Up @@ -35,6 +36,7 @@ impl Clone for Shared {
type OnEventHandle<T> = RefCell<Option<EventListenerHandle<dyn FnMut(T)>>>;

pub struct Execution {
proxy_spawner: WakerSpawner<Weak<Self>>,
control_flow: Cell<ControlFlow>,
poll_strategy: Cell<PollStrategy>,
exit: Cell<bool>,
Expand Down Expand Up @@ -139,30 +141,40 @@ impl Shared {
#[allow(clippy::disallowed_methods)]
let document = window.document().expect("Failed to obtain document");

Shared(Rc::new(Execution {
control_flow: Cell::new(ControlFlow::default()),
poll_strategy: Cell::new(PollStrategy::default()),
exit: Cell::new(false),
runner: RefCell::new(RunnerEnum::Pending),
suspended: Cell::new(false),
event_loop_recreation: Cell::new(false),
events: RefCell::new(VecDeque::new()),
window,
document,
id: RefCell::new(0),
all_canvases: RefCell::new(Vec::new()),
redraw_pending: RefCell::new(HashSet::new()),
destroy_pending: RefCell::new(VecDeque::new()),
page_transition_event_handle: RefCell::new(None),
device_events: Cell::default(),
on_mouse_move: RefCell::new(None),
on_wheel: RefCell::new(None),
on_mouse_press: RefCell::new(None),
on_mouse_release: RefCell::new(None),
on_key_press: RefCell::new(None),
on_key_release: RefCell::new(None),
on_visibility_change: RefCell::new(None),
on_touch_end: RefCell::new(None),
Shared(Rc::<Execution>::new_cyclic(|weak| {
let proxy_spawner = WakerSpawner::new(weak.clone(), |runner, count| {
if let Some(runner) = runner.upgrade() {
Shared(runner).send_events(iter::repeat(Event::UserEvent(())).take(count))
}
})
.expect("`EventLoop` has to be created in the main thread");

Execution {
proxy_spawner,
control_flow: Cell::new(ControlFlow::default()),
poll_strategy: Cell::new(PollStrategy::default()),
exit: Cell::new(false),
runner: RefCell::new(RunnerEnum::Pending),
suspended: Cell::new(false),
event_loop_recreation: Cell::new(false),
events: RefCell::new(VecDeque::new()),
window,
document,
id: RefCell::new(0),
all_canvases: RefCell::new(Vec::new()),
redraw_pending: RefCell::new(HashSet::new()),
destroy_pending: RefCell::new(VecDeque::new()),
page_transition_event_handle: RefCell::new(None),
device_events: Cell::default(),
on_mouse_move: RefCell::new(None),
on_wheel: RefCell::new(None),
on_mouse_press: RefCell::new(None),
on_mouse_release: RefCell::new(None),
on_key_press: RefCell::new(None),
on_key_release: RefCell::new(None),
on_visibility_change: RefCell::new(None),
on_touch_end: RefCell::new(None),
}
}))
}

Expand Down Expand Up @@ -779,6 +791,10 @@ impl Shared {
pub(crate) fn poll_strategy(&self) -> PollStrategy {
self.0.poll_strategy.get()
}

pub(crate) fn waker(&self) -> Waker<Weak<Execution>> {
self.0.proxy_spawner.waker()
}
}

pub(crate) enum EventWrapper {
Expand Down
9 changes: 7 additions & 2 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::clone::Clone;
use std::collections::{vec_deque::IntoIter as VecDequeIter, VecDeque};
use std::iter;
use std::marker::PhantomData;
use std::rc::Rc;
use std::rc::{Rc, Weak};

use super::runner::EventWrapper;
use super::runner::{EventWrapper, Execution};
use super::{
super::{monitor::MonitorHandle, KeyEventExtra},
backend,
Expand All @@ -19,6 +19,7 @@ use crate::event::{
use crate::event_loop::{ControlFlow, DeviceEvents};
use crate::keyboard::ModifiersState;
use crate::platform::web::PollStrategy;
use crate::platform_impl::platform::r#async::Waker;
use crate::window::{Theme, WindowId as RootWindowId};

#[derive(Default)]
Expand Down Expand Up @@ -702,4 +703,8 @@ impl<T> EventLoopWindowTarget<T> {
pub(crate) fn poll_strategy(&self) -> PollStrategy {
self.runner.poll_strategy()
}

pub(crate) fn waker(&self) -> Waker<Weak<Execution>> {
self.runner.waker()
}
}

0 comments on commit b376cf4

Please sign in to comment.