Fix notification event ordering

Use a single thread instead of spawning a new task for each
notification. This makes sure they are processed in the correct order.
macos-click-through
Fred Sundvik 3 years ago committed by Keith Simmons
parent e7eb1a0ca7
commit b83003a8ec

@ -1,8 +1,11 @@
use async_trait::async_trait; use async_trait::async_trait;
use futures::lock::Mutex;
use log::trace; use log::trace;
use nvim_rs::{Handler, Neovim}; use nvim_rs::{Handler, Neovim};
use rmpv::Value; use rmpv::Value;
use tokio::task; use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::Arc;
use std::thread;
#[cfg(windows)] #[cfg(windows)]
use crate::bridge::ui_commands::{ParallelCommand, UiCommand}; use crate::bridge::ui_commands::{ParallelCommand, UiCommand};
@ -14,50 +17,74 @@ use crate::{
settings::SETTINGS, settings::SETTINGS,
}; };
struct NotificationEvent {
event_name: String,
arguments: Vec<Value>,
}
#[derive(Clone)] #[derive(Clone)]
pub struct NeovimHandler {} pub struct NeovimHandler {
sender: Arc<Mutex<Sender<NotificationEvent>>>,
}
impl NeovimHandler { impl NeovimHandler {
pub fn new() -> Self { pub fn new() -> Self {
Self {} let (sender, receiver): (Sender<NotificationEvent>, Receiver<NotificationEvent>) =
} channel();
} thread::spawn(|| {
for event in receiver {
#[async_trait] match event.event_name.as_ref() {
impl Handler for NeovimHandler {
type Writer = TxWrapper;
async fn handle_notify(
&self,
event_name: String,
arguments: Vec<Value>,
_neovim: Neovim<TxWrapper>,
) {
trace!("Neovim notification: {:?}", &event_name);
task::spawn_blocking(move || match event_name.as_ref() {
"redraw" => { "redraw" => {
for events in arguments { for events in event.arguments {
let parsed_events = parse_redraw_event(events) let parsed_events = parse_redraw_event(events)
.unwrap_or_explained_panic("Could not parse event from neovim"); .unwrap_or_explained_panic("Could not parse event from neovim");
for parsed_event in parsed_events { for parsed_event in parsed_events {
EVENT_AGGREGATOR.send(EditorCommand::NeovimRedrawEvent(parsed_event)); EVENT_AGGREGATOR
.send(EditorCommand::NeovimRedrawEvent(parsed_event));
} }
} }
} }
"setting_changed" => { "setting_changed" => {
SETTINGS.handle_changed_notification(arguments); SETTINGS.handle_changed_notification(event.arguments);
} }
#[cfg(windows)] #[cfg(windows)]
"neovide.register_right_click" => { "neovide.register_right_click" => {
EVENT_AGGREGATOR.send(UiCommand::Parallel(ParallelCommand::RegisterRightClick)); EVENT_AGGREGATOR
.send(UiCommand::Parallel(ParallelCommand::RegisterRightClick));
} }
#[cfg(windows)] #[cfg(windows)]
"neovide.unregister_right_click" => { "neovide.unregister_right_click" => {
EVENT_AGGREGATOR.send(UiCommand::Parallel(ParallelCommand::UnregisterRightClick)); EVENT_AGGREGATOR
.send(UiCommand::Parallel(ParallelCommand::UnregisterRightClick));
} }
_ => {} _ => {}
}
}
}); });
Self {
sender: Arc::new(Mutex::new(sender)),
}
}
}
#[async_trait]
impl Handler for NeovimHandler {
type Writer = TxWrapper;
async fn handle_notify(
&self,
event_name: String,
arguments: Vec<Value>,
_neovim: Neovim<TxWrapper>,
) {
trace!("Neovim notification: {:?}", &event_name);
let sender = self.sender.lock().await;
sender
.send(NotificationEvent {
event_name,
arguments,
})
.ok();
} }
} }

Loading…
Cancel
Save