guard
note
since effector 20.4.0
Method for conditional event routing. It provides a way to control one dataflow with the help of another: when the condition and the data are in different places, we can use guard with stores as filters to trigger events when condition state is true, thereby modulate signals without mixing them.
#
FormulaeWhen clock
is triggered, check filter
for truthy and call target
with data from source
if true
.
- If the
clock
is not passed, guard will be trigged on everysource
update - If
target
is not passed, create Event with type ofsource
and return it fromguard()
- If
filter
is Store, check it value for truthy - If
filter
isFunction
, call it with data fromsource
and check result for truthy
since
clock
in guard is available since effector 21.8.0
guard({source, filter, target?})
#
Arguments
params
(Object): Configuration object
Returns
Event, which fires upon clock trigger
#
ExampleSee ui visualization
Also, guard accepts a common function predicate as filter, to drop events before forwarding them to target
#
Example 2guard(source, {filter: booleanStore})
#
Arguments
source
(Store/Event/Effect): Source unit. Will trigger given guard on updatesfilter
(Store): Filter store
#
Exampleguard(source, {filter: predicate})
#
Arguments
source
(Store/Event/Effect): Source unit. Will trigger given guard on updatesfilter
((payload) => Boolean): Predicate function, should be pure