rs_store/
effect.rs

1use crate::Dispatcher;
2
3/// Represents a side effect that can be executed.
4///
5/// `Effect` is used to encapsulate actions that should be performed as a result of a state change.
6/// an effect can be either simple function or more complex thunk that require a dispatcher.
7pub enum Effect<Action> {
8    /// An action that should be dispatched.
9    Action(Action),
10    /// A task which will be executed asynchronously.
11    Task(Box<dyn FnOnce() + Send>),
12    /// A task that takes the dispatcher as an argument.
13    Thunk(Box<dyn FnOnce(Box<dyn Dispatcher<Action>>) + Send>),
14    /// A function which has a result.
15    /// The result is an Any type which can be downcasted to the expected type,
16    /// you should know the type and the String key can help.
17    /// The result default ignored, if you want to get the result of the function, you can use `EffectMiddleware` to receive the result.
18    Function(String, EffectFunction),
19}
20
21pub type EffectResult = Result<Box<dyn std::any::Any + Send>, Box<dyn std::error::Error + Send>>;
22pub type EffectFunction = Box<dyn FnOnce() -> EffectResult + Send>;
23
24/// EffectResultReceiver is a trait that can receive the result of an effect function.
25pub trait EffectResultReceiver {
26    fn receive(&self, key: String, result: EffectResult);
27}