state_machine v0.1.2 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. Callback's return structurally analyzed

Applying a chain of callbacks. Application only happens if status hasn't been changed.

Link to this section Types

Link to this type

t(model)
t(model) ::
  (model -> {:ok, model} | {:error, any()} | any())
  | (model, StateMachine.Context.t(model) ->
       {:ok, model}
       | {:ok, StateMachine.Context.t(model)}
       | {:error, any()}
       | 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. Callback's return structurally analyzed:

Callback return processing

  • {:ok, %Context{...}} - replace the existing context with the new one
  • {:ok, %ModelString{...}} - update model in context
  • {:error, error} - set context status to :failed and populate message with tuple {step, error}, where step is an atom pointing at where this happened (i.e. :before_transition)
  • anything else - return original context, here we assume functions with side-effects and no meaningful return
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.