Refactor(transmitter): Go back to plain tx,rx channels

This commit is contained in:
Benedikt Peetz 2023-07-18 08:07:21 +02:00
parent 6412650686
commit a413171ffe
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
4 changed files with 21 additions and 59 deletions

View File

@ -18,7 +18,7 @@ pub async fn handle(app: &mut App<'_>, command: &str) -> Result<EventStatus> {
.with_context(|| format!("Failed to execute: `{command}`"))?; .with_context(|| format!("Failed to execute: `{command}`"))?;
info!("Function evaluated to: `{output}`"); info!("Function evaluated to: `{output}`");
app.transmitter.send(Event::CiOutput(output)); app.tx.send(Event::CiOutput(output)).await.context("Failed to send ci output to internal event stream")?;
Ok(EventStatus::Ok) Ok(EventStatus::Ok)
} }

View File

@ -1,4 +1,4 @@
use anyhow::Result; use anyhow::{Context, Result};
use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers};
use crate::{ use crate::{
@ -15,14 +15,14 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
CrosstermEvent::Key(KeyEvent { CrosstermEvent::Key(KeyEvent {
code: KeyCode::Esc, .. code: KeyCode::Esc, ..
}) => { }) => {
app.transmitter app.tx
.send(Event::CommandEvent(Command::Exit)) .send(Event::CommandEvent(Command::Exit))
.await?; .await?;
} }
CrosstermEvent::Key(KeyEvent { CrosstermEvent::Key(KeyEvent {
code: KeyCode::Tab, .. code: KeyCode::Tab, ..
}) => { }) => {
app.transmitter app.tx
.send(Event::CommandEvent(Command::CyclePlanes)) .send(Event::CommandEvent(Command::CyclePlanes))
.await?; .await?;
} }
@ -30,7 +30,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
code: KeyCode::BackTab, code: KeyCode::BackTab,
.. ..
}) => { }) => {
app.transmitter app.tx
.send(Event::CommandEvent(Command::CyclePlanesRev)) .send(Event::CommandEvent(Command::CyclePlanesRev))
.await?; .await?;
} }
@ -39,7 +39,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
modifiers: KeyModifiers::CONTROL, modifiers: KeyModifiers::CONTROL,
.. ..
}) => { }) => {
app.transmitter app.tx
.send(Event::CommandEvent(Command::CommandLineShow)) .send(Event::CommandEvent(Command::CommandLineShow))
.await?; .await?;
} }
@ -51,7 +51,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
modifiers: KeyModifiers::ALT, modifiers: KeyModifiers::ALT,
.. ..
}) => { }) => {
app.transmitter app.tx
.send(Event::CommandEvent(Command::RoomMessageSend( .send(Event::CommandEvent(Command::RoomMessageSend(
app.ui.message_compose.lines().join("\n"), app.ui.message_compose.lines().join("\n"),
))) )))

View File

@ -1,11 +1,10 @@
pub mod command_interface; pub mod command_interface;
pub mod events; pub mod events;
pub mod status; pub mod status;
pub mod transmitter;
use std::path::Path; use std::path::Path;
use anyhow::{Context, Error, Result}; use anyhow::{Error, Result, Context};
use cli_log::info; use cli_log::info;
use matrix_sdk::Client; use matrix_sdk::Client;
use mlua::Lua; use mlua::Lua;
@ -19,14 +18,16 @@ use crate::{
ui::{central, setup}, ui::{central, setup},
}; };
use self::{events::event_types, transmitter::Transmitter}; use self::events::event_types;
pub struct App<'ui> { pub struct App<'ui> {
ui: central::UI<'ui>, ui: central::UI<'ui>,
accounts_manager: AccountsManager, accounts_manager: AccountsManager,
status: Status, status: Status,
transmitter: Transmitter, tx: mpsc::Sender<Event>,
rx: mpsc::Receiver<Event>,
input_listener_killer: CancellationToken, input_listener_killer: CancellationToken,
matrix_listener_killer: CancellationToken, matrix_listener_killer: CancellationToken,
@ -36,7 +37,7 @@ pub struct App<'ui> {
impl App<'_> { impl App<'_> {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
fn set_up_lua(tx: mpsc::Sender<Event>) -> Lua { fn set_up_lua(tx: mpsc::Sender<Event>) -> Lua {
let lua = Lua::new(); let mut lua = Lua::new();
generate_ci_functions(&mut lua, tx); generate_ci_functions(&mut lua, tx);
lua lua
@ -50,17 +51,18 @@ impl App<'_> {
None None
}; };
let transmitter = Transmitter::new(); let (tx, rx) = mpsc::channel(256);
Ok(Self { Ok(Self {
ui: central::UI::new()?, ui: central::UI::new()?,
accounts_manager: AccountsManager::new(config)?, accounts_manager: AccountsManager::new(config)?,
status: Status::new(None), status: Status::new(None),
transmitter, tx: tx.clone(),
rx,
input_listener_killer: CancellationToken::new(), input_listener_killer: CancellationToken::new(),
matrix_listener_killer: CancellationToken::new(), matrix_listener_killer: CancellationToken::new(),
lua: set_up_lua(transmitter.tx()), lua: set_up_lua(tx),
}) })
} }
@ -82,7 +84,7 @@ impl App<'_> {
pub async fn run(&mut self) -> Result<()> { pub async fn run(&mut self) -> Result<()> {
// Spawn input event listener // Spawn input event listener
tokio::task::spawn(events::poll_input_events( tokio::task::spawn(events::poll_input_events(
self.transmitter.tx(), self.tx.clone(),
self.input_listener_killer.clone(), self.input_listener_killer.clone(),
)); ));
@ -98,11 +100,7 @@ impl App<'_> {
self.status.set_state(State::Main); self.status.set_state(State::Main);
self.ui.update(&self.status).await?; self.ui.update(&self.status).await?;
let event = self let event = self.rx.recv().await.context("Failed to get next event")?;
.transmitter
.recv()
.await
.context("Failed to get next event")?;
match event.handle(self).await? { match event.handle(self).await? {
event_types::EventStatus::Ok => (), event_types::EventStatus::Ok => (),
@ -122,11 +120,7 @@ impl App<'_> {
self.status.set_state(State::Setup); self.status.set_state(State::Setup);
self.ui.update_setup().await?; self.ui.update_setup().await?;
let event = self let event = self.rx.recv().await.context("Failed to get next event")?;
.transmitter
.recv()
.await
.context("Failed to get next event")?;
match event.handle(self).await? { match event.handle(self).await? {
event_types::EventStatus::Ok => (), event_types::EventStatus::Ok => (),
@ -150,7 +144,7 @@ impl App<'_> {
// Spawn Matrix Event Listener // Spawn Matrix Event Listener
tokio::task::spawn(events::poll_matrix_events( tokio::task::spawn(events::poll_matrix_events(
self.transmitter.tx(), self.tx.clone(),
self.matrix_listener_killer.clone(), self.matrix_listener_killer.clone(),
client.clone(), client.clone(),
)); ));

View File

@ -1,32 +0,0 @@
use anyhow::{bail, Context, Result};
use tokio::sync::mpsc;
use super::events::event_types::Event;
pub struct Transmitter {
tx: mpsc::Sender<Event>,
rx: mpsc::Receiver<Event>,
}
impl Transmitter {
pub fn new() -> Transmitter {
let (tx, rx) = mpsc::channel(256);
Transmitter {
tx,
rx,
}
}
pub fn tx(&self) -> mpsc::Sender<Event> {
self.tx.to_owned()
}
pub async fn recv(&mut self) -> Result<Event> {
match self.rx.recv().await {
Some(event) => Ok(event),
None => bail!("Event channel has no senders"),
}
}
pub async fn send(&mut self, event: Event) -> Result<()> {
self.tx.send(event).await.context("Failed to send event")
}
}