feat: with async commands instead of inline mutations phew.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
59
crates/hyperlog-tui/src/commands/create_section.rs
Normal file
59
crates/hyperlog-tui/src/commands/create_section.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{
|
||||
commander::{self, Commander},
|
||||
models::IOEvent,
|
||||
state::SharedState,
|
||||
};
|
||||
|
||||
pub struct CreateSectionCommand {
|
||||
commander: Commander,
|
||||
}
|
||||
|
||||
impl CreateSectionCommand {
|
||||
pub fn new(commander: Commander) -> Self {
|
||||
Self { commander }
|
||||
}
|
||||
|
||||
pub fn command(self, root: &str, path: &[&str]) -> super::Command {
|
||||
let root = root.to_owned();
|
||||
let path = path.iter().map(|s| s.to_string()).collect_vec();
|
||||
|
||||
super::Command::new(|dispatch| {
|
||||
tokio::spawn(async move {
|
||||
dispatch.send(crate::models::Msg::SectionCreated(IOEvent::Initialized));
|
||||
|
||||
match self
|
||||
.commander
|
||||
.execute(commander::Command::CreateSection { root, path })
|
||||
.await
|
||||
{
|
||||
Ok(()) => {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
}
|
||||
|
||||
dispatch.send(crate::models::Msg::SectionCreated(IOEvent::Success(())));
|
||||
}
|
||||
Err(e) => {
|
||||
dispatch.send(crate::models::Msg::SectionCreated(IOEvent::Failure(
|
||||
e.to_string(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
});
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CreateSectionCommandExt {
|
||||
fn create_section_command(&self) -> CreateSectionCommand;
|
||||
}
|
||||
|
||||
impl CreateSectionCommandExt for SharedState {
|
||||
fn create_section_command(&self) -> CreateSectionCommand {
|
||||
CreateSectionCommand::new(self.commander.clone())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user