View Source Reactor.Step.Around (reactor v0.10.2)

Wrap the execution of a number of steps in a function.

This allows you to provide custom context and filter the provided steps as needed.

Options

  • fun - a four-arity function that will be called when executing this step.
  • steps - a list of steps which are will be provided to the above mentioned function.
  • allow_async? - a boolean indicating whether the nested steps can be executed asynchronously or must remain within the current process.

Wrapper function

Your around function will be called by this step and will be passed the following arguments:

  • arguments - the arguments passed to the step.
  • context - the context passed to the step.
  • steps - the list of steps passed in the options.
  • callback - a 3 arity function that you can call to execute steps.

This provides you the opportunity to modify the arguments, context and list of steps to be executed. You then can call the callback with the modified arguments, context and steps and they will be executed in a Reactor of their own. The callback will return {:ok, results} where results is a map of all of the step results by name, or an error tuple.

You can then modify the result in any way before returning it as the return of the around step.

Callback function

The callback function will spawn a separate Reactor and run provided steps to completion using arguments as input.

It expects the following three arguments to be passed:

  1. arguments - a map of arguments to be used as input to the nested Reactor.
  2. context - the context passed to the nested Reactor.
  3. steps - the list of steps which will be executed in the nested Reactor.

Example

You could use a function like that below to cause some steps to be executed inside an Ecto database transaction.

def in_transaction(arguments, context, steps, callback) do
  MyApp.Repo.transaction(fn ->
    case callback.(arguments, context, steps) do
      {:ok, results} -> result
      {:error, reason} -> raise reason
    end
  end)
end

Summary

Types

Should the inner Reactor be allowed to run tasks asynchronously?

The type signature for the "around" function.

The type signature for the provided callback function.

The MFA or 4-arity function which this step will call.

The initial steps to pass into the "around" function.

Types

@type allow_async_option() :: {:allow_async?, boolean()}

Should the inner Reactor be allowed to run tasks asynchronously?

Optional. Defaults to true.

@type around_fun() ::
  (Reactor.inputs(), Reactor.context(), [Reactor.Step.t()], callback() ->
     {:ok, any()} | {:error, any()})

The type signature for the "around" function.

@type callback() ::
  (Reactor.inputs(), Reactor.context(), [Reactor.Step.t()] ->
     {:ok, any()} | {:error, any()})

The type signature for the provided callback function.

@type function_option() :: {:fun, mfa() | around_fun()}

The MFA or 4-arity function which this step will call.

@type options() :: [function_option() | steps_option() | allow_async_option()]
@type steps_option() :: {:steps, [Reactor.Step.t()]}

The initial steps to pass into the "around" function.

Optional.

Functions

Link to this function

around(arguments, context, steps, options)

View Source