From a413171ffe861dc7ee049a0202ec846d3181c623 Mon Sep 17 00:00:00 2001 From: Soispha Date: Tue, 18 Jul 2023 08:07:21 +0200 Subject: [PATCH] Refactor(transmitter): Go back to plain tx,rx channels --- .../event_types/event/handlers/lua_command.rs | 2 +- .../events/event_types/event/handlers/main.rs | 12 +++---- src/app/mod.rs | 34 ++++++++----------- src/app/transmitter.rs | 32 ----------------- 4 files changed, 21 insertions(+), 59 deletions(-) delete mode 100644 src/app/transmitter.rs diff --git a/src/app/events/event_types/event/handlers/lua_command.rs b/src/app/events/event_types/event/handlers/lua_command.rs index 5994a4f..e6b60ea 100644 --- a/src/app/events/event_types/event/handlers/lua_command.rs +++ b/src/app/events/event_types/event/handlers/lua_command.rs @@ -18,7 +18,7 @@ pub async fn handle(app: &mut App<'_>, command: &str) -> Result { .with_context(|| format!("Failed to execute: `{command}`"))?; 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) } diff --git a/src/app/events/event_types/event/handlers/main.rs b/src/app/events/event_types/event/handlers/main.rs index 01d3097..8e65604 100644 --- a/src/app/events/event_types/event/handlers/main.rs +++ b/src/app/events/event_types/event/handlers/main.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers}; use crate::{ @@ -15,14 +15,14 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { - app.transmitter + app.tx .send(Event::CommandEvent(Command::Exit)) .await?; } CrosstermEvent::Key(KeyEvent { code: KeyCode::Tab, .. }) => { - app.transmitter + app.tx .send(Event::CommandEvent(Command::CyclePlanes)) .await?; } @@ -30,7 +30,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { - app.transmitter + app.tx .send(Event::CommandEvent(Command::CyclePlanesRev)) .await?; } @@ -39,7 +39,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { - app.transmitter + app.tx .send(Event::CommandEvent(Command::CommandLineShow)) .await?; } @@ -51,7 +51,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { - app.transmitter + app.tx .send(Event::CommandEvent(Command::RoomMessageSend( app.ui.message_compose.lines().join("\n"), ))) diff --git a/src/app/mod.rs b/src/app/mod.rs index 058f8ef..8580990 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,11 +1,10 @@ pub mod command_interface; pub mod events; pub mod status; -pub mod transmitter; use std::path::Path; -use anyhow::{Context, Error, Result}; +use anyhow::{Error, Result, Context}; use cli_log::info; use matrix_sdk::Client; use mlua::Lua; @@ -19,14 +18,16 @@ use crate::{ ui::{central, setup}, }; -use self::{events::event_types, transmitter::Transmitter}; +use self::events::event_types; pub struct App<'ui> { ui: central::UI<'ui>, accounts_manager: AccountsManager, status: Status, - transmitter: Transmitter, + tx: mpsc::Sender, + rx: mpsc::Receiver, + input_listener_killer: CancellationToken, matrix_listener_killer: CancellationToken, @@ -36,7 +37,7 @@ pub struct App<'ui> { impl App<'_> { pub fn new() -> Result { fn set_up_lua(tx: mpsc::Sender) -> Lua { - let lua = Lua::new(); + let mut lua = Lua::new(); generate_ci_functions(&mut lua, tx); lua @@ -50,17 +51,18 @@ impl App<'_> { None }; - let transmitter = Transmitter::new(); + let (tx, rx) = mpsc::channel(256); Ok(Self { ui: central::UI::new()?, accounts_manager: AccountsManager::new(config)?, status: Status::new(None), - transmitter, + tx: tx.clone(), + rx, input_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<()> { // Spawn input event listener tokio::task::spawn(events::poll_input_events( - self.transmitter.tx(), + self.tx.clone(), self.input_listener_killer.clone(), )); @@ -98,11 +100,7 @@ impl App<'_> { self.status.set_state(State::Main); self.ui.update(&self.status).await?; - let event = self - .transmitter - .recv() - .await - .context("Failed to get next event")?; + let event = self.rx.recv().await.context("Failed to get next event")?; match event.handle(self).await? { event_types::EventStatus::Ok => (), @@ -122,11 +120,7 @@ impl App<'_> { self.status.set_state(State::Setup); self.ui.update_setup().await?; - let event = self - .transmitter - .recv() - .await - .context("Failed to get next event")?; + let event = self.rx.recv().await.context("Failed to get next event")?; match event.handle(self).await? { event_types::EventStatus::Ok => (), @@ -150,7 +144,7 @@ impl App<'_> { // Spawn Matrix Event Listener tokio::task::spawn(events::poll_matrix_events( - self.transmitter.tx(), + self.tx.clone(), self.matrix_listener_killer.clone(), client.clone(), )); diff --git a/src/app/transmitter.rs b/src/app/transmitter.rs deleted file mode 100644 index aaa96b6..0000000 --- a/src/app/transmitter.rs +++ /dev/null @@ -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, - rx: mpsc::Receiver, -} - -impl Transmitter { - pub fn new() -> Transmitter { - let (tx, rx) = mpsc::channel(256); - Transmitter { - tx, - rx, - } - } - pub fn tx(&self) -> mpsc::Sender { - self.tx.to_owned() - } - - pub async fn recv(&mut self) -> Result { - 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") - } -}