pub struct Context<State> {
pub action: ActionContext<State>,
pub state: Option<State>,
}
Expand description
The context of the action. This is handed into actions when they’re invoked, and is the underpinning mechanism for distributing side-effects, as well as action composition.
At compile time, the action context grants us, and the consumers, the ability to structure action systems in typesafe ways. At runtime, the context is used to distribute the state of the system, as well as any additional crate-specific plumbing that the action may require.
Fields§
§action: ActionContext<State>
The action context, describing any additional context that the action may carry.
state: Option<State>
The state of the system, determined by the consumer. This can be absent if the various actions are called before being properly initialized. The type system should prevent this most of the time by requiring the state to be present before invoking actions or calling them as services.
Implementations§
Source§impl<State> Context<State>
impl<State> Context<State>
Sourcepub fn from_action(action: ActionContext<State>) -> Self
pub fn from_action(action: ActionContext<State>) -> Self
Create a new context with the given action.
Sourcepub fn from_state(state: State) -> Self
pub fn from_state(state: State) -> Self
Create a new context with the given state.
Sourcepub fn state(&self) -> Result<State, StateNotReadyError>where
State: Clone,
pub fn state(&self) -> Result<State, StateNotReadyError>where
State: Clone,
Return the state. Returns an error if the state is not ready.
Sourcepub fn with_action(action: ActionContext<State>, state: State) -> Self
pub fn with_action(action: ActionContext<State>, state: State) -> Self
Create a new context with the given state and action context.