From 5a8d24b77c77be4370e9573ec7f368c1527370d6 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Fri, 24 Jan 2020 15:10:54 -0800 Subject: [PATCH] fix redraw event order --- src/bridge/handler.rs | 32 ++++++++++++++++++++++++++++---- src/bridge/mod.rs | 2 +- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/bridge/handler.rs b/src/bridge/handler.rs index 0f3def0..968c3de 100644 --- a/src/bridge/handler.rs +++ b/src/bridge/handler.rs @@ -4,13 +4,38 @@ use rmpv::Value; use nvim_rs::{Neovim, Handler, compat::tokio::Compat}; use async_trait::async_trait; use tokio::process::ChildStdin; +use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; use crate::error_handling::ResultPanicExplanation; use crate::editor::{EDITOR, Editor}; -use super::events::parse_neovim_event; +use super::events::{RedrawEvent, parse_neovim_event}; #[derive(Clone)] -pub struct NeovimHandler { } +pub struct NeovimHandler { + sender: UnboundedSender +} + +impl NeovimHandler { + pub fn new() -> NeovimHandler { + let (sender, mut receiver) = unbounded_channel::(); + + tokio::spawn(async move { + while let Some(event) = receiver.recv().await { + let mut editor = EDITOR.lock().unwrap(); + editor.handle_redraw_event(event); + } + }); + + NeovimHandler { + sender + } + } + + pub fn handle_redraw_event(&self, event: RedrawEvent) { + self.sender.send(event); + } +} + #[async_trait] impl Handler for NeovimHandler { @@ -20,8 +45,7 @@ impl Handler for NeovimHandler { let parsed_events = parse_neovim_event(event_name, arguments) .unwrap_or_explained_panic("Could not parse event", "Could not parse event from neovim"); for event in parsed_events { - let mut editor = EDITOR.lock().unwrap(); - editor.handle_redraw_event(event); + self.handle_redraw_event(event); } } } diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index c85f597..73b72f0 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -56,7 +56,7 @@ async fn drain(receiver: &mut UnboundedReceiver) -> Option) { let (width, height) = INITIAL_DIMENSIONS; - let (mut nvim, io_handler, _) = create::new_child_cmd(&mut create_nvim_command(), NeovimHandler { }).await + let (mut nvim, io_handler, _) = create::new_child_cmd(&mut create_nvim_command(), NeovimHandler::new()).await .unwrap_or_explained_panic("Could not create nvim process", "Could not locate or start the neovim process"); tokio::spawn(async move {