chore: refactor out graph created event
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-12 21:11:08 +02:00
parent 9bb5bc9e87
commit 64d59e069f
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
3 changed files with 12 additions and 25 deletions

View File

@ -1,7 +1,7 @@
use itertools::Itertools; use itertools::Itertools;
use crate::{ use crate::{
models::{GraphUpdatedEvent, Msg}, models::{IOEvent, Msg},
querier::Querier, querier::Querier,
state::SharedState, state::SharedState,
}; };
@ -22,27 +22,23 @@ impl UpdateGraphCommand {
super::Command::new(|dispatch| { super::Command::new(|dispatch| {
tokio::spawn(async move { tokio::spawn(async move {
let now = std::time::SystemTime::now(); let now = std::time::SystemTime::now();
dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Initiated)); dispatch.send(Msg::GraphUpdated(IOEvent::Initialized));
match self.querier.get_async(&root, path).await { match self.querier.get_async(&root, path).await {
Ok(Some(graph)) => { Ok(Some(graph)) => {
dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Optimistic( dispatch.send(Msg::GraphUpdated(IOEvent::Optimistic(graph.clone())));
graph.clone(),
)));
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
tokio::time::sleep(std::time::Duration::from_secs(1)).await; tokio::time::sleep(std::time::Duration::from_secs(1)).await;
} }
dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Success(graph))) dispatch.send(Msg::GraphUpdated(IOEvent::Success(graph)))
} }
Ok(None) => dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Failure( Ok(None) => dispatch.send(Msg::GraphUpdated(IOEvent::Failure(
"graph was not found user root".into(), "graph was not found user root".into(),
))), ))),
Err(e) => dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Failure( Err(e) => dispatch.send(Msg::GraphUpdated(IOEvent::Failure(format!("{e}")))),
format!("{e}"),
))),
} }
let elapsed = now.elapsed().expect("to be able to get time"); let elapsed = now.elapsed().expect("to be able to get time");

View File

@ -11,7 +11,7 @@ use crate::{
IntoCommand, IntoCommand,
}, },
components::movement_graph::GraphItemType, components::movement_graph::GraphItemType,
models::{GraphUpdatedEvent, Msg}, models::{IOEvent, Msg},
state::SharedState, state::SharedState,
}; };
@ -58,17 +58,17 @@ impl<'a> GraphExplorerState<'a> {
pub fn update(&mut self, msg: &Msg) -> Option<Command> { pub fn update(&mut self, msg: &Msg) -> Option<Command> {
if let Msg::GraphUpdated(graph_update) = msg { if let Msg::GraphUpdated(graph_update) = msg {
match graph_update { match graph_update {
GraphUpdatedEvent::Initiated => { IOEvent::Initialized => {
tracing::trace!("initialized graph"); tracing::trace!("initialized graph");
} }
GraphUpdatedEvent::Success(graph) => { IOEvent::Success(graph) => {
tracing::trace!("graph updated successfully"); tracing::trace!("graph updated successfully");
self.graph = Some(graph.clone()); self.graph = Some(graph.clone());
} }
GraphUpdatedEvent::Failure(e) => { IOEvent::Failure(e) => {
tracing::error!("graph update failed: {}", e); tracing::error!("graph update failed: {}", e);
} }
GraphUpdatedEvent::Optimistic(graph) => { IOEvent::Optimistic(graph) => {
tracing::trace!("graph updated optimistically"); tracing::trace!("graph updated optimistically");
self.graph = Some(graph.clone()); self.graph = Some(graph.clone());
} }

View File

@ -21,8 +21,7 @@ pub enum Msg {
Edit(EditMsg), Edit(EditMsg),
GraphUpdated(GraphUpdatedEvent), GraphUpdated(IOEvent<GraphItem>),
ItemCreated(IOEvent<()>), ItemCreated(IOEvent<()>),
ItemUpdated(IOEvent<()>), ItemUpdated(IOEvent<()>),
SectionCreated(IOEvent<()>), SectionCreated(IOEvent<()>),
@ -37,14 +36,6 @@ pub enum IOEvent<T> {
Failure(String), Failure(String),
} }
#[derive(Debug)]
pub enum GraphUpdatedEvent {
Initiated,
Optimistic(GraphItem),
Success(GraphItem),
Failure(String),
}
impl IntoCommand for Msg { impl IntoCommand for Msg {
fn into_command(self) -> crate::commands::Command { fn into_command(self) -> crate::commands::Command {
Command::new(|_| Some(self)) Command::new(|_| Some(self))