Skip to content

Commit

Permalink
web: move from async/waker to event_loop/proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
kchibisov committed Nov 11, 2024
1 parent 92d2369 commit 8bd65c7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
6 changes: 2 additions & 4 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,9 @@ pub struct EventLoopProxy {
pub(crate) proxy: Arc<dyn EventLoopProxyProvider>,
}

impl std::fmt::Debug for EventLoopProxy {
impl fmt::Debug for EventLoopProxy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EventLoopProxy")
.field("proxy", &(Arc::as_ptr(&self.proxy) as *const usize))
.finish_non_exhaustive()
f.debug_struct("EventLoopProxy").finish_non_exhaustive()
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/platform_impl/web/async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ mod channel;
mod concurrent_queue;
mod dispatcher;
mod notifier;
mod waker;
mod wrapper;

use atomic_waker::AtomicWaker;
pub(crate) use atomic_waker::AtomicWaker;
use concurrent_queue::{ConcurrentQueue, PushError};

pub use self::abortable::{AbortHandle, Abortable, DropAbortHandle};
pub use self::channel::{channel, Receiver, Sender};
pub use self::dispatcher::{DispatchRunner, Dispatcher};
pub use self::notifier::{Notified, Notifier};
pub use self::waker::EventLoopProxy;
use self::wrapper::Wrapper;
pub(crate) use self::wrapper::Wrapper;
1 change: 1 addition & 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::event::Event;
use crate::event_loop::ActiveEventLoop as RootActiveEventLoop;
use crate::platform::web::{PollStrategy, WaitUntilStrategy};

mod proxy;
pub(crate) mod runner;
mod state;
mod window_target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,43 @@ use std::sync::Arc;
use std::task::Poll;

use super::super::main_thread::MainThreadMarker;
use super::{AtomicWaker, Wrapper};
use crate::event_loop::EventLoopProxyProvider;
use crate::platform_impl::web::event_loop::runner::WeakShared;
use crate::platform_impl::web::r#async::{AtomicWaker, Wrapper};

pub struct EventLoopProxy<T: 'static>(Wrapper<Handler<T>, Sender, ()>);
pub struct EventLoopProxy(Wrapper<Handler, Sender, ()>);

struct Handler<T> {
value: T,
handler: fn(&T, bool),
struct Handler {
execution: WeakShared,
handler: fn(&WeakShared, bool),
}

#[derive(Clone)]
struct Sender(Arc<Inner>);

impl<T> EventLoopProxy<T> {
pub fn new(main_thread: MainThreadMarker, value: T, handler: fn(&T, bool)) -> Self {
impl EventLoopProxy {
pub fn new(
main_thread: MainThreadMarker,
execution: WeakShared,
handler: fn(&WeakShared, bool),
) -> Self {
let inner = Arc::new(Inner {
awoken: AtomicBool::new(false),
waker: AtomicWaker::new(),
closed: AtomicBool::new(false),
});

let handler = Handler { value, handler };
let handler = Handler { execution, handler };

let sender = Sender(inner.clone());
let sender = Sender(Arc::clone(&inner));

Self(Wrapper::new(
main_thread,
handler,
|handler, _| {
let handler = handler.borrow();
let handler = handler.as_ref().unwrap();
(handler.handler)(&handler.value, true);
(handler.handler)(&handler.execution, true);
},
{
let inner = Arc::clone(&inner);
Expand All @@ -62,7 +67,7 @@ impl<T> EventLoopProxy<T> {
{
let handler = handler.borrow();
let handler = handler.as_ref().unwrap();
(handler.handler)(&handler.value, false);
(handler.handler)(&handler.execution, false);
}
}
},
Expand All @@ -84,7 +89,7 @@ impl<T> EventLoopProxy<T> {
}
}

impl<T> Drop for EventLoopProxy<T> {
impl Drop for EventLoopProxy {
fn drop(&mut self) {
self.0.with_sender_data(|inner| {
inner.0.closed.store(true, Ordering::Relaxed);
Expand All @@ -93,7 +98,7 @@ impl<T> Drop for EventLoopProxy<T> {
}
}

impl<T> EventLoopProxyProvider for EventLoopProxy<T> {
impl EventLoopProxyProvider for EventLoopProxy {
fn wake_up(&self) {
self.0.send(())
}
Expand Down
7 changes: 4 additions & 3 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use super::super::event;
use super::super::main_thread::MainThreadMarker;
use super::super::monitor::MonitorHandler;
use super::backend;
use super::proxy::EventLoopProxy;
use super::state::State;
use crate::dpi::PhysicalSize;
use crate::event::{DeviceEvent, ElementState, Event, RawKeyEvent, StartCause, WindowEvent};
use crate::event_loop::{ControlFlow, DeviceEvents};
use crate::platform::web::{PollStrategy, WaitUntilStrategy};
use crate::platform_impl::platform::backend::EventListenerHandle;
use crate::platform_impl::platform::r#async::{DispatchRunner, EventLoopProxy};
use crate::platform_impl::platform::r#async::DispatchRunner;
use crate::platform_impl::platform::window::Inner;
use crate::window::WindowId;

Expand All @@ -38,7 +39,7 @@ type OnEventHandle<T> = RefCell<Option<EventListenerHandle<dyn FnMut(T)>>>;

struct Execution {
main_thread: MainThreadMarker,
event_loop_proxy: Arc<EventLoopProxy<WeakShared>>,
event_loop_proxy: Arc<EventLoopProxy>,
control_flow: Cell<ControlFlow>,
poll_strategy: Cell<PollStrategy>,
wait_until_strategy: Cell<WaitUntilStrategy>,
Expand Down Expand Up @@ -819,7 +820,7 @@ impl Shared {
self.0.wait_until_strategy.get()
}

pub(crate) fn event_loop_proxy(&self) -> &Arc<EventLoopProxy<WeakShared>> {
pub(crate) fn event_loop_proxy(&self) -> &Arc<EventLoopProxy> {
&self.0.event_loop_proxy
}

Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use web_sys::Element;

use super::super::monitor::MonitorPermissionFuture;
use super::super::{lock, KeyEventExtra};
use super::runner::{EventWrapper, WeakShared};
use super::runner::EventWrapper;
use super::{backend, runner};
use crate::error::{NotSupportedError, RequestError};
use crate::event::{ElementState, Event, KeyEvent, TouchPhase, WindowEvent};
Expand All @@ -20,7 +20,7 @@ use crate::keyboard::ModifiersState;
use crate::monitor::MonitorHandle as RootMonitorHandle;
use crate::platform::web::{CustomCursorFuture, PollStrategy, WaitUntilStrategy};
use crate::platform_impl::platform::cursor::CustomCursor;
use crate::platform_impl::web::r#async::EventLoopProxy;
use crate::platform_impl::web::event_loop::proxy::EventLoopProxy;
use crate::platform_impl::Window;
use crate::window::{CustomCursor as RootCustomCursor, CustomCursorSource, Theme, WindowId};

Expand Down Expand Up @@ -477,7 +477,7 @@ impl ActiveEventLoop {
self.runner.monitor().has_detailed_monitor_permission()
}

pub(crate) fn event_loop_proxy(&self) -> Arc<EventLoopProxy<WeakShared>> {
pub(crate) fn event_loop_proxy(&self) -> Arc<EventLoopProxy> {
self.runner.event_loop_proxy().clone()
}
}
Expand Down

0 comments on commit 8bd65c7

Please sign in to comment.