View Source Reactor.Step.Switch (reactor v0.10.2)
Conditionally decide which steps should be run at runtime.
Options
matches
- a list of match consisting of predicates and a list of steps to execute if the predicate returns a truthy value. Seet:matches
for more information. Required.default
- a list of steps to execute if none of the predicates match. Optional.allow_async?
- a boolean indicating whether to allow the steps to be executed asynchronously. Optional. Defaults totrue
.on
- the name of the argument to pass into the predicates. If this argument is not provided to this step, then an error will be returned.
Branching behaviour
Each of the predicates in matches
are tried in order, until either one
returns a truthy value, or all the matches are exhausted.
If there is a match, then the matching steps are emitted into the parent running Reactor.
In the case that no match is found, then the steps provided in the default
option are emitted. If no default is provided, then an error is returned.
Tip
Execution of predicates stops once the first match is found. This means that if multiple predicates potentially match, the subsequent ones will never be called.
Returning
By default the step returns nil
as it's result.
You can have the step return the result of a branch by adding a step to the branch with the same name as the switch which returns the expected value. This will be handled by normal Reactor step emission rules.
Summary
Types
A list of predicates and steps to execute if the predicate returns a truthy value.
A predicate is a 1-arity function. It can return anything. Any result which
is not nil
or false
is considered true.
Types
@type allow_async_option() :: {:allow_async?, boolean()}
@type default_option() :: {:default, [Reactor.Step.t()]}
@type match_option() :: {:matches, matches()}
@type matches() :: [{predicate(), [Reactor.Step.t()]}]
A list of predicates and steps to execute if the predicate returns a truthy value.
@type on_option() :: {:on, atom()}
@type options() :: [ match_option() | default_option() | allow_async_option() | on_option() ]
A predicate is a 1-arity function. It can return anything. Any result which
is not nil
or false
is considered true.