pub struct Cmd<M>where
M: Message,{ /* private fields */ }Expand description
A command is an asynchronous operation that produces a message.
Commands are used for side effects like:
- HTTP requests
- File I/O
- Timers
- Any async operation
Implementations§
Source§impl<M> Cmd<M>where
M: Message,
impl<M> Cmd<M>where
M: Message,
Sourcepub fn new<F>(f: F) -> Cmd<M>
pub fn new<F>(f: F) -> Cmd<M>
Create a new command from a function
Note: If the function returns None, consider using Cmd::noop() instead
for better performance and clearer intent.
§Example
let cmd: Cmd<Msg> = Cmd::new(|| {
// Perform a side effect
let data = std::fs::read_to_string("config.json").ok()?;
Some(Msg::DataLoaded(data))
});Sourcepub fn fallible<F>(f: F) -> Cmd<M>
pub fn fallible<F>(f: F) -> Cmd<M>
Create a new fallible command that can return errors
Use this when your command might fail and you want to handle errors gracefully.
§Example
let cmd: Cmd<Msg> = Cmd::fallible(|| {
let data = std::fs::read_to_string("config.json")?;
Ok(Some(Msg::ConfigLoaded(data)))
});Sourcepub fn noop() -> Cmd<M>
pub fn noop() -> Cmd<M>
Returns a no-op command that continues running without doing anything
This is the idiomatic way to return “no command” from update().
The program will continue running without executing any side effects.
§Example
match event {
Event::Tick => {
// Update internal state but don't trigger side effects
Cmd::noop()
}
_ => Cmd::noop()
}Sourcepub fn none() -> Cmd<M>
👎Deprecated since 0.2.1: Use Cmd::noop() instead. The name ‘none’ was confusing.
pub fn none() -> Cmd<M>
Returns a no-op command that continues running without doing anything
§Deprecated
This method is deprecated. Use Cmd::noop() instead for clearer intent.
The name “none” was confusing because it returns Some(Cmd) rather than None.
§Example
// Old way (deprecated):
let cmd: Cmd<Msg> = Cmd::noop();
// New way (preferred):
let cmd: Cmd<Msg> = Cmd::noop();Sourcepub fn tick<F>(duration: Duration, callback: F) -> Cmd<M>
👎Deprecated since 0.2.1: Use commands::tick() instead for consistency
pub fn tick<F>(duration: Duration, callback: F) -> Cmd<M>
Create a tick command
§Deprecated
This method is deprecated. Use commands::tick() instead for consistency.
§Example
// Old way (deprecated):
let cmd: Cmd<Msg> = Cmd::tick(Duration::from_secs(1), || Msg::Tick);
// New way (preferred):
let cmd: Cmd<Msg> = commands::tick(Duration::from_secs(1), || Msg::Tick);Sourcepub fn every<F>(duration: Duration, callback: F) -> Cmd<M>
👎Deprecated since 0.2.1: Use commands::every() instead for consistency
pub fn every<F>(duration: Duration, callback: F) -> Cmd<M>
Create an every command
§Deprecated
This method is deprecated. Use commands::every() instead for consistency.
§Example
// Old way (deprecated):
let cmd: Cmd<Msg> = Cmd::every(Duration::from_secs(1), |instant| Msg::Tick(instant));
// New way (preferred):
let cmd: Cmd<Msg> = commands::every(Duration::from_secs(1), |instant| Msg::Tick(instant));Sourcepub fn is_exec_process(&self) -> bool
pub fn is_exec_process(&self) -> bool
Check if this is an exec process command Check if this is an exec process command
Sourcepub fn map<N, F>(self, f: F) -> Cmd<N>
pub fn map<N, F>(self, f: F) -> Cmd<N>
Transform the messages produced by this command
This is essential for component composition, allowing child components to produce commands that can be lifted to parent message types.
§Example
let child_cmd: Cmd<ChildMsg> = Cmd::new(|| Some(ChildMsg::Click));
let parent_cmd: Cmd<ParentMsg> = child_cmd.map(ParentMsg::Child);Sourcepub fn inspect<F>(self, f: F) -> Cmd<M>
pub fn inspect<F>(self, f: F) -> Cmd<M>
Inspect this command for debugging
This allows you to observe command execution without modifying behavior.
§Example
let cmd: Cmd<Msg> = Cmd::noop()
.inspect(|cmd| println!("Executing command: {:?}", cmd));Sourcepub fn inspect_if<F>(self, condition: bool, f: F) -> Cmd<M>
pub fn inspect_if<F>(self, condition: bool, f: F) -> Cmd<M>
Conditionally inspect this command
Only runs the inspection function if the condition is true.
§Example
let cmd: Cmd<Msg> = Cmd::new(|| Some(Msg::Data("test".into())))
.inspect_if(debug_mode, |cmd| {
eprintln!("Debug: executing {}", cmd.debug_name());
});Sourcepub fn debug_name(&self) -> &'static str
pub fn debug_name(&self) -> &'static str
Get a string representation of the command type for debugging
§Example
let cmd: Cmd<Msg> = commands::tick(Duration::from_secs(1), || Msg::Tick);
assert_eq!(cmd.debug_name(), "Tick");
let noop: Cmd<Msg> = Cmd::noop();
assert_eq!(noop.debug_name(), "NoOp");Sourcepub fn then(self, other: Cmd<M>) -> Cmd<M>
pub fn then(self, other: Cmd<M>) -> Cmd<M>
Chain this command with another, running them sequentially
The second command will only run after the first completes.
This is equivalent to sequence(vec![self, other]).
§Example
use hojicha_core::{Cmd, Message};
#[derive(Debug, Clone)]
enum Msg {
First,
Second,
}
let cmd: Cmd<Msg> = Cmd::new(|| Some(Msg::First))
.then(Cmd::new(|| Some(Msg::Second)));Sourcepub fn and(self, other: Cmd<M>) -> Cmd<M>
pub fn and(self, other: Cmd<M>) -> Cmd<M>
Combine this command with another, running them concurrently
Both commands will run at the same time.
This is equivalent to batch(vec![self, other]).
§Example
use hojicha_core::{Cmd, Message};
use std::time::Duration;
#[derive(Debug, Clone)]
enum Msg {
Tick1,
Tick2,
}
let cmd: Cmd<Msg> = Cmd::tick(Duration::from_secs(1), || Msg::Tick1)
.and(Cmd::tick(Duration::from_secs(2), || Msg::Tick2));Sourcepub fn when(self, condition: bool) -> Cmd<M>
pub fn when(self, condition: bool) -> Cmd<M>
Execute this command only if a condition is met
If the condition is false, returns Cmd::noop().
§Example
use hojicha_core::{Cmd, Message};
#[derive(Debug, Clone)]
enum Msg {
Action,
}
let enabled = true;
let cmd: Cmd<Msg> = Cmd::new(|| Some(Msg::Action))
.when(enabled);Sourcepub fn unless(self, condition: bool) -> Cmd<M>
pub fn unless(self, condition: bool) -> Cmd<M>
Execute this command unless a condition is met
If the condition is true, returns Cmd::noop().
§Example
use hojicha_core::{Cmd, Message};
#[derive(Debug, Clone)]
enum Msg {
Action,
}
let disabled = false;
let cmd: Cmd<Msg> = Cmd::new(|| Some(Msg::Action))
.unless(disabled);