diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index 1060cf2..41a2976 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -37,7 +37,19 @@ fn create_nvim_command() -> Command { cmd } -pub async fn start_process(editor: Arc>, mut receiver: UnboundedReceiver, grid_dimensions: (u64, u64)) { +async fn drain(receiver: &mut UnboundedReceiver) -> Option> { + if let Some(ui_command) = receiver.recv().await { + let mut results = vec![ui_command]; + while let Ok(ui_command) = receiver.try_recv() { + results.push(ui_command); + } + Some(results) + } else { + None + } +} + +async fn start_process(editor: Arc>, mut receiver: UnboundedReceiver, grid_dimensions: (u64, u64)) { let (width, height) = grid_dimensions; let (mut nvim, io_handler, _) = create::new_child_cmd(&mut create_nvim_command(), NeovimHandler(editor)).await .unwrap_or_explained_panic("Could not create nvim process", "Could not locate or start the neovim process"); @@ -61,21 +73,19 @@ pub async fn start_process(editor: Arc>, mut receiver: UnboundedRe let nvim = Arc::new(nvim); loop { - let mut commands = Vec::new(); - let mut resize_command = None; - while let Ok(ui_command) = receiver.try_recv() { - if let UiCommand::Resize { .. } = ui_command { - resize_command = Some(ui_command); - } else { - commands.push(ui_command); + if let Some(commands) = drain(&mut receiver).await { + let (resize_list, other_commands): (Vec, Vec) = commands + .into_iter() + .partition(|command| command.is_resize()); + if let Some(resize_command) = resize_list.into_iter().last() { + resize_command.execute(&nvim).await; } - } - if let Some(resize_command) = resize_command { - commands.push(resize_command); - } - for ui_command in commands.into_iter() { - ui_command.execute(&nvim).await; + for ui_command in other_commands.into_iter() { + ui_command.execute(&nvim).await; + } + } else { + break; } } } diff --git a/src/bridge/ui_commands.rs b/src/bridge/ui_commands.rs index 48fd49e..580ddfa 100644 --- a/src/bridge/ui_commands.rs +++ b/src/bridge/ui_commands.rs @@ -34,4 +34,11 @@ impl UiCommand { .expect("Mouse Drag Failed") } } + + pub fn is_resize(&self) -> bool { + match self { + UiCommand::Resize { .. } => true, + _ => false + } + } }