Skip to content

Commit

Permalink
Pre-fetch user events when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Sep 15, 2023
1 parent 09339af commit d1e368c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/platform_impl/web/async/waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ impl<T> WakerSpawner<T> {
pub fn waker(&self) -> Waker<T> {
Waker(self.0.clone())
}

pub fn fetch(&self) -> usize {
debug_assert!(
self.0.is_main_thread(),
"this should only be called from the main thread"
);

self.0
.with_sender_data(|inner| inner.0.counter.swap(0, Ordering::Relaxed))
}
}

impl<T> Drop for WakerSpawner<T> {
Expand Down
13 changes: 12 additions & 1 deletion src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,18 @@ impl Shared {
if !is_closed && self.0.runner.borrow().maybe_runner().is_some() {
// Take an event out of the queue and handle it
// Make sure not to let the borrow_mut live during the next handle_event
let event = { self.0.events.borrow_mut().pop_front() };
let event = {
let mut events = self.0.events.borrow_mut();

// Pre-fetch `UserEvent`s to avoid having to wait until the next event loop cycle.
events.extend(
iter::repeat(Event::UserEvent(()))
.take(self.0.proxy_spawner.fetch())
.map(EventWrapper::from),
);

events.pop_front()
};
if let Some(event) = event {
self.handle_event(event);
}
Expand Down

0 comments on commit d1e368c

Please sign in to comment.