StateMachine.Callback (state_machine v0.1.6)
Callback defines a captured function, that can be called in a various points of a State Machine's lifecycle. Depending on return type (shape) of the callback, it can update the context or the model, stop the transition with error, or be ignored.
Link to this section Summary
Functions
Applying a single callback. There are three types of callbacks supported
Applying a chain of callbacks. Application only happens if status
hasn't been changed.
Link to this section Types
binary_t(model)
Specs
binary_t(model) :: (model, StateMachine.Context.t(model) -> {:ok, model} | {:ok, StateMachine.Context.t(model)} | {:error, any()} | any())
side_effect_t()
Specs
side_effect_t() :: (() -> any())
t(model)
Specs
t(model) :: unary_t(model) | binary_t(model) | side_effect_t()
unary_t(model)
Specs
Link to this section Functions
apply_callback(ctx, cb, step)
Specs
apply_callback(StateMachine.Context.t(model), t(model), atom()) :: StateMachine.Context.t(model)
Applying a single callback. There are three types of callbacks supported:
Unary (unary_t type)
A unary callback, receiving a model struct and that can be updated in current conetxt based on shape of the return:
{:ok, model}
— replaces model in the context{:error, e}
— stops the transition with a given errorany
— doesn't have any effect on the context
Binary (binary_t type)
A binary callback, receiving a model struct and a context. Either can be updated depending on the shape of the return:
{:ok, context}
— replaces context completely{:ok, model}
— replaces model in the context{:error, e}
— stops the transition with a given errorany
— doesn't have any effect on the context
Side effects (side_effect_t type)
A type of callback that does not expect any input and potentially produces a side effect.
Any return value is ignored, except for {:error, e}
that stops the transition with a given error.
apply_chain(ctx, cbs, step)
Specs
apply_chain(StateMachine.Context.t(model), [t(model)], atom()) :: StateMachine.Context.t(model)
Applying a chain of callbacks. Application only happens if status
hasn't been changed.