Rename backend to client and simplify event handling

This commit is contained in:
Joscha 2022-03-05 19:44:03 +01:00
parent 2400651483
commit 5680eb3ab2
9 changed files with 34 additions and 42 deletions

View file

@ -1,6 +0,0 @@
pub mod cove;
#[derive(Debug)]
pub enum Event {
Cove(String, cove::conn::Event),
}

1
cove-tui/src/client.rs Normal file
View file

@ -0,0 +1 @@
pub mod cove;

View file

@ -8,8 +8,7 @@ use tokio::sync::{Mutex, MutexGuard};
use crate::config::Config; use crate::config::Config;
use crate::never::Never; use crate::never::Never;
use super::super::Event; use super::conn::{self, CoveConn, CoveConnMt, Event};
use super::conn::{self, CoveConn, CoveConnMt};
struct ConnConfig { struct ConnConfig {
url: String, url: String,
@ -40,14 +39,15 @@ pub struct CoveRoom {
} }
impl CoveRoom { impl CoveRoom {
pub async fn new<E>( pub async fn new<E, F>(
config: &'static Config, config: &'static Config,
outer_ev_tx: UnboundedSender<E>,
name: String, name: String,
event_sender: UnboundedSender<E>,
convert_event: F,
) -> Self ) -> Self
where where
E: Send + 'static, E: Send + 'static,
Event: Into<E>, F: Fn(&str, Event) -> E + Send + 'static,
{ {
let (ev_tx, ev_rx) = mpsc::unbounded_channel(); let (ev_tx, ev_rx) = mpsc::unbounded_channel();
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
@ -70,7 +70,7 @@ impl CoveRoom {
tokio::spawn(async move { tokio::spawn(async move {
tokio::select! { tokio::select! {
_ = rx => {} // Watch dead man's switch _ = rx => {} // Watch dead man's switch
_ = Self::shovel_events(ev_rx, outer_ev_tx, name) => {} _ = Self::shovel_events(name, ev_rx, event_sender, convert_event) => {}
_ = Self::run(conn_clone, mt, conf) => {} _ = Self::run(conn_clone, mt, conf) => {}
} }
}); });
@ -88,15 +88,14 @@ impl CoveRoom {
} }
async fn shovel_events<E>( async fn shovel_events<E>(
name: String,
mut ev_rx: UnboundedReceiver<conn::Event>, mut ev_rx: UnboundedReceiver<conn::Event>,
ev_tx: UnboundedSender<E>, ev_tx: UnboundedSender<E>,
name: String, convert_event: impl Fn(&str, Event) -> E,
) where ) {
Event: Into<E>,
{
while let Some(event) = ev_rx.recv().await { while let Some(event) = ev_rx.recv().await {
let event = Event::Cove(name.clone(), event); let event = convert_event(&name, event);
if ev_tx.send(event.into()).is_err() { if ev_tx.send(event).is_err() {
break; break;
} }
} }

View file

@ -1,6 +1,6 @@
#![warn(clippy::use_self)] #![warn(clippy::use_self)]
pub mod backend; pub mod client;
mod config; mod config;
mod never; mod never;
mod ui; mod ui;

View file

@ -11,9 +11,7 @@ use std::collections::hash_map::Entry;
use std::collections::HashMap; use std::collections::HashMap;
use std::io::Stdout; use std::io::Stdout;
use crossterm::event::{ use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, MouseEvent, MouseEventKind};
Event as CEvent, EventStream, KeyCode, KeyEvent, MouseEvent, MouseEventKind,
};
use futures::StreamExt; use futures::StreamExt;
use tokio::sync::mpsc::error::TryRecvError; use tokio::sync::mpsc::error::TryRecvError;
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
@ -21,9 +19,8 @@ use tui::backend::CrosstermBackend;
use tui::layout::{Constraint, Direction, Layout, Rect}; use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::{Frame, Terminal}; use tui::{Frame, Terminal};
use crate::backend::cove::conn::Event as CoveEvent; use crate::client::cove::conn::Event as CoveEvent;
use crate::backend::cove::room::CoveRoom; use crate::client::cove::room::CoveRoom;
use crate::backend::Event as BEvent;
use crate::config::Config; use crate::config::Config;
use crate::ui::overlays::OverlayReaction; use crate::ui::overlays::OverlayReaction;
@ -42,14 +39,13 @@ pub enum RoomId {
#[derive(Debug)] #[derive(Debug)]
pub enum UiEvent { pub enum UiEvent {
Term(CEvent), Term(Event),
Room(BEvent), Cove(String, CoveEvent),
Redraw,
} }
impl From<BEvent> for UiEvent { impl UiEvent {
fn from(event: BEvent) -> Self { fn cove(room: &str, event: CoveEvent) -> Self {
Self::Room(event) Self::Cove(room.to_string(), event)
} }
} }
@ -142,13 +138,10 @@ impl Ui {
}; };
loop { loop {
let result = match event { let result = match event {
UiEvent::Term(CEvent::Key(event)) => self.handle_key_event(event).await?, UiEvent::Term(Event::Key(event)) => self.handle_key_event(event).await?,
UiEvent::Term(CEvent::Mouse(event)) => self.handle_mouse_event(event).await?, UiEvent::Term(Event::Mouse(event)) => self.handle_mouse_event(event).await?,
UiEvent::Term(CEvent::Resize(_, _)) => EventHandleResult::Continue, UiEvent::Term(Event::Resize(_, _)) => EventHandleResult::Continue,
UiEvent::Room(BEvent::Cove(name, event)) => { UiEvent::Cove(name, event) => self.handle_cove_event(name, event).await?,
self.handle_cove_event(name, event).await?
}
UiEvent::Redraw => EventHandleResult::Continue,
}; };
match result { match result {
EventHandleResult::Continue => {} EventHandleResult::Continue => {}
@ -332,8 +325,13 @@ impl Ui {
match &id { match &id {
RoomId::Cove(name) => { RoomId::Cove(name) => {
if let Entry::Vacant(entry) = self.cove_rooms.entry(name.clone()) { if let Entry::Vacant(entry) = self.cove_rooms.entry(name.clone()) {
let room = let room = CoveRoom::new(
CoveRoom::new(self.config, self.event_tx.clone(), name.clone()).await; self.config,
name.clone(),
self.event_tx.clone(),
UiEvent::cove,
)
.await;
entry.insert(CoveUi::new(room)); entry.insert(CoveUi::new(room));
} }
} }

View file

@ -6,7 +6,7 @@ use tui::text::Span;
use tui::widgets::{Block, BorderType, Borders, Paragraph}; use tui::widgets::{Block, BorderType, Borders, Paragraph};
use tui::Frame; use tui::Frame;
use crate::backend::cove::room::CoveRoom; use crate::client::cove::room::CoveRoom;
use self::users::CoveUsers; use self::users::CoveUsers;

View file

@ -7,7 +7,7 @@ use tui::layout::Rect;
use tui::text::{Span, Spans}; use tui::text::{Span, Spans};
use tui::widgets::{Paragraph, Widget}; use tui::widgets::{Paragraph, Widget};
use crate::backend::cove::conn::Present; use crate::client::cove::conn::Present;
use crate::ui::styles; use crate::ui::styles;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]