fix(scheduling): don't block the main thread when no actions are available to process (#416)

This commit is contained in:
Alexandre Pasmantier 2025-03-20 14:37:36 +01:00 committed by GitHub
parent fc2f6cde46
commit 05bd64afe9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -214,6 +214,7 @@ impl App {
} }
} }
} }
// It's important that this shouldn't block if no actions are available
let action_outcome = self.handle_actions(&mut action_buf).await?; let action_outcome = self.handle_actions(&mut action_buf).await?;
if self.should_quit { if self.should_quit {
@ -254,9 +255,9 @@ impl App {
fn convert_event_to_action(&self, event: Event<Key>) -> Option<Action> { fn convert_event_to_action(&self, event: Event<Key>) -> Option<Action> {
let action = match event { let action = match event {
Event::Input(keycode) => { Event::Input(keycode) => {
debug!("Converting {:?} to action", keycode);
// get action based on keybindings // get action based on keybindings
if let Some(action) = self.keymap.get(&keycode) { if let Some(action) = self.keymap.get(&keycode) {
debug!("Keybinding found: {action:?}");
action.clone() action.clone()
} else { } else {
// text input events // text input events
@ -281,6 +282,10 @@ impl App {
Event::Closed => Action::NoOp, Event::Closed => Action::NoOp,
}; };
if action != Action::Tick {
trace!("Converted event to action: {action:?}");
}
if action == Action::NoOp { if action == Action::NoOp {
None None
} else { } else {
@ -293,6 +298,8 @@ impl App {
/// This function will handle all actions that are sent to the application. /// This function will handle all actions that are sent to the application.
/// The function will return the selected entry if the application is exited. /// The function will return the selected entry if the application is exited.
/// ///
/// Note that this function will not block if no actions are available.
///
/// # Returns /// # Returns
/// The selected entry (if any) if the application is exited. /// The selected entry (if any) if the application is exited.
/// ///
@ -302,6 +309,9 @@ impl App {
&mut self, &mut self,
buf: &mut Vec<Action>, buf: &mut Vec<Action>,
) -> Result<ActionOutcome> { ) -> Result<ActionOutcome> {
if self.action_rx.is_empty() {
return Ok(ActionOutcome::None);
}
if self.action_rx.recv_many(buf, ACTION_BUF_SIZE).await > 0 { if self.action_rx.recv_many(buf, ACTION_BUF_SIZE).await > 0 {
for action in buf.drain(..) { for action in buf.drain(..) {
if action != Action::Tick { if action != Action::Tick {