StateMachine.Callback (state_machine v0.1.7)

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.

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.

Types

binary_t(model)

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

side_effect_t()

@type side_effect_t() :: (-> any())

t(model)

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

unary_t(model)

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

Functions

apply_callback(ctx, cb, step)

@spec 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 error
  • any — 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 error
  • any — 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)

@spec 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.