state_machine v0.1.3 StateMachine.Callback

Callback wraps 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, 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

Link to this type

binary_t(model)
binary_t(model) ::
  (model, StateMachine.Context.t(model) ->
     {:ok, model}
     | {:ok, StateMachine.Context.t(model)}
     | {:error, any()}
     | any())

Link to this type

side_effect_t()
side_effect_t() :: (() -> any())

Link to this type

t(model)
t(model) :: unary_t(model) | binary_t(model) | side_effect_t()

Link to this type

unary_t(model)
unary_t(model) :: (model -> {:ok, model} | {:error, any()} | any())

Link to this section Functions

Link to this function

apply_callback(ctx, cb, step)
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

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 error
  • any — doesn't have any effect on the context

Binary

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 error
  • any — doesn't have any effect on the context

Side effects

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.

Link to this function

apply_chain(ctx, cbs, step)
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.