Skip to content

Commit

Permalink
refactor!: Arc<Mutex<>> to mpsc::channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Onciul committed Sep 11, 2024
1 parent d09670d commit 9d9060b
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
path::PathBuf,
sync::{Arc, Mutex},
sync::mpsc::{channel, Receiver, Sender},
};

use itertools::Itertools;
Expand Down Expand Up @@ -167,7 +167,9 @@ pub struct Reedline {
#[cfg(feature = "external_printer")]
external_printer: Option<ExternalPrinter<String>>,

reedline_event_queue: Arc<Mutex<Vec<ReedlineEvent>>>,
/// A ReedlineEvent Sender, use this to submit events to the queue
pub reedline_event_sender: Sender<ReedlineEvent>,
reedline_event_receiver: Receiver<ReedlineEvent>,
}

struct BufferEditor {
Expand Down Expand Up @@ -207,6 +209,8 @@ impl Reedline {
let edit_mode = Box::<Emacs>::default();
let hist_session_id = None;

let (reedline_event_sender, reedline_event_receiver) = channel();

Reedline {
editor: Editor::default(),
history,
Expand Down Expand Up @@ -241,19 +245,11 @@ impl Reedline {
kitty_protocol: KittyProtocolGuard::default(),
#[cfg(feature = "external_printer")]
external_printer: None,
reedline_event_queue: Arc::new(Vec::new().into()),
reedline_event_receiver,
reedline_event_sender,
}
}

/// Setup a queue for reedline events that can be written from other places.
pub fn with_reedline_event_queue(
mut self,
reedline_events: Arc<Mutex<Vec<ReedlineEvent>>>,
) -> Self {
self.reedline_event_queue = reedline_events;
self
}

/// Get a new history session id based on the current time and the first commit datetime of reedline
pub fn create_history_session_id() -> Option<HistorySessionId> {
let nanos = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Expand Down Expand Up @@ -733,11 +729,13 @@ impl Reedline {

let mut latest_resize = None;
loop {
if let Ok(queue) = self.reedline_event_queue.lock() {
if !queue.is_empty() {
break;
}
while let Ok(reedline_event) = self.reedline_event_receiver.try_recv() {
reedline_events.push(reedline_event);
}
if !reedline_events.is_empty() {
break;
}

match event::read()? {
Event::Resize(x, y) => {
latest_resize = Some((x, y));
Expand Down Expand Up @@ -806,12 +804,6 @@ impl Reedline {
reedline_events.push(ReedlineEvent::Edit(ec));
}

if let Ok(mut queue) = self.reedline_event_queue.lock() {
for event in queue.drain(..) {
reedline_events.push(event);
}
}

for event in reedline_events.drain(..) {
match self.handle_event(prompt, event)? {
EventStatus::Exits(signal) => {
Expand Down

0 comments on commit 9d9060b

Please sign in to comment.