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.
Formulae#
When clock is triggered, check filter for truthy and call target with data from source if true.
- If the
clockis not passed, guard will be trigged on everysourceupdate - If
targetis not passed, create Event with type ofsourceand return it fromguard() - If
filteris Store, check it value for truthy - If
filterisFunction, call it with data fromsourceand 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
Example#
See ui visualization
Also, guard accepts a common function predicate as filter, to drop events before forwarding them to target
Example 2#
guard(source, {filter: booleanStore})#
Arguments
source(Store/Event/Effect): Source unit. Will trigger given guard on updatesfilter(Store): Filter store
Example#
guard(source, {filter: predicate})#
Arguments
source(Store/Event/Effect): Source unit. Will trigger given guard on updatesfilter((payload) => Boolean): Predicate function, should be pure