forked from trinitrix/core
1
0
Fork 0

Refactor(Events): Changed the `Event` type from a struct to an enum

This commit is contained in:
antifallobst 2023-07-14 00:06:10 +02:00
parent dd3c765ea2
commit dfeac4662d
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
6 changed files with 24 additions and 91 deletions

View File

@ -1,7 +1,7 @@
use anyhow::Result;
use tokio::sync::mpsc;
use super::events::event_types::{Event, EventBuilder};
use super::events::event_types::Event;
#[derive(Debug, Clone)]
pub enum Command {
@ -19,7 +19,7 @@ pub enum Command {
}
pub async fn execute(channel: &mpsc::Sender<Event>, command: Command) -> Result<()> {
let event = EventBuilder::default().command_event(command).build();
let event = Event::CommandEvent(command);
channel.send(event).await?;
Ok(())
}

View File

@ -10,39 +10,32 @@ use self::handlers::{command, main, matrix, setup};
use super::EventStatus;
#[derive(Debug)]
pub struct Event {
pub(super) input_event: Option<CrosstermEvent>,
pub(super) matrix_event: Option<matrix_sdk::deserialized_responses::SyncResponse>,
pub(super) command_event: Option<Command>,
pub enum Event {
InputEvent(CrosstermEvent),
MatrixEvent(matrix_sdk::deserialized_responses::SyncResponse),
CommandEvent(Command),
}
impl Event {
pub async fn handle(&self, app: &mut App<'_>) -> Result<EventStatus> {
if let Some(matrix_event) = &self.matrix_event {
return matrix::handle(app, matrix_event)
match &self {
Event::MatrixEvent(event) => matrix::handle(app, event)
.await
.with_context(|| format!("Failed to handle matrix event: `{:#?}`", matrix_event));
}
.with_context(|| format!("Failed to handle matrix event: `{:#?}`", event)),
if let Some(command_event) = &self.command_event {
return command::handle(app, command_event).await.with_context(|| {
format!("Failed to handle command event: `{:#?}`", command_event)
});
}
Event::CommandEvent(event) => command::handle(app, event)
.await
.with_context(|| format!("Failed to handle command event: `{:#?}`", event)),
if let Some(input_event) = &self.input_event {
let status = match app.status.state() {
State::None => EventStatus::Ok,
State::Main => main::handle(app, input_event).await.with_context(|| {
format!("Failed to handle input event: `{:#?}`", input_event)
})?,
State::Setup => setup::handle(app, input_event).await.with_context(|| {
format!("Failed to handle input event: `{:#?}`", input_event)
})?,
};
return Ok(status);
Event::InputEvent(event) => match app.status.state() {
State::None => Ok(EventStatus::Ok),
State::Main => main::handle(app, event)
.await
.with_context(|| format!("Failed to handle input event: `{:#?}`", event)),
State::Setup => setup::handle(app, event)
.await
.with_context(|| format!("Failed to handle input event: `{:#?}`", event)),
},
}
Ok(EventStatus::Ok)
}
}

View File

@ -1,51 +0,0 @@
use super::Event;
pub struct EventBuilder {
event: Event,
}
impl Default for Event {
fn default() -> Self {
Self {
input_event: None,
matrix_event: None,
command_event: None,
}
}
}
impl Default for EventBuilder {
fn default() -> Self {
Self {
event: Event::default(),
}
}
}
impl EventBuilder {
pub fn input_event(&mut self, input_event: crossterm::event::Event) -> &Self {
self.event.input_event = Some(input_event);
self
}
pub fn matrix_event(
&mut self,
matrix_event: matrix_sdk::deserialized_responses::SyncResponse,
) -> &Self {
self.event.matrix_event = Some(matrix_event);
self
}
pub fn command_event(&mut self, command_event: crate::app::command::Command) -> &Self {
self.event.command_event = Some(command_event);
self
}
pub fn build(&self) -> Event {
Event {
input_event: self.event.input_event.to_owned(),
matrix_event: self.event.matrix_event.to_owned(),
command_event: self.event.command_event.to_owned(),
}
}
}

View File

@ -1,8 +1,5 @@
pub mod event_builder;
pub mod event;
pub mod event_status;
pub use self::event_builder::*;
pub use self::event::*;
pub use self::event_status::*;

View File

@ -5,8 +5,6 @@ use matrix_sdk::{config::SyncSettings, Client, LoopCtrl};
use tokio::{sync::mpsc, time::Duration};
use tokio_util::sync::CancellationToken;
use crate::app::events::event_types::EventBuilder;
use self::event_types::Event;
pub async fn poll_input_events(
@ -16,10 +14,7 @@ pub async fn poll_input_events(
async fn poll_input_events_stage_2(channel: mpsc::Sender<Event>) -> Result<()> {
loop {
if crossterm::event::poll(Duration::from_millis(100))? {
let event = EventBuilder::default()
.input_event(crossterm::event::read()?)
.build();
let event = Event::InputEvent(crossterm::event::read()?);
channel.send(event).await?;
} else {
tokio::task::yield_now().await;
@ -49,7 +44,7 @@ pub async fn poll_matrix_events(
client
.sync_with_callback(sync_settings, |response| async move {
let event = EventBuilder::default().matrix_event(response).build();
let event = Event::MatrixEvent(response);
match tx.send(event).await {
Ok(_) => LoopCtrl::Continue,

View File

@ -14,10 +14,9 @@ use status::{State, Status};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use crate::app::command::Command;
use crate::{accounts, app::command_interface::generate_ci_functions, ui};
use self::events::event_types::{self, Event, EventBuilder};
use self::events::event_types::{self, Event};
pub struct App<'ui> {
ui: ui::UI<'ui>,